Apollo部署及使用

官网

github: https://github.com/ctripcorp/apollo/

official-website: https://www.apolloconfig.com/

简介

官方说明:Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境、不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限、流程治理等特性,适用于微服务配置管理场景。

Apollo分为三个部分,Apollo-configservice、Apollo-adminservice、Apollo-portal。其中configservice和adminservice构成配置管理的服务,同时configservice作为Eureka服务器提供服务发现的能力,部署在同一台服务器上;portal是Apollo的web管理端服务,可单独部署在不同的服务器上。

脚本方式安装

当前部署是三个服务都部署在同一台服务器上。

1. 准备

安装JDK

下载应用包

应用包名称 下载地址
apollo-configservice-1.8.1-github.zip https://github.com/ctripcorp/apollo/releases/download/v1.8.1/apollo-configservice-1.8.1-github.zip
apollo-adminservice-1.8.1-github.zip https://github.com/ctripcorp/apollo/releases/download/v1.8.1/apollo-adminservice-1.8.1-github.zip
apollo-portal-1.8.1-github.zip https://github.com/ctripcorp/apollo/releases/download/v1.8.1/apollo-portal-1.8.1-github.zip
apolloconfigdb.sql https://github.com/ctripcorp/apollo/blob/v1.8.1/scripts/sql/apolloconfigdb.sql
apolloportaldb.sql https://github.com/ctripcorp/apollo/blob/v1.8.1/scripts/sql/apolloportaldb.sql

上传应用包

1
2
3
scp apollo-configservice-1.8.1-github.zip root@xxx:/usr/local/src/
scp apollo-adminservice-1.8.1-github.zip root@xxx:/usr/local/src/
scp apollo-portal-1.8.1-github.zip root@xxx:/usr/local/src/

安装zip解压工具

1
yum install -y unzip

解压应用包

1
2
3
4
cd /usr/local/src
unzip -d apollo-configservice apollo-configservice-1.8.1-github.zip
unzip -d apollo-adminservice apollo-adminservice-1.8.1-github.zip
unzip -d apollo-portal apollo-portal-1.8.1-github.zip

移动到安装目录(/usr/local/apollo)

1
2
3
4
mkdir -p /usr/local/apollo
mv /usr/local/src/apollo-configservice /usr/local/apollo/apollo-configservice
mv /usr/local/src/apollo-adminservice /usr/local/apollo/apollo-adminservice
mv /usr/local/src/apollo-portal /usr/local/apollo/apollo-portal

2. 创建数据库

创建apolloconfigdb数据库

1
2
3
# MySQL命令行中
# 注意下载的sql文件已包含创建数据库的sql语句,按需修改
source apolloconfigdb.sql;

创建apolloportaldb数据库

1
2
3
# MySQL命令行中
# 注意下载的sql文件已包含创建数据库的sql语句,按需修改
source apolloportaldb.sql;

3. 部署apollo-configservice服务

配置数据库参数

1
2
# 修改spring.datasource.url、spring.datasource.username、spring.datasource.password三个参数
vi /usr/local/apollo/apollo-configservice/config/application-github.properties

启动apollo-configservice服务

1
sh /usr/local/apollo/apollo-configservice/scripts/startup.sh

结果截图:

查看apollo-configservice运行日志目录

1
tail -f /opt/logs/100003171/apollo-configservice.log

4. 部署apollo-adminservice

配置数据库参数

1
2
# 修改spring.datasource.url、spring.datasource.username、spring.datasource.password三个参数
vi /usr/local/apollo/apollo-adminservice/config/application-github.properties

启动apollo-adminservice服务

1
sh /usr/local/apollo/apollo-adminservice/scripts/startup.sh

结果截图:

查看apollo-adminservice运行日志目录

1
tail -f /opt/logs/100003172/apollo-adminservice.log

5. 部署apollo-portal服务

配置数据库参数

1
2
# 修改spring.datasource.url、spring.datasource.username、spring.datasource.password三个参数
vi /usr/local/apollo/apollo-portal/config/application-github.properties

配置portal服务所管理的环境

根据Apollo官方的部署方案,portal服务是同时管理各个环境下的Apollo-Config,所以需要指定portal服务能管理的环境参数以及对应的config服务地址

