Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。
如果对apollo想要有详细的研究和理解,请参阅它的官方文档,本文后续的所有阐述都是基于你对apollo有一定理解的前题下,只对基于文章叙述的必要的概念做简要说明。
其实官方也提供了针对apollo在k8s环境下的部署方案,但是个人觉得引入k8s有状态的StateFulSet似乎提高了部署运维和学习的成本,我提供的部署方式更为简单方便
在部署之前有必要简单介绍一下apollo如何组织自己的组件去协同工作去完成整个微服务动态配置的管理的,后续也是围绕它的组件去部署的
这是官方给出的架构图,如图一
图一
看着是不是一脸懵逼,这么多文字图片,似乎一下子根本抓不到重点,别着急接下来我来给出我自己理解的简化版的架构图,如图二
在eureka中,微服务本身作为被消费的一方在向eureka注册的时候基本有两种注册方式,一种是通过域名注册,一种是通过ip注册,apollo中默认使用的基于ip的注册方式,但是我们在容器中部署的时候最好基于域名去注册,这样做有2点好处:
二是 假如采用ip注册,当k8s中apollo的某个pod意外重启,pod的ip就会改变,然而eureka 默认的服务发现变更信息的时间一般在90s左右(虽然可配置,但是变短会加重eureka的负担),这90s内旧的不可用的服务注册信息依然存在,大量对apollo的服务调用就会报错.这是不是忍受的, 情况如下图四所示
图四
然后当你采用基于域名的方式注册,无论pod怎么重启它在eureka中注册的域名都是固定不变的,而k8s对pod层面的服务发现感知是非常迅速的,这样服务不可用的间隙被缩短到很小,极大的提高了服务的可用性,如下图五所示
图五
admin-service、config-service、 portal-service 都可以用k8s中的Deployment去单独部署为无状态服务,这三个服务都需要被容器之外的环境访问,所以需要给这三个deployment都配置对应的Service作为被访问的入口,如图三
首先需要从github上检出项目
git clone https://github.com/ctripcorp/apollo.git
apollo有两个库都需要创建
ApolloConfigDB
目录 apollo/scripts/db/migration/configdb/V1.0.0__initialization.sql
ApolloPortalDB
目录 apollo/scripts/db/migration/portaldb/V1.0.0__initialization.sql
修改ApolloPortalDB 的ServerConfig 表的 apollo.portal.envs,这里建议最好不要通过一套apollo管理多套服务器环境,由于多套环境之间portal是公用的,所以实践下来公用portal有相互干扰,建议每套环境配置如下
use ApolloPortalDB;
update ServerConfig set Value = "PRO" where Id=1;
修改对应ApolloConfigDB 中ServerConfig 表的 eureka.service.url分配置项,因为portal在检测对应环境config-service的心跳时会用到,这里已经假设你已经部署了一个容器化的eureka的微服务,它的访问地址是 http://service-registry:30013/erueka/,配置如下:
use ApolloConfigDB;
update ServerConfig set Value = "http://service-registry:30013/erueka/" where Id=1;
apollo/apollo-adminservice/src/main/resources/bootstrap.yml
原配置文件默认通过ip方式注册,需要改为通过域名注册
修改前:
eureka:
instance:
hostname: ${hostname:localhost}
## 通过ip方式注册
preferIpAddress: true
status-page-url-path: /info
health-check-url-path: /health
client:
serviceUrl:
# This setting will be overridden by eureka.service.url setting from ApolloConfigDB.ServerConfig or System Property
# see com.ctrip.framework.apollo.biz.eureka.ApolloEurekaClientConfig
defaultZone: http://${eureka.instance.hostname}:8080/eureka/
healthcheck:
enabled: true
eurekaServiceUrlPollIntervalSeconds: 60
management:
health:
status:
order: DOWN, OUT_OF_SERVICE, UNKNOWN, UP
这里指定admin-service注册时的域名为apollo-admin,修改后如下:
eureka:
instance:
## 通过域名注册
hostname: apollo-admin
status-page-url-path: /info
health-check-url-path: /health
client:
serviceUrl:
# This setting will be overridden by eureka.service.url setting from ApolloConfigDB.ServerConfig or System Property
# see com.ctrip.framework.apollo.biz.eureka.ApolloEurekaClientConfig
defaultZone: http://${eureka.instance.hostname}:8080/eureka/
healthcheck:
enabled: true
eurekaServiceUrlPollIntervalSeconds: 60
management:
health:
status:
order: DOWN, OUT_OF_SERVICE, UNKNOWN, UP
apollo/apollo-configservice/src/main/resources/bootstrap.yml
修改前:
eureka:
instance:
hostname: ${hostname:localhost}
## 默认通过ip注册服务
preferIpAddress: true
status-page-url-path: /info
health-check-url-path: /health
server:
peerEurekaNodesUpdateIntervalMs: 60000
enableSelfPreservation: false
client:
serviceUrl:
# This setting will be overridden by eureka.service.url setting from ApolloConfigDB.ServerConfig or System Property
# see com.ctrip.framework.apollo.biz.eureka.ApolloEurekaClientConfig
defaultZone: http://${eureka.instance.hostname}:8080/eureka/
healthcheck:
enabled: true
eurekaServiceUrlPollIntervalSeconds: 60
management:
health:
status:
order: DOWN, OUT_OF_SERVICE, UNKNOWN, UP
指定config-service 的注册时域名是apollo-config,修改后:
eureka:
instance:
## 通过域名注册
hostname: apollo-config
status-page-url-path: /info
health-check-url-path: /health
server:
peerEurekaNodesUpdateIntervalMs: 60000
enableSelfPreservation: false
client:
serviceUrl:
# This setting will be overridden by eureka.service.url setting from ApolloConfigDB.ServerConfig or System Property
# see com.ctrip.framework.apollo.biz.eureka.ApolloEurekaClientConfig
defaultZone: http://${eureka.instance.hostname}:8080/eureka/
healthcheck:
enabled: true
eurekaServiceUrlPollIntervalSeconds: 60
management:
health:
status:
order: DOWN, OUT_OF_SERVICE, UNKNOWN, UP
由于我们打算使用apollo外部的服务注册中心,所以需要做如下修改
1.5.0及以上版本
为apollo-configservice配置apollo.eureka.server.enabled=false即可,通过bootstrap.yml或-D参数等方式皆可。
1.5.0之前的版本
修改com.ctrip.framework.apollo.configservice.ConfigServiceApplication,把@EnableEurekaServer改为@EnableEurekaClient
假如之前部署apollo数据库的信息如下:
修改apollo/scripts/build.sh
#!/bin/sh
# apollo config db info
apollo_config_db_url=jdbc:mysql://mysqladdress:port/apolloconfigdb?characterEncoding=utf8
apollo_config_db_username=username
apollo_config_db_password=pwd
# apollo portal db info
apollo_portal_db_url=jdbc:mysql://mysqladdress:port/apolloportaldb?characterEncoding=utf8
apollo_portal_db_username=username
apollo_portal_db_password=pwd
# meta server url, different environments should have different meta server addresses
#dev_meta=http://fill-in-uat-meta-server:8080
#fat_meta=http://fill-in-uat-meta-server:8080
#uat_meta=http://fill-in-uat-meta-server:8080
pro_meta=http://apollo-config:8080
META_SERVERS_OPTS="-Ddev_meta=$dev_meta -Dfat_meta=$fat_meta -Duat_meta=$uat_meta -Dpro_meta=$pro_meta"
# =============== Please do not modify the following content =============== #
# go to script directory
cd "${0%/*}"
cd ..
# package config-service and admin-service
echo "==== starting to build config-service and admin-service ===="
mvn clean package -DskipTests -pl apollo-configservice,apollo-adminservice -am -Dapollo_profile=github -Dspring_datasource_url=$apollo_config_db_url -Dspring_datasource_username=$apollo_config_db_username -Dspring_datasource_password=$apollo_config_db_password
echo "==== building config-service and admin-service finished ===="
echo "==== starting to build portal ===="
mvn clean package -DskipTests -pl apollo-portal -am -Dapollo_profile=github,auth -Dspring_datasource_url=$apollo_portal_db_url -Dspring_datasource_username=$apollo_portal_db_username -Dspring_datasource_password=$apollo_portal_db_password $META_SERVERS_OPTS
echo "==== building portal finished ===="
./scripts/build.sh
在对应子模块的target目录分别生成了对应压缩包
apollo/apollo-adminservice/target/apollo-adminservice-1.5.0-SNAPSHOT-github.zip
apollo/apollo-configservice/target/apollo-configservice-1.5.0-SNAPSHOT-github.zip
apollo/apollo-portal/target/apollo-portal-1.5.0-SNAPSHOT-github.zip
分别将 admin、config、portal对应的压缩包解压
FROM openjdk:8-jre-alpine3.8
RUN \
echo "http://mirrors.aliyun.com/alpine/v3.8/main" > /etc/apk/repositories && \
echo "http://mirrors.aliyun.com/alpine/v3.8/community" >> /etc/apk/repositories && \
apk update upgrade && \
apk add --no-cache procps curl bash tzdata && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
echo "Asia/Shanghai" > /etc/timezone && \
mkdir -p /apollo
#将当前目录下所有文件copy 到容器的apollo目录下
ADD . /apollo/
EXPOSE port
ENTRYPOINT ["/apollo/scripts/startup.sh"]
## 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"
tail -f $LOG_DIR/$SERVICE_NAME.log
#exit 0;
在三个解压的根目录下执行如下命令构建:
sudo docker build -t apollo-admin:v1 .
sudo docker build -t apollo-config:v1 .
sudo docker build -t apollo-portal:v1 .
推送到对应的镜像仓库
sudo docker tag apollo-admin:v1 [dockerregistry]:[port]/[namespace]/apollo-admin:v1 && \
sudo docker tag apollo-config:v1 [dockerregistry]:[port]/[namespace]/apollo-config:v1 && \
sudo docker tag [dockerregistry]:[port]/[namespace]/apollo-portal:v1
sudo docker push apollo-admin:v1 [dockerregistry]:[port]/[namespace]/apollo-admin:v1 && \
sudo docker push apollo-config:v1 [dockerregistry]:[port]/[namespace]/apollo-config:v1 && \
sudo docker push [dockerregistry]:[port]/[namespace]/apollo-portal:v1
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: apollo-admin
namespace: prod
spec:
replicas: 1
selector:
matchLabels:
run: apollo-admin
template:
metadata:
labels:
run: apollo-admin
spec:
containers:
- name: apollo-admin-containers
image: [dockerregistry]:[port]/[namespace]/apollo-admin:v1
imagePullPolicy: Always
livenessProbe:
failureThreshold: 3
httpGet:
path: /
port: 8090
scheme: HTTP
initialDelaySeconds: 30
periodSeconds: 10
successThreshold: 1
readinessProbe:
failureThreshold: 3
httpGet:
path: /
port: 8090
scheme: HTTP
initialDelaySeconds: 30
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 1
volumeMounts:
- name: host-time
mountPath: /etc/localtime
ports:
- containerPort: 8090
resources:
requests:
memory: 6100Mi
limits:
memory: 6100Mi
volumes:
- name: host-time
hostPath:
path: /etc/localtime
---
apiVersion: v1
kind: Service
metadata:
name: apollo-admin
namespace: prod
labels:
run: apollo-admin
spec:
ports:
- port: 8090
targetPort: 8090
selector:
run: apollo-admin
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: apollo-config
namespace: prod
spec:
replicas: 1
selector:
matchLabels:
run: apollo-config
template:
metadata:
labels:
run: apollo-config
spec:
containers:
- name: apollo-config-containers
image: [dockerregistry]:[port]/[namespace]/apollo-config:v1
imagePullPolicy: Always
livenessProbe:
failureThreshold: 3
httpGet:
path: /services/meta
port: 8080
scheme: HTTP
initialDelaySeconds: 30
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 1
readinessProbe:
failureThreshold: 3
httpGet:
path: /services/meta
port: 8080
scheme: HTTP
initialDelaySeconds: 30
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 1
volumeMounts:
- name: host-time
mountPath: /etc/localtime
ports:
- containerPort: 8080
resources:
requests:
memory: 7100Mi
limits:
memory: 7100Mi
volumes:
- name: host-time
hostPath:
path: /etc/localtime
---
apiVersion: v1
kind: Service
metadata:
name: apollo-config
namespace: prod
labels:
run: apollo-config
system: paas
spec:
ports:
- port: 8080
targetPort: 8080
selector:
run: apollo-config
创建portal.yml 如下
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: apollo-portal
namespace: prod
labels:
system: paas
spec:
replicas: 1
template:
metadata:
labels:
run: apollo-portal
spec:
containers:
- name: apollo-portal-containers
image: [dockerregistry]:[port]/[namespace]/apollo-portal:v1
imagePullPolicy: Always
volumeMounts:
- name: host-time
mountPath: /etc/localtime
ports:
- containerPort: 8070
resources:
requests:
memory: 4100Mi
limits:
memory: 4100Mi
volumes:
- name: host-time
hostPath:
path: /etc/localtime
---
apiVersion: v1
kind: Service
metadata:
name: apollo-portal
namespace: prod
labels:
run: apollo-portal
spec:
ports:
- port: 8070
targetPort: 8070
selector:
run: apollo-portal
至此 apollo 容器化基本就算完成了