###################################### # Tasks for init k8s cluster # ###################################### --- - name: Get hostname command: hostname register: old_hostname changed_when: false - set_fact: hostname={{ old_hostname.stdout | lower }} - name: Pull k8s images command: kubeadm config images pull --kubernetes-version=v{{ kubernetesVersion }} - name: copy clusterConfig to remote location template: src: '../templates/k8s_cluster/cluster/clusterConfiguration.yml.j2' dest: /tmp/clusterConfiguration.yml - name: Initialize the Kubernetes cluster using kubeadm command: argv: - kubeadm - init - --config=/tmp/clusterConfiguration.yml - --node-name={{ hostname }} - --ignore-preflight-errors - Swap - --upload-certs - name: Remove clusterConfig on remote location ansible.builtin.file: path: /tmp/clusterConfiguration.yml state: absent - name: Setup kubeconfig for local usage command: "{{ item }}" loop: - mkdir -p ~/.kube - cp -i /etc/kubernetes/admin.conf ~/.kube/config - name: Wait for all k8s nodes to be ready shell: kubectl wait --for=condition=Ready nodes --all --timeout=600s register: nodes_ready - name: create Calico NetworkManager directory file: path: '/etc/NetworkManager/conf.d/' state: directory mode: 0755 - name: Configure Calico NetworkManager template: src: ../templates/k8s_cluster/cluster/calico.conf.j2 dest: /etc/NetworkManager/conf.d/calico.conf owner: root mode: '0644' - name: Install calico pod network kubernetes.core.k8s: state: present definition: "{{ lookup('template', '../templates/k8s_cluster/cluster/calico.yml.j2') | from_yaml_all }}" - name: Wait for calico daemonset become ready command: "kubectl rollout status daemonset calico-node -n kube-system --timeout 60s" - name: Generate join command command: kubeadm token create --print-join-command register: join_command