k8s 部署 apollo 配置中心

方便线上k8s部署pod 的时候 自动从apollo获取最新的配置

架构图
k8s 部署 apollo 配置中心_第1张图片

流程图k8s 部署 apollo 配置中心_第2张图片

部署数据库
1.1 四台mysql部署

mkdir -p /data/apollo/mysql-apollo-prd /data/apollo/mysql-apollo-test /data/apollo/mysql-apollo-dev /data/apollo/mysql-apollo-portal // nfs服务端创建文件夹
kubectl apply -f mysql-apollo-dev.yml # 开发 db

apiVersion: v1
kind: PersistentVolume
metadata:
  namespace: sre
  name: mysql-apollo-dev-pv
spec:
  accessModes:
    - ReadWriteMany
  capacity:
    storage: 150Gi
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nas
  nfs:
    path: /data/apollo/mysql-apollo-dev
    server: 192.168.1.20

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  namespace: sre
  name: mysql-apollo-dev-pvc
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: nas
  resources:
    requests:
      storage: 150Gi

---
apiVersion: v1
kind: Service
metadata:
  name: mysql-apollo-dev
  namespace: sre
spec:
  ports:
  - port: 3306
    nodePort: 30100
  type: NodePort
  selector:
    app: mysql-apollo-dev

---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: mysql-apollo-dev
  namespace: sre
spec:
  selector:
    matchLabels:
      app: mysql-apollo-dev
  template:
    metadata:
      labels:
        app: mysql-apollo-dev
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: password
        ports:
        - containerPort: 3306
          name: mysql-apollodev
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-apollo-dev-pvc

kubectl apply -f mysql-apollo-test.yml //测试 db

apiVersion: v1
kind: PersistentVolume
metadata:
  namespace: sre
  name: mysql-apollo-test-pv
spec:
  accessModes:
    - ReadWriteMany
  capacity:
    storage: 150Gi
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nas
  nfs:
    path: /data/apollo/mysql-apollo-test
    server: 192.168.1.20

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  namespace: sre
  name: mysql-apollo-test-pvc
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: nas
  resources:
    requests:
      storage: 150Gi

---
apiVersion: v1
kind: Service
metadata:
  name: mysql-apollo-test
  namespace: sre
spec:
  ports:
  - port: 3306
    nodePort: 30101
  type: NodePort
  selector:
    app: mysql-apollo-test

---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: mysql-apollo-test
  namespace: sre
spec:
  selector:
    matchLabels:
      app: mysql-apollo-test
  template:
    metadata:
      labels:
        app: mysql-apollo-test
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: password
        ports:
        - containerPort: 3306
          name: mysql-apollot
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-apollo-test-pvc

kubectl apply -f mysql-apollo-prd.yml // 生产 db

apiVersion: v1
kind: PersistentVolume
metadata:
  namespace: sre
  name: mysql-apollo-prd-pv
spec:
  accessModes:
    - ReadWriteMany
  capacity:
    storage: 150Gi
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nas
  nfs:
    path: /data/apollo/mysql-apollo-prd
    server: 192.168.1.20

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  namespace: sre
  name: mysql-apollo-prd-pvc
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: nas
  resources:
    requests:
      storage: 150Gi

---
apiVersion: v1
kind: Service
metadata:
  name: mysql-apollo-prd
  namespace: sre
spec:
  ports:
  - port: 3306
    nodePort: 30102
  type: NodePort
  selector:
    app: mysql-apollo-prd

---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: mysql-apollo-prd
  namespace: sre
spec:
  selector:
    matchLabels:
      app: mysql-apollo-prd
  template:
    metadata:
      labels:
        app: mysql-apollo-prd
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: password
        ports:
        - containerPort: 3306
          name: mysql-apolloprd
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-apollo-prd-pvc

kubectl apply -f mysql-apollo-portal.yml // 入口 db

