使用 ansible 安装 Kubernetes 因未找到 admin.conf 而失败

使用 ansible 安装 Kubernetes 因未找到 admin.conf 而失败

Ubuntu-18.04我正在尝试使用 ansible 和以下剧本在系统上设置 Kubernetes 集群。

kube-dependencies.yml

- hosts: all
  become: yes
  remote_user: root
  gather_facts: yes
  connection: ssh
  
  tasks:
   - name: Make the Swap inactive
     command: swapoff -a 

   - name: Ensure net.bridge.bridge-nf-call-ip6tables is set to 1
     sysctl:
      name: net.bridge.bridge-nf-call-ip6tables
      value: 1
      state: present

   - name: Ensure net.bridge.bridge-nf-call-iptables is set to 1
     sysctl:
      name: net.bridge.bridge-nf-call-iptables
      value: 1
      state: present
              
   - name: Install Docker
     apt:
       name: docker.io
       state: present
       update_cache: true

   - name: Installing Prerequisites for Kubernetes
     apt: 
       name:
         - apt-transport-https
         - ca-certificates
         - curl
         - gnupg-agent
         - vim
         - build-essential
         - software-properties-common
       state: present       

   - name: Enable service docker, and enable persistently
     service: 
       name: docker
       enabled: yes
         
   - name: Add Kubernetes apt-key
     apt_key:
       url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
       state: present

   - name: Add Kubernetes' APT repository
     apt_repository:
      repo: deb http://apt.kubernetes.io/ kubernetes-xenial main
      state: present
      filename: 'kubernetes'

   - name: Install kubelet
     apt:
       name: kubelet=1.19.4-00
       state: present
       update_cache: true

   - name: Install kubeadm
     apt:
       name: kubeadm=1.19.4-00
       state: present
       
   - name: Enable service kubelet, and enable persistently
     service: 
       name: kubelet
       enabled: yes
       
- hosts: master
  become: yes
  tasks:
   - name: Install kubectl
     apt:
       name: kubectl=1.19.4-00
       state: present
       force: yes 

主目录

- hosts: master
  become: yes
  remote_user: root
  gather_facts: yes
  connection: ssh
  
  tasks:
    - name: Intilizing Kubernetes Cluster
      shell: kubeadm init --pod-network-cidr=10.244.0.0/16 >> cluster_initialized.txt
      args:
        chdir: $HOME
        creates: cluster_initialized.txt
                
    - pause: seconds=180   
        
    - name: create .kube directory
      file:
        path: $HOME/.kube
        state: directory
        mode: 0755

    - name: Copy admin.conf to user's kube config
      copy:
        src: /etc/kubernetes/admin.conf
        dest: /root/.kube/config
        remote_src: yes
        owner: root
        mode: 0644

    - name: Install Pod network Flannel
      shell: kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml >> pod_network_setup.txt
      args:
        chdir: $HOME
        creates: pod_network_setup.txt

master.yml总是失败并出现以下错误。

TASK [Copy admin.conf to user's kube config] *************************************************************************************************************
fatal: [master]: FAILED! => {"changed": false, "msg": "Source /etc/kubernetes/admin.conf not found"}

PLAY RECAP ***********************************************************************************************************************************************
master                     : ok=4    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

不确定为何admin.conf未创建文件。只有manifests文件。

~# ls /etc/kubernetes/
manifests

在主服务器上,我可以看到具有给定内容的以下文件。

# cat ~/cluster_initialized.txt
[init] Using Kubernetes version: v1.19.13
[preflight] Running pre-flight checks

看起来有些问题kubeadm init。实际上我特别想安装,Kubernetes - v1.19.4. 那么我在剧本中是否缺少任何其他配置?请建议我解决这个问题的方法。谢谢。

答案1

在你的剧本中尝试这个你需要指定版本:

kubeadm init --kubernetes-version stable-1.19.4 --pod-network-cidr=10.244.0.0/16 >> cluster_initialized.txt

相关内容