Docker Swarm 搭建SpringCloud集群

本次會嘗試將上一篇 spring cloud 快速學習教程項目部署在Swarm集群中,如何搭建swarm集群請看Docker Swarm集成搭建
源碼github分支master-swarm https://github.com/liangxiaobo/springbootcloud-all.git

1. swarm環(huán)境

基于Docker Swarm集成搭建的基礎(chǔ)上税灌,我的三臺測試機

IP 角色
172.16.10.85 manager
172.16.10.86 worker
172.16.10.87 worker

我的自建Docker私庫地址 172.16.10.192:5000

2. 優(yōu)化springcloud項目

優(yōu)化項目的配置文件注釋掉顯示真實IP,并給應(yīng)用給加hostname妆偏,舉例:

eureka:
  instance:
    leaseRenewalIntervalInSeconds: 10
    health-check-url-path: /actuator/health
#    prefer-ip-address: true
    hostname: service-user

注釋的目的是讓hostname和swarm service的name相同府怯,這然在容器內(nèi)部就可以使用http://hostname:port訪問了,因為swarm內(nèi)置的DNS使用servicename就可以訪問

2.1 打包項目鏡像

從github上下載分支 master-swarm

git clone -b master-swarm https://github.com/liangxiaobo/springbootcloud-all.git

如果你有自己的私庫請將根目錄的pom.xml中的 docker.image.prefix 改為自己的或公共庫地址
如果不上傳鏡像的話矗烛,在創(chuàng)建service時會報找不到鏡像的錯誤異常

打包docker鏡像命令可以在項目根目錄執(zhí)行:

sh mvn-package-docker.sh

也可以手動在需要的項目下執(zhí)行:

mvn package docker:build -Dmaven.test.skip=true

2.2 上傳鏡像到私有庫中

查看打包出來的鏡像:

[root@swarm-m service-user]# docker images
REPOSITORY                                    TAG                 IMAGE ID            CREATED             SIZE
172.16.10.192:5000/service-user               latest              1454fd81bb0c        42 minutes ago      692MB
172.16.10.192:5000/spring-boot-admin-server   latest              7b650dd6b8e9        12 hours ago        697MB
172.16.10.192:5000/swagger-doc                latest              03461f85d700        14 hours ago        689MB
172.16.10.192:5000/client-turbine-monitor     latest              a13d83c7ae8a        14 hours ago        692MB
172.16.10.192:5000/client-gateway-zuul        latest              379566b57536        14 hours ago        694MB
172.16.10.192:5000/client-order-ribbon        latest              e352da8519ec        14 hours ago        694MB
172.16.10.192:5000/client-feign               latest              5f8a2769bf61        14 hours ago        694MB
172.16.10.192:5000/service-order              latest              aacca65e148b        14 hours ago        692MB
172.16.10.192:5000/eureka-server              latest              dfc6e58fadf4        14 hours ago        691MB

上傳鏡像需要先登錄到私庫:

docker login -u 用戶名 -p 密碼 172.16.10.192:5000<私庫的IP:PORT>

其它節(jié)點要也登錄到私庫服務(wù)器

登出的話:

docker logout 172.16.10.192:5000<私庫的IP:PORT>

執(zhí)行上傳命令:

docker push 172.16.10.192:5000/eureka-server
# docker push <IP:PORT/鏡像名>

查看上傳的鏡像:


WX20181017-113641@2x.png

3. 創(chuàng)建swarm service

這里特別說明一下玫镐,如果是在私有庫上拉鏡像需要在 docker service create 上加 --with-registry-auth倒戏,否則會報錯,下會演示:

[root@swarm-m service-user]# docker service create  --name service-user3 --replicas 3 --network my-overlay-network --publish 8863:8763 -e "SPRING_PROFILES_ACTIVE=test" 172.16.10.192:5000/service-user
image 172.16.10.192:5000/service-user:latest could not be accessed on a registry to record
its digest. Each node will access 172.16.10.192:5000/service-user:latest independently,
possibly leading to different nodes running different
versions of the image.

0rslh6ebjm0xpf9dbw6z6ou1b
overall progress: 1 out of 3 tasks 
1/3: No such image: 172.16.10.192:5000/service-user:latest 
2/3: No such image: 172.16.10.192:5000/service-user:latest 
3/3: running   [==================================================>] 
^COperation continuing in background.

為會么要加 --with-registry-auth 官網(wǎng)上有解釋:
https://docs.docker.com/engine/reference/commandline/service_create/#create-a-service

