【问题标题】:How to wait for StatefulSet using Ansible's community.kubernetes.k8s_info module如何使用 Ansible 的 community.kubernetes.k8s_info 模块等待 StatefulSet
【发布时间】:2021-06-15 17:37:59
【问题描述】:
Ansible 有一个很棒的 community.kubernetes 模块。
k8s_info 的有用标志之一是 wait,它是为 Deployment、DaemonSet 和 Pod 实现的。
对于其他 k8s kinds,除非提供 wait_condition,否则它将立即返回。
应该提供什么wait_condition 等待StatefulSet?
【问题讨论】:
标签:
kubernetes
ansible
kubernetes-statefulset
【解决方案1】:
我会使用“帮助”任务文件来执行此操作。假设我有一些roles/commes/tasks/helpers/wait-for.yaml,是这样的:
- name: "Waits for {{ obj_name }} to startup"
block:
- name: "Checks latest {{ obj_name }} status"
debug:
msg: |
Object Kind {{ check_with.kind | default('nothing returned') }}
delay: "{{ wait_for | default(10) }}"
ignore_errors: True
retries: "{{ retries_for | default(10) }}"
until:
- >
check_with.status is defined
and check_with.kind is defined
and check_with.status is defined
and ((check_with.kind == 'Pod'
and (check_with.status.containerStatuses[0].ready | default(False)))
or (check_with.kind == 'DataVolume'
and (check_with.status.phase | default(False)) == 'Succeeded')
or (check_with.kind in [ 'Deployment', 'DeploymentConfig' ]
and (check_with.status.availableReplicas | default(0)) >= 1)
or (check_with.kind == 'Job'
and check_with.status.completionTime is defined
and check_with.status.succeeded is defined)
or (check_with.kind == 'PersistentVolumeClaim'
and (check_with.status.phase | default(False)) == 'Bound')
or (check_with.kind == 'StatefulSet'
and (check_with.status.readyReplicas | default(0)) >= 1))
然后,每当我需要等待 Kubernetes 资源时,我都会包含该任务文件,使用:
- include_role:
name: commons
tasks_from: helpers/wait-for.yaml
vars:
check_with: "{{ lookup('k8s', api_version='apps/v1',
kind='StatefulSet', namespace='default',
resource_name='my-statefulset') }}"
obj_name: "Statefulset default/my-statefulset"
retries_for: 30
wait_for: 10