说明:

  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集群应用剧本

# 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服务的使用方法
以下列出部分常用命令,关键是通过指定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组件的使用例子

# 启动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、启动集群所有服务使用例子

# 启动集群所有服务使用例子
[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节点容器服务例子

# 启动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内容

# 指定执行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]# 

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