apiVersion: v1
kind: PersistentVolume
metadata:
  namespace: sre
  name: mysql-apollo-portal-pv
spec:
  accessModes:
    - ReadWriteMany
  capacity:
    storage: 150Gi
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nas
  nfs:
    path: /data/apollo/mysql-apollo-portal
    server: 192.168.1.20

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  namespace: sre
  name: mysql-apollo-portal-pvc
spec:
  accessModes:
    - ReadWriteMany
  storageClassName: nas
  resources:
    requests:
      storage: 150Gi

---
apiVersion: v1
kind: Service
metadata:
  name: mysql-apollo-portal
  namespace: sre
spec:
  ports:
  - port: 3306
    nodePort: 30104
  type: NodePort
  selector:
    app: mysql-apollo-portal

---
apiVersion: apps/v1beta1
kind: Deployment
metadata:
  name: mysql-apollo-portal
  namespace: sre
spec:
  selector:
    matchLabels:
      app: mysql-apollo-portal
  template:
    metadata:
      labels:
        app: mysql-apollo-portal
    spec:
      containers:
      - image: mysql:5.6
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: password
        ports:
        - containerPort: 3306
          name: mysql-apolloptl
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-apollo-portal-pvc

2.1.1 导入db相关的数据

# cd apollo/scripts/apollo-on-kubernetes/db/
# mysql -h127.0.0.1 -uroot -ppassword -P30100 < config-db-dev/apolloconfigdb.sql
# mysql -h127.0.0.1 -uroot -ppassword -P30101 < config-db-test-beta/apolloconfigdb.sql 
# mysql -h127.0.0.1 -uroot -ppassword -P30102 < config-db-prod/apolloconfigdb.sql
# mysql -h127.0.0.1 -uroot -ppassword -P30103 < portal-db/apolloconfigdb.sql 
  1. 构建镜像

https://github.com/ctripcorp/apollo/releases # 找到3个包
在这里插入图片描述

# wget https://github.com/ctripcorp/apollo/releases/download/v1.3.0/apollo-adminservice-1.3.0-github.zip
# wget https://github.com/ctripcorp/apollo/releases/download/v1.3.0/apollo-configservice-1.3.0-github.zip
# wget https://github.com/ctripcorp/apollo/releases/download/v1.3.0/apollo-portal-1.3.0-github.zip

2.1 # 解压3个包 相关的 jar 命名为
apollo-adminservice.jar
apollo-configservice.jar
apollo-portal.jar

jar包移动对应的目录 apollo-admin-server apollo-config-server apollo-portal-server

vim apollo-admin-server/Dockerfile //注释一行 避免文件configmap只读 3个Dockerfile都需要修改
vim apollo-config-server/Dockerfile
vim apollo-portal-server/Dockerfile
#ENTRYPOINT ["/apollo-admin-server/entrypoint.sh"]

2.2 build相关的镜像

# cd apollo/scripts/apollo-on-kubernetes/alpine-bash-3.8-image
# docker build -t apollo:alpine-bash-3.8 .
# docker push apollo:alpine-bash-3.8

# cd apollo/scripts/apollo-on-kubernetes/apollo-admin-server
# docker build -t apollo:apollo-admin-server-v1.3.0 .
# docker push apollo:apollo-admin-server-v1.3.0

# cd apollo/scripts/apollo-on-kubernetes/apollo-config-server
# docker build -t apollo:apollo-config-server-v1.3.0 .
# docker push  apollo:apollo-config-server-v1.3.0

# cd  apollo-portal-server
# docker build -t apollo:apollo-portal-server-v1.3.0 
# docker push apollo:apollo-portal-server-v1.3.0
  1. 修改配置文件
    cd apollo/scripts/apollo-on-kubernetes/kubernetes
    3.1 dev
    kubectl apply -f apollo-env-dev/service-apollo-admin-server-dev.yaml
#configmap for apollo-admin-server-dev
kind: ConfigMap
apiVersion: v1
metadata:
  namespace: sre
  name: configmap-apollo-admin-server-dev
