Skip to main content

在 K8S 中使用 Helm 部署微服务配置中心 Apollo

准备条件

环境规划

服务名称类型对应环境Database Namevalues.yaml
apollo-service-pro配置中心后端应用pro 生产环境apollo_config_provalues-apollo-service-pro.yaml
apollo-service-uat配置中心后端应用uat 预发布环境apollo_config_uatvalues-apollo-service-uat.yaml
apollo-portalWeb前端共用apollo_portalvalues-apollo-portal.yaml

创建外部 MySQL Database

外部数据库 MySQL 使用的是云高可用 MySQL 8.0,为两个环境分别创建 database ,分配授权一个可读写用户 apollo

导入 SQL

注:

注意

ApolloConfigDB 需要在每个环境部署一套,如本案例中 uat 和 pro 就需要部署2套 ApolloConfigDB

apollo-service

获取 Chart

添加仓库:

helm repo add apollo https://charts.apolloconfig.com
helm repo update

查看 bitnami/rabbitmq 仓库历史版本的 Charts,选择一个版本

$ helm search repo apollo/apollo-service --versions
NAME CHART VERSION APP VERSION DESCRIPTION
apollo/apollo-service 0.10.0 2.4.0 A Helm chart for Apollo Config Service and Apol...
apollo/apollo-service 0.9.0 2.3.0 A Helm chart for Apollo Config Service and Apol...
apollo/apollo-service 0.8.0 2.3.0 A Helm chart for Apollo Config Service and Apol...
apollo/apollo-service 0.7.0 2.2.0 A Helm chart for Apollo Config Service and Apol...
apollo/apollo-service 0.6.1 2.1.0 A Helm chart for Apollo Config Service and Apol...
apollo/apollo-service 0.6.0 2.1.0 A Helm chart for Apollo Config Service and Apol...
...


$ helm search repo apollo/apollo-portal --versions

本实例选择 Chart 0.6.1,对应的 apollo-service 版本为 2.1.0,获取 Chart

# 获取离线 Chart
helm pull apollo/apollo-service --version 0.6.1
helm search repo apollo/apollo-portal --versions 0.6.1

# 获取默认 values.yaml
helm show values apollo-service-0.6.1.tgz > values-apollo-service-pro.yaml
helm show values apollo-service-0.6.1.tgz > values-apollo-service-uat.yaml
helm show values apollo-portal-0.6.1.tgz > values-apollo-portal.yaml

配置生产环境 values-apollo-service-pro.yaml

configdb

configdb 使用的是外部 MySQL 8.0+ 云数据库实例,并且连接串是 IP:

configdb:
name: apollo-configdb

# 外部 mysql 数据库
host: "10.xx.xx.xx"
port: 3306
dbName: apollo_config_pro

# 外部 mysql 数据库账号密码
userName: "apollo"
password: "apollo"
connectionStringProperties: characterEncoding=utf8&useSSL=false
service:

# NodePort 暴露
enabled: true
fullNameOverride: ""
port: 3306
type: NodePort

如果 MySQL 数据库连接串是 ConfigDB的host是k8s集群外的域名ConfigDB的host是k8s集群内的一个服务 需要另外参考官方文档配置。

configService

configService:

# 设置副本数量
replicaCount: 1
image:

# 固定镜像版本号
repository: apolloconfig/apollo-configservice
tag: "2.1.0"

# NodePort 暴露
service:
type: NodePort

# 设置时区
env:
TZ: Asia/Shanghai
JAVA_OPTS: "-Duser.timezone=Asia/Shanghai"

# 限制资源
resources:
requests:
cpu: 0.2
memory: 1Gi
limits:
cpu: "1"
memory: 1Gi

adminService

adminService:
replicaCount: 1
image:
repository: apolloconfig/apollo-adminservice
tag: "2.1.0"
pullPolicy: IfNotPresent
service:
type: NodePort
env:
TZ: Asia/Shanghai
JAVA_OPTS: "-Duser.timezone=Asia/Shanghai"
resources:
requests:
cpu: 0.2
memory: 1Gi
limits:
cpu: "1"
memory: 1Gi
注意

以相同的方式修改 uat 预发布环境的 values-apollo-service-uat.yaml

以 Ingress 配置自定义路径 /config 形式暴露 apollo-configservice 服务

这个场景用于 K8S 集群外的应用,要访问 apollo-configservice 服务,要将其以域名的形式暴露出来。

configService:
ingress:
ingressClassName: ${INGRESSCLASEE}
enabled: true
annotations:
nginx.ingress.kubernetes.io/proxy-read-timeout: "300"
nginx.ingress.kubernetes.io/proxy-send-timeout: "300"
nginx.ingress.kubernetes.io/proxy-connect-timeout: "300"
nginx.ingress.kubernetes.io/keep-alive-timeout: "300"
hosts:
- host: apollo-configservice-pro.example.com
paths:
- /config

另外 Nginx Ingress Controller 需要增加 keepalive 心跳配置:

