学习
实践
活动
工具
TVP
写文章

(VMware)ubuntu环境下搭建Swarm+Stack一站式部署容器集群

本文主要讲述如何在 win10 系统上,使用 vmware 安装 Ubuntu 虚拟机,搭建 Swarm+Stack 一站式部署容器集群

环境准备

版本约定

  • 试验电脑系统为 Windows 10 专业版
  • VMware 版本为 VMware Workstation 16 Pro
  • Ubuntu 版本为 ubuntu-20.04.2

软件包准备

  • 下载 VMware Workstation 16 Pro ,笔者下载的版本是:VMware-workstation-full-16.1.1-17801498.exe
  • 下载 ubuntu镜像 ,笔者下载的版本是:ubuntu-20.04.2-live-server-amd64.iso

环境设置

由于笔者电脑之前安装了Docker Desktop环境,开启了 Hyper-V 功能,因此在 VMware 中安装 Ubuntu 虚拟机时报如下的错误:

VMware Workstation 与 Device/Credential Guard 不兼容.在禁用 Device/Credenti

解决此错误可以按照此文中的方案进行设置,方可解决~

安装 VMware workstation

安装 VMware 这里就不再赘述,基本上一路 next 即可~

安装 ubuntu 虚拟机

  • 打开 VMware workstation,并创建新的虚拟机
  • 选择典型(推荐)(T)配置即可
  • 选择下载好的 Ubuntu 镜像文件
  • 设置用户名密码等信息
  • 设置虚拟机名称以及位置信息
  • 设置磁盘相关配置,默认即可,点击下一步
  • 选中创建后开启此虚拟机,点击完成
  • 安装过程中需要设置 server name、username 等信息,并选择安装 OpenSSH server;如下面两图
  • 安装成功登陆查看系统版本信息
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:~$

复制代码

  • 设置 node-1 固定 IP 为 192.168.57.131
yishao@yishao:~$ sudo vi /etc/netplan/00-installer-config.yaml

复制代码

设置 dhcp4:false、设置 addresses、gateway4、nameservers.addresses 如下:

生效配置

yishao@yishao:~$ sudo netplan apply

复制代码

克隆 ubuntu 虚拟机

  • 创建完整克隆并命名为 node-2
  • 重新生成 mac 地址
  • 完成后启动虚拟机,参照上文中设置固定 IP 的方法设置 node-2 的 ip 地址为 192.168.57.132
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:~$

复制代码

  • 按照以上几步创建 node-3,并设置 node-3 的 ip 地址为 192.168.57.133
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 192.168.57.131
  • node-2 192.168.57.132
  • node-3 192.168.57.133

安装 docker

在 node-1 中执行如下步骤安装 docker 环境

  • Uninstall old versions
yishao@yishao:~$ sudo apt-get remove docker docker-engine docker.io containerd runc

复制代码

  • Update the apt package index and install packages to allow apt to use a repository over HTTPS
yishao@yishao:~$ sudo apt-get updateyishao@yishao:~$ sudo apt-get install \>     apt-transport-https \>     ca-certificates \>     curl \>     gnupg \>     lsb-release

复制代码

  • Add Docker’s official GPG key
yishao@yishao:~$ curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

复制代码

  • Use the following command to set up the stable repository. To add the nightly or test repository, add the word 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

复制代码

  • Update the apt package index, and install the latest version of Docker Engine and containerd, or go to the next step to install a specific version
yishao@yishao:~$ sudo apt-get updateyishao@yishao:~$ sudo apt-get install docker-ce docker-ce-cli containerd.io

复制代码

  • 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

复制代码

  • Verify that Docker Engine is installed correctly by running the hello-world image
yishao@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 环境已经安装成功~

  • 添加当前用户到 docker 用户组,可以不用 sudo 运行 docker
yishao@yishao:~$ sudo groupadd dockeryishao@yishao:~$ sudo usermod -aG docker yishao

复制代码

  • Install Docker Compose

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

配置 swarm、stack 环境

  • 在 node-1 中执行如下命令,初始化 swarm
yishao@yishao:~$ docker swarm init

复制代码

  • 根据上一条命令的执行结果提示,在 node-2、node-3 上执行 join 操作,如下
yishao@yishao:~$ docker swarm join --token SWMTKN-1-0882whu3j7zzpvb0cfc3hhidrq7abz8jvbfq63qq0itbl1wclo-b626cuw5lj31yysta0593yhuk 192.168.57.131:2377

复制代码

  • 在 node-1 上查看集群信息
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 环境已经搭建成功~

部署应用

  • 在/home/yishao/nginx 目录下创建 docker-compose.yml 文件,内容如下
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

复制代码

  • 在 node-1 查看 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

复制代码

  • 在 node-2 查看 nginx 容器是否运行成功
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

复制代码

  • 在 node-3 查看 nginx 容器是否运行成功
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

https://docs.docker.com/engine/install/ubuntu/

https://docs.docker.com/compose/install/

  • 发表于:
  • 本文为 InfoQ 中文站特供稿件
  • 首发地址https://www.infoq.cn/article/50c56dde15f8d5377c45dd989
  • 如有侵权,请联系 cloudcommunity@tencent.com 删除。

扫码关注腾讯云开发者

领取腾讯云代金券