通过ansible一键启停k8s集群的应用

说明:

  1. 由于k8s集群kubelet等组件都是配置会自动重启,就没必要使用systemctl stop kubelet了,毕竟kubelet停止也会自动启动
  2. 由于etcd部署方式可能是容器也可能是二进制等部署方式,这里就不写停止etcd键值数据库的内容了;如果是二进制部署的etcd 可以复制一个tasks来停止etcd服务就好了
  3. 我的集群使用到了nginx服务来做负载均衡,所以剧本里面有写关于nginx服务的tasks
  4. k8s集群依次关闭 master节点以及node节点的docker或者containerd服务
  5. 使用ansible剧本前需要自行配置好集群各个主机的ssh免密信息
  6. 基本设置了多个tags标签,可指定标签选择性对不同的服务进行不同的操作
  7. 我的ansible剧本里面的启动服务部分可有可无,毕竟所有应用的服务都是设置了开机启动
  8. 执行指定的task任务

一、ansible一键启停k8s集群应用剧本

bash 复制代码
# cat k8s-apps.yaml

# 设置变量 action 的默认值,并用于全部主机调用 
- name: prepare
  hosts: all
  become: true
  gather_facts: no
  tasks:
  - name: define var with default value
    set_fact:
      action: "{{ action | default('started') }}"
      # 如果执行剧本没有指定变量 action的值,则使用默认的值: started
    tags: 
      - k8s-master
      - k8s-nginx
      - k8s-nginx
      - started
      - stopped
      - restarted
  - name: 控制台输出 action 变量的值
    debug:
      var: action

# 启动集群服务
- name: start k8s master containerd
  hosts: k8s-master
  become: true
  gather_facts: no
  tasks:
  - name: start master containerd service
    service: 
      name: containerd
      state: started
    tags: 
      - k8s-master
      - started
    when: action == "started"

- name: start k8s node containerd
  hosts: k8s-node
  become: true
  gather_facts: no
  tasks:
  - name: start node containerd service
    service: 
      name: containerd
      state: started
    tags:  
      - k8s-node
      - started
    when: action == "started"

- name: start k8s nginx
  hosts: k8s-nginx
  become: true
  gather_facts: no
  tasks:
  - name: start nginx service
    service: 
      name: nginx
      state: started
    tags: 
      - k8s-nginx
      - started
    when: action == "started"

# 停止集群服务
- name: stop k8s master containerd
  hosts: k8s-master
  become: true
  gather_facts: no
  tasks:
  - name: stop master containerd service
    service: 
      name: containerd
      state: stopped
    tags: 
      - k8s-master
      - stopped
    when: action == "stopped"

- name: stop k8s node containerd
  hosts: k8s-node
  become: true
  gather_facts: no
  tasks:
  - name: stop node containerd service
    service: 
      name: containerd
      state: stopped
    tags: 
      - k8s-node
      - stopped
    when: action == "stopped"

- name: stop k8s nginx
  hosts: k8s-nginx
  become: true
  gather_facts: no
  tasks:
  - name: stop nginx service
    service: 
      name: nginx
      state: stopped
    tags: 
      - k8s-nginx
      - stopped
    when: action == "stopped"

# 重启集群服务
- name: restart k8s master containerd
  hosts: k8s-master
  become: true
  gather_facts: no
  tasks:
  - name: restart master containerd service
    service: 
      name: containerd
      state: restarted
    tags: 
      - k8s-master
      - restarted
    when: action == "restarted"

- name: restart k8s node containerd
  hosts: k8s-node
  become: true
  gather_facts: no
  tasks:
  - name: restart node containerd service
    service: 
      name: containerd
      state: restarted
    tags:  
      - k8s-node
      - restarted
    when: action == "restarted"

- name: restart k8s nginx
  hosts: k8s-nginx
  become: true
  gather_facts: no
  tasks:
  - name: restart nginx service
    service: 
      name: nginx
      state: restarted
    tags: 
      - k8s-nginx
      - restarted
    when: action == "restarted"

二、使用指导

  1. 启动k8s集群nginx服务的使用方法
bash 复制代码
以下列出部分常用命令,关键是通过指定tags标签以及task任务来执行相应的任务动作

1、关闭集群应用使用命令,为了安全起见,设置一个条件变量action,防止错误操作导致停止应用服务
ansible-playbook k8s-apps.yaml -t stopped -e action=stopped

2、启动集群应用使用命令
ansible-playbook k8s-apps.yaml -t started -e action=started 

3、重启集群应用启动命令
ansible-playbook k8s-apps.yaml -t restarted -e action=restarted 

4、停止集群master节点的docker服务

