随笔 - 60  文章 - 0  评论 - 14 

在kubernetes集群里集成Apollo配置中心(1)之交付Apollo-adminservice至Kubernetes集群

1.部署apollo-adminservice软件包

apollo-adminservice软件包链接地址:https://github.com/ctripcorp/apollo/releases/download/v1.5.1/apollo-adminservice-1.5.1-github.zip

在运维主机(mfyxw50l.mfyxw.com)上操作

(1)下载软件

[root@mfyxw50 ~]# cd /opt/src/
[root@mfyxw50 src]# wget https://github.com/ctripcorp/apollo/releases/download/v1.5.1/apollo-adminservice-1.5.1-github.zip

(2)解压到指定的目录

[root@mfyxw50 ~]# mkdir -p /data/dockerfile/apollo-adminservice
[root@mfyxw50 ~]# unzip /opt/src/apollo-adminservice-1.5.1-github.zip -d /data/dockerfile/apollo-adminservice/

(3)删除不使用的文件

[root@mfyxw50 ~]# rm -fr /data/dockerfile/apollo-adminservice/apollo-adminservice-1.5.1-sources.jar
[root@mfyxw50 ~]# rm -fr /data/dockerfile/apollo-adminservice/scripts/shutdown.sh
[root@mfyxw50 ~]# rm -fr /data/dockerfile/apollo-adminservice/apollo-adminservice.conf

2.制作apollo-adminservice的Docker镜像

(1)配置数据库连接串

[root@mfyxw50 ~]# cat > /data/dockerfile/apollo-adminservice/config/application-github.properties << EOF
# DataSource
spring.datasource.url = jdbc:mysql://mysql.od.com:3306/ApolloConfigDB?characterEncoding=utf8
spring.datasource.username = apolloconfig
spring.datasource.password = 123456
EOF

(2)更新startup.sh文件

[root@mfyxw50 ~]# cat > /data/dockerfile/apollo-adminservice/scripts/startup.sh << EOF
#!/bin/bash
SERVICE_NAME=apollo-adminservice
## Adjust log dir if necessary
LOG_DIR=/opt/logs/apollo-adminservice
## Adjust server port if necessary
SERVER_PORT=8080
APOLLO_ADMIN_SERVICE_NAME=\$(hostname -i)
# SERVER_URL="http://localhost:\${SERVER_PORT}"
SERVER_URL="http://\${APOLLO_ADMIN_SERVICE_NAME}:\${SERVER_PORT}"

## Adjust memory settings if necessary
#export JAVA_OPTS="-Xms2560m -Xmx2560m -Xss256k -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=384m -XX:NewSize=1536m -XX:MaxNewSize=1536m -XX:SurvivorRatio=8"

## Only uncomment the following when you are using server jvm
#export JAVA_OPTS="\$JAVA_OPTS -server -XX:-ReduceInitialCardMarks"

########### The following is the same for configservice, adminservice, portal ###########
export JAVA_OPTS="\$JAVA_OPTS -XX:ParallelGCThreads=4 -XX:MaxTenuringThreshold=9 -XX:+DisableExplicitGC -XX:+ScavengeBeforeFullGC -XX:SoftRefLRUPolicyMSPerMB=0 -XX:+ExplicitGCInvokesConcurrent -XX:+PrintGCDetails -XX:+HeapDumpOnOutOfMemoryError -XX:-OmitStackTraceInFastThrow -Duser.timezone=Asia/Shanghai -Dclient.encoding.override=UTF-8 -Dfile.encoding=UTF-8 -Djava.security.egd=file:/dev/./urandom"
export JAVA_OPTS="\$JAVA_OPTS -Dserver.port=\$SERVER_PORT -Dlogging.file=\$LOG_DIR/\$SERVICE_NAME.log -XX:HeapDumpPath=\$LOG_DIR/HeapDumpOnOutOfMemoryError/"

# Find Java
if [[ -n "\$JAVA_HOME" ]] && [[ -x "\$JAVA_HOME/bin/java" ]]; then
    javaexe="\$JAVA_HOME/bin/java"
elif type -p java > /dev/null 2>&1; then
    javaexe=\$(type -p java)
elif [[ -x "/usr/bin/java" ]];  then
    javaexe="/usr/bin/java"
else
    echo "Unable to find Java"
    exit 1
fi

if [[ "\$javaexe" ]]; then
    version=\$("\$javaexe" -version 2>&1 | awk -F '"' '/version/ {print \$2}')
    version=\$(echo "\$version" | awk -F. '{printf("%03d%03d",\$1,\$2);}')
    # now version is of format 009003 (9.3.x)
    if [ \$version -ge 011000 ]; then
        JAVA_OPTS="\$JAVA_OPTS -Xlog:gc*:\$LOG_DIR/gc.log:time,level,tags -Xlog:safepoint -Xlog:gc+heap=trace"
    elif [ \$version -ge 010000 ]; then
        JAVA_OPTS="\$JAVA_OPTS -Xlog:gc*:\$LOG_DIR/gc.log:time,level,tags -Xlog:safepoint -Xlog:gc+heap=trace"
    elif [ \$version -ge 009000 ]; then
        JAVA_OPTS="\$JAVA_OPTS -Xlog:gc*:\$LOG_DIR/gc.log:time,level,tags -Xlog:safepoint -Xlog:gc+heap=trace"
    else
        JAVA_OPTS="\$JAVA_OPTS -XX:+UseParNewGC"
        JAVA_OPTS="\$JAVA_OPTS -Xloggc:\$LOG_DIR/gc.log -XX:+PrintGCDetails"
        JAVA_OPTS="\$JAVA_OPTS -XX:+UseConcMarkSweepGC -XX:+UseCMSCompactAtFullCollection -XX:+UseCMSInitiatingOccupancyOnly -XX:CMSInitiatingOccupancyFraction=60 -XX:+CMSClassUnloadingEnabled -XX:+CMSParallelRemarkEnabled -XX:CMSFullGCsBeforeCompaction=9 -XX:+CMSClassUnloadingEnabled  -XX:+PrintGCDateStamps -XX:+PrintGCApplicationConcurrentTime -XX:+PrintHeapAtGC -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5 -XX:GCLogFileSize=5M"
    fi
