# https://www.reddit.com/r/ansible/comments/sn9l9d/microk8s_join_cluster_with_ansible/ # https://canonical.com/microk8s/docs/getting-started # https://docs.ansible.com/projects/ansible/latest/collections/kubernetes/core/docsite/kubernetes_scenarios/scenario_k8s_object.html # https://www.reddit.com/r/kubernetes/comments/1cdfq13/microk8s_clustering_connection_filed_invalid/ --- - hosts: all become: true vars_files: - vars/default.yml tasks: - name: Update prerequisites apt: update_cache: true #Install dependencies - name: Install Snap packet apt: name: [ 'snap' ] state: present - name: Install microk8s for All Node become: true # needed if the other tasks are not played as root snap: name: microk8s classic: yes channel: stable # Control Node, create join key for worker joins - hosts: "controlnode" become: true tasks: # Create join key - name: Create join key # --token-ttl to avoid ERROR 500 shell: microk8s add-node --token-ttl 300 register: join_cluster run_once: true - name: Create persistent var set_fact: #join: "{{ join_cluster.stdout_lines[4] | regex_replace('10\\.0\\.2\\.15', '192.168.56.141') }}" join: "{{ join_cluster.stdout_lines[4] | regex_replace('[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+', hostvars[groups['controlnode'][0]]['ansible_ssh_host']) }}" run_once: true # DEBUG: We want to see if we have the good IP or not, if not we cannot join the control # Normally, it will use eht0 or docker bridge IP so we have to change - name: Test var debug: var: join # Tâche 2 : Provisionnement du Worker node # Worker Node, join from Control Node - hosts: "workernode" become: true tasks: # Join clustor with - name: Join cluster # Have to use microk8s-controller here instead of a group controlnode #shell: "{{ hostvars['microk8s-controller']['join'] }}" # But because of it see the IP of the machine virtual, so we have to use IP of real host assigned shell: "{{ hostvars[groups['controlnode'][0]]['join'] }} --skip-verify" - hosts: "controlnode" become: true tasks: # Tâche 3: Déploiement d'un service dans le cluster #Install dependencies - name: Install Snap packet apt: name: [ 'python3-pip' ] state: present - name: Install kubernetes pip: name: kubernetes executable: pip3 - name: Create Nginx pod kubernetes.core.k8s: kubeconfig: /var/snap/microk8s/current/credentials/client.config state: present definition: apiVersion: v1 kind: Pod metadata: name: nginx-service namespace: default spec: containers: - name: nginx image: nginx:stable-alpine-perl #Tache 3.3: Vérifiez que le pod est bien déployé. - name: Verify nginx works kubernetes.core.k8s_info: kubeconfig: /var/snap/microk8s/current/credentials/client.config kind: Pod namespace: default register: nginx_info - name: Debug pod's infomations debug: var: nginx_info.resources