本文主要讲述如何在 win10 系统上,使用 vmware 安装 Ubuntu 虚拟机,搭建 Swarm+Stack 一站式部署容器集群
由于笔者电脑之前安装了Docker Desktop环境,开启了 Hyper-V 功能,因此在 VMware 中安装 Ubuntu 虚拟机时报如下的错误:
VMware Workstation 与 Device/Credential Guard 不兼容.在禁用 Device/Credenti
解决此错误可以按照此文中的方案进行设置,方可解决~
安装 VMware 这里就不再赘述,基本上一路 next 即可~
yishao@yishao:~$ uname -aLinux yishao 5.4.0-73-generic #82-Ubuntu SMP Wed Apr 14 17:39:42 UTC 2021 x86_64 x86_64 x86_64 GNU/Linuxyishao@yishao:~$ cat /proc/versionLinux version 5.4.0-73-generic (buildd@lcy01-amd64-019) (gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)) #82-Ubuntu SMP Wed Apr 14 17:39:42 UTC 2021yishao@yishao:~$
复制代码
yishao@yishao:~$ sudo vi /etc/netplan/00-installer-config.yaml
复制代码
设置 dhcp4:false、设置 addresses、gateway4、nameservers.addresses 如下:
生效配置
yishao@yishao:~$ sudo netplan apply
复制代码
yishao@yishao:~$ cat /etc/netplan/00-installer-config.yaml# This is the network config written by 'subiquity'network: ethernets: ens33: dhcp4: false addresses: [192.168.57.132/24] gateway4: 192.168.57.2 nameservers: addresses: [192.168.57.2,114.114.114.114,8.8.8.8] version: 2yishao@yishao:~$
复制代码
yishao@yishao:~$ cat /etc/netplan/00-installer-config.yaml# This is the network config written by 'subiquity'network: ethernets: ens33: dhcp4: false addresses: [192.168.57.133/24] gateway4: 192.168.57.2 nameservers: addresses: [192.168.57.2,114.114.114.114,8.8.8.8] version: 2yishao@yishao:~$
复制代码
相关虚拟机信息如下:
在 node-1 中执行如下步骤安装 docker 环境
yishao@yishao:~$ sudo apt-get remove docker docker-engine docker.io containerd runc
复制代码
apt
package index and install packages to allow apt
to use a repository over HTTPSyishao@yishao:~$ sudo apt-get updateyishao@yishao:~$ sudo apt-get install \> apt-transport-https \> ca-certificates \> curl \> gnupg \> lsb-release
复制代码
yishao@yishao:~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
复制代码
nightly
or test
(or both) after the word stable
in the commands below. Learn about nightly and test channels.yishao@yishao:~$ echo \> "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu \> $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
复制代码
apt
package index, and install the latest version of Docker Engine and containerd, or go to the next step to install a specific versionyishao@yishao:~$ sudo apt-get updateyishao@yishao:~$ sudo apt-get install docker-ce docker-ce-cli containerd.io
复制代码
yishao@yishao:~$ sudo apt-get install docker-ce docker-ce-cli containerd.io
复制代码
sudo mkdir -p /etc/dockersudo tee /etc/docker/daemon.json <<-'EOF'{ "registry-mirrors": ["https://9w2ypf5d.mirror.aliyuncs.com"]}EOFsudo systemctl daemon-reloadsudo systemctl restart docker
复制代码
hello-world
imageyishao@yishao:~$ sudo docker run hello-world
Hello from Docker!This message shows that your installation appears to be working correctly.
To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.
To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bash
Share images, automate workflows, and more with a free Docker ID: https://hub.docker.com/
For more examples and ideas, visit: https://docs.docker.com/get-started/
复制代码
看到如上输出,表示 docker 环境已经安装成功~
yishao@yishao:~$ sudo groupadd dockeryishao@yishao:~$ sudo usermod -aG docker yishao
复制代码
Run this command to download the current stable release of Docker Compose:
yishao@yishao:~$ sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
复制代码
Apply executable permissions to the binary:
yishao@yishao:~$ sudo chmod +x /usr/local/bin/docker-compose
复制代码
Note: If the command
docker-compose
fails after installation, check your path. You can also create a symbolic link to/usr/bin
or any other directory in your path.
yishao@yishao:~$ sudo ln -s /usr/local/bin/docker-compose /usr/bin/docker-compose
复制代码
Test the installation:
yishao@yishao:~$ docker-compose --versiondocker-compose version 1.29.2, build 5becea4c
复制代码
docker compose 安装成功
以上步骤分别在 node-2、node-3 执行,安装 docker-ce docker-ce-cli containerd.io docker-compose
yishao@yishao:~$ docker swarm init
复制代码
yishao@yishao:~$ docker swarm join --token SWMTKN-1-0882whu3j7zzpvb0cfc3hhidrq7abz8jvbfq63qq0itbl1wclo-b626cuw5lj31yysta0593yhuk 192.168.57.131:2377
复制代码
yishao@yishao:~$ docker node lsID HOSTNAME STATUS AVAILABILITY MANAGER STATUS ENGINE VERSION6gln03vr6814qbs0izau27csw * yishao Ready Active Leader 20.10.6sjwlf1zh3ict9t415m5nqhpy6 yishao Ready Active 20.10.6wawizhmt4s5bk2yxqo5e36rzz yishao Ready Active 20.10.6yishao@yishao:~$
复制代码
至此 swarm、stack 环境已经搭建成功~
version: '3.1'services: nginx: image: nginx ports: - 8888:80 deploy: mode: replicated replicas: 3
复制代码
yishao@yishao:~$ docker stack deploy -c docker-compose.yml nginx
复制代码
yishao@yishao:~/nginx$ docker stack lsNAME SERVICES ORCHESTRATORnginx 1 Swarmyishao@yishao:~/nginx$ docker stack ps nginxID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTSb3wxkumchs6y nginx_nginx.1 nginx:latest yishao Running Running 22 seconds ago32ehesrcab9m nginx_nginx.2 nginx:latest yishao Running Running 23 seconds agom1lle3eyllpp nginx_nginx.3 nginx:latest yishao Running Running 22 seconds agoyishao@yishao:~/nginx$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESfca198136a37 nginx:latest "/docker-entrypoint.…" 5 minutes ago Up 5 minutes 80/tcp nginx_nginx.2.32ehesrcab9m3d4uo32tod3df
复制代码
yishao@yishao:~$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESf1731191374c nginx:latest "/docker-entrypoint.…" 4 minutes ago Up 3 minutes 80/tcp nginx_nginx.3.m1lle3eyllpp9gm06tou07yqk
复制代码
yishao@yishao:~$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESa94479191d20 nginx:latest "/docker-entrypoint.…" 4 minutes ago Up 4 minutes 80/tcp nginx_nginx.1.b3wxkumchs6y4u1qy0k9kcsfr
复制代码
至此最简单的 Swarm+Stack 一站式部署容器集群搭建成功
参考
https://www.cnblogs.com/oneWhite/p/12522050.html
领取专属 10元无门槛券
私享最新 技术干货