1
2
# 指定portal服务所管理的环境参数
vi /usr/local/apollo/apollo-portal/config/application-github.properties
1
2
## vi中添加所管理的环境参数,多个参数逗号(,)隔开。(下面参数值只是例子,按需修改)
apollo.portal.envs = DEV,UAT
1
2
# 配置对应的config服务地址
vi /usr/local/apollo/apollo-portal/config/apollo-env.properties
1
2
3
## 修改dev.meta和uat.meta的值,此处配置的服务地址需和apollo.portal.envs参数值对应(下面参数值只是例子,按需修改)
dev.meta=http://fill-in-dev-meta-server:8080
uat.meta=http://fill-in-uat-meta-server:8080

启动apollo-portal服务

1
sh /usr/local/apollo/apollo-portal/scripts/startup.sh

结果截图:

查看apollo-adminservice运行日志目录

1
tail -f /opt/logs/100003173/apollo-portal.log

6. 开放端口

1
2
3
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --zone=public --add-port=8070/tcp --permanent
firewall-cmd --reload

7. 服务停止

apollo-configservice服务停止

1
sh /usr/local/apollo/apollo-configservice/scripts/shutdown.sh

apollo-adminservice服务停止

1
sh /usr/local/apollo/apollo-adminservice/scripts/shutdown.sh

apollo-portal服务停止

1
sh /usr/local/apollo/apollo-portal/scripts/shutdown.sh

Kuebrnetes方式安装

1. 创建数据库

和脚本方式安装中创建数据库步骤一致

2. 部署apollo-configservice服务

修改其中<namespace-name><mysql-username><mysql-password><mysql-url>的内容。其中<mysql-username><mysql-password>的内容需要进行Base64编码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
---
# secret for apollo-config-server
kind: Secret
apiVersion: v1
metadata:
name: secret-apollo-config-server-mysql
namespace: <namespace-name>
labels:
app: secret-apollo-config-server-mysql
data:
username: <mysql-username>
password: <mysql-password>
type: Opaque
---
# configmap for apollo-config-server
kind: ConfigMap
apiVersion: v1
metadata:
namespace: <namespace-name>
name: configmap-apollo-config-server
data:
application-github.properties: |
spring.datasource.url = jdbc:mysql://service-mysql-for-apollo-env:3306/ProdApolloConfigDB?characterEncoding=utf8
spring.datasource.username = FillInCorrectUser
spring.datasource.password = FillInCorrectPassword
# 根据statefulset-apollo-config-server的副本数按需修改
eureka.service.url = http://statefulset-apollo-config-server-0.service-apollo-meta-server:8080/eureka/,http://statefulset-apollo-config-server-1.service-apollo-meta-server:8080/eureka/,http://statefulset-apollo-config-server-2.service-apollo-meta-server:8080/eureka/
---
# service for apollo-meta
kind: Service
apiVersion: v1
metadata:
namespace: <namespace-name>
name: service-apollo-meta-server
labels:
app: service-apollo-meta-server
spec:
ports:
- protocol: TCP
port: 8080
targetPort: 8080
selector:
app: apollo-config-server
type: ClusterIP
clusterIP: None
sessionAffinity: ClientIP
---
# service for apollo-config
kind: Service
apiVersion: v1
metadata:
namespace: <namespace-name>
name: service-apollo-config-server
labels:
app: service-apollo-config-server
spec:
ports:
- protocol: TCP
port: 8080
targetPort: 8080
selector:
app: apollo-config-server
type: ClusterIP
sessionAffinity: ClientIP
---
# statefulset for apollo-config
kind: StatefulSet
apiVersion: apps/v1
metadata:
namespace: <namespace-name>
name: statefulset-apollo-config-server
labels:
app: apollo-config-server
spec:
serviceName: service-apollo-meta-server
replicas: 3
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
configMap:
name: configmap-apollo-config-server
items:
- key: application-github.properties
path: application-github.properties
containers:
- image: apolloconfig/apollo-configservice:1.8.1
securityContext:
privileged: true
# 镜像拉取策略
imagePullPolicy: IfNotPresent
name: container-apollo-config-server
ports:
- protocol: TCP
containerPort: 8080
# 存储资源挂载
volumeMounts:
- name: volume-configmap-apollo-config-server
mountPath: /apollo-configservice/config/application-github.properties
subPath: application-github.properties
# 环境变量
env:
- name: SPRING_DATASOURCE_URL
value: "<mysql-url>"
- name: SPRING_DATASOURCE_USERNAME
valueFrom:
secretKeyRef:
name: secret-apollo-config-server-mysql
key: username
- name: SPRING_DATASOURCE_PASSWORD
valueFrom:
secretKeyRef:
name: secret-apollo-config-server-mysql
key: password
# 就绪探针
readinessProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 60
periodSeconds: 10
# 存活探针
livenessProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 300
periodSeconds: 10
dnsPolicy: ClusterFirst
# 重启策略
restartPolicy: Always