data:
  application-github.properties: |
    spring.datasource.url = jdbc:mysql://mysql-apollo-dev:3306/DevApolloConfigDB?characterEncoding=utf8
    spring.datasource.username = root
    spring.datasource.password = password
    eureka.service.url = http://statefulset-apollo-config-server-dev-0.service-apollo-meta-server-dev:8080/eureka/,http://statefulset-apollo-config-server-dev-1.service-apollo-meta-server-dev:8080/eureka/,http://statefulset-apollo-config-server-dev-2.service-apollo-meta-server-dev:8080/eureka/

---
kind: Service
apiVersion: v1
metadata:
  namespace: sre
  name: service-apollo-admin-server-dev
  labels:
    app: service-apollo-admin-server-dev
spec:
  ports:
    - protocol: TCP
      port: 8090
      targetPort: 8090
  selector:
    app: pod-apollo-admin-server-dev
  type: ClusterIP
  sessionAffinity: ClientIP

---
kind: Deployment
apiVersion: apps/v1beta2
metadata:
  namespace: sre
  name: deployment-apollo-admin-server-dev
  labels:
    app: deployment-apollo-admin-server-dev
spec:
  replicas: 3
  selector:
    matchLabels:
      app: pod-apollo-admin-server-dev
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: pod-apollo-admin-server-dev
    spec:

      volumes:
        - name: volume-configmap-apollo-admin-server-dev
          configMap:
            name: configmap-apollo-admin-server-dev
            items:
              - key: application-github.properties
                path: application-github.properties

      initContainers:
        #- image: alpine-bash:3.8
        - image: apollo:alpine-bash-3.8
          name: check-service-apollo-config-server-dev
          command: ['bash', '-c', "curl --connect-timeout 2 --max-time 5 --retry 60 --retry-delay 1 --retry-max-time 120 service-apollo-config-server-dev.sre:8080"]

      containers:
        #- image: apollo-admin-server:v1.0.0
        - image: apollo:apollo-admin-server-v1.3.0
          securityContext:
            privileged: true
          imagePullPolicy: IfNotPresent
          name: container-apollo-admin-server-dev
          ports:
            - protocol: TCP
              containerPort: 8090

          volumeMounts:
            - name: volume-configmap-apollo-admin-server-dev
              mountPath: /apollo-admin-server/config/application-github.properties
              subPath: application-github.properties

          env:
            - name: APOLLO_ADMIN_SERVICE_NAME
              value: "service-apollo-admin-server-dev.sre"

          readinessProbe:
            tcpSocket:
              port: 8090
            initialDelaySeconds: 10
            periodSeconds: 5

          livenessProbe:
            tcpSocket:
              port: 8090
            initialDelaySeconds: 120
            periodSeconds: 10

      dnsPolicy: ClusterFirst
      restartPolicy: Always

kubectl apply -f apollo-env-dev/service-apollo-config-server-dev.yaml

# configmap for apollo-config-server-dev

kind: ConfigMap
apiVersion: v1
metadata:
  namespace: sre
  name: configmap-apollo-config-server-dev
data:
  application-github.properties: |
    spring.datasource.url = jdbc:mysql://mysql-apollo-dev:3306/DevApolloConfigDB?characterEncoding=utf8
    spring.datasource.username = root
    spring.datasource.password = password
    eureka.service.url = http://statefulset-apollo-config-server-dev-0.service-apollo-meta-server-dev:8080/eureka/,http://statefulset-apollo-config-server-dev-1.service-apollo-meta-server-dev:8080/eureka/,http://statefulset-apollo-config-server-dev-2.service-apollo-meta-server-dev:8080/eureka/

---
kind: Service
apiVersion: v1
metadata:
  namespace: sre
  name: service-apollo-meta-server-dev
  labels:
    app: service-apollo-meta-server-dev
