集群
最后更新于:2022-04-02 03:06:13
[TOC]
> [参考网址]
## 设置 host
```
# cat /etc/hosts
127.0.0.1 localhost
192.168.0.110 swarm01
192.168.0.111 swarm02
192.168.0.229 swarm03
```
## 在管理节点配置 ssh
```
# ssh-keygen -t rsa -P ''
# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.0.111
# ssh-copy-id -i .ssh/id_rsa.pub root@192.168.0.229
```
把管理节点生成的 ssh 发送到其他节点上
## 安装 ansible
方便批量修改
```
# yum -y install ansible
# cat /etc/ansible/hosts | grep -v ^# | grep -v ^$
[node]
192.168.0.111
192.168.0.229
# sed -i "s/SELINUX=enforcing/SELINUX=disabled/" /etc/selinux/config
# ansible node -m copy -a 'src=/etc/selinux/config dest=/etc/selinux/" # 把修改好的文件发送到所有节点
# systemctl stop firewalld # 以下操作关闭防火墙, 也可根据实际需要开放端口
# systemctl disable firewalld
# ansible node -a 'systemctl stop firewalld'
# ansible node -a 'systemctl disable firewalld'
```
## 创建 swarm 集群
```
# docker swarm init --listen-addr 0.0.0.0
Swarm initialized: current node (a1tno675d14sm6bqlc512vf10) is now a manager.
To add a worker to this swarm, run the following command:
docker swarm join --token SWMTKN-1-3sp9uxzokgr252u1jauoowv74930s7f8f5tsmm5mlk5oim359e-dk52k5uul50w49gbq4j1y7zzb 192.168.139.175:2377
To add a manager to this swarm, run 'docker swarm join-token manager' and follow the instructions.
```
### 批量加入其他节点
```
ansible node -a "docker swarm join --token SWMTKN-1-5h4qmjjt6kcioxzth5n4alf4dxmdz64a4m0f0ldqpl5lzzoct3-7jq900h8mg1recydraj91qgts 192.168.0.110:2377"
```
### 查看节点
```
# docker node ls
```
## 安装 可视化 Portainer
[Portainer 容器,集群可视化管理](Portainer%E5%AE%B9%E5%99%A8,%E9%9B%86%E7%BE%A4%E5%8F%AF%E8%A7%86%E5%8C%96%E7%AE%A1%E7%90%86.md)
```
docker volume create portainer_data \
&& docker run -d -p 9000:9000 -p 8000:8000 \
--name portainer --restart always \
-v /var/run/docker.sock:/var/run/docker.sock \
-v portainer_data:/data portainer/portainer
```
访问 9000 端口
## 在Swarm中部署服务
### 创建Hello服务,查看服务信息
```
[root@master ~]# docker service create --replicas 1 --name hello busybox
[root@master ~]# docker service ls
ID NAME MODE REPLICAS IMAGE PORTS
kosznwn4ombx hello replicated 0/1 busybox:latest
```
replicas 为0 说明服务未启动,因为他没有持续运行,busybox 只是个空容器
```
docker service update --args "ping www.baidu.com" hello
````
通过update 命令就可以让服务起来
### 为服务扩容(缩融)scale
```
docker service scale hello=4
```
### 查看服务跑在哪些节点上
```
[root@localhost ~]# docker service ps -f "desired-state=running" hello
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR PORTS
pepfg0k9axvk hello.1 busybox:latest localhost Running Running 7 minutes ago
89k9sia7msej hello.2 busybox:latest swarm-master-172.18.0.1 Running Running 7 minutes ago
884jj15ta07i hello.3 busybox:latest swarm-node-123 Running Running 4 minutes ago
pb1s5lrcxjm8 hello.4 busybox:latest swarm-node-123 Running Running 4 minutes ago
# -f "desired-state=running" : 状态为runngin的服务
```
### 工作节点排除manager,manager只作为管理节点
上面中有管理节点做服务,去之
```
docker node update --availability drain swarm-master-172.18.0.1
说明
# node update 节点名 : 更改节点状态
# --availability : 三种状态
active: 正常
pause:挂起
drain:排除
```
### 滚动更新服务
```
docker service create \
--replicas 3 \
--name redis \
--update-delay 10s \
redis:3.0.6
# 启动3个副本集的redis
# update-delay 10s :每个容器依次更新,间隔10s
```
滚动升级redis
`docker service update --image redis:3.0.7 redis
`
';