1. 宿主机环境
ubuntu 20.04 docker 20.10.8
重装集群时先删除以下服务
# 删除所有使用 dind:ssh 镜像启动的容器
sudo docker rm -f $(docker ps -f "ancestor=dind:ssh" -aq)
# 删除 dind:ssh 镜像
sudo bash docker rmi -f dind:ssh
# 删除 docker-swarm 网络
sudo docker network rm docker-swarm
2. 构建swarm环境
在宿主机创建 docker-swarm.sh 文件
#!/bin/bash
set -e
mkdir -p /app/docker/dockerfile
tee /app/docker/dockerfile/dind <<-'EOOF'
FROM docker:dind
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
&& apk update \
&& apk add --no-cache openssh tzdata \
&& cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&& sed -i "s/#PermitRootLogin.*/PermitRootLogin yes/g" /etc/ssh/sshd_config \
&& ssh-keygen -t dsa -P "" -f /etc/ssh/ssh_host_dsa_key \
&& ssh-keygen -t rsa -P "" -f /etc/ssh/ssh_host_rsa_key \
&& ssh-keygen -t ecdsa -P "" -f /etc/ssh/ssh_host_ecdsa_key \
&& ssh-keygen -t ed25519 -P "" -f /etc/ssh/ssh_host_ed25519_key \
&& echo "root:admin" | chpasswd
RUN sed -i '2 a /usr/sbin/sshd -D &' /usr/local/bin/dockerd-entrypoint.sh
RUN sed -i '3 a echo ***** sshd is run *****' /usr/local/bin/dockerd-entrypoint.sh
EOOF
docker build -f /app/docker/dockerfile/dind --rm -t dind:ssh .
ip12="192.179"
docker network create --driver=bridge --subnet=${ip12}.0.1/16 docker-swarm
ip3=0
ip4=1
manager1ip=""
start=1
end=4
for i in $(seq $start $end); do
((ip4++))
if [ "$ip4" -ge 255 ]; then
((ip3++))
ip4=1
fi
docker run --privileged --name dind-"${i}" -d \
--network docker-swarm --ip ${ip12}."${ip3}".${ip4} \
--restart=always \
-e DOCKER_TLS_CERTDIR=/certs \
-v some-docker-certs-ca:/certs/ca \
-v some-docker-certs-client:/certs/client \
-p $((2000+i)):22 \
dind:ssh
if [ "$i" == 1 ]; then
manager1ip="${ip12}.${ip3}.${ip4}"
fi
done
sleep 5
((max_manager=start+3))
for i in $(seq $start $end); do
if [ "$i" == $start ]; then
docker exec dind-"${i}" docker swarm init --advertise-addr ${manager1ip}
elif [ "$i" -lt "$max_manager" ]; then
token=$(docker exec -it dind-${start} docker swarm join-token -q manager)
docker exec dind-"${i}" docker swarm join --token "${token:0:85}" ${manager1ip}:2377
else
token=$(docker exec dind-${start} docker swarm join-token -q worker)
docker exec dind-"${i}" docker swarm join --token "${token:0:85}" ${manager1ip}:2377
fi
done
在宿主机root账户下执行下述代码
chmod +x ./docker-swarm.sh
bash -x ./docker-swarm.sh
3. 测试
使用ssh工具连上我们上述创建的容器
root@宿主机ip:2001~200x
|