spec:
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
  selector:
    app: pod-apollo-config-server-dev
  type: ClusterIP
  clusterIP: None
  sessionAffinity: ClientIP

---
kind: Service
apiVersion: v1
metadata:
  namespace: sre
  name: service-apollo-config-server-dev
  labels:
    app: service-apollo-config-server-dev
spec:
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
      nodePort: 30002
  selector:
    app: pod-apollo-config-server-dev 
  type: NodePort
  sessionAffinity: ClientIP

---
kind: StatefulSet
apiVersion: apps/v1beta2
metadata:
  namespace: sre
  name: statefulset-apollo-config-server-dev
  labels:
    app: statefulset-apollo-config-server-dev
spec:
  serviceName: service-apollo-meta-server-dev
  replicas: 3
  selector:
    matchLabels:
      app: pod-apollo-config-server-dev
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: pod-apollo-config-server-dev
    spec:

      volumes:
        - name: volume-configmap-apollo-config-server-dev
          configMap:
            name: configmap-apollo-config-server-dev
            items:
              - key: application-github.properties
                path: application-github.properties

      containers:
        #- image: apollo-config-server:v1.0.0
        - image: apollo:apollo-config-server-v1.3.0
          securityContext:
            privileged: true
          imagePullPolicy: IfNotPresent
          name: container-apollo-config-server-dev
          ports:
            - protocol: TCP
              containerPort: 8080

          volumeMounts:
            - name: volume-configmap-apollo-config-server-dev
              mountPath: /apollo-config-server/config/application-github.properties
              subPath: application-github.properties

          env:
            - name: APOLLO_CONFIG_SERVICE_NAME
              value: "service-apollo-config-server-dev.sre"

          readinessProbe:
            tcpSocket:
              port: 8080
            initialDelaySeconds: 10
            periodSeconds: 5

          livenessProbe:
            tcpSocket:
              port: 8080
            initialDelaySeconds:  120
            periodSeconds: 10

      dnsPolicy: ClusterFirst
      restartPolicy: Always

3.2 test

kubectl apply -f apollo-env-test-beta/service-apollo-admin-server-test-beta.yaml

# configmap for apollo-admin-server-test-beta

kind: ConfigMap
apiVersion: v1
metadata:
  namespace: sre
  name: configmap-apollo-admin-server-test-beta
data:
  application-github.properties: |
    spring.datasource.url = jdbc:mysql://mysql-apollo-test.sre:3306/TestBetaApolloConfigDB?characterEncoding=utf8
    spring.datasource.username = root
    spring.datasource.password = password
    eureka.service.url = http://statefulset-apollo-config-server-test-beta-0.service-apollo-meta-server-test-beta:8080/eureka/,http://statefulset-apollo-config-server-test-beta-1.service-apollo-meta-server-test-beta:8080/eureka/,http://statefulset-apollo-config-server-test-beta-2.service-apollo-meta-server-test-beta:8080/eureka/

---
kind: Service
apiVersion: v1
metadata:
  namespace: sre
  name: service-apollo-admin-server-test-beta
  labels:
    app: service-apollo-admin-server-test-beta
spec:
  ports:
    - protocol: TCP
      port: 8090
      targetPort: 8090
  selector:
    app: pod-apollo-admin-server-test-beta
  type: ClusterIP
  sessionAffinity: ClientIP

---
kind: Deployment
apiVersion: apps/v1beta2
metadata:
  namespace: sre
  name: deployment-apollo-admin-server-test-beta
  labels:
    app: deployment-apollo-admin-server-test-beta