WX20181017-115328@2x.png

大概是說:這能將登錄令牌從本地客戶端傳遞到部署服務(wù)的swarm節(jié)點恐似,從而是節(jié)點能登陸到私有注冊表拉取鏡像

3.2. 執(zhí)行docker service create

這是要創(chuàng)建服務(wù)的命令:
--network my-overlay-network 是自定義網(wǎng)絡(luò)

docker service create --name zipkin-service --replicas 2 --network my-overlay-network --publish 9411:9411  openzipkin/zipkin

docker service create --with-registry-auth --name eureka-server --replicas 2 --network my-overlay-network --publish 8761:8761 -e "SPRING_PROFILES_ACTIVE=test-peer1" 172.16.10.192:5000/eureka-server

docker service create --with-registry-auth --name service-user --replicas 2 --network my-overlay-network --publish 8763:8763 -e "SPRING_PROFILES_ACTIVE=test" 172.16.10.192:5000/service-user

docker service create --with-registry-auth --name service-order --replicas 2 --network my-overlay-network --publish 8764:8764 -e "SPRING_PROFILES_ACTIVE=test" 172.16.10.192:5000/service-order

docker service create --with-registry-auth --name spring-boot-admin-server --replicas 1 --network my-overlay-network --publish 8773:8773 -e "SPRING_PROFILES_ACTIVE=test" 172.16.10.192:5000/spring-boot-admin-server

docker service create --with-registry-auth --name client-order-ribbon --replicas 2 --network my-overlay-network --publish 8766:8766 -e "SPRING_PROFILES_ACTIVE=test" 172.16.10.192:5000/client-order-ribbon

docker service create --with-registry-auth --name client-feign --replicas 2 --network my-overlay-network --publish 8765:8765 -e "SPRING_PROFILES_ACTIVE=test" 172.16.10.192:5000/client-feign

docker service create --with-registry-auth --name client-gateway-zuul --replicas 2 --network my-overlay-network --publish 8771:8771 -e "SPRING_PROFILES_ACTIVE=test" 172.16.10.192:5000/client-gateway-zuul

docker service create --with-registry-auth --name client-turbine-monitor --replicas 2 --network my-overlay-network --publish 8767:8767 -e "SPRING_PROFILES_ACTIVE=test" 172.16.10.192:5000/client-turbine-monitor

服務(wù)創(chuàng)建完可以查看

[root@swarm-m /]# docker service ls
ID                  NAME                       MODE                REPLICAS            IMAGE                                                PORTS
lbwpqv24hw9b        client-feign               replicated          2/2                 172.16.10.192:5000/client-feign:latest               *:8765->8765/tcp
vwtnddgl94ck        client-gateway-zuul        replicated          2/2                 172.16.10.192:5000/client-gateway-zuul:latest        *:8771->8771/tcp
14vmm45dtnl9        client-order-ribbon        replicated          2/2                 172.16.10.192:5000/client-order-ribbon:latest        *:8766->8766/tcp
y8twu0mclhia        client-turbine-monitor     replicated          2/2                 172.16.10.192:5000/client-turbine-monitor:latest     *:8767->8767/tcp
p0sy1vwrvq6f        eureka-server              replicated          2/2                 172.16.10.192:5000/eureka-server:latest              *:8761->8761/tcp
zqlpb42ipqhu        service-order              replicated          2/2                 172.16.10.192:5000/service-order:latest              *:8764->8764/tcp
kh89t4hpgr70        service-user               replicated          2/2                 172.16.10.192:5000/service-user:latest               *:8763->8763/tcp
0rslh6ebjm0x        service-user3              replicated          3/3                 172.16.10.192:5000/service-user:latest               *:8863->8763/tcp
25ji5lwx66cq        spring-boot-admin-server   replicated          1/1                 172.16.10.192:5000/spring-boot-admin-server:latest   *:8773->8773/tcp
iptkiejwkuyu        zipkin-service             replicated          2/2                 openzipkin/zipkin:latest                             *:9411->9411/tcp

訪問 http://172.16.10.85:8761/

WX20181017-142759@2x.png

訪問 http://172.16.10.85:8773 并用admin登錄

WX20181017-142956@2x.png

4. 使用stack發(fā)布服務(wù)

編寫docker-compose.yml

version: '3'

