docker swarm 管理服务

docker swarm中服务的概念是:一个服务包含swarm集群中多个节点,每个节点都可以部署服务,每个服务也包含多个相关的容器。

docker service --help
Commands:
  create      Create a new service
  inspect     Display detailed information on one or more services
  ls          List services
  ps          List the tasks of a service
  rm          Remove one or more services
  scale       Scale one or multiple replicated services
  update      Update a service

在主节点上创建一个服务,副本数是2个,防止服务在运行结束后退出,增加命令 ping "www.baidu.com"

[root@worker3 ~]# docker service create --name busy-box --replicas 2 busybox ping "www.baidu.com"
dnmw2wdgs4xbj6bw3ma4d5jam

查看节点的所有服务,

[root@worker3 ~]# docker service ls
ID            NAME      MODE        REPLICAS  IMAGE
3z658klw8dbd  my-web    replicated  2/2       nginx:latest
dnmw2wdgs4xb  busy-box  replicated  2/2       busybox:latest
[root@worker3 ~]# docker service create --replicas 3 --name redis  --update-delay 10s redis:3.0.6
383zekusyout2r639vmfx3ejy
[root@worker3 ~]# docker service ps redis
ID            NAME     IMAGE        NODE     DESIRED STATE  CURRENT STATE            ERROR  PORTS
kta1fy9wcdzf  redis.1  redis:3.0.6  master   Running        Preparing 8 seconds ago         
bu3xvvq6jghu  redis.2  redis:3.0.6  master   Running        Preparing 8 seconds ago         
j7b7c0d86ey0  redis.3  redis:3.0.6  worker3  Running        Running 8 seconds ago           

 

查看服务的具体的信息

[root@worker3 ~]# docker service ps busy-box
ID            NAME        IMAGE           NODE     DESIRED STATE  CURRENT STATE          ERROR  PORTS
mrv2xv10g1t5  busy-box.1  busybox:latest  worker3  Running        Running 3 minutes ago         
v26re4k304c2  busy-box.2  busybox:latest  master   Running        Running 3 minutes ago    

docker swarm 删除一个服务

[root@worker3 ~]# docker service rm my-web
my-web

docker swarm 服务更新,以一个常见的镜像更新为例子

[root@worker3 ~]# docker service create --replicas 3 --name redis  --update-delay 10s redis:3.0.6
383zekusyout2r639vmfx3ejy
[root@worker3 ~]# docker service ps redis
ID            NAME     IMAGE        NODE     DESIRED STATE  CURRENT STATE            ERROR  PORTS
kta1fy9wcdzf  redis.1  redis:3.0.6  master   Running        Preparing 8 seconds ago         
bu3xvvq6jghu  redis.2  redis:3.0.6  master   Running        Preparing 8 seconds ago         
j7b7c0d86ey0  redis.3  redis:3.0.6  worker3  Running        Running 8 seconds ago           

docker swarm update 更新服务的镜像

[root@worker3 ~]# docker service update --image redis:latest redis
[root@worker3 ~]# docker service ps  --filter "desired-state =running" redis
ID            NAME     IMAGE         NODE     DESIRED STATE  CURRENT STATE          ERROR  PORTS
nk7tktytm7kk  redis.1  redis:latest  master   Running        Running 4 minutes ago         
uobcdvbzq4kk  redis.2  redis:latest  worker3  Running        Running 4 minutes ago         
cdozh8bw6m8p  redis.3  redis:latest  worker3  Running        Running 4 minutes ago         

 

你可能感兴趣的:(云计算/大数据,Docker)