spec:
  replicas: 3
  selector:
    matchLabels:
      app: pod-apollo-admin-server-test-beta
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: pod-apollo-admin-server-test-beta
    spec:
      nodeSelector:
        node: "apollo"

      volumes:
        - name: volume-configmap-apollo-admin-server-test-beta
          configMap:
            name: configmap-apollo-admin-server-test-beta
            items:
              - key: application-github.properties
                path: application-github.properties

      initContainers:
        - image: apollo:alpine-bash-3.8
        #- image: alpine-bash:3.8
          name: check-service-apollo-config-server-test-beta
          command: ['bash', '-c', "curl --connect-timeout 2 --max-time 5 --retry 60 --retry-delay 1 --retry-max-time 120 service-apollo-config-server-test-beta.sre:8080"]

      containers:
        #- image: apollo-admin-server:v1.0.0
        - image: apollo:apollo-admin-server-v1.3.0
          imagePullPolicy: IfNotPresent
          name: container-apollo-admin-server-test-beta
          ports:
            - protocol: TCP
              containerPort: 8090

          volumeMounts:
            - name: volume-configmap-apollo-admin-server-test-beta
              mountPath: /apollo-admin-server/config/application-github.properties
              subPath: application-github.properties

          env:
            - name: APOLLO_ADMIN_SERVICE_NAME
              value: "service-apollo-admin-server-test-beta.sre"

          readinessProbe:
            tcpSocket:
              port: 8090
            initialDelaySeconds: 10
            periodSeconds: 5

          livenessProbe:
            tcpSocket:
              port: 8090
            initialDelaySeconds: 120
            periodSeconds: 10

      dnsPolicy: ClusterFirst
      restartPolicy: Always

# kubectl apply -f apollo-env-test-beta/service-apollo-config-server-test-beta.yaml 
---
# configmap for apollo-config-server-test-beta
kind: ConfigMap
apiVersion: v1
metadata:
  namespace: sre
  name: configmap-apollo-config-server-test-beta
data:
  application-github.properties: |
    spring.datasource.url = jdbc:mysql://mysql-apollo-test.sre:3306/TestBetaApolloConfigDB?characterEncoding=utf8
    spring.datasource.username = root
    spring.datasource.password = password
    eureka.service.url = http://statefulset-apollo-config-server-test-beta-0.service-apollo-meta-server-test-beta:8080/eureka/,http://statefulset-apollo-config-server-test-beta-1.service-apollo-meta-server-test-beta:8080/eureka/,http://statefulset-apollo-config-server-test-beta-2.service-apollo-meta-server-test-beta:8080/eureka/

---
kind: Service
apiVersion: v1
metadata:
  namespace: sre
  name: service-apollo-meta-server-test-beta
  labels:
    app: service-apollo-meta-server-test-beta
spec:
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
  selector:
    app: pod-apollo-config-server-test-beta
  type: ClusterIP
  clusterIP: None
  sessionAffinity: ClientIP

---
kind: Service
apiVersion: v1
metadata:
  namespace: sre
  name: service-apollo-config-server-test-beta
  labels:
    app: service-apollo-config-server-test-beta
spec:
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
      nodePort: 30111
  selector:
    app: pod-apollo-config-server-test-beta
  type: NodePort
  sessionAffinity: ClientIP

---
kind: StatefulSet
apiVersion: apps/v1beta2
metadata:
  namespace: sre
  name: statefulset-apollo-config-server-test-beta
  labels:
    app: statefulset-apollo-config-server-test-beta
spec:
  serviceName: service-apollo-meta-server-test-beta
  replicas: 3
  selector:
    matchLabels:
      app: pod-apollo-config-server-test-beta
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: pod-apollo-config-server-test-beta
    spec:

      volumes:
        - name: volume-configmap-apollo-config-server-test-beta
          configMap:
            name: configmap-apollo-config-server-test-beta
            items:
              - key: application-github.properties
                path: application-github.properties

      containers:
        #- image: apollo-config-server:v1.0.0
        - image: apollo:apollo-config-server-v1.3.0
          securityContext:
            privileged: true
          imagePullPolicy: IfNotPresent
          name: container-apollo-config-server-test-beta
          ports:
            - protocol: TCP
              containerPort: 8080

          volumeMounts:
            - name: volume-configmap-apollo-config-server-test-beta
              mountPath: /apollo-config-server/config/application-github.properties
              subPath: application-github.properties
          env:
            - name: APOLLO_CONFIG_SERVICE_NAME
              value: "service-apollo-config-server-test-beta.sre"

          readinessProbe:
            tcpSocket:
              port: 8080
            initialDelaySeconds: 10
            periodSeconds: 5

          livenessProbe:
            tcpSocket:
              port: 8080
            initialDelaySeconds: 120
            periodSeconds: 15

      dnsPolicy: ClusterFirst
      restartPolicy: Always

