1、首先init二代swarm集群,本实验中只有两个节点(192.168.110.144和192.168.110.147),具体搭建二代swarm集
群的方法可以参考我以前的博客或者是官网https://docs.docker.com/engine/swarm/swarm-tutorial/create-swarm/
2、制作nginx镜像的Dockerfile,内容如下
FROM debian:jessie
MAINTAINER NGINX Docker Maintainers "docker-maint@nginx.com"
ENV NGINX_VERSION 1.11.6-1~jessie
RUN apt-key adv --keyserver hkp://pgp.mit.edu:80 --recv-keys 573BFD6B3D8FBC641079A6ABABF5BD827BD9BF62 \
&& echo "deb http://nginx.org/packages/mainline/debian/ jessie nginx" >> /etc/apt/sources.list \
&& apt-get update \
&& apt-get install --no-install-recommends --no-install-suggests -y \
ca-certificates \
nginx=${NGINX_VERSION} \
nginx-module-xslt \
nginx-module-geoip \
nginx-module-image-filter \
nginx-module-perl \
nginx-module-njs \
gettext-base \
&& rm -rf /var/lib/apt/lists/*
# forward request and error logs to docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
&& ln -sf /dev/stderr /var/log/nginx/error.log
COPY default.conf /etc/nginx/conf.d
EXPOSE 80 443
CMD ["nginx", "-g", "daemon off;"]
其中比较关键的是COPY default.conf /etc/nginx/conf.d
注意:因为发现如果通过docker cp将default.conf拷贝到已经启动的nginx容器中,是不能生效的,所以选择将拷贝的
动作放在Dockerfile中
default.conf的内容如下:
server {
listen 80;
server_name mynginx;
location / {
proxy_pass http://myweb:8080/Redirect/hello.html;
}
}
其中mynginx是nginx的service name(下面的步骤会看见),myweb是tomcat的service name(下面的步骤会看见)
,其实mynginx和myweb分别对应的是vip,关于vip的解释可以参看我以前转载的博客。
3.创建测试web(Redirect工程,本工程只是实验,所有只有一个hello.jsp),hello.jsp内容如下
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
Hello world
</body>
</html>
docker service create \
--name myweb \
--network net1 \
--publish 8080:8080 \
--replicas 1 \
dockertest1:5000/tomcat
其中dockertest1:5000/tomcat是本地镜像,net1是overlay网络(docker network create net1 -d overlay)
5、通过docker cp 命令将Redirect.war拷贝到tomcat容器里面。其实在本实验中--replicas=1,如果>1时候,还是参考
nginx Dockerfile的方式
6、启动nginx service,脚本内容如下:
docker service create \
--name mynginx \
--network net1 \
--publish 80:80 \
--replicas 1 \
dockertest1:5000/nginx:1.0
dockertest1:5000/nginx:1.0是通过第4步骤中的Dockerfile build的镜像,net1和第5步骤的net1是一样的
7、service都正常启动的结果
ID NAME REPLICAS IMAGE COMMAND
2yz6diw5qywg myweb 1/1 dockertest1:5000/tomcat
dyr5m86oaslt mynginx 1/1 dockertest1:5000/nginx:1.0
8、通过浏览器访问http://192.168.110.144,如果打印出Hello world,说明实验成功