services:

  zipkin-service:
    image: openzipkin/zipkin:latest
    deploy:
      mode: replicated
      replicas: 2
      restart_policy:
        condition: on-failure
    ports:
      - "9411:9411"
    networks:
      my-overlay-network:
        aliases:
          - zipkin-service

  eureka-server:
    image: 172.16.10.192:5000/eureka-server:latest
    deploy:
      mode: replicated
      replicas: 2
      restart_policy:
        condition: on-failure
#      placement:
#        constraints: [node.role == worker]
    ports:
      - "8761:8761"
    networks:
      my-overlay-network:
        aliases:
          - eureka-server
    environment:
        - "SPRING_PROFILES_ACTIVE=test-peer1"

  service-user:
    image: 172.16.10.192:5000/service-user:latest
    deploy:
      mode: replicated
      replicas: 2
      restart_policy:
        condition: on-failure
    ports:
      - "8763:8763"
    networks:
      my-overlay-network:
        aliases:
          - service-user
    environment:
        - "SPRING_PROFILES_ACTIVE=test"

  service-order:
    image: 172.16.10.192:5000/service-order:latest
    deploy:
      mode: replicated
      replicas: 2
      restart_policy:
        condition: on-failure
    ports:
      - "8764:8764"
    networks:
      my-overlay-network:
        aliases:
          - service-order
    environment:
        - "SPRING_PROFILES_ACTIVE=test"

  client-order-ribbon:
    image: 172.16.10.192:5000/client-order-ribbon:latest
    deploy:
      mode: replicated
      replicas: 2
      restart_policy:
        condition: on-failure
    ports:
      - "8766:8766"
    networks:
      my-overlay-network:
        aliases:
          - client-order-ribbon
    environment:
        - "SPRING_PROFILES_ACTIVE=test"

  client-feign:
    image: 172.16.10.192:5000/client-feign:latest
    deploy:
      mode: replicated
      replicas: 2
      restart_policy:
        condition: on-failure
    ports:
      - "8765:8765"
    networks:
      my-overlay-network:
        aliases:
          - client-feign
    environment:
        - "SPRING_PROFILES_ACTIVE=test"

  client-gateway-zuul:
    image: 172.16.10.192:5000/client-gateway-zuul:latest
    deploy:
      mode: replicated
      replicas: 2
      restart_policy:
        condition: on-failure
    ports:
      - "8771:8771"
    networks:
      my-overlay-network:
        aliases:
          - client-gateway-zuul
    environment:
        - "SPRING_PROFILES_ACTIVE=test"

  client-turbine-monitor:
    image: 172.16.10.192:5000/client-turbine-monitor:latest
    deploy:
      mode: replicated
      replicas: 2
      restart_policy:
        condition: on-failure
    ports:
      - "8767:8767"
    networks:
      my-overlay-network:
        aliases:
          - client-turbine-monitor
    environment:
        - "SPRING_PROFILES_ACTIVE=test"

  spring-boot-admin-server:
    image: 172.16.10.192:5000/spring-boot-admin-server:latest
    deploy:
      mode: replicated
      replicas: 1
      restart_policy:
        condition: on-failure
    ports:
      - "8773:8773"
    networks:
      my-overlay-network:
        aliases:
          - spring-boot-admin-server
    environment:
        - "SPRING_PROFILES_ACTIVE=test"



  visualizer:
    image: dockersamples/visualizer:stable
    ports:
      - "8080:8080"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock"
    deploy:
      placement:
        constraints: [node.role == manager]
    networks:
      - my-overlay-network

networks:
  my-overlay-network:
    driver: overlay

配置中配置的自定義網(wǎng)絡(luò)

networks:
  my-overlay-network:
    driver: overlay

還可以在網(wǎng)絡(luò)中配置別名

networks:
      my-overlay-network:
        aliases:
          - spring-boot-admin-server

執(zhí)行命令:
如果是私有庫必須加上 --with-registry-auth 否則鏡像下載不了

docker stack deploy -c docker-compose.yml --with-registry-auth app

公共庫可以使用:

docker stack deploy -c docker-compose.yml  app

查看