ansible-playbook k8s-apps.yaml -t k8s-master,stopped -e action=stopped

5、停止集群node节点的docker服务

ansible-playbook k8s-apps.yaml -t k8s-node,stopped -e action=stopped

6、停止集群nginx负载均衡服务

ansible-playbook k8s-apps.yaml -t k8s-nginx,stopped -e action=stopped

7、执行指定的task剧本任务,以移动nginx服务为例

ansible-playbook k8s-apps.yaml --start-at-task="start nginx service" -e action=started

三、使用例子

1、启动nginx组件的使用例子

bash 复制代码
# 启动nginx组件服务使用例子
[root@master01 ansible]# ansible-playbook k8s-apps.yaml -t k8s-nginx,started -e action=started 
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details

PLAY [start k8s master containerd] ***************************************************************************************************************************************

TASK [start master containerd service] ***********************************************************************************************************************************
ok: [172.18.30.54]
ok: [172.18.30.55]
ok: [172.18.30.53]

PLAY [start k8s node containerd] *****************************************************************************************************************************************

TASK [start node containerd service] *************************************************************************************************************************************
ok: [172.18.30.57]
ok: [172.18.30.59]
ok: [172.18.30.56]
ok: [172.18.30.60]
ok: [172.18.30.58]

PLAY [start k8s nginx] ***************************************************************************************************************************************************

TASK [start nginx service] ***********************************************************************************************************************************************
ok: [172.18.30.51]

PLAY [stop k8s master containerd] ****************************************************************************************************************************************

PLAY [stop k8s node containerd] ******************************************************************************************************************************************

PLAY [stop k8s nginx] ****************************************************************************************************************************************************

TASK [stop nginx service] ************************************************************************************************************************************************
changed: [172.18.30.51]

PLAY [restart k8s master containerd] *************************************************************************************************************************************

PLAY [restart k8s node containerd] ***************************************************************************************************************************************

PLAY [restart k8s nginx] *************************************************************************************************************************************************

TASK [restart nginx service] *********************************************************************************************************************************************
changed: [172.18.30.51]

PLAY RECAP ***************************************************************************************************************************************************************
172.18.30.51               : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.53               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.54               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.55               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.56               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.57               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.58               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.59               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.60               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@master01 ansible]# 

2、启动集群所有服务使用例子

bash 复制代码
# 启动集群所有服务使用例子
[root@master01 ansible]# ansible-playbook k8s-apps.yaml -t started -e action=started 
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details

PLAY [start k8s master containerd] ***************************************************************************************************************************************

TASK [start master containerd service] ***********************************************************************************************************************************
ok: [172.18.30.54]
ok: [172.18.30.55]
ok: [172.18.30.53]

PLAY [start k8s node containerd] *****************************************************************************************************************************************

TASK [start node containerd service] *************************************************************************************************************************************
ok: [172.18.30.59]
ok: [172.18.30.57]
ok: [172.18.30.56]
ok: [172.18.30.60]
ok: [172.18.30.58]

PLAY [start k8s nginx] ***************************************************************************************************************************************************

TASK [start nginx service] ***********************************************************************************************************************************************
ok: [172.18.30.51]

PLAY [stop k8s master containerd] ****************************************************************************************************************************************

PLAY [stop k8s node containerd] ******************************************************************************************************************************************

PLAY [stop k8s nginx] ****************************************************************************************************************************************************

PLAY [restart k8s master containerd] *************************************************************************************************************************************

PLAY [restart k8s node containerd] ***************************************************************************************************************************************

PLAY [restart k8s nginx] *************************************************************************************************************************************************

PLAY RECAP ***************************************************************************************************************************************************************
172.18.30.51               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.53               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.54               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.55               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.56               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.57               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.58               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.59               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.60               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@master01 ansible]# 

3、启动master节点容器服务例子

bash 复制代码
# 启动master节点容器服务例子
[root@master01 ansible]# ansible-playbook k8s-apps.yaml -t k8s-master,started -e action=started 
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details

PLAY [start k8s master containerd] ***************************************************************************************************************************************

TASK [start master containerd service] ***********************************************************************************************************************************
ok: [172.18.30.54]
ok: [172.18.30.55]
ok: [172.18.30.53]

PLAY [start k8s node containerd] *****************************************************************************************************************************************

TASK [start node containerd service] *************************************************************************************************************************************
ok: [172.18.30.57]
ok: [172.18.30.59]
ok: [172.18.30.56]
ok: [172.18.30.60]
ok: [172.18.30.58]

PLAY [start k8s nginx] ***************************************************************************************************************************************************

TASK [start nginx service] ***********************************************************************************************************************************************
ok: [172.18.30.51]

