ubuntu14.04下docker swarm集群的搭建并验证overlay跨主机网络通信
1、安装说明
操作系统:Ubuntu 14.04 LTS
测试环境:2台虚拟机,IP分别为192.168.85.155/157
swarm manager: 192.168.85.155 swarm集群的管理机, 简称管理机
swarm node1: 192.168.85.155 swarm集群的节点机1, 简称节点1,主机名:udocker01
swarm node2: 192.168.85.157 swarm集群的节点机2, 简称节点2,主机名:udocker03
注意: 必须确保节点机的hostname不相同。
Docker Swarm工作原理
Docker Swarm使用cli.go(https://github.com/codegangsta/cli)作为CLI的框架开发Swarm CLI,目前支持4个子命令:
Create – 向Docker Hub服务发现服务器申请一个全球唯一的token标识此集群
List – 列举当前集群的所有节点
Join – 加入一个集群
Manage – 管理一个集群
2、初始化swarm集群,创建管理节点
# docker swarm init --listen-addr 192.168.85.155:2377
Swarm initialized: current node (cnyyivx55xgydeuc7brd3tsd9) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join \
--token SWMTKN-1-0e266os2zpywu0x78t05yqlle4hcy1xrr9r20boghvih6joc59-e6eaut3wc2ycrxpfyhnt957zw \
192.168.85.155:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
--listen-addr指定监听的ip与端口
查看结果
# docker node ls
3、把节点2添加到swarm集群
在udocker03上执行操作
# docker swarm join --token SWMTKN-1-0e266os2zpywu0x78t05yqlle4hcy1xrr9r20boghvih6joc59-e6eaut3wc2ycrxpfyhnt957zw 192.168.85.155:2377
注:如果不加上 --token SWMTKN-1-0e266os2zpywu0x78t05yqlle4hcy1xrr9r20boghvih6joc59-e6eaut3wc2ycrxpfyhnt957zw ,会报错:Error response from daemon: rpc error: code = 3 desc = A valid join token is necessary to join this cluster
到节点1上,查看添加结果
# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS
avnp4j9domfznqg6h4kfsgige udocker03 Ready Active
cnyyivx55xgydeuc7brd3tsd9 * udocker01 Ready Active Leader
4、创建一个overlay 跨主机网络
4.1 先查看原有网络
# docker network ls
NETWORK ID NAME DRIVER SCOPE
9ef2e25b796c bridge bridge local
dd5975e2146c deploy_default bridge local
94ecadd78224 docker_gwbridge bridge local
42f8a2c775b6 host host local
7yqnnxtm87mw ingress overlay swarm
0597476b0bcf none null local
4.2 创建一个新的overlay网络
# docker network create --driver overlay docker-nethlg
d0qvi5993lgdg4c2szp9rh4kq
# docker network ls
NETWORK ID NAME DRIVER SCOPE
9ef2e25b796c bridge bridge local
dd5975e2146c deploy_default bridge local
d0qvi5993lgd docker-nethlg overlay swarm
94ecadd78224 docker_gwbridge bridge local
42f8a2c775b6 host host local
7yqnnxtm87mw ingress overlay swarm
0597476b0bcf none null local
4.3 在新的跨主机overlay 网络(docker-nethlg)上创建应用
# docker service create --replicas 2 --name helloworld --network=docker-nethlg alpine ping docker.com
aoeoclf85dtsxo5q2hiujxvgq
4.4 查看部署结果
# docker service ls
ID NAME REPLICAS IMAGE COMMAND
aoeoclf85dts helloworld 2/2 alpine ping docker.com
# docker service ps helloworld
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR
atf4jxs9k9tewnjiisa8bxll7 helloworld.1 alpine udocker01 Running Running 13 minutes ago
2q1f6mx8l5lmk0hk42ala5d5v helloworld.2 alpine udocker03 Running Running less than a second ago
5py2lcuy6o5p944twe2rdu1h6 \_ helloworld.2 alpine udocker03 Shutdown Rejected less than a second ago "No such image: alpine:latest"
cqddi64bwyiakfr8azrzcvu0i \_ helloworld.2 alpine udocker03 Shutdown Rejected less than a second ago "No such image: alpine:latest"
8awxp109kwvj823x0rjlvqjaq \_ helloworld.2 alpine udocker03 Shutdown Rejected less than a second ago "No such image: alpine:latest"
3twgl1dydh9chfgii4xryr6s4 \_ helloworld.2 alpine udocker03 Shutdown Rejected less than a second ago "No such image: alpine:latest"
可以看到两个实例分别运行在两个节点上,helloworld.1运行在udocker01上,helloworld.2运行在udocker03上
4.5 在节点2连接容器测试
# docker exec -it helloworld.2.2q1f6mx8l5lmk0hk42ala5d5v sh
/ # ping helloworld.1.atf4jxs9k9tewnjiisa8bxll7 // 进入容器,ping helloworld.1.atf4jxs9k9tewnjiisa8bxll7 测试网络连通性
PING helloworld.1.atf4jxs9k9tewnjiisa8bxll7 (10.0.0.3): 56 data bytes
64 bytes from 10.0.0.3: seq=0 ttl=64 time=15.526 ms
64 bytes from 10.0.0.3: seq=1 ttl=64 time=0.911 ms
64 bytes from 10.0.0.3: seq=2 ttl=64 time=0.593 ms
64 bytes from 10.0.0.3: seq=3 ttl=64 time=0.907 ms
64 bytes from 10.0.0.3: seq=4 ttl=64 time=0.672 ms
^C
--- helloworld.1.atf4jxs9k9tewnjiisa8bxll7 ping statistics ---
5 packets transmitted, 5 packets received, 0% packet loss
round-trip min/avg/max = 0.593/3.721/15.526 ms
ctrl+p+q 退出容器
至此,完成对ubuntu14.04下docker swarm集群的搭建并验证overlay跨主机网络通信
评论