这篇文章主要介绍了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 |
swarm初始化
初始化manager
docker swarm init --advertise-addr 172.31.68.241
初始化worker
docker swarm join --token SWMTKN-1-1mn8x6itne5ldwad5bmtgtdkemoim3o53dc7u2b5y5zj1lwj4l-etyttp60hpns5i4jifym7y3hd 172.31.68.241:2377
ps:注意要在两台worker上都要执行
查看node状态
docker node ls
禁止manager的worker功能
docker node update --availability drain ubuntu16.04-1
ps:这样就不会在manager上分配执行任务了
给worker增加label
docker node update --label-add app ubuntu16.04-2
docker node update --label-add app ubuntu16.04-3
查看一个node的详细信息
docker node inspect ubuntu16.04-3
ps:要查看某一项具体的信息,可以使用如下命令模式
docker node inspect ubuntu16.04-3 --format "{{ .Status.Addr }}"
创建service
创建service,一个副本
docker service create --replicas 1 -p 4000:80 --name hello 172.31.68.241/library/friendlyhello
查看service
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
查看service的详细信息
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"
}
]
}
}
]
访问service
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集群内的任何一个节点,都可以正常访问该服务。
扩展service
docker service scale hello=2
查看扩展厚的结果
docker service ps hello
swarm config
创建config
docker config create hello-config ./hello-config
查看config
docker config ls
查看详细信息
docker config inspect hello-config
在service中使用config
docker service create --replicas 1 --name config-test --config source=hello-config,target=/mnt/hello-config 172.31.68.241/library/friendlyhello
查看task的运行worker
docker service ps config-test
进入容器查看配置
docker container ls
docker container exec -it b06c415e7cac /bin/bash
ps:如果在启动service的时候不指定config的source/target,则默认config在容器内的挂载目录在/hello-config
查看config的本地目录信息
config一般在容器所在宿主机的/var/lib/docker/containers/container_id/mounts/secrets目录下面,是一个随机字符串文件
swarm secret
config和secret的差别就是一个是本地文件,一个是在tmpfs中存储的
创建secret
#查看secretr文件
cat hello-config-secret
#创建secret
docker secret create hello-secret ./hello-config-secret
#查看secret
docker secret ls
在service中使用secret
启动带secret的service
#启动服务
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
查看容器中secret信息
docker container exec -it 47c8f6f74ae3 /bin/bash
ps:在使用secret的时候,secret配置一般在/run/secrets/目录下面,名称就是secret名称
感谢你能够认真阅读完这篇文章,希望小编分享的“Docker Swarm如何部署”这篇文章对大家有帮助,同时也希望大家多多支持天达云,关注天达云行业资讯频道,更多相关知识等着你来学习!