Mar*_*rie 8 ansible kubernetes
我有这个 ansible (working) playbook,它查看kubectl get pods -o jsonpod的输出,直到 pod 处于该Running状态。现在我想将其扩展到多个 Pod。核心问题是kubectl查询的json结果是一个列表,我知道如何访问第一项,但不是所有的项...
- name: wait for pods to come up
shell: kubectl get pods -o json
register: kubectl_get_pods
until: kubectl_get_pods.stdout|from_json|json_query('items[0].status.phase') == "Running"
retries: 20
Run Code Online (Sandbox Code Playgroud)
json 对象看起来像,
[ { ... "status": { "phase": "Running" } },
{ ... "status": { "phase": "Running" } },
...
]
Run Code Online (Sandbox Code Playgroud)
使用[0]访问处理列表中的一个对象合作的第一个项目,但我无法弄清楚如何将其扩展到多个项目。我试过[*]哪个不起作用。
Edu*_*llo 16
kubectl wait命令Kubernetes 引入了kubectl waitinv1.11版本:
kubectl wait是一个允许等待一个或多个资源被删除或达到特定条件的新命令。它添加了一个kubectl wait --for=[delete|condition=condition-name]资源/字符串命令。
kubectl wait现在支持使用除 true 以外的条件值检查--for condition=available=false
- 扩展
kubectl wait以使用更多类型的选择器。kubectl wait命令现在支持--all选择指定资源类型的命名空间中的所有资源的标志。
它不是为了等待阶段,而是等待条件。我认为等待条件比等待阶段更加自信。请参阅以下条件:
- PodScheduled:Pod 已经被调度到一个节点;
- 就绪:Pod 能够处理请求,应该被添加到所有匹配服务的负载均衡池中;
- Initialized : 所有 init 容器都已成功启动;
- ContainersReady:Pod 中的所有容器都已准备就绪。
kubectl wait与 Ansible 一起使用假设您正在使用kubeadm+ Ansible自动化 Kubernetes 安装,并且需要等待安装完成:
- name: Wait for all control-plane pods become created
shell: "kubectl get po --namespace=kube-system --selector tier=control-plane --output=jsonpath='{.items[*].metadata.name}'"
register: control_plane_pods_created
until: item in control_plane_pods_created.stdout
retries: 10
delay: 30
with_items:
- etcd
- kube-apiserver
- kube-controller-manager
- kube-scheduler
- name: Wait for control-plane pods become ready
shell: "kubectl wait --namespace=kube-system --for=condition=Ready pods --selector tier=control-plane --timeout=600s"
register: control_plane_pods_ready
- debug: var=control_plane_pods_ready.stdout_lines
Run Code Online (Sandbox Code Playgroud)
结果示例:
TASK [Wait for all control-plane pods become created] ******************************
FAILED - RETRYING: Wait all control-plane pods become created (10 retries left).
FAILED - RETRYING: Wait all control-plane pods become created (9 retries left).
FAILED - RETRYING: Wait all control-plane pods become created (8 retries left).
changed: [localhost -> localhost] => (item=etcd)
changed: [localhost -> localhost] => (item=kube-apiserver)
changed: [localhost -> localhost] => (item=kube-controller-manager)
changed: [localhost -> localhost] => (item=kube-scheduler)
TASK [Wait for control-plane pods become ready] ********************************
changed: [localhost -> localhost]
TASK [debug] *******************************************************************
ok: [localhost] => {
"control_plane_pods_ready.stdout_lines": [
"pod/etcd-localhost.localdomain condition met",
"pod/kube-apiserver-localhost.localdomain condition met",
"pod/kube-controller-manager-localhost.localdomain condition met",
"pod/kube-scheduler-localhost.localdomain condition met"
]
}
Run Code Online (Sandbox Code Playgroud)
我会尝试这样的事情(对我有用):
tasks:
- name: wait for pods to come up
shell: kubectl get pods -o json
register: kubectl_get_pods
until: kubectl_get_pods.stdout|from_json|json_query('items[*].status.phase')|unique == ["Running"]
Run Code Online (Sandbox Code Playgroud)
您基本上是获取所有 pod 的所有状态并将它们组合成一个唯一的列表,然后直到该列表是["Running"]. 因此,例如,如果您的所有 Pod 都没有运行,您将得到类似["Running", "Starting"].
| 归档时间: |
|
| 查看次数: |
7273 次 |
| 最近记录: |