Kubernetes - preparare noduri cu Ansible

Postat la Mon 11 May 2020 in tutoriale

O metoda rapida de pregatire a nodurilor dintr-un cluster Kubernetes este folosirea Ansible.

Nodurile sunt cu Ubuntu 18.04 LTS si au cheile de access adaugate la userul ubuntu.

Creem o lista cu hosturi: hosts:

[master]
kube-master.lxd

[worker]
kube-node1.lxd
kube-node2.lxd
kube-node3.lxd
kube-node4.lxd
kube-node5.lxd

[reg]
kube-reg.lxd

[all:vars]
ansible_user = ubuntu

Pntru configurare docker avem nevoie de fisierul files/daemon.json:

{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}

Playbook-un in sine include pasi de adaugare a repo-urilor pentru docker-ce si kubernetes precum si instalarea propriu zisa a pachetelor:

---
- hosts: all
  become: true
  tasks:
    - name: Install packages that allow apt to be used over HTTPS
      apt:
        name: "{{ packages }}"
        state: present
        update_cache: yes
        vars:
          packages:
          - apt-transport-https
          - ca-certificates
          - curl
          - gnupg-agent
          - software-properties-common

    - name: Add an apt signing key for Docker
      apt_key:
        url: https://download.docker.com/linux/ubuntu/gpg
        state: present

    - name: Add apt repository for stable version
      apt_repository:
        repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable
        state: present

    - name: Install docker and its dependecies
      apt:
        name: "{{ packages }}"
        state: present
        update_cache: yes
        vars:
          packages:
            - docker-ce
            - docker-ce-cli
            - containerd.io

    - name: Copy Docker daemon config files
      copy:
        src: files/daemon.json
        dest: /etc/docker/daemon.json
        owner: root
        group: root

    - name: Restart docker service
      service:
        name: docker
        state: restarted

    - name: Add an apt signing key for Kubernetes
      apt_key:
        url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
        state: present

    - name: Adding apt repository for Kubernetes
      apt_repository:
        repo: deb https://apt.kubernetes.io/ kubernetes-xenial main
        state: present
        filename: kubernetes.list

    - name: Install Kubernetes binaries
      apt:
        name: "{{ packages }}"
      state: present
      update_cache: yes
      vars:
        packages:
        - kubelet
        - kubeadm
        - kubectl

    - name: Configure node ip
      lineinfile:
        path: /etc/default/kubelet
        line: KUBELET_EXTRA_ARGS=--node-ip={{ hostvars[inventory_hostname]['ansible_default_ipv4']['address'] }}

    - name: Restart kubelet
      service:
        name: kubelet
        daemon_reload: yes
        state: restarted

Instalarea este simpla:

ansible-playbook -i ./hosts kube.yml --limit kube-node1.lxd

Dupa care putem trece la configurarea clusterului de Kubernetes.