3.3 prd

kubectl apply -f apollo-env-prod/service-apollo-admin-server-prod.yaml

# configmap for apollo-admin-server-prod

kind: ConfigMap
apiVersion: v1
metadata:
  namespace: sre
  name: configmap-apollo-admin-server-prod
data:
  application-github.properties: |
    spring.datasource.url = jdbc:mysql://mysql-apollo-prd.sre:3306/ProdApolloConfigDB?characterEncoding=utf8
    spring.datasource.username = root
    spring.datasource.password = password
    eureka.service.url = http://statefulset-apollo-config-server-prod-0.service-apollo-meta-server-prod:8080/eureka/,http://statefulset-apollo-config-server-prod-1.service-apollo-meta-server-prod:8080/eureka/,http://statefulset-apollo-config-server-prod-2.service-apollo-meta-server-prod:8080/eureka/

---
kind: Service
apiVersion: v1
metadata:
  namespace: sre
  name: service-apollo-admin-server-prod
  labels:
    app: service-apollo-admin-server-prod
spec:
  ports:
    - protocol: TCP
      port: 8090
      targetPort: 8090
  selector:
    app: pod-apollo-admin-server-prod  
  type: ClusterIP
  sessionAffinity: ClientIP

---
kind: Deployment
apiVersion: apps/v1beta2
metadata:
  namespace: sre
  name: deployment-apollo-admin-server-prod
  labels:
    app: deployment-apollo-admin-server-prod
spec:
  replicas: 3
  selector:
    matchLabels:
      app: pod-apollo-admin-server-prod
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: pod-apollo-admin-server-prod
    spec:

      volumes:
        - name: volume-configmap-apollo-admin-server-prod
          configMap:
            name: configmap-apollo-admin-server-prod
            items:
              - key: application-github.properties
                path: application-github.properties

      initContainers:
        #- image: alpine-bash:3.8
        - image: apollo:alpine-bash-3.8
          name: check-service-apollo-config-server-prod
          command: ['bash', '-c', "curl --connect-timeout 2 --max-time 5 --retry 50 --retry-delay 1 --retry-max-time 120 service-apollo-config-server-prod.sre:8080"]

      containers:
        #- image: apollo-admin-server:v1.0.0
        - image: apollo:apollo-admin-server-v1.3.0
          securityContext:
            privileged: true
          imagePullPolicy: IfNotPresent
          name: container-apollo-admin-server-prod
          ports:
            - protocol: TCP
              containerPort: 8090

          volumeMounts:
            - name: volume-configmap-apollo-admin-server-prod
              mountPath: /apollo-admin-server/config/application-github.properties
              subPath: application-github.properties

          env:
            - name: APOLLO_ADMIN_SERVICE_NAME
              value: "service-apollo-admin-server-prod.sre"

          readinessProbe:
            tcpSocket:
              port: 8090
            initialDelaySeconds: 10
            periodSeconds: 5

          livenessProbe:
            tcpSocket:
              port: 8090
            initialDelaySeconds: 120
            periodSeconds: 10

      dnsPolicy: ClusterFirst
      restartPolicy: Always

apollo-env-prod/service-apollo-config-server-prod.yaml
---
# configmap for apollo-config-server-prod
kind: ConfigMap
apiVersion: v1
metadata:
  namespace: sre
  name: configmap-apollo-config-server-prod
