这篇文章主要介绍了Docker Swarm如何部署,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
软件 | 版本 |
OS | Ubuntu 16.04.3 LTS |
Docker | 18.03.0-ce |
ubuntu16.04-1 | 172.31.68.241 | swarm manager |
ubuntu16.04-2 | 172.31.68.242 | swarm node |
ubuntu16.04-3 | 172.31.68.243 | swarm node |
docker swarm init --advertise-addr 172.31.68.241
docker swarm join --token SWMTKN-1-1mn8x6itne5ldwad5bmtgtdkemoim3o53dc7u2b5y5zj1lwj4l-etyttp60hpns5i4jifym7y3hd 172.31.68.241:2377
ps:注意要在两台worker上都要执行
docker node ls
docker node update --availability drain ubuntu16.04-1
ps:这样就不会在manager上分配执行任务了
docker node update --label-add app ubuntu16.04-2 docker node update --label-add app ubuntu16.04-3
docker node inspect ubuntu16.04-3
ps:要查看某一项具体的信息,可以使用如下命令模式
docker node inspect ubuntu16.04-3 --format "{{ .Status.Addr }}"
docker service create --replicas 1 -p 4000:80 --name hello 172.31.68.241/library/friendlyhello
docker service ls docker service ps hello
ps:通过service ps可以看到真实的task是运行在ubuntu16.04-2的,容器的名称叫hello.1。
docker container ls
ps:可以看到在ubuntu16.04-2上,容器的真实名称是:service名称+任务id+一串随机码。而在ubuntu16.04-3上并无相关task运行,因为replicas为1
docker service inspect hello
[ { "ID": "woxqqkoqr6viu8flojtuuht97", "Version": { "Index": 25 }, "CreatedAt": "2018-08-17T07:20:13.503484813Z", "UpdatedAt": "2018-08-17T07:20:13.515429684Z", "Spec": { "Name": "hello", "Labels": {}, "TaskTemplate": { "ContainerSpec": { "Image": "172.31.68.241/library/friendlyhello:latest@sha256:05def471119a0937508cdcb2445cb55375d15257b0a117e954bcd3527c36eac2", "StopGracePeriod": 10000000000, "DNSConfig": {}, "Isolation": "default" }, "Resources": { "Limits": {}, "Reservations": {} }, "RestartPolicy": { "Condition": "any", "Delay": 5000000000, "MaxAttempts": 0 }, "Placement": { "Platforms": [ { "Architecture": "amd64", "OS": "linux" } ] }, "ForceUpdate": 0, "Runtime": "container" }, "Mode": { "Replicated": { "Replicas": 1 } }, "UpdateConfig": { "Parallelism": 1, "FailureAction": "pause", "Monitor": 5000000000, "MaxFailureRatio": 0, "Order": "stop-first" }, "RollbackConfig": { "Parallelism": 1, "FailureAction": "pause", "Monitor": 5000000000, "MaxFailureRatio": 0, "Order": "stop-first" }, "EndpointSpec": { "Mode": "vip", "Ports": [ { "Protocol": "tcp", "TargetPort": 80, "PublishedPort": 4000, "PublishMode": "ingress" } ] } }, "Endpoint": { "Spec": { "Mode": "vip", "Ports": [ { "Protocol": "tcp", "TargetPort": 80, "PublishedPort": 4000, "PublishMode": "ingress" } ] }, "Ports": [ { "Protocol": "tcp", "TargetPort": 80, "PublishedPort": 4000, "PublishMode": "ingress" } ], "VirtualIPs": [ { "NetworkID": "zhamhv5cl7zhr7c7g6n1so6kv", "Addr": "10.255.0.5/16" } ] } } ]
curl http://172.31.68.241:4000 -I curl http://172.31.68.242:4000 -I curl http://172.31.68.243:4000 -I
ps:随便访问swarm mode集群内的任何一个节点,都可以正常访问该服务。
docker service scale hello=2
docker service ps hello
docker config create hello-config ./hello-config
docker config ls
docker config inspect hello-config
docker service create --replicas 1 --name config-test --config source=hello-config,target=/mnt/hello-config 172.31.68.241/library/friendlyhello
docker service ps config-test
docker container ls docker container exec -it b06c415e7cac /bin/bash
ps:如果在启动service的时候不指定config的source/target,则默认config在容器内的挂载目录在/hello-config
config一般在容器所在宿主机的/var/lib/docker/containers/container_id/mounts/secrets目录下面,是一个随机字符串文件
config和secret的差别就是一个是本地文件,一个是在tmpfs中存储的
#查看secretr文件 cat hello-config-secret #创建secret docker secret create hello-secret ./hello-config-secret #查看secret docker secret ls
#启动服务 docker service create --replicas 1 --label app --secret hello-secret --name secret-test 172.31.68.241/library/friendlyhello #查看task在哪台机器运行 docker service ps secret-test
docker container exec -it 47c8f6f74ae3 /bin/bash
ps:在使用secret的时候,secret配置一般在/run/secrets/目录下面,名称就是secret名称
感谢你能够认真阅读完这篇文章,希望小编分享的“Docker Swarm如何部署”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。