阿里云k8s上部署Apollo

dev环境k8s集群部署Apollo

基本信息

apollo版本: 1.7.1
数据库使用阿里云rds,版本为MySQL5.6
数据库用户: apollo db_passwd
数据库连接地址: rm-xxxxxxxxxxxxxxxxx.mysql.rds.aliyuncs.com

部署

下载1.7.1安装包

下载地址

ops@dev-jump:/tmp/apollo$ pwd
/tmp/apollo
ops@dev-jump:/tmp/apollo$ ls
apollo-1.7.1.zip  apollo-adminservice-1.7.1-github.zip  apollo-configservice-1.7.1-github.zip  apollo-portal-1.7.1-github.zip

处理安装包

# 创建部署目录
ops@dev-jump:/tmp/apollo$ mkdir ./deploy
ops@dev-jump:/tmp/apollo$ unzip apollo-1.7.1.zip
ops@dev-jump:/tmp/apollo$ unzip -o apollo-configservice-1.7.1-github.zip  -d ./deploy/apollo-config-server/
ops@dev-jump:/tmp/apollo$ unzip -o apollo-adminservice-1.7.1-github.zip -d ./deploy/apollo-admin-server/
ops@dev-jump:/tmp/apollo$ unzip -o apollo-portal-1.7.1-github.zip -d ./deploy/apollo-portal-server/