[root@swarm-m /]# docker stack ls
NAME                SERVICES            ORCHESTRATOR
app                 10                  Swarm
[root@swarm-m /]# docker service ls
ID                  NAME                           MODE                REPLICAS            IMAGE                                                PORTS
0f4hpqx7u5p3        app_client-feign               replicated          2/2                 172.16.10.192:5000/client-feign:latest               *:8765->8765/tcp
ju5rg4l5x0ir        app_client-gateway-zuul        replicated          2/2                 172.16.10.192:5000/client-gateway-zuul:latest        *:8771->8771/tcp
i1t6wbyd2si6        app_client-order-ribbon        replicated          2/2                 172.16.10.192:5000/client-order-ribbon:latest        *:8766->8766/tcp
rkld2rq7ntw5        app_client-turbine-monitor     replicated          2/2                 172.16.10.192:5000/client-turbine-monitor:latest     *:8767->8767/tcp
j1s5yy1lkw9f        app_eureka-server              replicated          2/2                 172.16.10.192:5000/eureka-server:latest              *:8761->8761/tcp
u38tk0j0ez4l        app_service-order              replicated          2/2                 172.16.10.192:5000/service-order:latest              *:8764->8764/tcp
sv6h294y9r60        app_service-user               replicated          2/2                 172.16.10.192:5000/service-user:latest               *:8763->8763/tcp
ybu18hzo4ra6        app_spring-boot-admin-server   replicated          1/1                 172.16.10.192:5000/spring-boot-admin-server:latest   *:8773->8773/tcp
2m48cqlcip6v        app_visualizer                 replicated          1/1                 dockersamples/visualizer:stable                      *:8080->8080/tcp
692kwr04oa5u        app_zipkin-service             replicated          2/2                 openzipkin/zipkin:latest                             *:9411->9411/tcp
WX20181018-093328@2x.png
WX20181017-180634@2x.png
WX20181017-180615@2x.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末峭梳,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子蹂喻,更是在濱河造成了極大的恐慌,老刑警劉巖捂寿,帶你破解...
    沈念sama閱讀 222,681評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件口四,死亡現(xiàn)場離奇詭異,居然都是意外死亡秦陋,警方通過查閱死者的電腦和手機蔓彩,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,205評論 3 399
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人赤嚼,你說我怎么就攤上這事旷赖。” “怎么了更卒?”我有些...
    開封第一講書人閱讀 169,421評論 0 362
  • 文/不壞的土叔 我叫張陵等孵,是天一觀的道長。 經(jīng)常有香客問我蹂空,道長俯萌,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,114評論 1 300
  • 正文 為了忘掉前任上枕,我火速辦了婚禮咐熙,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘辨萍。我一直安慰自己棋恼,他們只是感情好,可當我...
    茶點故事閱讀 69,116評論 6 398
  • 文/花漫 我一把揭開白布锈玉。 她就那樣靜靜地躺著爪飘,像睡著了一般。 火紅的嫁衣襯著肌膚如雪嘲玫。 梳的紋絲不亂的頭發(fā)上悦施,一...
    開封第一講書人閱讀 52,713評論 1 312
  • 那天,我揣著相機與錄音去团,去河邊找鬼抡诞。 笑死,一個胖子當著我的面吹牛土陪,可吹牛的內(nèi)容都是我干的昼汗。 我是一名探鬼主播,決...
    沈念sama閱讀 41,170評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼鬼雀,長吁一口氣:“原來是場噩夢啊……” “哼顷窒!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起源哩,我...
    開封第一講書人閱讀 40,116評論 0 277
  • 序言:老撾萬榮一對情侶失蹤鞋吉,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后励烦,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體谓着,經(jīng)...
    沈念sama閱讀 46,651評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,714評論 3 342
  • 正文 我和宋清朗相戀三年坛掠,在試婚紗的時候發(fā)現(xiàn)自己被綠了赊锚。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片治筒。...
    茶點故事閱讀 40,865評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖舷蒲,靈堂內(nèi)的尸體忽然破棺而出耸袜,到底是詐尸還是另有隱情,我是刑警寧澤牲平,帶...
    沈念sama閱讀 36,527評論 5 351
  • 正文 年R本政府宣布堤框,位于F島的核電站,受9級特大地震影響欠拾,放射性物質(zhì)發(fā)生泄漏胰锌。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 42,211評論 3 336
  • 文/蒙蒙 一藐窄、第九天 我趴在偏房一處隱蔽的房頂上張望资昧。 院中可真熱鬧,春花似錦荆忍、人聲如沸格带。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,699評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽叽唱。三九已至,卻和暖如春微宝,著一層夾襖步出監(jiān)牢的瞬間棺亭,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,814評論 1 274
  • 我被黑心中介騙來泰國打工蟋软, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留镶摘,地道東北人。 一個月前我還...
    沈念sama閱讀 49,299評論 3 379
  • 正文 我出身青樓岳守,卻偏偏與公主長得像凄敢,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子湿痢,可洞房花燭夜當晚...
    茶點故事閱讀 45,870評論 2 361

推薦閱讀更多精彩內(nèi)容