data:
  application-github.properties: |
    spring.datasource.url = jdbc:mysql://mysql-apollo-prd.sre:3306/ProdApolloConfigDB?characterEncoding=utf8
    spring.datasource.username = root
    spring.datasource.password = password
    eureka.service.url = http://statefulset-apollo-config-server-prod-0.service-apollo-meta-server-prod:8080/eureka/,http://statefulset-apollo-config-server-prod-1.service-apollo-meta-server-prod:8080/eureka/,http://statefulset-apollo-config-server-prod-2.service-apollo-meta-server-prod:8080/eureka/

---
kind: Service
apiVersion: v1
metadata:
  namespace: sre
  name: service-apollo-meta-server-prod
  labels:
    app: service-apollo-meta-server-prod
spec:
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
  selector:
    app: pod-apollo-config-server-prod
  type: ClusterIP
  clusterIP: None
  sessionAffinity: ClientIP

---
kind: Service
apiVersion: v1
metadata:
  namespace: sre
  name: service-apollo-config-server-prod
  labels:
    app: service-apollo-config-server-prod
spec:
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 8080
      nodePort: 30112
  selector:
    app: pod-apollo-config-server-prod
  type: NodePort
  sessionAffinity: ClientIP

---
kind: StatefulSet
apiVersion: apps/v1beta2
metadata:
  namespace: sre
  name: statefulset-apollo-config-server-prod
  labels:
    app: statefulset-apollo-config-server-prod
spec:
  serviceName: service-apollo-meta-server-prod
  replicas: 3
  selector:
    matchLabels:
      app: pod-apollo-config-server-prod
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: pod-apollo-config-server-prod
    spec:

      volumes:
        - name: volume-configmap-apollo-config-server-prod
          configMap:
            name: configmap-apollo-config-server-prod
            items:
              - key: application-github.properties
                path: application-github.properties

      containers:
        #- image: apollo-config-server:v1.0.0
        - image: apollo:apollo-config-server-v1.3.0
          securityContext:
            privileged: true
          imagePullPolicy: IfNotPresent
          name: container-apollo-config-server-prod
          ports:
            - protocol: TCP
              containerPort: 8080

          volumeMounts:
            - name: volume-configmap-apollo-config-server-prod
              mountPath: /apollo-config-server/config/application-github.properties
              subPath: application-github.properties
          env:
            - name: APOLLO_CONFIG_SERVICE_NAME
              value: "service-apollo-config-server-prod.sre"

          readinessProbe:
            tcpSocket:
              port: 8080
            initialDelaySeconds: 10
            periodSeconds: 5

          livenessProbe:
            tcpSocket:
              port: 8080
            initialDelaySeconds: 120
            periodSeconds: 10

      dnsPolicy: ClusterFirst
      restartPolicy: Always

3.4 portal

kubectl apply -f service-apollo-portal-server.yaml

# configmap for apollo-portal-server
kind: ConfigMap
apiVersion: v1
metadata:
  namespace: sre
  name: configmap-apollo-portal-server
data:
  application-github.properties: |
    spring.datasource.url = jdbc:mysql://mysql-apollo-portal.sre:3306/ApolloPortalDB?characterEncoding=utf8
    # mysql username
    spring.datasource.username = root
    # mysql password
    spring.datasource.password = password
  apollo-env.properties: |
    dev.meta=http://service-apollo-config-server-dev.sre:8080
    #fat.meta=http://service-apollo-config-server-test-alpha.sre:8080
    uat.meta=http://service-apollo-config-server-test-beta.sre:8080
    pro.meta=http://service-apollo-config-server-prod.sre:8080

---
kind: Service
apiVersion: v1
metadata:
  namespace: sre
  name: service-apollo-portal-server
  labels:
    app: service-apollo-portal-server
