(第二版)记录一次Jenkins + Docker + Rancher实现持续集成、持续部署--门店掌柜项目

前言

因为直接使用Docker Swarm集群总是产生总总问题,所以还是采用Rancher管理工具来搭建集群

Docker Swarm遇到的问题:服务提供者注册到Nacos时,使用的是容器内部的ip地址,导致只能由同一台宿主机的消费者进行消费。在这之后试了很多种方法把注册的ip地址改成宿主机ip,可惜都没用,出各种问题,所以最后还是决定使用Rancher工具来搭建集群

附上第一版地址:(第一版)记录一次Jenkins + Docker实现持续集成、持续部署——门店掌柜项目

Rancher安装使用以及配置NFS部分参考:docker高级之——Docker-Compose、Docker-Swarm、Jakins持续集成、Rancher持续部署

Jenkins配置持续集成

基础的配置与第一版一样,不一样的地方如下

  • 不需要配置父工程任务
  • 构建步骤如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 1、Maven安装整个项目
clean install

# 2、生成镜像并且上传至私有镜像仓库(每个微服务都需要,具体代码在下文)
dockerfile:build -f shopkeeper-gateway/gateway-uniapp/pom.xml dockerfile:push
# 如果有问题出错了,那么先执行清理并安装之后再生成镜像
clean install dockerfile:build -f shopkeeper-gateway/gateway-uniapp/pom.xml dockerfile:push

# 3、调用Rancher触发部署微服务(每个微服务都需要,具体配置看Docker高级那篇文章)
curl "http://192.168.0.131:9090/v1-webhooks/endpoint?key=DzN5t6riMAQgllvOXfDbK8PoJu5QaO45R3eLg5KL&projectId=1a7" \
-H "Content-Type:application/json" \
-d "{\"push_data\": {\"tag\": \"latest\"},\"repository\": {\"repo_name\": \"registry.liuhaoan.com:5000/shopkeeper/model-trading-web\"}}"


# 4、最后每个主机节点都删除老版本的微服务(远程调用ssh)
docker images | grep none | awk '{print $3}' | xargs docker rmi

生成镜像并且上传至私有镜像仓库:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
dockerfile:build -f shopkeeper-gateway/gateway-uniapp/pom.xml dockerfile:push
dockerfile:build -f shopkeeper-gateway/gateway-shop/pom.xml dockerfile:push
dockerfile:build -f shopkeeper-gateway/gateway-operator/pom.xml dockerfile:push
dockerfile:build -f shopkeeper-model-basic/model-basic-job-listen/pom.xml dockerfile:push
dockerfile:build -f shopkeeper-model-basic/model-basic-producer/pom.xml dockerfile:push
dockerfile:build -f shopkeeper-model-basic/model-basic-web/pom.xml dockerfile:push
dockerfile:build -f shopkeeper-model-security/model-security-producer/pom.xml dockerfile:push
dockerfile:build -f shopkeeper-model-security/model-security-web/pom.xml dockerfile:push
dockerfile:build -f shopkeeper-model-shop/model-shop-applet/pom.xml dockerfile:push
dockerfile:build -f shopkeeper-model-shop/model-shop-job-listen/pom.xml dockerfile:push
dockerfile:build -f shopkeeper-model-shop/model-shop-producer/pom.xml dockerfile:push
dockerfile:build -f shopkeeper-model-shop/model-shop-user/pom.xml dockerfile:push
dockerfile:build -f shopkeeper-model-shop/model-shop-web/pom.xml dockerfile:push
dockerfile:build -f shopkeeper-model-trading/model-trading-job-listen/pom.xml dockerfile:push
dockerfile:build -f shopkeeper-model-trading/model-trading-producer/pom.xml dockerfile:push
dockerfile:build -f shopkeeper-model-trading/model-trading-web/pom.xml dockerfile:push

Rancher常见问题

  • 配置命令时必须用双引号引起来:

Snipaste_2022-08-31_16-43-51

  • 配置环境变量不能用双引号引起来

Snipaste_2022-08-31_16-44-54

  • 搭建的集群必须在同一个局域网下,否则不同局域网的主机无法互相访问
  • “服务链接”是用于配置当前服务运行之前哪些服务要先运行