3. 部署apollo-adminservice服务

修改其中<namespace-name><mysql-url>的内容。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
---
# configmap for apollo-admin-server
kind: ConfigMap
apiVersion: v1
metadata:
namespace: <namespace-name>
name: configmap-apollo-admin-server
data:
application-github.properties: |
spring.datasource.url = jdbc:mysql://service-mysql-for-apollo-env:3306/ProdApolloConfigDB?characterEncoding=utf8
spring.datasource.username = FillInCorrectUser
spring.datasource.password = FillInCorrectPassword
# 根据statefulset-apollo-config-server的副本数按需修改
eureka.service.url = http://statefulset-apollo-config-server-0.service-apollo-meta-server:8080/eureka/,http://statefulset-apollo-config-server-1.service-apollo-meta-server:8080/eureka/,http://statefulset-apollo-config-server-2.service-apollo-meta-server:8080/eureka/
---
# service for apollo-admin
kind: Service
apiVersion: v1
metadata:
namespace: <namespace-name>
name: service-apollo-admin-server
labels:
app: service-apollo-admin-server
spec:
ports:
- protocol: TCP
port: 8090
targetPort: 8090
selector:
app: apollo-admin-server
type: ClusterIP
sessionAffinity: ClientIP
---
# deployment for apollo-admin
kind: Deployment
apiVersion: apps/v1
metadata:
namespace: <namespace-name>
name: deployment-apollo-admin-server
labels:
app: apollo-admin-server
spec:
replicas: 3
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
configMap:
name: configmap-apollo-admin-server
items:
- key: application-github.properties
path: application-github.properties
# 初始容器
initContainers:
- image: dwdraju/alpine-curl-jq
name: check-service-apollo-config-server
command: ['bash', '-c', "curl --connect-timeout 2 --max-time 5 --retry 50 --retry-delay 1 --retry-max-time 120 service-apollo-config-server:8080"]
containers:
- image: apolloconfig/apollo-adminservice:1.8.1
securityContext:
privileged: true
# 镜像拉取策略
imagePullPolicy: IfNotPresent
name: container-apollo-admin-server
ports:
- protocol: TCP
containerPort: 8090
# 存储资源挂载
volumeMounts:
- name: volume-configmap-apollo-admin-server
mountPath: /apollo-adminservice/config/application-github.properties
subPath: application-github.properties
# 环境变量
env:
- name: SPRING_DATASOURCE_URL
value: "<mysql-url>"
- name: SPRING_DATASOURCE_USERNAME
valueFrom:
secretKeyRef:
name: secret-apollo-config-server-mysql
key: username
- name: SPRING_DATASOURCE_PASSWORD
valueFrom:
secretKeyRef:
name: secret-apollo-config-server-mysql
key: password
# 就绪探针
readinessProbe:
tcpSocket:
port: 8090
initialDelaySeconds: 60
periodSeconds: 10
# 存活探针
livenessProbe:
tcpSocket:
port: 8090
initialDelaySeconds: 300
periodSeconds: 10
dnsPolicy: ClusterFirst
restartPolicy: Always

4. 部署apollo-portal服务

