共计 17059 个字符,预计需要花费 43 分钟才能阅读完成。
先来架构,如图:
1. 前端出口服务为阿里 SLB
2.Zookeeper 集群为整个微服务环境提供注册中心,Zookeeper 集群每个 znode 的 IP 固定,也同样由 config-service 服务配置。
3. 集中配置服务 config-service,由 git 集中管理配置文件,config-service 配置映射宿主机某个目录。配置如需修改直接 Git 中修改,触发宿主机映射的目录中进行 git pull 操作,然后重启对应的 provider,配置即可生效。
4.facade 作为分发器做分发服务。
微服务工作流程:
1)provider 启动,读取写死的 config-service 配置服务,从配置服务中获取到 Zookeeper 地址,进行注册。
2)请求由阿里 SLB 分发至对应微服务 facade,网关前往 Zookeeper 集群注册中心获取可用服务。
3)获取到可用 provider 后,facade 进行分发请求,策略为轮询。
4)如后端 provider 挂了,无法提供服务,Zookeeper 会自动剔除该服务的注册。
建立微服务基础运行环境,gateway 网关、Zookeeper 注册中心、config-service 配置基础运行环境。开始构建以上基础运行环境:
一、创建 zookeeper3.4.10 集群服务
需包含了 jdk 和配置好环境变量的镜像,jdk1.8 镜像。
jdk1.8 镜像交付到阿里云 Docker Registry 仓库
Docker Rancher 宿主机
1. 下载 jdk1.8.0_25.tar 包,自行下载。
创建 jdk 构建 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. 编写 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. 构建
[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. 登录阿里云容器镜像服务
创建镜像仓库名称 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 集群创建,并将 Zookeeper3.4.10 镜像交付到阿里云 Docker Registry 仓库。
1.zookeeper3.4.10 集群构建前提准备:
zookeeper3.4.10 集群需其配置文件中指定各个选举节点的 ip 和端口,并且 zookeeper 集群中的 datadir(zoo.cfg 配置中:dataDir=/tmp/zookeeper)中,需指定节点的 myid,才能完成 Zookeeper 集群搭建。在不使用 docker link 功能前提下,容器化 Zookeeper 集群,需要解决:
1).myid 问题。
2).Zookeeper 集群节点(leader、follower)选举配置。
可使用 docker 内置的 volume 功能,将配置文件和 Zookeeper 的 datadir 映射至宿主机进行管理。
如在 rancher 中:
所以,宿主机必须存在:
1) zookeeper-config/,目录为 Zookeeper 所有配置文件,包含集群的选举节点 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/,目录对应 zookeeper 集群中的 datadir,有存放一个 myid 文件,Zookeeper 集群启动后产生的数据也会持久化到此宿主机目录下。Zookeeper 集群有 3 个 znode 节点,所以 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. 镜像构建
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
创建 zookeeper-3.4.10 构建 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 编写 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 目录添加至镜像中,然后交付。但只是一个 Zookeeper 运行的模板,具体配置可通过 volume,用宿主机的配置覆盖镜像中的配置。
2.3 构建,镜像
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 |
镜像运行,本地 docker 本身命令行启动 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 命令行启动卷映射目录正常,宿主机目录能映射到容器中。但 rancher 中用 UI 界面进行卷映射时,发现宿主机映射的目录在容器中并未映射到。待解决。
此问题是由于,rancher 有两台主机,而容器都运行在未进行设置映射目录的宿主机中。此问题困扰近一周时间,<_>
2.4 登录阿里云容器镜像服务
创建镜像仓库名称 tmp-zookeeper-ms
2.5 zookeeper 镜像 push 到阿里云 Docker Registry
切换到 rancher/server
登录到阿里云 Docker Registry
登录 Docker Registry -> 打 tag -> push 到 Docker Registry
[ | ]|
Password: | |
Login Succeeded | |
[ | ]|
[ | ]|
[ | ]|
[ | ]|
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 | |
[ | ]|
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 来做 docker 集群的管理和编排,除了使用 docker 本身命令行界面启动 Zookeeper 容器外,推荐使用 rancher UI 界面进行容器管理和创建。
在 rancher 中,添加 zk1、zk2、zk3 容器服务:
创建 zk1:
卷管理:
设置容器主机:
另两个 znode 节点,zk2、zk3 同理操作,只是路径和名称做稍微改动。
进入 Zookeeper 容器中查看启动信息,查看 Zookeeper 机器 znode2 为集群 leader:
二、创建 config-service 服务
集中配置服务 config-service,作为公司微服务基础运行环境部分,需要优先单独创建。
为做好后期集中打包,使用 maven 工具,需先创建一 maven 镜像,该 maven 镜像集成了公司自己 nexus 服务,任何开发都可通过此镜像进行快速打包本地的开发环境的 jar 包。
Docker Rancher 宿主机
1. 下载 apache-maven-3.3.9-bin.tar.gz 包,自行下载。
创建 maven 构建 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. 编写 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 环境 | |
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
cd /data/httpd/${msname} | |
mvn clean install |
3. 构建
[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 |
运行此 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
说明:- v 宿主机配置目录 /data/docker/ms-config/ 映射到容器 /data/httpd/config-service/,- e 定义 maven.sh 脚本 ${msname}变量。
4. 登录阿里云容器镜像服务
创建本地仓库名称 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 镜像交付完成。此镜像交付完成后,后期基本无需改动,直接 latest 版本。
服务 config-service 镜像开始交付:
1.1 准备集中配置项目 alaxiaoyou-config-service-0.0.1-SNAPSHOT.jar 包,gitlab 仓库中。
创建 config-service 构建 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 编写 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 |
说明:ENTRYPOINT 进入容器中执行此 jar 包。
1.3 构建,镜像
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 登录阿里云容器镜像服务
创建镜像本地仓库名称 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 镜像交付完成。
- tmp-config-service-ms 镜像启动,统一用 rancher 编排 UI 工具来启动。
tmp-config-service-ms 服务启动需依赖宿主机的目录,这个目录,是本地 git 仓库项目,gitlab 中为:git@xxxx.git。provider 配置修改,都通过 gitlab 进行修改,会进行更新到本地目录中,重启此 provider 即可生效。
2.1 启动 tmp-config-service-ms 服务,rancher 中操作。
创建服务 tmp-config-service-ms:
设置卷:
设置容器主机名:
2.2 启动 tmp-config-service-ms 容器服务。此 provider 只在启动初,会找一次 config-service,但此 config-service 服务为单点服务。不过,容器可快速销毁和生成的特性,即使此 config-service 配置服务出现问题,也可立即自动重启或手工销毁有问题的容器,并重新生成新的 config-service 服务容器,此过程为秒级,是可接受配置服务为单点。
进入 config-service 服务容器,查看启动情况,日志查看:
三、创建 gateway-service 服务
网关 gateway-service 服务是整个微服务架构的入口,在构建 provider 前,必须建立先创建好网关服务。
git clone 网关项目代码,git@xxxx.git
1. 创建网关项目目录:
mkdir -p /data/httpd/ms-api-gateway-tmp/alaxiaoyou-api-gateway
[ | ]|
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. | |
[ | ]|
[ | ]|
total 0 | |
drwxr-xr-x 4 root root 44 Jan 14 19:10 alaxiaoyou-api-gateway | |
[ | ]|
/data/httpd/ms-api-gateway-tmp | |
[ | ]|
[ | ]|
[ | ]|
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 | |
[ | ]|
[ | ]|
/data/httpd/ms-api-gateway-tmp/alaxiaoyou-api-gateway |
在 gateway-service 项目目录下,运行网关代码打包命令:
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 打出,该包从其他机器 cp 过来。待解决。
2. 创建网关项目 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 #此处虽然开启了 80 端口映射,但是并没有做 link 链接 | |
WORKDIR /data/httpd/ | |
ENTRYPOINT java -jar alaxiaoyou-api-gateway-0.0.1-SNAPSHOT.jar |
说明:ENTRYPOINT 进入容器中执行此 jar 包。
3. 构建,镜像
[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. 登录阿里云容器镜像服务
创建本地仓库名称: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 工具来���动 gateway-service:
1. 进入 rancher 应用栈添加服务。因为网关 gateway 服务需映射 80 端口至宿主机,因此,创建容器选择“总是在每台主机上运行一个此容器的实例”。映射 80 端口。添加关联的目标服务。
网关服务 gateway-service 启动会连接关联的目标服务 config-service,config-service 是通过 git 管理的 config 配置项目来获取配置信息给网关服务。关联 Zookeeper 集群注册服务。
2. 自动重启选择“从不(仅启动一次)”:
3. 对于网关服务,可使用 docker 本身来管理内存分配,可进行动态调整:
4. 进入容器查看 gateway-service 服务启动。
自此,网关服务 gateway-service 添加完成。
:
