简体   繁体   English

ansible kubectl 等待节点准备就绪

[英]ansible kubectl wait for nodes to be READY

Is there any existing ansible module I can use for the following.是否有任何现有的 ansible 模块可以用于以下目的。 I can wait for kubectl get nodes STATUS = Ready ?我可以等待kubectl get nodes STATUS = Ready吗?

$ kubectl get nodes
NAME      STATUS     ROLES     AGE       VERSION
master1   NotReady   master    42s       v1.8.4

I didn't know any existing module for this.我不知道任何现有的模块。 you can do something like this.你可以做这样的事情。

---
- hosts: localhost
  gather_facts: no
  tasks:
  - name: Wait for nodes to be ready
    shell: "/usr/bin/kubectl get nodes"
    register: nodes
    until:      
      - '" Ready "  in nodes.stdout'      
    retries: 6
    delay: 2

The kubectl wait command kubectl wait命令

Kubernetes supports the use of kubectl wait from version v1.11. Kubernetes 从 v1.11 版本开始支持使用kubectl wait
It waits for a specific condition on one or many resources.它等待一个或多个资源的特定条件。

Using the kubectl wait command with ansible tasks:对 ansible 任务使用 kubectl wait 命令:

  - name: Wait for all k8s nodes to be ready
    shell: kubectl wait --for=condition=Ready nodes --all --timeout=600s
    register: nodes_ready

  - debug: var=nodes_ready.stdout_lines

If you want to check the condition for some particular nodes only, you can use a --selector instead of --all like this:如果只想检查某些特定节点的条件,可以使用--selector而不是--all如下所示:

  - name: Wait for k8s nodes with node label 'purpose=test' to be ready
    shell: kubectl wait --for=condition=Ready nodes --selector purpose=test --timeout=600s
    register: nodes_ready

  - debug: var=nodes_ready.stdout_lines

If you want to check a specific node to be ready you can do this.如果要检查特定节点是否准备就绪,可以执行此操作。 In this case it will check if the current node is ready.在这种情况下,它将检查当前节点是否准备就绪。

- name: Wait for node to be ready
  become: yes
  ansible.builtin.shell: $([[ $(kubectl get node $(cat /etc/hostname) -o jsonpath='{.status.conditions[?(@.type=="Ready")].status}') == "True" ]])
  args:
    executable: /bin/bash
  register: kubelet_ready
  until: kubelet_ready.rc == 0
  retries: 60
  delay: 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM