docker swarm 初体验

作者: 大鹏一怒乘风起 | 来源:发表于2020-10-15 18:57 被阅读0次

Linux环境部署:
1.初始化环境
安装docker服务
关闭防火墙,关闭selinux
2.环境搭建
a.初始化manager节点
docker swarm init --advertise-addr 10.1.1.2
b,初始化node节点,依赖上一步骤执行的命令
docker swarm join --token SWMTKN-1-1cqvxqya4jhuxuw-691w09ovwrturp2rygqioewj5 10.1.1.2:2377
3.使用操作
i.镜像要提前拉取,不会自动拉
docker pull nexus.mingyuanyun.com:5000/devops/nginx:latest

ii.创建实例
docker service create --replicas 3 --name nginx -p 8099:80 nexus.mingyuanyun.com:5000/devops/nginx:latest

image nexus.mingyuanyun.com:5000/devops/nginx:latest could not be accessed on a registry to record
its digest. Each node will access nexus.mingyuanyun.com:5000/devops/nginx:latest independently,
possibly leading to different nodes running different
versions of the image.

nmyxyyb00gstwt8prfwxrqbfk
overall progress: 3 out of 3 tasks 
1/3: running   [==================================================>] 
2/3: running   [==================================================>] 
3/3: running   [==================================================>] 
verify: Service converged 

实例创建完成之后,会分别在三个node节点上显示三个docker进程及启动三个8099端口
访问三台主机:

[root@swarm11220 ~]# curl 10.1.1.2:8099
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@swarm11220 ~]# curl 10.1.1.3:8099
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@swarm11220 ~]# curl 10.1.1.4:8099
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

iii.更新服务
使用redis镜像,先创建服务

[root@swarm11220 ~]# docker service create --replicas 1 --name redis -p 16379:6379 nexus.mingyuanyun.com:5000/devops/redis:6.0.6
image nexus.mingyuanyun.com:5000/devops/redis:6.0.6 could not be accessed on a registry to record
its digest. Each node will access nexus.mingyuanyun.com:5000/devops/redis:6.0.6 independently,
possibly leading to different nodes running different
versions of the image.

wl4rpvtdayccpzqq87yl1euxu
overall progress: 1 out of 1 tasks 
1/1: running   [==================================================>] 
verify: Service converged 
[root@swarm11220 ~]# docker ls
docker: 'ls' is not a docker command.
See 'docker --help'
[root@swarm11220 ~]# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                                            PORTS
nmyxyyb00gst        nginx               replicated          3/3                 nexus.mingyuanyun.com:5000/devops/nginx:latest   *:8099->80/tcp
wl4rpvtdaycc        redis               replicated          1/1                 nexus.mingyuanyun.com:5000/devops/redis:6.0.6    *:16379->6379/tcp
[root@swarm11220 ~]# netstat -tnlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      4108/sshd           
tcp6       0      0 :::2377                 :::*                    LISTEN      12603/dockerd       
tcp6       0      0 :::7946                 :::*                    LISTEN      12603/dockerd       
tcp6       0      0 :::22                   :::*                    LISTEN      4108/sshd           
tcp6       0      0 :::16379                :::*                    LISTEN      12603/dockerd       
tcp6       0      0 :::8099                 :::*                    LISTEN      12603/dockerd       
[root@swarm11220 ~]# telnet 10.1.1.2 6379
Trying 10.1.1.2...
telnet: connect to address 10.1.1.2: Connection refused
[root@swarm11220 ~]# telnet 10.1.1.2 16379
Trying 10.1.1.2...
Connected to 10.1.1.2.
Escape character is '^]'.
set 123 123
+OK
get 123
$3
123
^]
telnet> quit
Connection closed.
[root@swarm11220 ~]# telnet 10.1.1.3 16379
Trying 10.1.1.3...
telnet: connect to address 10.1.1.3: Connection refused
[root@swarm11220 ~]# telnet 10.1.1.3 16379
Trying 10.1.1.3...
Connected to 10.1.1.3.
Escape character is '^]'.
get 123
$3
123

更新镜像

docker service update --image nexus.mingyuanyun.com:5000/devops/redis:6.0.7 redis
[root@swarm11220 ~]# docker service update --image nexus.mingyuanyun.com:5000/devops/redis:6.0.7 redis
image nexus.mingyuanyun.com:5000/devops/redis:6.0.7 could not be accessed on a registry to record
its digest. Each node will access nexus.mingyuanyun.com:5000/devops/redis:6.0.7 independently,
possibly leading to different nodes running different
versions of the image.

redis
overall progress: 1 out of 1 tasks 
1/1: running   [==================================================>] 
verify: Service converged 
[root@swarm11220 ~]# docker service ls
ID                  NAME                MODE                REPLICAS            IMAGE                                            PORTS
nmyxyyb00gst        nginx               replicated          3/3                 nexus.mingyuanyun.com:5000/devops/nginx:latest   *:8099->80/tcp
wl4rpvtdaycc        redis               replicated          1/1                 nexus.mingyuanyun.com:5000/devops/redis:6.0.7    *:16379->6379/tcp
[root@swarm11220 ~]# telnet 10.1.1.3 16379
Trying 10.1.1.3...
Connected to 10.1.1.3.
Escape character is '^]'.
get 123
$-1
注意:持久化配置一定要挂磁盘


删除服务
docker service rm nginx

减少服务实例
docker service scale nginx=0

增加服务实例
docker service scale nginx=5

查看所有服务
docker service ls

查看服务的容器状态
docker service ps nginx

查看服务的详细信息。
docker service inspect nginx

docker volume --help
docker volume create --name testvolume
docker volume ls
docker volume inspect testvolume
第一种挂载方式:
docker service create --replicas 3 --mount type=volume,src=testvolume,dst=/zjz --name test_nginx nginx
第二种挂载方式:
命令格式:
docker service create --mount type=bind,target=/container_data/,source=/host_data/
其中,参数target表示容器里面的路径,source表示本地硬盘路径

相关文章

网友评论

    本文标题:docker swarm 初体验

    本文链接:https://www.haomeiwen.com/subject/ogmopktx.html