fi

printf "\$(date) ==== Starting ==== \n"

cd \`dirname \$0\`/..
chmod 755 \$SERVICE_NAME".jar"
./\$SERVICE_NAME".jar" start

rc=\$?;

if [[ \$rc != 0 ]];
then
    echo "\$(date) Failed to start \$SERVICE_NAME.jar, return code: \$rc"
    exit \$rc;
fi

tail -f /dev/null
EOF

(3)编写apollo-adminservice的Dockerfile文件

[root@mfyxw50 ~]# cat > /data/dockerfile/apollo-adminservice/Dockerfile << EOF
FROM harbor.od.com/base/jre8:8u112

ENV VERSION 1.5.1

RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime &&\
    echo "Asia/Shanghai" > /etc/timezone

ADD apollo-adminservice-\${VERSION}.jar /apollo-adminservice/apollo-adminservice.jar
ADD config/ /apollo-adminservice/config
ADD scripts/ /apollo-adminservice/scripts

CMD ["/apollo-adminservice/scripts/startup.sh"]
EOF

(4)制作镜像并推送

[root@mfyxw50 ~]# cd /data/dockerfile/apollo-adminservice
[root@mfyxw50 apollo-adminservice]# docker build . -t harbor.od.com/infra/apollo-adminservice:v1.5.1
[root@mfyxw50 ~]# docker login harbor.od.com
[root@mfyxw50 ~]# docker push harbor.od.com/infra/apollo-adminservice:v1.5.1

(5)登录harbor查看是否已经上传成功

image-20200706162816479

3.准备apollo-adminservice的资源配置清单

在运维主机mfyxw50.mfyxw.com上操作

(1)创建存放资源配置清单的目录

[root@mfyxw50 ~]# mkdir -p /data/k8s-yaml/apollo-adminservice

(2)Deployment.yaml文件内容如下

[root@mfyxw50 ~]# cat > /data/k8s-yaml/apollo-adminservice/deployment.yaml << EOF
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: apollo-adminservice
  namespace: infra
  labels: 
    name: apollo-adminservice
spec:
  replicas: 1
  selector:
    matchLabels: 
      name: apollo-adminservice
  template:
    metadata:
      labels: 
        app: apollo-adminservice 
        name: apollo-adminservice
    spec:
      volumes:
      - name: configmap-volume
        configMap:
          name: apollo-adminservice-cm
      containers:
      - name: apollo-adminservice
        image: harbor.od.com/infra/apollo-adminservice:v1.5.1
        ports:
        - containerPort: 8080
          protocol: TCP
        volumeMounts:
        - name: configmap-volume
          mountPath: /apollo-adminservice/config
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
        imagePullPolicy: IfNotPresent
      imagePullSecrets:
      - name: harbor
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      securityContext: 
        runAsUser: 0
      schedulerName: default-scheduler
  strategy:
    type: RollingUpdate
    rollingUpdate: 
      maxUnavailable: 1
      maxSurge: 1
  revisionHistoryLimit: 7
  progressDeadlineSeconds: 600
EOF

(3)ConfigMap.yaml文件内容如下

[root@mfyxw50 ~]# cat > /data/k8s-yaml/apollo-adminservice/configmap.yaml << EOF
apiVersion: v1
kind: ConfigMap
metadata:
  name: apollo-adminservice-cm
  namespace: infra
data:
  application-github.properties: |
    # DataSource
    spring.datasource.url = jdbc:mysql://mysql.od.com:3306/ApolloConfigDB?characterEncoding=utf8
    spring.datasource.username = apolloconfig
    spring.datasource.password = 123456
    eureka.service.url = http://config.od.com/eureka
  app.properties: |
    appId=100003172
EOF

4.应用apollo-adminservice资源配置清单

在master节点(mfyxw30.mfyxw.com或mfyxw40.mfyxw.com)任意一台操作

(1)应用资源配置清单

[root@mfyxw30 ~]# kubectl apply -f http://k8s-yaml.od.com/apollo-adminservice/deployment.yaml
deployment.extensions/apollo-adminservice created
[root@mfyxw30 ~]# 
[root@mfyxw30 ~]# kubectl apply -f http://k8s-yaml.od.com/apollo-adminservice/configmap.yaml
configmap/apollo-adminservice-cm created
[root@mfyxw30 ~]# 

(2)查看apollo-adminservice的pod是否运行成功

[root@mfyxw30 ~]# kubectl get pod -n infra
NAME                                   READY   STATUS    RESTARTS   AGE
apollo-adminservice-5cccf97c64-bhqzb   1/1     Running   0          72s
apollo-configservice-5f6555448-wwgsc   1/1     Running   1          2d17h
dubbo-monitor-6676dd74cc-9hghb         1/1     Running   11         17d
dubbo-monitor-6676dd74cc-rd86g         1/1     Running   10         17d
jenkins-b99776c69-p6skp 

5.浏览器访问config.od.com是否出现有apollo-adminservice

image-20200706164332402

posted on 2020-07-06 16:48  杨枫哥  阅读(184)  评论(0编辑  收藏