Docker-学习总结(三剑客之Docker-swarm实践补充)

文章目录

  • docker-swarm实践补充
    • 服务自定义网络
    • 服务挂载自定义数据卷

docker-swarm实践补充

服务自定义网络

Overlay是在传统网络上虚拟出一个虚拟网络来,传统网络不需要在做任何适配,这样物理层网络只对应物理层的计算(物理机、虚拟化层管理网),虚拟的网络只对应虚拟计算(虚拟机的业务IP)
VXLAN(Virtual eXtensible LAN)技术是当前最为主流的Overlay标准

  1. Docker-swarm通常会在Docker自定义网络中,生成一个名称为ingress,网络驱动为overlay的网络模型,是swarm集群默认网络
[root@node1 ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
3188fafd36fc        bridge              bridge              local
53ab6074c35d        docker_gwbridge     bridge              local
6371ab551939        host                host                local
ibd2iclozugf        ingress             overlay             swarm
275cc8b2d5cb        none                null                local

  1. 自定义一个overlay的网络模型,名称为web_net
[root@node1 ~]# docker volume create --name my_volume
my_volume
[root@node1 ~]# docker network create -d overlay web_net
43buc1z36f1u72l871qjtgs88
[root@node1 ~]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
3188fafd36fc        bridge              bridge              local
53ab6074c35d        docker_gwbridge     bridge              local
6371ab551939        host                host                local
ibd2iclozugf        ingress             overlay             swarm
275cc8b2d5cb        none                null                local
43buc1z36f1u        web_net             overlay             swarm
  1. 创建服务使用自定义网络web_net
[root@node1 ~]# docker service create -p 80:80 --replicas 3 --name web_cluster --network web_net nginx
image nginx:latest could not be accessed on a registry to record
its digest. Each node will access nginx:latest independently,
possibly leading to different nodes running different
versions of the image.

ca9dke38d3vhpzyaoszig17yi
overall progress: 3 out of 3 tasks
1/3: running
2/3: running
3/3: running
verify: Service converged
  1. 查看服务使用的网络信息
[root@node1 ~]# docker service ps web_cluster
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE                    ERROR               PORTS
3mjcen0t2svm        web_cluster.1       nginx:latest        node2               Running             Running 7 minutes ago
z59dmy0n2kzb        web_cluster.2       nginx:latest        node1               Running             Running 7 minutes ago
icdhyriarvbf        web_cluster.3       nginx:latest        node3               Running             Running less than a second ago**
#查看服务中运行在本机的容器ID,ID为z59dmy0n2kzb
[root@node1 ~]# docker inspect z59dmy0n2kzb |grep -A 10 web_net
                        "Name": "web_net",
                        "Labels": {},
                        "DriverConfiguration": {
                            "Name": "overlay"
                        },
                        "IPAMOptions": {
                            "Driver": {
                                "Name": "default"
                            }
                        },
                        "Scope": "swarm"

#可以看出容器使用的是自定义网络web_net,驱动模式是overlay

服务挂载自定义数据卷

通过挂载数据卷,统一管理数据

  1. 自定义数据卷,定义数据卷前,先清除没用的数据卷
[root@node1 _data]# docker volume prune
WARNING! This will remove all local volumes not used by at least one container.
Are you sure you want to continue? [y/N] y
Total reclaimed space: 0B
[root@node1 _data]# docker volume ls
DRIVER              VOLUME NAME
[root@node1 volumes]# docker volume create --name web_data
web_data
  1. 挂载数据卷在已有服务上
[root@node1 volumes]# docker service update --mount-add type=volume,src=web_data,dst=/usr/share/nginx/html web_cluster
web_cluster
overall progress: 3 out of 3 tasks
1/3: running
2/3: running
3/3: running
verify: Service converged
#将数据卷改在到发布目录上,由于使用的是volume,可将容器目录内的内容复制在指定卷目录中
  1. 修改访问页面
[root@node1 volumes]# docker volume inspect web_data
[
    {
        "CreatedAt": "2020-02-15T13:40:36-05:00",
        "Driver": "local",
        "Labels": {},
        "Mountpoint": "/var/lib/docker/volumes/web_data/_data",
        "Name": "web_data",
        "Options": {},
        "Scope": "local"
    }
]
[root@node1 volumes]# cd /var/lib/docker/volumes/web_data/_data
[root@node1 _data]# ls
50x.html  index.html
[root@node1 _data]# echo hello  > index.html
  1. 测试结果
[root@node1 _data]# for i in {1..10};do curl 192.168.27.11;done
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
#服务使用了统一的卷,修改了内容,访问变了

你可能感兴趣的:(Linux运维-企业实战,Docker)