如何使用 Ansible 的 community.kubernetes.k8s_info 模块等待 StatefulSet
How to wait for StatefulSet using Ansible's community.kubernetes.k8s_info module
Ansible 有一个很棒的 community.kubernetes
模块。
k8s_info
的一个有用标志是 wait,它是为 Deployment
、DaemonSet
和 Pod
实现的。
对于其他 k8s kind
s 它将立即 return 除非提供 wait_condition
。
应该提供什么wait_condition
等待StatefulSet
?
我会使用“助手”任务文件来执行此操作。假设我有一些 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
Ansible 有一个很棒的 community.kubernetes
模块。
k8s_info
的一个有用标志是 wait,它是为 Deployment
、DaemonSet
和 Pod
实现的。
对于其他 k8s kind
s 它将立即 return 除非提供 wait_condition
。
应该提供什么wait_condition
等待StatefulSet
?
我会使用“助手”任务文件来执行此操作。假设我有一些 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