文章目录

docker swarm

1.初始化:
root@k8s-master1:~# docker swarm init --advertise-addr 172.31.7.101 #初始化manager

root@k8s-master1:~# docker swarm join-token  worker #获取token

root@k8s-master1:~# docker node ls #列出swarm集群的节点

2.添加节点
root@k8s-master2:~# docker swarm join --token SWMTKN-1-5vmi21qrc9thymrs9lxsoaebt4j5o01ma8bhfbhasrxwkzo8ch-6zfq0l4mqjzbzt8m0vfm1zjur 172.31.7.101:2377
root@k8s-master3:~# docker swarm join --token SWMTKN-1-5vmi21qrc9thymrs9lxsoaebt4j5o01ma8bhfbhasrxwkzo8ch-6zfq0l4mqjzbzt8m0vfm1zjur 172.31.7.101:2377

3.添加label
root@k8s-master1:~# docker node update --label-add name=k8s-master1 k8s-master1.magedu.local.priview
k8s-master1.magedu.local.priview
root@k8s-master1:~# docker node update --label-add name=k8s-master2 k8s-master2.magedu.local.priview
k8s-master2.magedu.local.priview
root@k8s-master1:~# docker node update --label-add name=k8s-master3 k8s-master3.magedu.local.priview
k8s-master3.magedu.local.priview

4.将其他节点提升为manager角色以实现高可用:
root@k8s-master1:~# docker node promote k8s-master2.magedu.local.priview
Node k8s-master2.magedu.local.priview promoted to a manager in the swarm.
root@k8s-master1:~# docker node promote k8s-master3.magedu.local.priview
Node k8s-master3.magedu.local.priview promoted to a manager in the swarm.
root@k8s-master1:~#
root@k8s-master1:~# docker node ls
ID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION
nikz1m7wn4go2k5fkr86lhole * k8s-master1.magedu.local.priview Ready Active Leader 19.03.14
xnloavs2yruuhpx060tmlf6w1 k8s-master2.magedu.local.priview Ready Active Reachable 19.03.14
e0t0mbjagphaqe87okvj11lyd k8s-master3.magedu.local.priview Ready Active Reachable 19.03.14

5.查看node信息:
root@k8s-master1:~# docker node inspect k8s-master2.magedu.local.priview

6.创建网络:
root@k8s-master1:~# docker network --help
root@k8s-master1:~# docker network create -d overlay --subnet=10.220.0.0/21 --gateway=10.220.0.1 --attachable jie-net
tzm6el34dt3k1juwoti618u9x
root@k8s-master1:~# docker network inspect jie-net #验证网络信息

7.创建容器测试
root@k8s-master1:/opt/docker-compose-test# docker service create --replicas 2 -p 8888:80 --network jie-net --name nginx nginx:1.18-alpine
vw6fqt3jwtkazv5n8p76ivz9b
overall progress: 2 out of 2 tasks
1/2: running [>]
verify: Service converged

root@k8s-master1:/opt/docker-compose-test# docker service  ls #验证service
    ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
    vw6fqt3jwtka        nginx               replicated          2/2                 nginx:1.18-alpine   *:8888->80/tcp

root@k8s-master2:~# ss -tnl #验证端口监听,每个服务器都会监听service端口8888
    State                Recv-Q                Send-Q                                Local Address:Port                                 Peer Address:Port                
    LISTEN               0                     128                                   127.0.0.53%lo:53                                        0.0.0.0:*                   
    LISTEN               0                     128                                         0.0.0.0:22                                        0.0.0.0:*                   
    LISTEN               0                     128                                       127.0.0.1:6010                                      0.0.0.0:*                   
    LISTEN               0                     128                                               *:2377                                            *:*                   
    LISTEN               0                     128                                               *:7946                                            *:*                   
    LISTEN               0                     128                                            [::]:22                                           [::]:*                   
    LISTEN               0                     128                                               *:8888                                            *:*                   
    LISTEN               0                     128                                           [::1]:6010                                         [::]:* 

8.访问测试:

root@k8s-master2:~# curl  http://172.31.7.102:8888

9.验证service
root@k8s-master3:~# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
vw6fqt3jwtka nginx replicated 2/2 nginx:1.18-alpine *:8888->80/tcp

root@k8s-master3:~# docker service ps vw6fqt3jwtka
ID                  NAME                IMAGE               NODE                               DESIRED STATE       CURRENT STATE           ERROR               PORTS
bppmk2mak974        nginx.1             nginx:1.18-alpine   k8s-master1.magedu.local.priview   Running             Running 5 minutes ago                       
9qbddx756jzc        nginx.2             nginx:1.18-alpine   k8s-master3.magedu.local.priview   Running             Running 5 minutes ago

10.验证高可用:

参考资料马哥

12-18 10:15