部署docker swarm集群
阅读原文时间:2023年07月08日阅读:3

基础环境

机器名称

IP地址

安装的软件

node-1

192.168.10.190

docker-ce

node-2

192.168.10.191

docker-ce

node-3

192.168.10.192

docker-ce

系统信息

[root@huanglf ~]# cat /etc/redhat-release

CentOS Linux release 7.8.2003 (Core)

[root@huanglf ~]# uname -a

Linux huanglf 3.10.0-1127.18.2.el7.x86_64 #1 SMP Sun Jul 26 15:27:06 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

[root@huanglf ~]#

1、时间同步

echo "#time sync by fage at 2020-7-22" >>/var/spool/cron/root && echo "*/5 * * * *  /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1" >>/var/spool/cron/root && systemctl restart crond.service

2、关闭防火墙和selinux

systemctl stop firewalld && systemctl disable firewalld && setenforce 0 && sed -i s#SELINUX=enforcing#SELINUX=disable#g /etc/selinux/config

3、更改主机名

hostname node-1

echo "node-1" >/etc/hostname

hostname node-2

echo "node-2" >/etc/hostname

hostname node-3

echo "node-3" >/etc/hostname

4、更改hosts文件

cat >/etc/hosts <<EOF

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4

::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.10.190 node-1

192.168.10.191 node-2

192.168.10.192 node-3

EOF

5、分发公钥,每台都要执行

ssh-keygen -t rsa -f /root/.ssh/id_rsa -P ""

ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.10.190

ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.10.191

ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.10.192

6、安装基础软件

yum install -y lrzsz nmap tcpdump screen tree dos2unix nc iproute net-tools unzip wget vim bash-completion.noarch telnet ntp ntpdate lsof curl

7、更换国内原

wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

yum install -y yum-utils device-mapper-persistent-data lvm2

wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo

yum makecache fast

yum install -y docker-ce-19.03.12

mkdir -p /etc/docker

cat > /etc/docker/daemon.json << EOF

{

"registry-mirrors": ["https://b9pmyelo.mirror.aliyuncs.com"]

}

EOF

systemctl daemon-reload && systemctl enable docker

systemctl start docker && systemctl status docker

docker info

docker -v

#Docker version 19.03.12, build 48a66213fe

docker swarm init --advertise-addr 192.168.10.190     #执行 docker swarm init 命令的节点自动成为管理节点

docker swarm join-token manager                             #获取加入集群的token

第一个token是以woker节点进行加入,

docker swarm join --token SWMTKN-1-3vlcflfbvznbgs7jpwoyhxvxkeuvegj9a7y03z0kanx1g8x2br-7wr0xgld21j37cah9y4l78reg 192.168.10.190:2377

第二个是以manager方式进行加入

docker swarm join --token SWMTKN-1-3vlcflfbvznbgs7jpwoyhxvxkeuvegj9a7y03z0kanx1g8x2br-bz1kyjun7waedda9iusenl2e9 192.168.10.190:2377

常用指令

docker node ls                            #查看节点数量

docker node promote node-2     # 将工作节点提升为管理节点

docker node demote node-3      # 将管理节点降级为woker节点

service管理

docker service create --name web -p 80:80 nginx

Ÿ   查看service

docker service ps web

Ÿ   扩容和缩容service

docker service scale web=3

Ÿ   删除service

docker service rm web