使用Ansible设置Kubernetes [关闭]

hgc7kmma  于 2023-10-17  发布在  Kubernetes
关注(0)|答案(2)|浏览(104)

已关闭此问题为not about programming or software development。它目前不接受回答。

这个问题似乎不是关于a specific programming problem, a software algorithm, or software tools primarily used by programmers的。如果你认为这个问题与another Stack Exchange site的主题有关,你可以留下评论,解释在哪里可以回答这个问题。
10天前关闭。
Improve this question
我正在使用Ansible设置Kubernetes设置。为了设置master,我写了一个剧本,如下所示。

- hosts: master
  become: yes
  tasks:
    - name: initialize the cluster
      shell: kubeadm init --pod-network-cidr=10.244.0.0/16 

    - name: create .kube directory
      become: yes
      become_user: ubuntu
      file:
        path: $HOME/.kube
        state: directory
        mode: 0755

    - name: copy admin.conf to user's kube config
      copy:
        src: /etc/kubernetes/admin.conf
        dest: /home/ubuntu/.kube/config
        remote_src: yes
        owner: ubuntu

    - name: install Pod network
      become: yes
      become_user: ubuntu
      shell: kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml >> pod_network_setup.txt
      args:
        chdir: $HOME
        creates: pod_network_setup.txt

问题是,当我运行playbook时,它不会等待初始化完成。命令“kubeadm init“返回并依次运行每个任务。由于初始化需要时间,一旦完成,只会创建文件“/etc/kubernetes/admin.conf”。由于Ansible不会等待它完成,因此在任务3中存在错误“/etc/kubernetes/admin.conf”未找到。
如果我运行下面的剧本,那么Ansible会等到初始化完成,即该控件挂起,直到“kubeadm init“返回。

- hosts: master
  become: yes
  tasks:
    - name: initialize the cluster
      shell: kubeadm init --pod-network-cidr=10.244.0.0/16

如何让Ansible等待命令“kubeadm init“完成,然后只启动另一个命令?

yzckvree

yzckvree1#

有三种方法:

  1. wait_for在执行kubeadm init ansible任务后的某个时间。
- hosts: master
  become: yes
  tasks:
    - name: initialize the cluster
      shell: kubeadm init --pod-network-cidr=10.244.0.0/16 
    - name: sleep for 20 seconds
      wait_for:
        timeout: 20

1.在ansible中使用until。

- hosts: master
  become: yes
  tasks:
    - name: initialize the cluster
      shell: kubeadm init --pod-network-cidr=10.244.0.0/16
      register: result
      until: result.stdout.find("Your Kubernetes master has initialized successfully!") != -1
      retries: 1
      delay: 20
  • 注意:**这里我们重试kubeadm init,直到我们在输出中得到字符串Your Kubernetes master has initialized successfully!

1.执行kubeadm init ansible任务后,检查文件/etc/kubernetes/admin.conf是否存在。

- hosts: master
  become: yes
  tasks:
    - name: initialize the cluster
      shell: kubeadm init --pod-network-cidr=10.244.0.0/16
    - name: create .kube directory
      become: yes
      become_user: ubuntu
      file:
        path: $HOME/.kube
        state: directory
        mode: 0755
    - name: Check admin.conf file exists.
      stat:
        path: /etc/kubernetes/admin.conf
      register: k8s_conf
    - name: copy admin.conf to user's kube config
      copy:
        src: /etc/kubernetes/admin.conf
        dest: /home/ubuntu/.kube/config
        remote_src: yes
        owner: ubuntu
      when: k8s_conf.stat.exists
  • 注意:*这里我们只在k8s配置文件/etc/kubernetes/admin.conf存在的情况下执行admin.conf copy。
z9smfwbn

z9smfwbn2#

当我使用命令模块而不是shell模块时,它似乎在等待,因为它在复制admin. conf时没有问题。在创建.kube/config之后,我的下一步是应用flannel overlay,这也可以工作。我的任务是这样的。

- name: Initialize the Kubernetes cluster using kubeadm
    command: kubeadm init --config /etc/kubernetes/kubeadminit.yaml

  - name: create .kube in root home
    file:
      path: /root/.kube
      state: directory

  - name: copy kubernetes admin.conf to root home dir
    copy:
      src: /etc/kubernetes/admin.conf
      dest: /root/.kube/config
      remote_src: yes

相关问题