# 阿里云环境下 swarm 安装配置

swarm 是 docker 官方开发维护的容器集群运行方案。相比 k8s 资源占用更低,初始要求更低。

操作演示视频 (opens new window)

# 初始化 swarm

docker swarm init

执行后得到类似下面的结果

[root@alidev ~]# docker swarm init
Swarm initialized: current node (qwl4yk7abve4exugttemn3107) is now a manager.

To add a worker to this swarm, run the following command:

    docker swarm join --token SWMTKN-1-36mjrp4luaan9svpd2vjiqgkjwprn8nj4qyff3irz9xw4hwr2p-4mgxpdq7xtlmn096cmx1nx3h4 172.26.213.112:2377

To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.

如果没有记录下来,可以用以下命令查看

docker swarm join-token worker

# 安装 docker-machine

官方源码地址https://github.com/docker/machine (opens new window)

官方文档地址https://docs.docker.com/machine/ (opens new window)

官方安装命令

curl -L https://github.com/docker/machine/releases/download/v0.16.2/docker-machine-`uname -s`-`uname -m` >/tmp/docker-machine &&
    chmod +x /tmp/docker-machine &&
    sudo cp /tmp/docker-machine /usr/local/bin/docker-machine

如果速度慢,可以试试我的镜像

base=https://github.gcslaoli.workers.dev/github.com/docker/machine/releases/download/v0.16.2 &&
  curl -L $base/docker-machine-$(uname -s)-$(uname -m) >/tmp/docker-machine &&
  sudo mv /tmp/docker-machine /usr/local/bin/docker-machine &&
  chmod +x /usr/local/bin/docker-machine

检查是否安装成功

docker-machine version

安装阿里云插件

插件源码https://github.com/AliyunContainerService/docker-machine-driver-aliyunecs (opens new window)

安装脚本

mkdir docker-machine-aliyun

cd docker-machine-aliyun

wget https://docker-machine-aliyunecs-drivers.oss-cn-beijing.aliyuncs.com/docker-machine-driver-aliyunecs_linux-amd64.tgz

tar zxvf docker-machine-driver-aliyunecs_linux-amd64.tgz

mv bin/docker-machine-driver-aliyunecs.linux-amd64 /usr/local/bin/docker-machine-driver-aliyunecs


确认安装状态

docker-machine create -d aliyunecs --help

# 配置网络

创建安全组

安全组创建完成后将管理机加入安全组

# 生成创建工作节点的脚本

swarm-aliyun-creat.sh

#!/bin/sh
set -e # 遇到错误停止
# set -x # 输出执行过程
export ECS_ACCESS_KEY_ID='id'
export ECS_ACCESS_KEY_SECRET='secret'
export ECS_REGION='cn-zhangjiakou'   # 默认值 'cn-hangzhou'
export ECS_ZONE='cn-zhangjiakou-c'
export ECS_INSTANCE_TYPE='ecs.t6-c2m1.large'
export ECS_SYSTEM_DISK_SIZE='20'
export ECS_IMAGE_ID='centos_7_9_x64_20G_alibase_20201120.vhd' # 默认值 ubuntu_16_0402_64_20G_alibase_20171227.vhd
export ECS_VPC_ID='vpc-8vbl3x738rpm67l68xqvo'
export ECS_VSWITCH_ID='vsw-8vbxjf5n39ascgoe1ky4t'
export ECS_SECURITY_GROUP='swarm-worker'
export ECS_INTERNET_MAX_BANDWIDTH='10'
export ECS_INTERNET_CHARGE_TYPE='PayByTraffic'
# Using mirrors from Aliyun
export MACHINE_DOCKER_INSTALL_URL=http://kubernetes.oss-cn-hangzhou.aliyuncs.com/docker_install.sh
export ENGINE_REGISTRY_MIRROR=https://registry.docker-cn.com
# 节点创建
echo "环境变量配置完成,开始创建节点!"
docker-machine create -d aliyunecs $1
echo "主机创建完成,当前主机清单:"
docker-machine ls
echo "将主机加入swarm集群"
docker-machine ssh $1 docker swarm join --token SWMTKN-1-36mjrp4luaan9svpd2vjiqgkjwprn8nj4qyff3irz9xw4hwr2p-4mgxpdq7xtlmn096cmx1nx3h4 172.26.213.112:2377
echo "当前swarm集群节点清单:"
docker node ls

swarm-aliyun-remove.sh

#!/bin/sh
set -e # 遇到错误停止
# set -x # 输出执行过程
echo "开始删除主机 $1"
docker-machine rm $1 -f -y
echo "主机 $1 删除完成,当前主机清单:"
docker-machine ls
echo "从swarm集群移除 $1"
docker node rm $1 -f
echo "当前swarm集群节点清单:"
docker node ls

脚本增加执行权限

chmod u+x swarm-aliyun-creat.sh
chmod u+x swarm-aliyun-remove.sh

将脚本移动到系统目录方便调用

# 使用\cp跳过覆盖确认
\cp swarm-aliyun-creat.sh /usr/local/bin/swarm-aliyun-creat.sh
\cp swarm-aliyun-remove.sh /usr/local/bin/swarm-aliyun-remove.sh
Last Updated: 2022/11/13 12:30:47