# 将部署所需文件移动至部署目录
ops@dev-jump:/tmp/apollo$ cp -R ./apollo-1.7.1/scripts/apollo-on-kubernetes/* ./deploy
ops@dev-jump:/tmp/apollo$ mv ./deploy/apollo-config-server/apollo-configservice-1.7.1.jar ./deploy/apollo-config-server/apollo-configservice.jar
ops@dev-jump:/tmp/apollo$ mv ./deploy/apollo-admin-server/apollo-adminservice-1.7.1.jar ./deploy/apollo-admin-server/apollo-adminservice.jar
ops@dev-jump:/tmp/apollo$ mv ./deploy/apollo-portal-server/apollo-portal-1.7.1.jar  ./deploy/apollo-portal-server/apollo-portal.jar

# 构建镜像
ops@dev-jump:/tmp/apollo$ docker build -t registry-vpc.cn-beijing.aliyuncs.com/xxx/alpine-bash:3.8 ./deploy/alpine-bash-3.8-image
ops@dev-jump:/tmp/apollo$ docker build -t registry-vpc.cn-beijing.aliyuncs.com/xxx/apollo-config-server:v1.7.1 ./deploy/apollo-config-server
ops@dev-jump:/tmp/apollo$ docker build -t registry-vpc.cn-beijing.aliyuncs.com/xxx/apollo-admin-server:v1.7.1 ./deploy/apollo-admin-server
ops@dev-jump:/tmp/apollo$ docker build -t registry-vpc.cn-beijing.aliyuncs.com/xxx/apollo-portal-server:v1.7.1 ./deploy/apollo-portal-server

# 推送镜像到镜像仓库
ops@dev-jump:/tmp/apollo$ docker push registry-vpc.cn-beijing.aliyuncs.com/xxx/alpine-bash:3.8
ops@dev-jump:/tmp/apollo$ docker push registry-vpc.cn-beijing.aliyuncs.com/xxx/apollo-config-server:v1.7.1
ops@dev-jump:/tmp/apollo$ docker push registry-vpc.cn-beijing.aliyuncs.com/xxx/apollo-admin-server:v1.7.1
ops@dev-jump:/tmp/apollo$ docker push registry-vpc.cn-beijing.aliyuncs.com/xxx/apollo-portal-server:v1.7.1

创建数据库

ops@dev-jump:/tmp/apollo$ ls ./deploy/db/
config-db-dev  config-db-prod  config-db-test-alpha  config-db-test-beta  portal-db

ops@dev-jump:/tmp/apollo$ ls ./deploy/db/config-db-dev/
apolloconfigdb.sql

ops@dev-jump:/tmp/apollo$ ls ./deploy/db/portal-db/
apolloportaldb.sql

打开上面两个sql文件,可以看到apollo需要创建两个数据库apolloportaldb、devapolloconfigdb
分别在rds控制台的"账号管理"和"数据库管理"上创建apollo用户和两个数据库,注意给apollo分配这两个数据库的读写权限
然后在dms控制台导入sql文件

部署apollo资源

apollo相关资源将部署在ops命名空间下

ops@dev-jump:/tmp/apollo$ ls ./deploy/kubernetes/apollo-env-dev/
service-apollo-admin-server-dev.yaml  service-apollo-config-server-dev.yaml  service-mysql-for-apollo-dev-env.yaml
apollo-config-server

修改安装包给的默认yaml文件

ops@dev-jump:/tmp/apollo$ vim ./deploy/kubernetes/apollo-env-dev/service-apollo-config-server-dev.yaml
# 修改资源名称,默认的资源名称太长了
# 所有资源的namespace改成ops
# 数据库连接地址、用户、用户名密码
# 因为只保留一个副本,故仅保留ConfigMap的第一个eureka.service.url
# 因修改了资源名称,故eureka.service.url也需要按照资源名称进行修改
# 修改label、selector,默认的太长了
# 同时StatfulSet副本保留1个
# 修改默认的镜像为咱们自己构建的镜像
# 增加imagePullSecrets,这是集群拉取镜像所使用的的secret
# 增加一个volume,挂载时区
# 修改APOLLO_CONFIG_SERVICE_NAME 环境变量的值

修改完如下

---
# configmap for apollo-config-server-dev
kind: ConfigMap
apiVersion: v1
metadata:
  namespace: ops
  name: apollo-config-server
data:
  application-github.properties: |
    spring.datasource.url = jdbc:mysql://rm-xxxxxxxxxxxxxxxxx.mysql.rds.aliyuncs.com:3306/DevApolloConfigDB?characterEncoding=utf8
    spring.datasource.username = apollo
    spring.datasource.password = db_passwd
    eureka.service.url = http://apollo-config-server-0.apollo-meta-server:8080/eureka/

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

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

---
kind: StatefulSet
apiVersion: apps/v1
metadata:
  namespace: ops
  name: apollo-config-server
  labels:
    app: apollo-config-server
spec:
  serviceName: apollo-meta-server
  replicas: 1
  selector:
    matchLabels:
      app: apollo-config-server
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: apollo-config-server
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - apollo-config-server
              topologyKey: kubernetes.io/hostname

      volumes:
        - name: volume-configmap-apollo-config-server-dev
          configMap:
            name: apollo-config-server
            items:
              - key: application-github.properties
                path: application-github.properties
        - hostPath:
            path: /usr/share/zoneinfo/Asia/Shanghai
            type: ""
          name: timezone
          # 增加时区
        
        
      containers:
        - image: registry-vpc.cn-beijing.aliyuncs.com/xxx/apollo-config-server:v1.7.1
          securityContext:
            privileged: true
          imagePullPolicy: IfNotPresent
          name: apollo-config-server
          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
            - mountPath: /etc/localtime
              name: timezone

          env:
            - name: APOLLO_CONFIG_SERVICE_NAME
              value: "apollo-config-server.ops"
              # 注意修改namespace

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

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

      dnsPolicy: ClusterFirst
      restartPolicy: Always
      imagePullSecrets:
      - name: regsecret
# 若未创建ns,则创建
ops@dev-jump:/tmp/apollo$ kubectl create namespace ops
ops@dev-jump:/tmp/apollo$ kubectl create -f ./deploy/kubernetes/apollo-env-dev/service-apollo-config-server-dev.yaml

观察apollo-config-server的Pod,若已正常启动,则可进入下一步

ops@dev-jump:/tmp/apollo$ watch 'kubectl get -f deploy/kubernetes/apollo-env-dev/service-apollo-config-server-dev.yaml -o wide '
apollo-admin-server
ops@dev-jump:/tmp/apollo$ vim  deploy/kubernetes/apollo-env-dev/service-apollo-admin-server-dev.yaml
# 修改的地方和apollo-config-server类似
# 修改initContainers启动命令中的url

修改完的yaml文件如下

---
kind: ConfigMap
apiVersion: v1
metadata:
  namespace: ops
  name: apollo-admin-server
data:
  application-github.properties: |
    spring.datasource.url = jdbc:mysql://rm-xxxxxxxxxxxxxxxxx.mysql.rds.aliyuncs.com:3306/DevApolloConfigDB?characterEncoding=utf8
    spring.datasource.username = apollo
    spring.datasource.password = db_passwd
    eureka.service.url = http://apollo-config-server-0.apollo-meta-server:8080/eureka/

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

---
kind: Deployment
apiVersion: apps/v1
metadata:
  namespace: ops
  name: apollo-admin-server
  labels:
    app: apollo-admin-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: apollo-admin-server
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: apollo-admin-server
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - apollo-admin-server
              topologyKey: kubernetes.io/hostname

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

      initContainers:
        - image: registry-vpc.cn-beijing.aliyuncs.com/xxx/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 apollo-config-server.ops:8080"]

      containers:
        - image: registry-vpc.cn-beijing.aliyuncs.com/xxx/apollo-admin-server:v1.7.1
          securityContext:
            privileged: true
          imagePullPolicy: IfNotPresent
          name: apollo-admin-server
          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: "apollo-admin-server.ops"

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

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

      dnsPolicy: ClusterFirst
      restartPolicy: Always
      imagePullSecrets:
      - name: regsecret

部署

ops@dev-jump:/tmp/apollo$ kubectl create -f deploy/kubernetes/apollo-env-dev/service-apollo-admin-server-dev.yaml
apollo-portal-server
ops@dev-jump:/tmp/apollo$ vim deploy/kubernetes/service-apollo-portal-server.yaml
# 修改命名空间
# 修改资源名称
# 去掉mysql svc和ep
# 修改数据库连接地址、用户密码
# 删除多余的apollo-env.properties,仅保留dev环境,并修改url
# 修改initContainers启动命令中的url,并去掉其他环境命令
# 修改APOLLO_PORTAL_SERVICE_NAME环境变量的值和命名空间

修改完的yaml文件如下

---
kind: ConfigMap
apiVersion: v1
metadata:
  namespace: ops
  name: apollo-portal-server
data:
  application-github.properties: |
    spring.datasource.url = jdbc:mysql://rm-xxxxxxxxxxxxxxxxx.mysql.rds.aliyuncs.com:3306/ApolloPortalDB?characterEncoding=utf8
    spring.datasource.username = apollo
    spring.datasource.password = db_passwd
  apollo-env.properties: |
    dev.meta=http://apollo-config-server.ops:8080

---
kind: Service
apiVersion: v1
metadata:
  namespace: ops
  name: apollo-portal-server
  labels:
    app: apollo-portal-server
spec:
  ports:
    - protocol: TCP
      port: 8070
      targetPort: 8070
      nodePort: 30001
  selector:
    app: apollo-portal-server
  type: NodePort
  sessionAffinity: ClientIP

---
kind: Deployment
apiVersion: apps/v1
metadata:
  namespace: ops
  name: apollo-portal-server
  labels:
    app: apollo-portal-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: apollo-portal-server
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: apollo-portal-server
    spec:
      affinity:
        podAntiAffinity:
          preferredDuringSchedulingIgnoredDuringExecution:
          - weight: 100
            podAffinityTerm:
              labelSelector:
                matchExpressions:
                - key: app
                  operator: In
                  values:
                  - apollo-portal-server
              topologyKey: kubernetes.io/hostname
      volumes:
        - name: volume-configmap-apollo-portal-server
          configMap:
            name: apollo-portal-server
            items:
              - key: application-github.properties
                path: application-github.properties
              - key: apollo-env.properties
                path: apollo-env.properties

      initContainers:
        - image: registry-vpc.cn-beijing.aliyuncs.com/xxx/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 apollo-admin-server.ops:8090"]

      containers:
        - image: registry-vpc.cn-beijing.aliyuncs.com/xxx/apollo-portal-server:v1.7.1
          securityContext:
            privileged: true
          imagePullPolicy: IfNotPresent
          name: 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: "apollo-portal-server.ops"

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

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

      dnsPolicy: ClusterFirst
      restartPolicy: Always
      imagePullSecrets:
      - name: regsecret

部署

ops@dev-jump:/tmp/apollo$ kubectl create -f deploy/kubernetes/service-apollo-portal-server.yaml

配置访问

使用阿里云内网slb,负载均衡到k8s集群的30001NodePort上
slb实例ip: 172.x.x.x
配置tcp监听,前端端口为30001,后端服务器为开发环境k8s-node,端口30001(apollo-portal-server的svc的NodePort端口)
本地浏览器访问172.x.x.x:30001,便可以看到apollo-portal界面

默认账号
apollo admin

修改密码为 my-apollo

参考文章

Apollo on k8s是指在Kubernetes(简称k8s)上部署和运行Apollo配置中心。Apollo是携程框架部门开发的一款开源配置管理平台,用于集中管理和动态配置应用程序的配置信息。Kubernetes是一个开源的容器编排平台,用于自动化部署、扩展和管理容器化应用程序。 将Apollo配置中心部署Kubernetes集群上可以带来以下好处: 1. 弹性伸缩:Kubernetes可以根据应用程序的负载情况自动调整Apollo实例的数量,以满足不同规模的应用需求。 2. 高可用性:Kubernetes提供了故障恢复和自动重启的机制,可以确保Apollo配置中心的高可用性。 3. 灵活性:Kubernetes支持多种部署策略,可以根据需要选择合适的部署方式,如单节点、多节点、分布式等。 4. 简化管理:Kubernetes提供了丰富的管理工具和API,可以方便地进行配置、监控和扩展等操作。 要在Kubernetes部署Apollo配置中心,通常需要进行以下步骤: 1. 创建Kubernetes集群:可以使用云服务提供商(如AWS、Azure、GCP)或自建集群。 2. 编写Apollo配置文件:根据应用程序的需求,编写Apollo的配置文件,包括应用信息、环境配置、数据库连接等。 3. 创建Kubernetes资源:使用Kubernetes的资源定义文件(如Deployment、Service、ConfigMap)创建Apollo的相关资源。 4. 部署Apollo配置中心:使用Kubernetes的命令或管理工具,将Apollo配置中心部署Kubernetes集群中。 5. 验证和测试:确保Apollo配置中心在Kubernetes上正常运行,并进行相关的验证和测试。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值