controller:
config:
upstream-keepalive-timeout: "300"
说明:

Nginx 默认的 keepailved 时间为 75s,在与 K8S 集群外部的 apollo-client 与建联是,发现每隔 75s 会引起心跳中断,以 WARN 告警的形式在业务应用中输出:

因此可知 apollo-client 的心跳保活时间是小于 75s 的。所以我们应当调大 Nginx 的 Keepalived 时间来规避这个告警,避免 apollo-client 与 apollo-configservice 重新 TCP 建联浪费开销。

指定Meta Server返回的apollo-configservice地址

对于 K8S 集群外部的 apollo-client 无法直接访问 apollo-configservice 的 Service,还需要参照下面的示例指定 Meta Server 返回给 apollo-client 的为外部的域名地址

configService:
config:
# override apollo.config-service.url: config service url to be accessed by apollo-client
configServiceUrlOverride: "http://apollo-configservice-pro.example.com/config"
# specify the context path, e.g. /apollo
contextPath: "/config"
apollo-configservic 统一对外的域名地址
  • 最终无论业务应用是在 K8S 集群内部、或外部,统一提供 apollo-configservice 的地址为带 /config 的 Ingress 域名:http://apollo-configservice-pro.example.com/config
  • 而 apollo-adminservice 为管理服务端,无关紧要,不需要绑定自定义域名,直接使用默认的集群内部域名即可。

apollo-portal

portaldb

portaldb:
name: apollo-portaldb
host: 10.xx.xx.xx
port: 3306
dbName: apollo_portal
userName: apollo
password: apollo
connectionStringProperties: characterEncoding=utf8
service:
enabled: true
fullNameOverride: ""
port: 3306
type: NodePort

固定镜像版本号,方便以后升级:

image:
repository: apolloconfig/apollo-portal
tag: "2.1.0"

service:
type: NodePort

replicaCount: 1

env:
TZ: Asia/Shanghai
JAVA_OPTS: "-Duser.timezone=Asia/Shanghai"

resources:
requests:
cpu: 0.2
memory: 1Gi
limits:
cpu: 1
memory: 1Gi

config

config:
# specify the env names, e.g. dev,pro
envs: "uat,pro"
# specify the meta servers, e.g.
# dev: http://apollo-configservice-dev:8080
# pro: http://apollo-configservice-pro:8080
metaServers:
uat: http://apollo-configservice-pro.example.com
pro: http://apollo-configservice-uat.example.cn
# specify the context path, e.g. /apollo
contextPath: ""

Ingress Web后台

以下生产配置案例,分别使用 TLS 和 白名单,在公网环境下安全加固。

ingress:
ingressClassName: clb
enabled: true

# 公网环境下限制白名单
annotations:
nginx.ingress.kubernetes.io/whitelist-source-range: "10.0.0.0/8,172.16.0.0/12,192.168.0.0/16"

# 绑定域名与 TLS
hosts:
- host: apollomg.example.com
paths:
- /
tls:
- host: apollomg.example.com
secretName: ssl-star-example-com

安装/更新 Chart

pro 环境:

# 部署 Chart
helm install apollo-service-pro -f values-apollo-service-pro.yaml -n ${APP_NAMESPACE} apollo-service-0.6.1.tgz

# 更新 Release
helm upgrade --install apollo-service-pro -f values-apollo-service-pro.yaml -n ${APP_NAMESPACE} apollo-service-0.6.1.tgz

uat 环境:

# 部署 Chart
helm install apollo-service-uat -f values-apollo-service-uat.yaml -n ${APP_NAMESPACE} apollo-service-0.6.1.tgz

# 更新 Release
helm upgrade --install apollo-service-uat -f values-apollo-service-uat.yaml -n ${APP_NAMESPACE} apollo-service-0.6.1.tgz

apollo-portal

# 部署 Chart
helm install apollo-portal -f values-apollo-portal.yaml -n ${APP_NAMESPACE} apollo-portal-0.6.1.tgz

# 更新 Release
helm upgrade --install apollo-portal -f values-apollo-portal.yaml -n ${APP_NAMESPACE} apollo-portal-0.6.1.tgz

访问服务

apollo-service K8S 集群内部 DNS

#apollo-service-pro
Config service: http://apollo-service-pro-apollo-configservice.${APP_NAMESPACE}:8080
Admin service: http://apollo-service-pro-apollo-adminservice.${APP_NAMESPACE}:8090

#apollo-service-uat
Config service: http://apollo-service-uat-apollo-configservice.${APP_NAMESPACE}:8080
Admin service: http://apollo-service-uat-apollo-adminservice.${APP_NAMESPACE}:8090

使用 Ingress 域名访问 apoll-configservice (推荐)

http://apollo-configservice-pro.example.com/config
http://apollo-configservice-uat.example.cn/config

apollo-portal Web 管理后台

在白名单访问内访问:https://apollomg.example.com/

默认账号密码:apollo/admin

参考文档