Deploying Docker and Kubernetes on a Raspberry Pi cluster using Ansible


I would like to thank Alex Ellis for his wonderful work in the container world.
From the moment I first read his blog, I wanted to build a RPi cluster and test stuff :)

And what you are going to find in that article is no more no less than his work that I "translated" in a playbook.
Why Ansible ? Simply because it is probably the tool that has impressed me most these past years by its simplicity, efficiency and power.
Nothing new, nothing fancy : just a playbook I wrote for me and I thought could be interesting to others.

For the playbook to run smoothly, I'm assuming you already have a configured RPi cluster and Ansible set up to interact with it. If not, you still can check my previous article on the topic.
If you are just willing to install Ansible, go to the dedicated section.

The playbook

This playbook, along with some others to come (I'm really eager to play with Kafka and Pulsar) is available on my Github.

It's here for you to have a quick overview, but if some changes were to occur, I don't think I will reflect them here. You've been warned.

- name: Install Docker and K8s
  hosts: pi-cluster
  remote_user: pi

  - block:
      - name: Add encryption key for the Docker and K8s repository
          url: "{{ item }}"
          state: present
          - https://download.docker.com/linux/raspbian/gpg
          - https://packages.cloud.google.com/apt/doc/apt-key.gpg
      - name: Clean Docker and K8s repository files to be idempotent
          name: "{{ item }}"
          state: absent
          - /etc/apt/sources.list.d/docker.list
          - /etc/apt/sources.list.d/kubernetes.list
      - name: Recreate Docker and K8s repository files
          name: "{{ item }}"
          state: touch
          - /etc/apt/sources.list.d/docker.list
          - /etc/apt/sources.list.d/kubernetes.list
      - name: Add Docker and K8s repository to the list of repositories
          path: /etc/apt/sources.list.d/{{ item.category }}.list
          line: "{{ item.url }}"
          - { url: 'deb [arch=armhf] https://download.docker.com/linux/raspbian stretch stable', category: 'docker'     }
          - { url: 'deb http://apt.kubernetes.io/ kubernetes-xenial main'                      , category: 'kubernetes' }
      - name: Install packages to allow apt to use HTTPS repositories
          name: "{{ item }}"
          state: present
          - apt-transport-https
          - ca-certificates
          - software-properties-common
      - name: Update list of available repositories
          update_cache: yes
      - name: Update all packages to the latest version
          upgrade: dist
      - name: Install Docker and K8s binaries
          name: "{{ item }}"
          state: present
          - docker-ce
          - kubelet
          - kubeadm
          - kubectl
          - kubernetes-cni
      - name: Turn off swap
        shell: dphys-swapfile swapoff && dphys-swapfile uninstall && update-rc.d dphys-swapfile remove
      - name: Activating cgroup
          path: /boot/cmdline.txt
          backrefs: true
          regexp: '^(.*rootwait)$'
          line: '\1 cgroup_enable=cpuset cgroup_memory=1'
      - name: Rebooting
        shell: reboot now
        ignore_errors: true
    become: true

Then to run it, use the following command :
ansible-playbook nameOfYourFile.yml --ask-become-pass
Then you will be prompted the password (that's what the option --ask-become-pass do) for executing commands as root.

Once you've run it (without any errors I hope), Docker and Kubernetes should be installed and ready to use.

Have fun !

No comments: