kubernetes ansible验证k8s部署是否成功

ilmyapht  于 2023-01-12  发布在  Kubernetes
关注(0)|答案(2)|浏览(281)

我正在使用Ansible K8s模块来做Pod部署,但不幸的是,我没有找到正确使用waitwait_condition的方法。
所以我尝试使用untilretry来实现另一个等待
每次运行行动手册后,在使用k8s模块进行部署后,直到NewReplicaSetAvailable在2分钟(60 * 2秒)内显示在部署中,否则部署失败。
如果您需要验证部署结果,我在这里写下来。

- name: Verify the deploy result
      shell: "kubectl -n kube-system get deployment coredns --output=jsonpath='{.status.conditions[*].reason}'"
      register: deploy_res
      until: "'NewReplicaSetAvailable' in deploy_res.stdout"
      retries: 60
      delay: 2
fhity93d

fhity93d1#

模块k8s不提供部署的状态。对于部署(或任何其他资源)的状态,您应该使用模块k8s_info。然后您可以检查此任务。
示例:

---
- hosts: localhost
  tasks:
  - name: Create a Service object from an inline definition
    k8s:
      state: present
      definition:
        apiVersion: v1
        kind: Deployment
        metadata:
          name: example
          namespace: default
        spec:
          replicas: 3
          selector:
            matchLabels:
              app: example-nginx
          template:
            metadata:
              labels:
                app: example-nginx
            spec:
              containers:
              - image: nginx
                name: nginx
  - name: check if deployment is ready
    k8s_info:
      kind: Deployment
      label_selectors: 
        - app = example-nginx
    register: output_info
    until: output_info.resources | json_query('[*].status.conditions[?reason==`NewReplicaSetAvailable`][].status') | select ('match','True') | list | length == 1
    delay: 2
    retries: 5

对于until过滤器,当然有更好的解决方案,但我已经用这种方法解决了类似的用例。until过滤器检查是否存在原因为NewReplicaSetAvailable的条件,并且它必须是True。如果发生这种情况,长度等于1

snz8szmq

snz8szmq2#

可以像副本集的数量相等或不相等一样进行确认,在所有pod上更新图像。

- name: Add a deployment
      k8s:
        definition:
          apiVersion: apps/v1
          kind: Deployment
          metadata:
            name: wait-deploy
            namespace: "{{ wait_namespace }}"
          spec:
            replicas: 3
            selector:
              matchLabels:
                app: "{{ k8s_pod_name }}"
            template: "{{ k8s_pod_template }}"
        wait: yes
        wait_timeout: "{{ k8s_wait_timeout | default(omit) }}"
      vars:
        k8s_pod_name: wait-deploy
        k8s_pod_image: gcr.io/kuar-demo/kuard-amd64:1
        k8s_pod_ports:
          - containerPort: 8080
            name: http
            protocol: TCP

      register: deploy

    - name: Check that deployment wait worked
      assert:
        that:
          - deploy.result.status.availableReplicas == deploy.result.status.replicas

    - name: Update a deployment
      k8s:
        definition:
          apiVersion: apps/v1
          kind: Deployment
          metadata:
            name: wait-deploy
            namespace: "{{ wait_namespace }}"
          spec:
            replicas: 3
            selector:
              matchLabels:
                app: "{{ k8s_pod_name }}"
            template: "{{ k8s_pod_template }}"
        wait: yes
        wait_timeout: "{{ k8s_wait_timeout | default(omit) }}"
      vars:
        k8s_pod_name: wait-deploy
        k8s_pod_image: gcr.io/kuar-demo/kuard-amd64:2
        k8s_pod_ports:
          - containerPort: 8080
            name: http
            protocol: TCP
      register: update_deploy

    # It looks like the Deployment is updated to have the desired state *before* the pods are terminated
    # Wait a couple of seconds to allow the old pods to at least get to Terminating state
    - name: Avoid race condition
      pause:
        seconds: 2

    - name: Get updated pods
      k8s_info:
        api_version: v1
        kind: Pod
        namespace: "{{ wait_namespace }}"
        label_selectors:
          - app=wait-deploy
        field_selectors:
          - status.phase=Running
      register: updated_deploy_pods
      until: updated_deploy_pods.resources[0].spec.containers[0].image.endswith(':2')
      retries: 6
      delay: 5

    - name: Check that deployment wait worked
      assert:
        that:
          - deploy.result.status.availableReplicas == deploy.result.status.replicas

可在kubernetes Ansible repo k8s_waiter上找到提及的解决方案

相关问题