修改其中<namespace-name><mysql-username><mysql-password><mysql-url><apollo-portal-outside-port>的内容。其中<mysql-username><mysql-password>的内容需要进行Base64编;<apollo-portal-outside-port>为Portal向外暴露端口,Kubernetes默认可向外暴露端口范围为30000-32767。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
---
# secret for apollo-portal-server
kind: Secret
apiVersion: v1
metadata:
name: secret-apollo-portal-server-mysql
namespace: <namespace-name>
labels:
app: secret-apollo-portal-server-mysql
data:
username: <mysql-username>
password: <mysql-password>
type: Opaque
---
# configmap for apollo-portal-server
kind: ConfigMap
apiVersion: v1
metadata:
namespace: <namespace-name>
name: configmap-apollo-portal-server
data:
application-github.properties: |
spring.datasource.url = jdbc:mysql://service-mysql-for-portal-server:3306/ApolloPortalDB?characterEncoding=utf8
spring.datasource.username = FillInCorrectUser
spring.datasource.password = FillInCorrectPassword
# 指定访问前缀
server.servlet.context-path = /apollo
apollo-env.properties: |
dev.meta=http://fill-in-dev-meta-server:8080
fat.meta=http://fill-in-fat-meta-server:8080
uat.meta=http://fill-in-uat-meta-server:8080
lpt.meta=http://fill-in-lpt-meta-server:8080
pro.meta=http://fill-in-pro-meta-server:8080
---
# service for apollo-portal
kind: Service
apiVersion: v1
metadata:
namespace: <namespace-name>
name: service-apollo-portal-server
labels:
app: service-apollo-portal-server
spec:
ports:
- protocol: TCP
port: 8070
targetPort: 8070
nodePort: <apollo-portal-outside-port>
selector:
app: apollo-portal-server
type: NodePort
sessionAffinity: ClientIP
---
# deployment for apollo-portal
kind: Deployment
apiVersion: apps/v1
metadata:
namespace: <namespace-name>
name: deployment-apollo-portal-server
labels:
app: apollo-portal-server
spec:
replicas: 3
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: configmap-apollo-portal-server
items:
- key: application-github.properties
path: application-github.properties
- key: apollo-env.properties
path: apollo-env.properties
# 初始容器
initContainers:
# 确保 apollo-admin 正常提供服务
- image: dwdraju/alpine-curl-jq
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.<namespace-name>:8090"]
containers:
- image: apolloconfig/apollo-portal:1.8.1
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/config/application-github.properties
subPath: application-github.properties
- name: volume-configmap-apollo-portal-server
mountPath: /apollo-portal/config/apollo-env.properties
subPath: apollo-env.properties
# 环境变量
env:
- name: SPRING_DATASOURCE_URL
value: "<mysql-url>"
- name: SPRING_DATASOURCE_USERNAME
valueFrom:
secretKeyRef:
name: secret-apollo-portal-server-mysql
key: username
- name: SPRING_DATASOURCE_PASSWORD
valueFrom:
secretKeyRef:
name: secret-apollo-portal-server-mysql
key: password
- name: APOLLO_PORTAL_ENVS
value: 'dev'
- name: DEV_META
value: 'http://service-apollo-config-server.<namespace-name>:8080'
# 就绪探针
readinessProbe:
tcpSocket:
port: 8070
initialDelaySeconds: 60
periodSeconds: 10
# 存活探针
livenessProbe:
tcpSocket:
port: 8070
initialDelaySeconds: 300
periodSeconds: 10
dnsPolicy: ClusterFirst
# 重启策略
restartPolicy: Always

服务访问

直接访问Portal服务对外的端口,账号密码默认为:apollo/admin
访问界面截图:

SpringBoot中使用

1. 引入依赖

注意版本强烈建议和部署的服务版本一致

1
2
3
4
5
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>${apollo-client.version}</version>
</dependency>

2. 修改SpringBoot配置文件(application.yml或application.properties)

1
2
3
4
5
6
app.id=xxx
apollo.meta=xxx
apollo.cluster=xxx
apollo.bootstrap.enabled=true
apollo.bootstrap.eagerLoad.enabled = true
apollo.bootstrap.namespaces=xxx

注意事项

如何修改apollo-portal服务中的部门列表

Apollo-Portal服务中的部门列表数据是直接存储在数据库中的,页面上不能进行修改,所以需要在创建apolloportaldb的时候按需进行修改,数据表名称是ServerConfig,字段是organizations,内容为json格式数据,可根据官方数据修改即可。

(•̀ᴗ•́)و ̑̑

Share