Rancher导入应用

  • docker-compose.yml配置文件:
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
version: '2'
services:
frpc-txy:
image: snowdreamtech/frpc
stdin_open: true
network_mode: host
volumes:
- frp_txy:/etc/frp
tty: true
labels:
io.rancher.container.hostname_override: container_name
io.rancher.container.pull_image: always
io.rancher.scheduler.affinity:container_label: io.rancher.stack_service.name=shopkeeper-test/gateway-operator,io.rancher.stack_service.name=shopkeeper-test/gateway-shop,io.rancher.stack_service.name=shopkeeper-test/gateway-uniapp
model-trading-web:
image: registry.liuhaoan.com:5000/shopkeeper/model-trading-web:latest
environment:
PARAMS: --spring.profiles.active=test
TZ: Asia/Shanghai
stdin_open: true
volumes:
- /etc/localtime:/etc/localtime (ro)
tty: true
links:
- mysql:mysql
- seata-server:seata-server
- nacos:nacos
- redis:redis
- rabbitmq:rabbitmq
labels:
io.rancher.container.hostname_override: container_name
io.rancher.container.pull_image: always
service: model-trading-web
io.rancher.scheduler.affinity:host_label_soft_ne: hostname=131
model-trading-producer:
image: registry.liuhaoan.com:5000/shopkeeper/model-trading-producer:latest
environment:
PARAMS: --spring.profiles.active=test
TZ: Asia/Shanghai
stdin_open: true
volumes:
- /etc/localtime:/etc/localtime (ro)
tty: true
links:
- mysql:mysql
- model-security-web:model-security-web
- model-trading-web:model-trading-web
- model-basic-web:model-basic-web
- model-shop-web:model-shop-web
- seata-server:seata-server
- nacos:nacos
- redis:redis
- rabbitmq:rabbitmq
labels:
io.rancher.container.hostname_override: container_name
io.rancher.container.pull_image: always
service: model-trading-producer
io.rancher.scheduler.affinity:host_label_soft_ne: hostname=131
model-shop-producer:
image: registry.liuhaoan.com:5000/shopkeeper/model-shop-producer:latest
environment:
PARAMS: --spring.profiles.active=test
TZ: Asia/Shanghai
stdin_open: true
volumes:
- /etc/localtime:/etc/localtime (ro)
tty: true
links:
- mysql:mysql
- model-security-web:model-security-web
- model-trading-web:model-trading-web
- model-basic-web:model-basic-web
- model-shop-web:model-shop-web
- seata-server:seata-server
- nacos:nacos
- redis:redis
- rabbitmq:rabbitmq
labels:
io.rancher.container.hostname_override: container_name
io.rancher.container.pull_image: always
service: model-shop-producer
io.rancher.scheduler.affinity:host_label_soft_ne: hostname=131
gateway-uniapp:
image: registry.liuhaoan.com:5000/shopkeeper/gateway-uniapp:latest
environment:
PARAMS: --spring.profiles.active=test
TZ: Asia/Shanghai
stdin_open: true
volumes:
- /etc/localtime:/etc/localtime (ro)
tty: true
links:
- mysql:mysql
- seata-server:seata-server
- nacos:nacos
- redis:redis
- rabbitmq:rabbitmq
ports:
- 8018:8018/tcp
labels:
io.rancher.scheduler.affinity:host_label: hostname=131
io.rancher.container.hostname_override: container_name
io.rancher.container.pull_image: always
service: gateway-uniapp
gateway-shop:
image: registry.liuhaoan.com:5000/shopkeeper/gateway-shop:latest
environment:
PARAMS: --spring.profiles.active=test
TZ: Asia/Shanghai
stdin_open: true
volumes:
- /etc/localtime:/etc/localtime (ro)
tty: true
links:
- mysql:mysql
- seata-server:seata-server
- nacos:nacos
- redis:redis
- rabbitmq:rabbitmq
ports:
- 8017:8017/tcp
labels:
io.rancher.scheduler.affinity:host_label: hostname=131
io.rancher.container.hostname_override: container_name
io.rancher.container.pull_image: always
service: gateway-shop
xxl-job:
image: xuxueli/xxl-job-admin:2.1.2
environment:
PARAMS: --spring.datasource.url=jdbc:mysql://mysql:3306/xxl-job?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&serverTimezone=Asia/Shanghai --spring.datasource.username=root --spring.datasource.password=aA2428933105.
stdin_open: true
volumes:
- xxlJob_data:/data/applogs
- waitFor:/home/wait-for
tty: true
extra_hosts:
- xxl-job:192.168.0.133
links:
- mysql:mysql
ports:
- 8280:8080/tcp
command:
- sh -c '/home/wait-for/wait-for.sh mysql:3306 -- ./bin/docker-startup.sh'
labels:
io.rancher.scheduler.affinity:host_label: hostname=133
io.rancher.container.hostname_override: container_name
io.rancher.container.pull_image: always
model-shop-applet:
image: registry.liuhaoan.com:5000/shopkeeper/model-shop-applet:latest
environment:
PARAMS: --spring.profiles.active=test
TZ: Asia/Shanghai
stdin_open: true
volumes:
- /etc/localtime:/etc/localtime (ro)
tty: true
links:
- mysql:mysql
- model-security-web:model-security-web
- model-trading-web:model-trading-web
- model-basic-web:model-basic-web
- model-shop-web:model-shop-web
- seata-server:seata-server
- nacos:nacos
- redis:redis
- rabbitmq:rabbitmq
labels:
io.rancher.container.hostname_override: container_name
io.rancher.container.pull_image: always
service: model-shop-applet
io.rancher.scheduler.affinity:host_label_soft_ne: hostname=131
model-shop-user:
image: registry.liuhaoan.com:5000/shopkeeper/model-shop-user:latest
environment:
PARAMS: --spring.profiles.active=test
TZ: Asia/Shanghai
stdin_open: true
volumes:
- /etc/localtime:/etc/localtime (ro)
tty: true
links:
- mysql:mysql
- model-security-web:model-security-web
- model-trading-web:model-trading-web
- model-basic-web:model-basic-web
- model-shop-web:model-shop-web
- seata-server:seata-server
- nacos:nacos
- redis:redis
- rabbitmq:rabbitmq
labels:
io.rancher.container.hostname_override: container_name
io.rancher.container.pull_image: always
service: model-shop-user
io.rancher.scheduler.affinity:host_label_soft_ne: hostname=131
gateway-operator:
image: registry.liuhaoan.com:5000/shopkeeper/gateway-operator:latest
environment:
PARAMS: --spring.profiles.active=test
TZ: Asia/Shanghai
stdin_open: true
volumes:
- /etc/localtime:/etc/localtime (ro)
tty: true
links:
- mysql:mysql
- seata-server:seata-server
- nacos:nacos
- redis:redis
- rabbitmq:rabbitmq
ports:
- 8011:8011/tcp
labels:
io.rancher.scheduler.affinity:host_label: hostname=131
io.rancher.container.hostname_override: container_name
io.rancher.container.pull_image: always
service: gateway-operator
frpc-aly:
image: snowdreamtech/frpc
stdin_open: true
network_mode: host
volumes:
- frp_aly:/etc/frp
tty: true
labels:
io.rancher.container.hostname_override: container_name
io.rancher.container.pull_image: always
io.rancher.scheduler.affinity:container_label: io.rancher.stack_service.name=shopkeeper-test/gateway-operator,io.rancher.stack_service.name=shopkeeper-test/gateway-shop,io.rancher.stack_service.name=shopkeeper-test/gateway-uniapp
model-trading-job-listen:
image: registry.liuhaoan.com:5000/shopkeeper/model-trading-job-listen:latest
environment:
PARAMS: --spring.profiles.active=test
TZ: Asia/Shanghai
stdin_open: true
volumes:
- /etc/localtime:/etc/localtime (ro)
tty: true
extra_hosts:
- xxl-job:192.168.0.133
links:
- mysql:mysql
- seata-server:seata-server
- nacos:nacos
- redis:redis
- rabbitmq:rabbitmq
ports:
- 8089:8089/tcp
- 9999:9999/tcp
labels:
io.rancher.container.hostname_override: container_name
io.rancher.container.pull_image: always
service: model-trading-job-listen
io.rancher.scheduler.affinity:host_label_soft_ne: hostname=131
model-shop-job-listen:
image: registry.liuhaoan.com:5000/shopkeeper/model-shop-job-listen:latest
environment:
PARAMS: --spring.profiles.active=test
TZ: Asia/Shanghai
stdin_open: true
volumes:
- /etc/localtime:/etc/localtime (ro)
tty: true
extra_hosts:
- xxl-job:192.168.0.133
links:
- mysql:mysql
- seata-server:seata-server
- nacos:nacos
- redis:redis
- rabbitmq:rabbitmq
ports:
- 8088:8088/tcp
- 9998:9998/tcp
labels:
io.rancher.container.hostname_override: container_name
io.rancher.container.pull_image: always
service: model-shop-job-listen
io.rancher.scheduler.affinity:host_label_soft_ne: hostname=131
rabbitmq:
image: rabbitmq:3.8.3-management
environment:
RABBITMQ_DEFAULT_PASS: '2428933105'
RABBITMQ_DEFAULT_USER: admin
stdin_open: true
volumes:
- rabbitmq_data:/var/lib/rabbitmq
tty: true
ports:
- 15672:15672/tcp
- 5672:5672/tcp
labels:
io.rancher.container.pull_image: always
io.rancher.container.hostname_override: container_name
redis:
image: redis:5.0.0
stdin_open: true
volumes:
- redis_data:/data
tty: true
ports:
- 6379:6379/tcp
command:
- redis-server
- --requirepass
- pass
labels:
io.rancher.container.pull_image: always
io.rancher.container.hostname_override: container_name
nacos:
image: nacos/nacos-server:1.4.0
environment:
JVM_MMS: 256m
JVM_XMN: 128m
JVM_XMS: 512m
MODE: standalone
MYSQL_SERVICE_DB_NAME: nacos
MYSQL_SERVICE_HOST: mysql
MYSQL_SERVICE_PASSWORD: aA2428933105.
MYSQL_SERVICE_PORT: '3306'
MYSQL_SERVICE_USER: root
NACOS_APPLICATION_PORT: '8848'
SPRING_DATASOURCE_PLATFORM: mysql
stdin_open: true
volumes:
- waitFor:/home/wait-for
tty: true
links:
- mysql:mysql
ports:
- 8848:8848/tcp
command:
- sh -c '/home/wait-for/wait-for.sh mysql:3306 -- ./bin/docker-startup.sh'
labels:
io.rancher.container.pull_image: always
io.rancher.container.hostname_override: container_name
model-security-web:
image: registry.liuhaoan.com:5000/shopkeeper/model-security-web:latest
environment:
PARAMS: --spring.profiles.active=test
TZ: Asia/Shanghai
stdin_open: true
volumes:
- /etc/localtime:/etc/localtime (ro)
tty: true
links:
- mysql:mysql
- seata-server:seata-server
- nacos:nacos
- redis:redis
- rabbitmq:rabbitmq
labels:
io.rancher.container.hostname_override: container_name
io.rancher.container.pull_image: always
service: model-security-web
io.rancher.scheduler.affinity:host_label_soft_ne: hostname=131
model-basic-producer:
image: registry.liuhaoan.com:5000/shopkeeper/model-basic-producer:latest
environment:
PARAMS: --spring.profiles.active=test
TZ: Asia/Shanghai
stdin_open: true
volumes:
- /etc/localtime:/etc/localtime (ro)
tty: true
links:
- mysql:mysql
- model-trading-web:model-trading-web
- seata-server:seata-server
- nacos:nacos
- redis:redis
- rabbitmq:rabbitmq
- model-security-web:model-security-web
- model-basic-web:model-basic-web
- model-shop-web:model-shop-web
labels:
io.rancher.container.hostname_override: container_name
io.rancher.container.pull_image: always
service: model-basic-producer
io.rancher.scheduler.affinity:host_label_soft_ne: hostname=131
model-basic-web:
image: registry.liuhaoan.com:5000/shopkeeper/model-basic-web:latest
environment:
PARAMS: --spring.profiles.active=test
TZ: Asia/Shanghai
stdin_open: true
volumes:
- /etc/localtime:/etc/localtime (ro)
tty: true
links:
- mysql:mysql
- seata-server:seata-server
- nacos:nacos
- redis:redis
- rabbitmq:rabbitmq
labels:
io.rancher.container.hostname_override: container_name
io.rancher.container.pull_image: always
service: model-basic-web
io.rancher.scheduler.affinity:host_label_soft_ne: hostname=131
model-security-producer:
image: registry.liuhaoan.com:5000/shopkeeper/model-security-producer:latest
environment:
PARAMS: --spring.profiles.active=test
TZ: Asia/Shanghai
stdin_open: true
volumes:
- /etc/localtime:/etc/localtime (ro)
tty: true
links:
- mysql:mysql
- model-security-web:model-security-web
- model-trading-web:model-trading-web
- model-basic-web:model-basic-web
- model-shop-web:model-shop-web
- seata-server:seata-server
- nacos:nacos
- redis:redis
- rabbitmq:rabbitmq
labels:
io.rancher.container.hostname_override: container_name
io.rancher.container.pull_image: always
service: model-security-producer
io.rancher.scheduler.affinity:host_label_soft_ne: hostname=131
mysql:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: aA2428933105.
stdin_open: true
volumes:
- mysql_mydir:/mydir
- mysql_data:/var/lib/mysql
- mysql_source:/docker-entrypoint-initdb.d
- mysql_conf:/etc/mysql
tty: true
ports:
- 3306:3306/tcp
labels:
io.rancher.container.pull_image: always
io.rancher.container.hostname_override: container_name
model-shop-web:
image: registry.liuhaoan.com:5000/shopkeeper/model-shop-web:latest
environment:
PARAMS: --spring.profiles.active=test
TZ: Asia/Shanghai
stdin_open: true
volumes:
- /etc/localtime:/etc/localtime (ro)
tty: true
links:
- mysql:mysql
- seata-server:seata-server
- nacos:nacos
- redis:redis
- rabbitmq:rabbitmq
labels:
io.rancher.container.hostname_override: container_name
io.rancher.container.pull_image: always
service: model-shop-web
io.rancher.scheduler.affinity:host_label_soft_ne: hostname=131
seata-server:
image: seataio/seata-server:1.3.0
environment:
SEATA_CONFIG_NAME: file:/root/seata-config/registry
SEATA_IP: 192.168.0.132
SEATA_PORT: '9200'
stdin_open: true
volumes:
- seata_config:/root/seata-config
- waitFor:/home/wait-for
tty: true
links:
- nacos:nacos
ports:
- 9200:9200/tcp
command:
- sh -c '/home/wait-for/wait-for.sh nacos:8848 -- ./bin/docker-startup.sh'
labels:
io.rancher.scheduler.affinity:host_label: hostname=132
io.rancher.container.hostname_override: container_name
io.rancher.container.pull_image: always
model-basic-job-listen:
image: registry.liuhaoan.com:5000/shopkeeper/model-basic-job-listen:latest
environment:
PARAMS: --spring.profiles.active=test
TZ: Asia/Shanghai
stdin_open: true
volumes:
- /etc/localtime:/etc/localtime (ro)
tty: true
extra_hosts:
- xxl-job:192.168.0.133
links:
- mysql:mysql
- seata-server:seata-server
- nacos:nacos
- redis:redis
- rabbitmq:rabbitmq
ports:
- 7070:7070/tcp
- 9997:9997/tcp
labels:
io.rancher.container.hostname_override: container_name
io.rancher.container.pull_image: always
service: model-basic-job-listen
io.rancher.scheduler.affinity:host_label_soft_ne: hostname=131

  • rancher-compose.yml配置文件:
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
version: '2'
services:
frpc-txy:
scale: 1
start_on_create: true
model-trading-web:
scale: 1
start_on_create: true
model-trading-producer:
scale: 1
start_on_create: true
model-shop-producer:
scale: 1
start_on_create: true
gateway-uniapp:
scale: 1
start_on_create: true
gateway-shop:
scale: 1
start_on_create: true
xxl-job:
scale: 1
start_on_create: true
model-shop-applet:
scale: 1
start_on_create: true
model-shop-user:
scale: 1
start_on_create: true
gateway-operator:
scale: 1
start_on_create: true
frpc-aly:
scale: 1
start_on_create: true
model-trading-job-listen:
scale: 1
start_on_create: true
model-shop-job-listen:
scale: 1
start_on_create: true
rabbitmq:
scale: 1
start_on_create: true
redis:
scale: 1
start_on_create: true
nacos:
scale: 1
start_on_create: true
model-security-web:
scale: 1
start_on_create: true
model-basic-producer:
scale: 1
start_on_create: true
model-basic-web:
scale: 1
start_on_create: true
model-security-producer:
scale: 1
start_on_create: true
mysql:
scale: 1
start_on_create: true
model-shop-web:
scale: 1
start_on_create: true
seata-server:
scale: 1
start_on_create: true
model-basic-job-listen:
scale: 1
start_on_create: true