PLAY [stop k8s master containerd] ****************************************************************************************************************************************

TASK [stop master containerd service] ************************************************************************************************************************************
changed: [172.18.30.54]
changed: [172.18.30.55]
changed: [172.18.30.53]

PLAY [stop k8s node containerd] ******************************************************************************************************************************************

PLAY [stop k8s nginx] ****************************************************************************************************************************************************

PLAY [restart k8s master containerd] *************************************************************************************************************************************

TASK [restart master containerd service] *********************************************************************************************************************************
changed: [172.18.30.55]
changed: [172.18.30.54]
changed: [172.18.30.53]

PLAY [restart k8s node containerd] ***************************************************************************************************************************************

PLAY [restart k8s nginx] *************************************************************************************************************************************************

PLAY RECAP ***************************************************************************************************************************************************************
172.18.30.51               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.53               : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.54               : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.55               : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.56               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.57               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.58               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.59               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.60               : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@master01 ansible]#

4、指定执行nginx启动服务的task内容

bash 复制代码
# 指定执行nginx启动服务的task内容
[root@master01 ansible]# ansible-playbook k8s-apps.yaml --start-at-task="start nginx service" -e action=started 
[WARNING]: Invalid characters were found in group names but not replaced, use -vvvv to see details

PLAY [start k8s master containerd] ***************************************************************************************************************************************

PLAY [start k8s node containerd] *****************************************************************************************************************************************

PLAY [start k8s nginx] ***************************************************************************************************************************************************

TASK [start nginx service] ***********************************************************************************************************************************************
ok: [172.18.30.51]

PLAY [stop k8s master containerd] ****************************************************************************************************************************************

TASK [stop master containerd service] ************************************************************************************************************************************
changed: [172.18.30.55]
changed: [172.18.30.54]
changed: [172.18.30.53]

PLAY [stop k8s node containerd] ******************************************************************************************************************************************

TASK [stop node containerd service] **************************************************************************************************************************************
changed: [172.18.30.59]
changed: [172.18.30.56]
changed: [172.18.30.57]
changed: [172.18.30.58]
changed: [172.18.30.60]

PLAY [stop k8s nginx] ****************************************************************************************************************************************************

TASK [stop nginx service] ************************************************************************************************************************************************
changed: [172.18.30.51]

PLAY [restart k8s master containerd] *************************************************************************************************************************************

TASK [restart master containerd service] *********************************************************************************************************************************
changed: [172.18.30.55]
changed: [172.18.30.54]
changed: [172.18.30.53]

PLAY [restart k8s node containerd] ***************************************************************************************************************************************

TASK [restart node containerd service] ***********************************************************************************************************************************
changed: [172.18.30.57]
changed: [172.18.30.59]
changed: [172.18.30.56]
changed: [172.18.30.60]
changed: [172.18.30.58]

PLAY [restart k8s nginx] *************************************************************************************************************************************************

TASK [restart nginx service] *********************************************************************************************************************************************
changed: [172.18.30.51]

PLAY RECAP ***************************************************************************************************************************************************************
172.18.30.51               : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.53               : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.54               : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.55               : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.56               : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.57               : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.58               : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.59               : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
172.18.30.60               : ok=2    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

[root@master01 ansible]# 
相关推荐
遇见你真好。12 分钟前
自定义注解进行数据脱敏
java·springboot
NMBG2216 分钟前
[JAVAEE] 面试题(四) - 多线程下使用ArrayList涉及到的线程安全问题及解决
java·开发语言·面试·java-ee·intellij-idea
像污秽一样35 分钟前
Spring MVC初探
java·spring·mvc
计算机-秋大田35 分钟前
基于微信小程序的乡村研学游平台设计与实现,LW+源码+讲解
java·spring boot·微信小程序·小程序·vue
LuckyLay38 分钟前
Spring学习笔记_36——@RequestMapping
java·spring boot·笔记·spring·mapping
醉颜凉1 小时前
【NOIP提高组】潜伏者
java·c语言·开发语言·c++·算法
狂奔solar1 小时前
分享个好玩的,在k8s上部署web版macos
前端·macos·kubernetes
阿维的博客日记1 小时前
java八股-jvm入门-程序计数器,堆,元空间,虚拟机栈,本地方法栈,类加载器,双亲委派,类加载执行过程
java·jvm
qiyi.sky1 小时前
JavaWeb——Web入门(8/9)- Tomcat:基本使用(下载与安装、目录结构介绍、启动与关闭、可能出现的问题及解决方案、总结)
java·前端·笔记·学习·tomcat
lapiii3581 小时前
图论-代码随想录刷题记录[JAVA]
java·数据结构·算法·图论