1.Swarm模式简介
Docker 1.12 Swarm mode内置于Docker引擎中,成为docker子命令docker swarm。
请注意与旧的 Docker Swarm 区分开来。Swarm mode 内置 kv 存储功能,提供了众多的新特性,比如:具有容错能力的去中心化设计、内置服务发现、负载均衡、路由网格、动态伸缩、滚动更新、安全传输等。使得 Docker 原生的 Swarm 集群具备与 Mesos、Kubernetes 竞争的实力。
2. 基本概念
(1)节点
运行 Docker 的主机可以主动初始化一个 Swarm 集群或者加入一个已存在的 Swarm 集群,这样这个运行 Docker 的主机就成为一个 Swarm 集群的节点 (node) 。
节点分为管理 (manager) 节点和工作 (worker) 节点。
管理节点用于 Swarm 集群的管理,docker swarm 命令基本只能在管理节点执行(节点退出集群命令 docker swarm leave 可以在工作节点执行)。一个 Swarm 集群可以有多个管理节点,但只有一个管理节点可以成为 leader,leader 通过 raft 协议实现。
工作节点是任务执行节点,管理节点将服务 (service) 下发至工作节点执行。管理节点默认也作为工作节点。你也可以通过配置让服务只运行在管理节点。
(2)服务和任务
任务 (Task)是 Swarm 中的最小的调度单位,目前来说就是一个单一的容器。
服务 (Services) 是指一组任务的集合,服务定义了任务的属性。服务有两种模式:
replicated services 按照一定规则在各个工作节点上运行指定个数的任务。
global services 每个工作节点上运行一个任务
两种模式通过 docker service create 的 --mode 参数指定。
【例子】swarm manager上有一个服务(Services),是3个nginx的replicas;另外在其他三个节点上有3个任务,分别有一个运行nginx的容器。
3. 创建swarm集群
(1)创建管理节点
[root@centos1 ~]# docker-machine create -d virtualbox manager Running pre-create checks... (manager) Unable to get the latest Boot2Docker iso release version: Get : dial tcp: lookup a on 8.8.4.4:53: read udp 192.168.87.136:34158->8.8.4.4:53: i/o timeout Creating machine... (manager) Unable to get the latest Boot2Docker ISO release version: Get : EOF (manager) Copying /root/.docker/machine/cache to /root/.docker/machine/machines/manager... (manager) Creating VirtualBox VM... (manager) Creating SSH key... (manager) Starting the VM... (manager) Check network to re-create if needed... (manager) Waiting for an IP... Waiting for machine to be running, this may take a few minutes... Detecting operating system of created instance... Waiting for SSH to be available... Detecting the provisioner... Provisioning with boot2docker... Copying certs to the local machine directory... Copying certs to the remote machine... Setting Docker configuration on the remote daemon... Checking connection to Docker... Docker is up and running! To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env manager [root@centos1 ~]# docker-machine create -d virtualbox manager Running pre-create checks... (manager) Default Boot2Docker ISO is out-of-date, downloading the latest release... (manager) Latest release for gi is v18.09.6 (manager) Downloading /root/.docker/machine/cache from https://gi/releases/download/v18.09.6... (manager) 0%....10%....20%....30%....40%....50%....60%....70%....80%....90%....100% Creating machine... (manager) Copying /root/.docker/machine/cache to /root/.docker/machine/machines/manager... (manager) Creating VirtualBox VM... (manager) Creating SSH key... (manager) Starting the VM... (manager) Check network to re-create if needed... (manager) Waiting for an IP... Waiting for machine to be running, this may take a few minutes... Detecting operating system of created instance... Waiting for SSH to be available... Detecting the provisioner... Provisioning with boot2docker... Copying certs to the local machine directory... Copying certs to the remote machine... Setting Docker configuration on the remote daemon... Checking connection to Docker... Docker is up and running! To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env manager [root@centos1 ~]# docker-machine ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS manager - virtualbox Running tc v18.09.6(2)管理节点上初始化
[root@centos1 ~]# docker-machine ssh manager ( '>') /) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY. (/-_--_-\) www. docker@manager:~$ ifconfig docker0 Link encap:Ethernet HWaddr 02:42:79:C7:46:BC inet addr:172.17.0.1 Bca Mask:255.255.0.0 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:0 B) TX bytes:0 B) eth0 Link encap:Ethernet HWaddr 08:00:27:16:DC:68 inet addr:10.0.2.15 Bca Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe16:dc68/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:840 errors:0 dropped:0 overruns:0 frame:0 TX packets:560 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:136701 KiB) TX bytes:129750 KiB) eth1 Link encap:Ethernet HWaddr 08:00:27:40:20:1B inet addr:192.168.99.133 Bca Mask:255.255.255.0 inet6 addr: fe80::a00:27ff:fe40:201b/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:61 errors:0 dropped:0 overruns:0 frame:0 TX packets:39 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:11193 KiB) TX bytes:8759 KiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 B) TX bytes:0 B) docker@manager:~$ docker@manager:~$ docker@manager:~$ docker swarm init --advertise-addr 192.168.99.133 Swarm initialized: current node (70lie3wnxsdrx2ufofpscjox2) is now a manager. To add a worker to this swarm, run the following command: docker swarm join --token SWMTKN-1-23e10iciwe36dlgrle4d90df2x9n2ti428jrbhcse608k5v9ns-9h6gtihwcfpjjl6fa2vgijdnu 192.168.99.133:2377 To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.【注释】如果 Docker 主机有多个网卡,拥有多个 IP,初始化时必须使用 --advertise-addr 指定 IP。
(3)创建工作节点,并加入到集群
[root@centos1 ~]# docker-machine create -d virtualbox worker1 Running pre-create checks... Creating machine... (worker1) Copying /root/.docker/machine/cache to /root/.docker/machine/machines/worker1... (worker1) Creating VirtualBox VM... (worker1) Creating SSH key... (worker1) Starting the VM... (worker1) Check network to re-create if needed... (worker1) Waiting for an IP... Waiting for machine to be running, this may take a few minutes... Detecting operating system of created instance... Waiting for SSH to be available... Detecting the provisioner... Provisioning with boot2docker... Copying certs to the local machine directory... Copying certs to the remote machine... Setting Docker configuration on the remote daemon... Checking connection to Docker... Docker is up and running! To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env worker1 [root@centos1 image]# docker-machine ssh worker1 ( '>') /) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY. (/-_--_-\) www. docker@worker1:~$ docker swarm join --token SWMTKN-1-23e10iciwe36dlgrle4d90df2x9n2ti428jrbhcse608k5v9ns-9h6gtihwcfpjjl6fa2vgijdnu 192.168.99.133:2377 This node joined a swarm as a worker. [root@centos1 ~]# docker-machine create -d virtualbox worker2 Running pre-create checks... Creating machine... (worker2) Copying /root/.docker/machine/cache to /root/.docker/machine/machines/worker2... (worker2) Creating VirtualBox VM... (worker2) Creating SSH key... (worker2) Starting the VM... (worker2) Check network to re-create if needed... Error creating machine: Error in driver during machine creation: Error setting up host only network on machine start: /usr/bin/VBoxManage dhcpserver modify --netname HostInterfaceNetworking-vboxnet0 --ip 192.168.99.0 --netmask 255.255.255.0 --lowerip 192.168.99.100 --upperip 192.168.99.254 --enable failed: VBoxManage: error: Invalid server address VBoxManage: error: Details: code NS_ERROR_INVALID_ARG (0x80070057), component VirtualBoxWrap, interface IVirtualBox, callee nsISupports VBoxManage: error: Context: "SetConfiguration ( Bstr(pIp).mutableRaw(), Bstr(pNetmask).mutableRaw(), Bstr(pLowerIp).mutableRaw(), Bstr(pUpperIp).mutableRaw())" at line 432 of file VBoxManageDHCPServer.cpp VBoxManage: error: Failed to set configuration [root@centos1 ~]# docker-machine start worker2 Starting "worker2"... (worker2) Check network to re-create if needed... (worker2) Waiting for an IP... Machine "worker2" was started. Waiting for SSH to be available... Detecting the provisioner... Started machines may have new IP addresses. You may need to re-run the `docker-machine env` command.经过上边的两步,我们已经拥有了一个最小的 Swarm 集群,包含一个管理节点和两个工作节点。
在管理节点使用 docker node ls 查看集群。
[root@centos1 ~]# docker-machine ssh manager ( '>') /) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY. (/-_--_-\) www. docker@manager:~$ docker node ls NAME ACTIVE DRIVER STATE URL SWARM DOCKER ERRORS manager - virtualbox Running tc v18.09.6 worker1 - virtualbox Running tc v18.09.6 worker2 - virtualbox Running tc Unknown Unable to query docker version: Get : x509: certificate signed by unknown authority1.《0x80070057专题之Docker进阶---docker-machine创建Swarm集群》援引自互联网,旨在传递更多网络信息知识,仅代表作者本人观点,与本网站无关,侵删请联系页脚下方联系方式。
2.《0x80070057专题之Docker进阶---docker-machine创建Swarm集群》仅供读者参考,本网站未对该内容进行证实,对其原创性、真实性、完整性、及时性不作任何保证。
3.文章转载时请保留本站内容来源地址,https://www.lu-xu.com/yule/2059947.html