先來(lái)架構(gòu),如圖:
1.前端出口服務(wù)為阿里SLB
2.Zookeeper集群為整個(gè)微服務(wù)環(huán)境提供注冊(cè)中心,Zookeeper集群每個(gè)znode的IP固定,也同樣由config-service服務(wù)配置。
3.集中配置服務(wù)config-service,由git集中管理配置文件,config-service配置映射宿主機(jī)某個(gè)目錄。配置如需修改直接Git中修改,觸發(fā)宿主機(jī)映射的目錄中進(jìn)行g(shù)it pull操作,然后重啟對(duì)應(yīng)的provider,配置即可生效。
4.facade作為分發(fā)器做分發(fā)服務(wù)。
十多年專注建站、設(shè)計(jì)、互聯(lián)網(wǎng)產(chǎn)品按需網(wǎng)站建設(shè)服務(wù),業(yè)務(wù)涵蓋品牌網(wǎng)站制作、商城建設(shè)、微信小程序、軟件系統(tǒng)開(kāi)發(fā)、app軟件開(kāi)發(fā)等。憑借多年豐富的經(jīng)驗(yàn),我們會(huì)仔細(xì)了解每個(gè)客戶的需求而做出多方面的分析、設(shè)計(jì)、整合,為客戶設(shè)計(jì)出具風(fēng)格及創(chuàng)意性的商業(yè)解決方案,成都創(chuàng)新互聯(lián)公司更提供一系列網(wǎng)站制作和網(wǎng)站推廣的服務(wù),以推動(dòng)各中小企業(yè)全面信息數(shù)字化,并利用創(chuàng)新技術(shù)幫助各行業(yè)提升企業(yè)形象和運(yùn)營(yíng)效率。
微服務(wù)工作流程:
1)provider啟動(dòng),讀取寫(xiě)死的config-service配置服務(wù),從配置服務(wù)中獲取到Zookeeper地址,進(jìn)行注冊(cè)。
2)請(qǐng)求由阿里SLB分發(fā)至對(duì)應(yīng)微服務(wù)facade,網(wǎng)關(guān)前往Zookeeper集群注冊(cè)中心獲取可用服務(wù)。
3)獲取到可用provider后,facade進(jìn)行分發(fā)請(qǐng)求,策略為輪詢。
4)如后端provider掛了,無(wú)法提供服務(wù),Zookeeper會(huì)自動(dòng)剔除該服務(wù)的注冊(cè)。
建立微服務(wù)基礎(chǔ)運(yùn)行環(huán)境,gateway網(wǎng)關(guān)、Zookeeper注冊(cè)中心、config-service配置基礎(chǔ)運(yùn)行環(huán)境。開(kāi)始構(gòu)建以上基礎(chǔ)運(yùn)行環(huán)境:
一、創(chuàng)建zookeeper3.4.10集群服務(wù)
需包含了jdk和配置好環(huán)境變量的鏡像,jdk1.8鏡像。
jdk1.8鏡像交付到阿里云Docker Registry倉(cāng)庫(kù)
Docker Rancher宿主機(jī)
1.下載jdk1.8.0_25.tar包,自行下載。
創(chuàng)建jdk構(gòu)建build目錄
mkdir -p /data/docker/ms-jdk
ll
-rw-r--r-- 1 root root 155 Dec 29 15:11 dockerfile
drwxr-xr-x 9 root root 268 Nov 17 2017 jdk1.8.0_25
2.編寫(xiě)dockfile
cat dockerfile
FROM centos
MAINTAINER jdk1.8 "name@alaxiaoyou.com"
ADD jdk1.8.0_25/ /usr/local/jdk1.8
ENV JAVA_HOME /usr/local/jdk1.8
ENV PATH $JAVA_HOME/bin:$PATH
3.構(gòu)建
[root@rancher-qa ms-jdk]# docker build -t ms/jdk1.8:v1 .
Sending build context to Docker daemon 308.6MB
Step 1/5 : FROM centos
latest: Pulling from library/centos
a02a4930cb5d: Pull complete
Digest: sha256:184e5f35598e333bfa7de10d8fb1cebb5ee4df5bc0f970bf2b1e7c7345136426
Status: Downloaded newer image for centos:latest
---> 1e1148e4cc2c
Step 2/5 : MAINTAINER jdk1.8 "yuhuanghui@alaxiaoyou.com"
---> Running in 74ba397d2ab5
---> 57601effbcb9
Removing intermediate container 74ba397d2ab5
Step 3/5 : ADD jdk1.8.0_25/ /usr/local/jdk1.8
---> e960522c0399
Removing intermediate container 9408672a94d9
Step 4/5 : ENV JAVA_HOME /usr/local/jdk1.8
---> Running in 42d9c2b3239c
---> 9bb3badf22e0
Removing intermediate container 42d9c2b3239c
Step 5/5 : ENV PATH $JAVA_HOME/bin:$PATH
---> Running in d8af2f726409
---> 00d3c71ae0be
Removing intermediate container d8af2f726409
Successfully built 00d3c71ae0be
Successfully tagged ms/jdk1.8:v1
docker images 查看
4.登錄阿里云容器鏡像服務(wù)
創(chuàng)建鏡像倉(cāng)庫(kù)名稱ms-jdk1.8
5.鏡像push到阿里云Docker Registry
切換到rancher/server
登錄到阿里云Docker Registry
登錄Docker Registry -> 打tag -> push到Docker Registry
[root@rancher-qa ms-jdk]# docker login --username=name@alaxiaoyou.com registry.cn-hangzhou.aliyuncs.com
Password: ********
Login Succeeded
[root@rancher-qa ms-jdk]#
[root@rancher-qa ms-jdk]# docker tag 00d3c71ae0be registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/ms-jdk1.8:v1.0
[root@rancher-qa ms-jdk]# docker push registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/ms-jdk1.8:v1.0
The push refers to a repository [registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/ms-jdk1.8]
96f6833b8de8: Pushed
071d8bd76517: Pushed
v1.0: digest: sha256:1c2f642e7e49cd607c36032612efcd875c191abc5edb642d7e86483d1d5b392d size: 742
查看:
至此,ms-jdk1.8鏡像交付完成。
zookeeper3.4.10集群創(chuàng)建,并將Zookeeper3.4.10鏡像交付到阿里云Docker Registry倉(cāng)庫(kù)。
1.zookeeper3.4.10集群構(gòu)建前提準(zhǔn)備:
zookeeper3.4.10集群需其配置文件中指定各個(gè)選舉節(jié)點(diǎn)的ip和端口,并且zookeeper集群中的datadir(zoo.cfg配置中:dataDir=/tmp/zookeeper)中,需指定節(jié)點(diǎn)的myid,才能完成Zookeeper集群搭建。在不使用docker link功能前提下,容器化Zookeeper集群,需要解決:
1).myid問(wèn)題。
2).Zookeeper集群節(jié)點(diǎn)(leader、follower)選舉配置。
可使用docker內(nèi)置的volume功能,將配置文件和Zookeeper的datadir映射至宿主機(jī)進(jìn)行管理。
如在rancher中:
所以,宿主機(jī)必須存在:
1) zookeeper-config/,目錄為Zookeeper所有配置文件,包含集群的選舉節(jié)點(diǎn)IP和選舉端口,主要zoo.cfg文件中。
ll zookeeper-config/
total 16
-rw-r--r-- 1 root root 535 Jan 4 17:43 configuration.xsl
-rw-r--r-- 1 root root 2161 Jan 4 17:43 log4j.properties
-rw-r--r-- 1 root root 1012 Jan 4 17:43 zoo.cfg
-rw-r--r-- 1 root root 922 Jan 4 17:43 zoo_sample.cf
2) zookeeper-data01/,目錄對(duì)應(yīng)zookeeper集群中的datadir,有存放一個(gè)myid文件,Zookeeper集群?jiǎn)?dòng)后產(chǎn)生的數(shù)據(jù)也會(huì)持久化到此宿主機(jī)目錄下。Zookeeper集群有3個(gè)znode節(jié)點(diǎn),所以datadir有:
zookeeper-data02/
zookeeper-data03/
ll /data/docker/
drwxr-xr-x 3 root root 48 Jan 5 17:25 ms-zookeeper3.4.10
drwxr-xr-x 2 root root 92 Jan 5 16:54 zookeeper-config
drwxr-xr-x 2 root root 18 Jan 4 17:59 zookeeper-data01
drwxr-xr-x 2 root root 18 Jan 4 18:02 zookeeper-data02
drwxr-xr-x 2 root root 18 Jan 4 18:02 zookeeper-data03
2.鏡像構(gòu)建
2.1 下載zookeeper-3.4.10.tar.gz包
wget https://mirrors.cnnic.cn/apache/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz
創(chuàng)建zookeeper-3.4.10構(gòu)建build目錄
mkdir -p /data/docker/ms-zookeeper3.4.10/
-rw-r--r-- 1 root root 207 Jan 4 18:58 dockerfile
drwxr-xr-x 10 root root 4096 Mar 23 2017 zookeeper-3.4.10
2.2 編寫(xiě)dockfile
vim /data/docker/ms-zookeeper3.4.10/dockerfile
FROM ms/jdk1.8:v1
MAINTAINER zookeeper3.4.10 "name@alaxiaoyou.com"
ADD zookeeper-3.4.10/ /usr/local/zookeeper/
WORKDIR /usr/local/zookeeper
ENTRYPOINT ./bin/zkServer.sh start && tail -f zookeeper.out
其中zookeeper-3.4.10目錄添加至鏡像中,然后交付。但只是一個(gè)Zookeeper運(yùn)行的模板,具體配置可通過(guò)volume,用宿主機(jī)的配置覆蓋鏡像中的配置。
2.3 構(gòu)建,鏡像
cd /data/docker/ms-zookeeper3.4.10
docker build -t ms/zookeeper3.4.10:v1 .
docker images | grep zook
ms/zookeeper3.4.10 v1 cb1bea8cf3d5 23 hours ago 567MB
registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/tmp-zookeeper-ms v1 cb1bea8cf3d5 23 hours ago 567MB
鏡像運(yùn)行,本地docker本身命令行啟動(dòng)Zookeeper容器
[root@rancher-qa docker]# docker run --name zookeeper01 -it -v /data/docker/zookeeper-config/:/usr/local/zookeeper/conf/ -v /data/docker/zookeeper-data01/:/tmp/zookeeper/ ms/zookeeper3.4.10 /bin/bash
以上docker命令行啟動(dòng)卷映射目錄正常,宿主機(jī)目錄能映射到容器中。但rancher中用UI界面進(jìn)行卷映射時(shí),發(fā)現(xiàn)宿主機(jī)映射的目錄在容器中并未映射到。待解決。
此問(wèn)題是由于,rancher有兩臺(tái)主機(jī),而容器都運(yùn)行在未進(jìn)行設(shè)置映射目錄的宿主機(jī)中。此問(wèn)題困擾近一周時(shí)間,<_>
2.4 登錄阿里云容器鏡像服務(wù)
創(chuàng)建鏡像倉(cāng)庫(kù)名稱tmp-zookeeper-ms
2.5 zookeeper鏡像push到阿里云Docker Registry
切換到rancher/server
登錄到阿里云Docker Registry
登錄Docker Registry -> 打tag -> push到Docker Registry
[root@rancher-qa ms-zookeeper3.4.10]# docker login --username=name@alaxiaoyou.com registry.cn-hangzhou.aliyuncs.com
Password:
Login Succeeded
[root@rancher-qa ms-zookeeper3.4.10]#
[root@rancher-qa ms-zookeeper3.4.10]# docker tag cb1bea8cf3d5 registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/tmp-zookeeper-ms:v1
[root@rancher-qa ms-zookeeper3.4.10]#
[root@rancher-qa ms-zookeeper3.4.10]# docker push registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/tmp-zookeeper-ms:v1
The push refers to a repository [registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/tmp-zookeeper-ms]
ed1b06ecb437: Pushed
96f6833b8de8: Mounted from xmbaby-tmp/ms-jdk1.8
071d8bd76517: Mounted from xmbaby-tmp/ms-jdk1.8
v1: digest: sha256:c55b36fb410d7db9409e3ca4fb27e0d8d2d05e1970d99270ff9d5a6d8ee25e0b size: 954
[root@rancher-qa ms-zookeeper3.4.10]# docker images | grep zook
registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/tmp-zookeeper-ms latest cb1bea8cf3d5 8 days ago 567MB
registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/tmp-zookeeper-ms v1 cb1bea8cf3d5 8 days ago 567MB
ms/zookeeper3.4.10 latest cb1bea8cf3d5 8 days ago 567MB
ms/zookeeper3.4.10 v1 cb1bea8cf3d5 8 days ago 567MB
2.6查看
至此,tmp-zookeeper-ms鏡像交付完成。
3.這里選擇rancher來(lái)做docker集群的管理和編排,除了使用docker本身命令行界面啟動(dòng)Zookeeper容器外,推薦使用rancher UI界面進(jìn)行容器管理和創(chuàng)建。
在rancher中,添加zk1、zk2、zk3容器服務(wù):
創(chuàng)建zk1:
卷管理:
設(shè)置容器主機(jī):
另兩個(gè)znode節(jié)點(diǎn),zk2、zk3同理操作,只是路徑和名稱做稍微改動(dòng)。
進(jìn)入Zookeeper容器中查看啟動(dòng)信息,查看Zookeeper機(jī)器znode2為集群leader:
二、創(chuàng)建config-service服務(wù)
集中配置服務(wù)config-service,作為公司微服務(wù)基礎(chǔ)運(yùn)行環(huán)境部分,需要優(yōu)先單獨(dú)創(chuàng)建。
為做好后期集中打包,使用maven工具,需先創(chuàng)建一maven鏡像,該maven鏡像集成了公司自己nexus服務(wù),任何開(kāi)發(fā)都可通過(guò)此鏡像進(jìn)行快速打包本地的開(kāi)發(fā)環(huán)境的jar包。
Docker Rancher宿主機(jī)
1.下載apache-maven-3.3.9-bin.tar.gz包,自行下載。
創(chuàng)建maven構(gòu)建build目錄
mkdir -p /data/docker/maven3.3.9
ll
drwxr-xr-x 6 root root 99 Jan 13 15:36 apache-maven-3.3.9
-rw-r--r-- 1 root root 8491533 Jan 13 15:34 apache-maven-3.3.9-bin.tar.gz
-rw-r--r-- 1 root root 393 Jan 13 16:53 dockerfile
drwxr-xr-x 9 root root 268 Jan 13 15:43 jdk1.8.0_25
-rwxr-xr-x 1 root root 55 Jan 13 16:53 maven.sh
2.編寫(xiě)dockfile
cat dockerfile
FROM ms/jdk1.8:v1
MAINTAINER maven-3.3.9 "name@alaxiaoyou.com"
RUN mkdir /usr/local/maven-3.3.9/ #&& mkdir /usr/local/jdk1.8/ #因基于ms/jdk1.8:v1鏡像,已有的jdk環(huán)境
ADD apache-maven-3.3.9/ /usr/local/maven-3.3.9/
#ADD jdk1.8.0_25/ /usr/local/jdk1.8/
ADD maven.sh /tmp/
ENV JAVA_HOME /usr/local/jdk1.8
ENV M2_HOME /usr/local/maven-3.3.9
ENV PATH $JAVA_HOME/bin:$M2_HOME/bin:$PATH
CMD /tmp/maven.sh
cat maven.sh
#!/bin/bash
cd /data/httpd/${msname}
mvn clean install
3.構(gòu)建
[root@rancher-qa maven3.3.9]# docker build -t ms/maven-3.3.9 .
Sending build context to Docker daemon 327.2MB
Step 1/9 : FROM ms/jdk1.8:v1
---> 00d3c71ae0be
Step 2/9 : MAINTAINER maven-3.3.9 "yuhuanghui@alaxiaoyou.com"
---> Using cache
---> 80a6d05eae5a
Step 3/9 : RUN mkdir /usr/local/maven-3.3.9/ #&& mkdir /usr/local/jdk1.8/
---> Using cache
---> 92ac17c859a8
Step 4/9 : ADD apache-maven-3.3.9/ /usr/local/maven-3.3.9/
---> Using cache
---> fa81af79d732
Step 5/9 : ADD maven.sh /tmp/
---> Using cache
---> 034053775e26
Step 6/9 : ENV JAVA_HOME /usr/local/jdk1.8
---> Using cache
---> ad8e78b5a021
Step 7/9 : ENV M2_HOME /usr/local/maven-3.3.9
---> Running in cfae5d0a6e53
---> ccfcc8638e43
Removing intermediate container cfae5d0a6e53
Step 8/9 : ENV PATH $JAVA_HOME/bin:$M2_HOME/bin:$PATH
---> Running in 30b2f568c1a3
---> 27e68ef34a42
Removing intermediate container 30b2f568c1a3
Step 9/9 : CMD /tmp/maven.sh
---> Running in db48895f5502
---> 21c6f1a3b0cf
Removing intermediate container db48895f5502
Successfully built 21c6f1a3b0cf
Successfully tagged ms/maven-3.3.9:latest
[root@rancher-qa maven3.3.9]#
[root@rancher-qa maven3.3.9]# docker images | grep maven
ms/maven-3.3.9 latest 21c6f1a3b0cf 34 minutes ago 519MB
運(yùn)行此maven鏡像,打包命令示例:
[root@rancher-qa maven3.3.9]# docker run --rm -it -v /data/docker/ms-config/:/data/httpd/config-service/ -e msname=config-service ms/maven-3.3.9
說(shuō)明:-v宿主機(jī)配置目錄/data/docker/ms-config/映射到容器/data/httpd/config-service/,-e定義maven.sh腳本${msname}變量。
4.登錄阿里云容器鏡像服務(wù)
創(chuàng)建本地倉(cāng)庫(kù)名稱ms-maven-3.3.9
5.鏡像push到阿里云Docker Registry
切換到rancher/server
登錄到阿里云Docker Registry
登錄Docker Registry -> 打tag -> push到Docker Registry
[root@rancher-qa ms-jdk]# docker login --username=name@alaxiaoyou.com registry.cn-hangzhou.aliyuncs.com
Password: **********
Login Succeeded
[root@rancher-qa ms-jdk]#
[root@rancher-qa ms-jdk]# docker tag 21c6f1a3b0cf registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/ms-maven-3.3.9:latest
[root@rancher-qa ms-jdk]# docker push registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/ms-maven-3.3.9:latest
The push refers to a repository [registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/ms-maven-3.3.9]
607c588ba0d1: Pushed
2e4a77c00b58: Pushed
48c8f5464987: Pushed
96f6833b8de8: Mounted from xmbaby-tmp/tmp-zookeeper-ms
071d8bd76517: Mounted from xmbaby-tmp/tmp-zookeeper-ms
latest: digest: sha256:9ceac47cffbf0c35ed66212a042ad10f3905cae924033caf4cbca8cbf4dbf4dd size: 1367
查看:
至此,ms-maven-3.3.9鏡像交付完成。此鏡像交付完成后,后期基本無(wú)需改動(dòng),直接latest版本。
服務(wù)config-service鏡像開(kāi)始交付:
1.1準(zhǔn)備集中配置項(xiàng)目alaxiaoyou-config-service-0.0.1-SNAPSHOT.jar包,gitlab倉(cāng)庫(kù)中。
創(chuàng)建config-service構(gòu)建build目錄
mkdir -p /data/httpd/ms-config-service-tmp
-rw-r--r-- 1 root root 276 Jan 14 17:34 dockerfile
drwxr-xr-x 2 root root 58 Jan 14 17:31 target
1.2 編寫(xiě)dockfile
cat /data/httpd/ms-config-service-tmp/dockerfile
FROM ms/jdk1.8:v1
MAINTAINER tmp-config-service "name@alaxiaoyou.com"
RUN mkdir -p /data/httpd/
ADD target/alaxiaoyou-config-service-0.0.1-SNAPSHOT.jar /data/httpd/
WORKDIR /data/httpd/
ENTRYPOINT java -Xmx128m -Xss512k -jar alaxiaoyou-config-service-0.0.1-SNAPSHOT.jar
說(shuō)明:ENTRYPOINT進(jìn)入容器中執(zhí)行此jar包。
1.3 構(gòu)建,鏡像
docker build -t ms/config-service .
Sending build context to Docker daemon 21.2MB
Step 1/6 : FROM ms/jdk1.8:v1
---> 00d3c71ae0be
Step 2/6 : MAINTAINER tmp-config-service "yuhuanghui@alaxiaoyou.com"
---> Running in a3e0038610ff
---> 75d7642e16ec
Removing intermediate container a3e0038610ff
Step 3/6 : RUN mkdir -p /data/httpd/
---> Running in 4b1ff33f58df
---> f43b916f6835
Removing intermediate container 4b1ff33f58df
Step 4/6 : ADD target/alaxiaoyou-config-service-0.0.1-SNAPSHOT.jar /data/httpd/
---> 99608e52e219
Removing intermediate container 0bd4d9130dd0
Step 5/6 : WORKDIR /data/httpd/
---> 656a442c7ac2
Removing intermediate container d18fbdc1c6a6
Step 6/6 : ENTRYPOINT java -Xmx128m -Xss512k -jar alaxiaoyou-config-service-0.0.1-SNAPSHOT.jar
---> Running in 0371f9addb5d
---> c7375c40286e
Removing intermediate container 0371f9addb5d
Successfully built c7375c40286e
Successfully tagged ms/config-service:latest
[root@rancher-qa ms-config-service-tmp]# pwd
/data/httpd/ms-config-service-tmp
[root@rancher-qa ms-config-service-tmp]# docker images | grep config
ms/config-service latest c7375c40286e 2 minutes ago 530MB
registry.cn-hangzhou.aliyuncs.com/xmbaby-pre/ms-config latest 51a4521d7581 24 months ago 578MB
1.4 登錄阿里云容器鏡像服務(wù)
創(chuàng)建鏡像本地倉(cāng)庫(kù)名稱tmp-config-service-ms
1.5 鏡像push到阿里云Docker Registry
切換到rancher/server
登錄到阿里云Docker Registry
登錄Docker Registry -> 打tag -> push到Docker Registry
[root@rancher-qa ms-config-service-tmp]# docker login --username=name@alaxiaoyou.com registry.cn-hangzhou.aliyuncs.com
Password: ******
Login Succeeded
[root@rancher-qa ms-config-service-tmp]# docker tag c7375c40286e registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/tmp-config-service-ms:latest
[root@rancher-qa ms-config-service-tmp]#
[root@rancher-qa ms-config-service-tmp]# docker push registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/tmp-config-service-ms:latest
The push refers to a repository [registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/tmp-config-service-ms]
76925e259a39: Pushed
d31d3604ba1d: Pushed
96f6833b8de8: Mounted from xmbaby-tmp/ms-maven-3.3.9
071d8bd76517: Mounted from xmbaby-tmp/ms-maven-3.3.9
latest: digest: sha256:78c0489e045a1a9a7220ea941ca8b7fd6db348e5423e478a45ac2bbc2117c180 size: 1161
至此,tmp-config-service-ms鏡像交付完成。
2.1 啟動(dòng)tmp-config-service-ms服務(wù),rancher中操作。
創(chuàng)建服務(wù)tmp-config-service-ms:
設(shè)置卷:
設(shè)置容器主機(jī)名:
2.2 啟動(dòng)tmp-config-service-ms容器服務(wù)。此provider只在啟動(dòng)初,會(huì)找一次config-service,但此config-service服務(wù)為單點(diǎn)服務(wù)。不過(guò),容器可快速銷毀和生成的特性,即使此config-service配置服務(wù)出現(xiàn)問(wèn)題,也可立即自動(dòng)重啟或手工銷毀有問(wèn)題的容器,并重新生成新的config-service服務(wù)容器,此過(guò)程為秒級(jí),是可接受配置服務(wù)為單點(diǎn)。
進(jìn)入config-service服務(wù)容器,查看啟動(dòng)情況,日志查看:
三、創(chuàng)建gateway-service服務(wù)
網(wǎng)關(guān)gateway-service服務(wù)是整個(gè)微服務(wù)架構(gòu)的入口,在構(gòu)建provider前,必須建立先創(chuàng)建好網(wǎng)關(guān)服務(wù)。
git clone網(wǎng)關(guān)項(xiàng)目代碼,git@xxxx.git
1.創(chuàng)建網(wǎng)關(guān)項(xiàng)目目錄:
mkdir -p /data/httpd/ms-api-gateway-tmp/alaxiaoyou-api-gateway
[root@rancher-qa ms-api-gateway-tmp]# git clone git@xxxxx.git
Cloning into 'alaxiaoyou-api-gateway'...
remote: Counting objects: 1198, done.
remote: Compressing objects: 100% (552/552), done.
remote: Total 1198 (delta 423), reused 554 (delta 241)
Receiving objects: 100% (1198/1198), 108.00 KiB | 0 bytes/s, done.
Resolving deltas: 100% (423/423), done.
[root@rancher-qa ms-api-gateway-tmp]#
[root@rancher-qa ms-api-gateway-tmp]# ll
total 0
drwxr-xr-x 4 root root 44 Jan 14 19:10 alaxiaoyou-api-gateway
[root@rancher-qa ms-api-gateway-tmp]# pwd
/data/httpd/ms-api-gateway-tmp
[root@rancher-qa ms-api-gateway-tmp]# cd alaxiaoyou-api-gateway/
[root@rancher-qa alaxiaoyou-api-gateway]#
[root@rancher-qa alaxiaoyou-api-gateway]# ll
total 8
-rw-r--r-- 1 root root 4811 Jan 14 19:10 pom.xml
drwxr-xr-x 3 root root 18 Jan 14 19:10 src
[root@rancher-qa alaxiaoyou-api-gateway]#
[root@rancher-qa alaxiaoyou-api-gateway]# pwd
/data/httpd/ms-api-gateway-tmp/alaxiaoyou-api-gateway
在gateway-service項(xiàng)目目錄下,運(yùn)行網(wǎng)關(guān)代碼打包命令:
docker run --rm -it -v /data/httpd/ms-api-gateway-tmp/alaxiaoyou-api-gateway:/data/httpd/gateway-service/ -e msname=gateway-service ms/maven-3.3.9
[root@rancher-qa alaxiaoyou-api-gateway]# docker run --rm -it -v /data/httpd/ms-api-gateway-tmp/alaxiaoyou-api-gateway:/data/httpd/gateway-service/ -e msname=gateway-service ms/maven-3.3.9
[root@rancher-qa alaxiaoyou-api-gateway]# ll
total 58436
-rw-r--r-- 1 root root 59823092 Jan 14 19:27 alaxiaoyou-api-gateway-0.0.1-SNAPSHOT.jar
-rw-r--r-- 1 root root 343 Jan 14 19:28 dockerfile
drwxr-xr-x 3 root root 20 Jan 14 19:28 gateway-service
-rw-r--r-- 1 root root 4811 Jan 14 19:10 pom.xml
drwxr-xr-x 3 root root 18 Jan 14 19:10 src
此打包命令,目前并未成功將包alaxiaoyou-api-gateway-0.0.1-SNAPSHOT.jar打出,該包從其他機(jī)器cp過(guò)來(lái)。待解決。
2.創(chuàng)建網(wǎng)關(guān)項(xiàng)目dockerfile
vim /data/httpd/ms-api-gateway-tmp/alaxiaoyou-api-gateway/dockerfile
FROM ms/jdk1.8:v1
MAINTAINER gateway-service "name@alaxiaoyou.com"
RUN mkdir /data/httpd/ -p
ADD gateway-service/target/alaxiaoyou-api-gateway-0.0.1-SNAPSHOT.jar /data/httpd/
EXPOSE 80 #此處雖然開(kāi)啟了80端口映射,但是并沒(méi)有做link鏈接
WORKDIR /data/httpd/
ENTRYPOINT java -jar alaxiaoyou-api-gateway-0.0.1-SNAPSHOT.jar
說(shuō)明:ENTRYPOINT進(jìn)入容器中執(zhí)行此jar包。
3.構(gòu)建,鏡像
[root@rancher-qa alaxiaoyou-api-gateway]# docker build -t ms/tmp-gateway-service .
Sending build context to Docker daemon 60.27MB
Step 1/7 : FROM ms/jdk1.8:v1
---> 00d3c71ae0be
Step 2/7 : MAINTAINER gateway-service "yuhuanghui@alaxiaoyou.com"
---> Using cache
---> bf3f97f0bda2
Step 3/7 : RUN mkdir -p /data/httpd/
---> Running in da244cb64552
---> 743c35c3c4d8
Removing intermediate container da244cb64552
Step 4/7 : ADD gateway-service/target/alaxiaoyou-api-gateway-0.0.1-SNAPSHOT.jar /data/httpd/
---> 9e45fe4b20a4
Removing intermediate container 8e3022556068
Step 5/7 : EXPOSE 80
---> Running in e4dbaaa58778
---> 299398d4af46
Removing intermediate container e4dbaaa58778
Step 6/7 : WORKDIR /data/httpd/
---> 2e6bc4bdf39d
Removing intermediate container 8428111522be
Step 7/7 : ENTRYPOINT java -jar alaxiaoyou-api-gateway-0.0.1-SNAPSHOT.jar
---> Running in 8ce9968b7bef
---> c2f59d36052d
Removing intermediate container 8ce9968b7bef
Successfully built c2f59d36052d
Successfully tagged ms/tmp-gateway-service:latest
[root@rancher-qa alaxiaoyou-api-gateway]#
[root@rancher-qa alaxiaoyou-api-gateway]# docker images|grep gate
ms/tmp-gateway-service latest c2f59d36052d 16 seconds ago 569MB
registry.cn-hangzhou.aliyuncs.com/xmbaby-test/test-gateway-ms latest d86bef9f01e9 14 months ago 617MB
4.登錄阿里云容器鏡像服務(wù)
創(chuàng)建本地倉(cāng)庫(kù)名稱:tmp-gateway-service-ms
5.鏡像push到阿里云Docker Registry
切換到rancher/server
登錄到阿里云Docker Registry
登錄Docker Registry -> 打tag -> push到Docker Registry
[root@rancher-qa alaxiaoyou-api-gateway]# docker login --username=name@alaxiaoyou.com registry.cn-hangzhou.aliyuncs.com
Password:
Login Succeeded
[root@rancher-qa alaxiaoyou-api-gateway]# docker tag c2f59d36052d registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/tmp-gateway-service-ms
[root@rancher-qa alaxiaoyou-api-gateway]#
[root@rancher-qa alaxiaoyou-api-gateway]# docker push registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/tmp-gateway-service-ms
The push refers to a repository [registry.cn-hangzhou.aliyuncs.com/xmbaby-tmp/tmp-gateway-service-ms]
29ffcfda7ded: Pushed
202759902df6: Pushed
96f6833b8de8: Mounted from xmbaby-tmp/tmp-config-service-ms
071d8bd76517: Mounted from xmbaby-tmp/tmp-config-service-ms
latest: digest: sha256:825a0ec026c1f7c6eea01c2e5236c42dba11209b17ccd2ffad2b314c99b58b2d size: 1161
6.鏡像查看:
至此,gateway-service-ms鏡像交付完成。
用rancher編排UI工具來(lái)啟動(dòng)gateway-service:
1.進(jìn)入rancher應(yīng)用棧添加服務(wù)。因?yàn)榫W(wǎng)關(guān)gateway服務(wù)需映射80端口至宿主機(jī),因此,創(chuàng)建容器選擇“總是在每臺(tái)主機(jī)上運(yùn)行一個(gè)此容器的實(shí)例”。映射80端口。添加關(guān)聯(lián)的目標(biāo)服務(wù)。
網(wǎng)關(guān)服務(wù)gateway-service啟動(dòng)會(huì)連接關(guān)聯(lián)的目標(biāo)服務(wù)config-service,config-service是通過(guò)git管理的config配置項(xiàng)目來(lái)獲取配置信息給網(wǎng)關(guān)服務(wù)。關(guān)聯(lián)Zookeeper集群注冊(cè)服務(wù)。
2.自動(dòng)重啟選擇“從不(僅啟動(dòng)一次)”:
3.對(duì)于網(wǎng)關(guān)服務(wù),可使用docker本身來(lái)管理內(nèi)存分配,可進(jìn)行動(dòng)態(tài)調(diào)整:
4.進(jìn)入容器查看gateway-service服務(wù)啟動(dòng)。
自此,網(wǎng)關(guān)服務(wù)gateway-service添加完成
標(biāo)題名稱:Docker微服務(wù)-鏡像構(gòu)建交付和使用rancher進(jìn)行容器創(chuàng)建管理
瀏覽路徑:http://www.rwnh.cn/article2/jihooc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)公司、用戶體驗(yàn)、企業(yè)網(wǎng)站制作、營(yíng)銷型網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)