k8s系列-11-基于kubespary的集群运维
老板们,点个关注吧。
k8s集群是一成不变的吗?显然不是,随着我们系统业务的变动,相关节点肯定是要有增加和缩减的,那么我们如何给集群添加或者删除一些指定的节点呢?下面咱们来看看。
集群节点类型
[ ]
all:
hosts:
node1:
ansible_host: 192.168.112.130
ip: 192.168.112.130
access_ip: 192.168.112.130
node2:
ansible_host: 192.168.112.131
ip: 192.168.112.131
access_ip: 192.168.112.131
node3:
ansible_host: 192.168.112.132
ip: 192.168.112.132
access_ip: 192.168.112.132
Master节点
# 编辑host文件,需要在最开始安装的目录下尽心修改
[root@node1 kubespray-2.18.0]# pwd
/root/jier/kubespray-2.18.0
[root@node1 kubespray-2.18.0]# vim inventory/mycluster/hosts.yaml
# 新增节点写到下面即可,例如新增node4,就在node2下面写node4即可
kube_control_plane:
hosts:
node1:
node2:
# 执行
[root@node1 kubespray-2.18.0]# ansible-playbook -i inventory/mycluster/hosts.yaml cluster.yml -b -v
# 重启
[root@node1 kubespray-2.18.0]# crictl ps | grep -E "nginx|proxy"| awk '{print $1}' | xargs crictl restart
[root@node1 kubespray-2.18.0]# vim inventory/mycluster/hosts.yaml
# 原状态
children:
kube_control_plane:
hosts:
node1:
node2:
node3:
# 需要改成下面的样子
children:
kube_control_plane:
hosts:
node2:
node3:
node1:
# 生效
[root@node1 kubespray-2.18.0]# ansible-playbook -i inventory/mycluster/hosts.yaml -b cluster.yml
# 比如移除master节点之node1
[root@node1 kubespray-2.18.0]# ansible-playbook -i inventory/mycluster/hosts.yaml remove-node.yml -b -v -e "node=node1"
# 修改配置文件,和现有集群保持一致
[root@node1 kubespray-2.18.0]# vim inventory/mycluster/hosts.yaml
# 原配置文件
children:
kube_control_plane:
hosts:
node2:
node3:
node1:
# 需要改成
children:
kube_control_plane:
hosts:
node2:
node3:
[root@node1 kubespray-2.18.0]#
Worker节点
# 刷新缓存
[root@node1 kubespray-2.18.0]# ansible-playbook -i inventory/mycluster/hosts.yaml facts.yml -b -v
# 编辑配置文件
[root@node1 kubespray-2.18.0]# vim inventory/mycluster/hosts.yaml
# 原配置
kube_node:
hosts:
node1:
node2:
node3:
# 需要改成
kube_node:
hosts:
node1:
node2:
node3:
node4:
# 生效
[root@node1 kubespray-2.18.0]# ansible-playbook -i inventory/mycluster/hosts.yaml scale.yml --limit=node4 -b -v
# 下线worker节点,且会清理该节点上的所有容器,如果是多个节点,以逗号分割开就行
[root@node1 kubespray-2.18.0]# ansible-playbook -i inventory/mycluster/hosts.yaml remove-node.yml -b -v -e "node=node1,node2,..."
# 修改配置文件进行下线
[root@node1 kubespray-2.18.0]# vim inventory/mycluster/hosts.yaml
# 原配置
kube_node:
hosts:
node1:
node2:
node3:
# 需要改成
kube_node:
hosts:
node1:
node2:
[root@node1 kubespray-2.18.0]#
Etcd节点
# 编辑配置
[root@node1 kubespray-2.18.0]# vim inventory/mycluster/hosts.yaml
# 原配置
etcd:
hosts:
node1:
node2:
node3:
# 修改成
etcd:
hosts:
node1:
node2:
node3:
node4:
# 更新
[root@node1 kubespray-2.18.0]# ansible-playbook -i inventory/mycluster/hosts.yaml upgrade-cluster.yml --limit=etcd,kube_control_plane -e ignore_assert_errors=yes -e etcd_retries=10
# 下线
[root@node1 kubespray-2.18.0]# ansible-playbook -i inventory/mycluster/hosts.yaml remove-node.yml -b -v -e "node=NODE-NAME"
# 编辑配置
[root@node1 kubespray-2.18.0]# vim inventory/mycluster/hosts.yaml
# 重新生成配置
[root@node1 kubespray-2.18.0]# ansible-playbook -i inventory/mycluster/hosts.yaml -b cluster.yml
其他
# 清理集群
[root@node1 kubespray-2.18.0]# ansible-playbook -i inventory/mycluster/hosts.yaml -b -v reset.yml
# 当我们执行ansible-playbook的时候,如果中途有问题
# 我们需要重新执行,如果重头来,太慢了,这个时候我们可以指定从哪儿开始
# 通过--start-at-task指定从哪个task处开始执行
[root@node1 kubespray-2.18.0]# ansible-playbook --start-at-task="reset | gather mounted kubelet dirs"
至此,本文结束。
下面我们将写一下如何使用二进制的方式,部署kubernetes,使用二进制比较困难和繁琐,但是学习嘛~~~有长进。
往期推荐
添加关注,带你高效运维