spec:
  ports:
    - protocol: TCP
      port: 8070
      targetPort: 8070
      nodePort: 30110
  selector:
    app: pod-apollo-portal-server
  type: NodePort
  # portal session 保持
  sessionAffinity: ClientIP

---
kind: Deployment
apiVersion: apps/v1beta2
metadata:
  namespace: sre
  name: deployment-apollo-portal-server
  labels:
    app: deployment-apollo-portal-server
spec:
  # 3 个实例
  replicas: 1
  selector:
    matchLabels:
      app: pod-apollo-portal-server
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: pod-apollo-portal-server
    spec:

      volumes:
        - name: volume-configmap-apollo-portal-server
          configMap:
            name: configmap-apollo-portal-server
            items:
              - key: application-github.properties
                path: application-github.properties
              - key: apollo-env.properties
                path: apollo-env.properties

      initContainers:
        # 确保 admin-service 正常提供服务
        #- image: alpine-bash:3.8
        - image: apollo:alpine-bash-3.8
          name: check-service-apollo-admin-server-dev
          command: ['bash', '-c', "curl --connect-timeout 2 --max-time 5 --retry 60 --retry-delay 1 --retry-max-time 120 service-apollo-admin-server-dev.sre:8090"]
        #- image: alpine-bash:3.8
        #  name: check-service-apollo-admin-server-alpha
        #  command: ['bash', '-c', "curl --connect-timeout 2 --max-time 5 --retry 60 --retry-delay 1 --retry-max-time 120 service-apollo-admin-server-test-alpha.sre:8090"]
        #- image: alpine-bash:3.8
        - image: apollo:alpine-bash-3.8
          name: check-service-apollo-admin-server-beta
          command: ['bash', '-c', "curl --connect-timeout 2 --max-time 5 --retry 60 --retry-delay 1 --retry-max-time 120 service-apollo-admin-server-test-beta.sre:8090"]
        #- image: alpine-bash:3.8
        - image: apollo:alpine-bash-3.8
          name: check-service-apollo-admin-server-prod
          command: ['bash', '-c', "curl --connect-timeout 2 --max-time 5 --retry 60 --retry-delay 1 --retry-max-time 120 service-apollo-admin-server-prod.sre:8090"]    

      containers:
        #- image: apollo-portal-server:v1.0.0    # 更改为你的 docker registry 下的 image
        - image: apollo:apollo-portal-server-v1.3.0    # 更改为你的 docker registry 下的 image
          securityContext:
            privileged: true
          imagePullPolicy: IfNotPresent
          name: container-apollo-portal-server
          ports:
            - protocol: TCP
              containerPort: 8070

          volumeMounts:
            - name: volume-configmap-apollo-portal-server
              mountPath: /apollo-portal-server/config/application-github.properties
              subPath: application-github.properties
            - name: volume-configmap-apollo-portal-server
              mountPath: /apollo-portal-server/config/apollo-env.properties
              subPath: apollo-env.properties

          env:
            - name: APOLLO_PORTAL_SERVICE_NAME
              value: "service-apollo-portal-server.sre"

          readinessProbe:
            tcpSocket:
              port: 8070
            initialDelaySeconds: 10
            periodSeconds: 5

          livenessProbe:
            tcpSocket:
              port: 8070
            # 120s 内, server 未启动则重启 container
            initialDelaySeconds: 120
            periodSeconds: 15

      dnsPolicy: ClusterFirst
      restartPolicy: Always

打开portal 入口
http://127.0.0.1:30110/ # 查看是否正常
默认账号密码: apollo admin

如果需要外部访问的话就需要添加对外服务的配置
admin和config都需要配置 启动参数

      - name: JAVA_OPTS
          value: "-Dapollo.configService=http://127.0.0.1:30002 -Deureka.instance.homePageUrl=http://127.0.0.1:30002"

点外卖的小伙伴可以进群
k8s 部署 apollo 配置中心_第3张图片

你可能感兴趣的:(k8s 部署 apollo 配置中心)