U1.26 Ubuntu Quick Start (QS): Kubernetes on premises. Installing Docker with Ansible. - chempkovsky/CS2WPF-and-CS2XAMARIN GitHub Wiki

Handmade playbook

Starting with

  • Pre-installed DHCP in the virtual environment (for example, a hardware implementation of a DHCP server in a modem)

  • Deploy three Ubuntu 20.04 TLS virtual machines. Consult the articles U1.01 and U1.02

    • Device name = u200401, ip = 192.168.100.12
    • Device name = u200402, ip = 192.168.100.13
    • Device name = u200403, ip = 192.168.100.14
  • For each machine u200401, u200402, u200403

    • run the command python3 --version
      • In our case it returns : Python 3.8.10
  • For each machine u200401, u200402, u200403

    • we have sudo-enabled user = yury with identical password for each machine

u200401, u200402, u200403 with openssh-server installed

  • For each machine u200401, u200402, u200403
    • run the command
sudo apt install openssh-server

u200401 with ansible installed

  • For the machine u200401
    • run the commands (no sudo prefix for the first four commands and for the last command)
      • to clarify what YOUR_OUTPUT_WITH_BIN is meant, please read the Step 15.1-15.3
ssh-keygen
ssh-copy-id 192.168.100.12
ssh-copy-id 192.168.100.13
ssh-copy-id 192.168.100.14

sudo apt-get install python3-pip
python3 -m pip install --user ansible
python -m site --user-base
export PATH="$PATH:YOUR_OUTPUT_WITH_BIN"
  • For machine ** u200401 ** the inventory file /etc/ansible/hosts is filled with data
192.168.100.12
192.168.100.13
192.168.100.14

Important notes

sudo apt-get update
sudo apt-get install ca-certificates
sudo apt-get install curl
sudo apt-get install gnupg
sudo apt-get install lsb-release
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
  • Our second goal is to check /etc/docker/daemon.json file and decide if it requires the recommended changes:
sudo mkdir /etc/docker
cat <<EOF | sudo tee /etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}
EOF

Commands mapping

sudo apt-get update

  • Source
sudo apt-get update
  • Ansible task
tasks:
  - name: sudo apt-get update
    become_method: sudo
    become: yes
    ansible.builtin.apt:
      update_cache: yes
  • Note: playbook must be executed with --ask-become-pass or -K flag.
ansible-playbook my_play_book.yml --ask-become-pass
ansible-playbook my_play_book.yml -K

sudo apt-get install

  • Source
sudo apt-get install ca-certificates
sudo apt-get install curl
sudo apt-get install gnupg
sudo apt-get install lsb-release
  • Ansible task
tasks:
  - name: sudo apt-get install ca-certificates curl gnupg lsb-release
    become_method: sudo
    become: yes
    ansible.builtin.apt: name={{ item }} state=latest update_cache=yes
    loop: [ 'ca-certificates', 'curl', 'gnupg', 'lsb-release']
  • Note: playbook must be executed with --ask-become-pass or -K flag.

curl -fsSL ...

  • Source
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
  • Ansible task
tasks:
  - name: "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg"
    become_method: sudo
    become: yes
    ansible.builtin.apt_key:
      url: "https://download.docker.com/linux/ubuntu/gpg"
      keyring: /usr/share/keyrings/docker-archive-keyring.gpg
      state: present
  • It should be noted that the file created using the source has a different size than the file created using ansible task.
  • Note: playbook must be executed with --ask-become-pass or -K flag.

echo "deb ..."

  • Source
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
  • Ansible task
tasks:
  - name: "echo deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null"
    become_method: sudo
    become: yes
   ansible.builtin.apt_repository:
      repo: "deb [arch={{ architecture_map[ansible_architecture] }} signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
      state: present
    vars:
      architecture_map:
        "x86_64": "amd64"
        "aarch64": "arm64"
        "aarch": "arm64"
        "armhf": "armhf"
        "armv7l": "armhf"
  • Note: playbook must be executed with --ask-become-pass or -K flag.

sudo apt-get update

  • Source
sudo apt-get update
  • Ansible task
tasks:
  - name: sudo apt-get update
    become_method: sudo
    become: yes
    ansible.builtin.apt:
      update_cache: yes
  • Note: playbook must be executed with --ask-become-pass or -K flag.

sudo apt-get install docker-ce docker-ce-cli containerd.io

  • Source
sudo apt-get install docker-ce docker-ce-cli containerd.io
  • Ansible task
tasks:
  - name: sudo apt-get install docker-ce docker-ce-cli containerd.io
    become_method: sudo
    become: yes
    ansible.builtin.apt: name={{ item }} state=latest update_cache=yes
    loop: [ 'docker-ce', 'docker-ce-cli', 'containerd.io']
  • Note: playbook must be executed with --ask-become-pass or -K flag.

Resulting instdockerpb.yml playbook file

  • Here is a resulting file:
---
- hosts: all

  tasks:
    - name: Update apt packages
      become_method: sudo
      become: true
      ansible.builtin.apt:
        update_cache: yes

    - name: sudo apt-get install ca-certificates curl gnupg lsb-release
      become_method: sudo
      become: yes
      ansible.builtin.apt: name={{ item }} state=latest update_cache=yes
      loop: [ 'ca-certificates', 'curl', 'gnupg', 'lsb-release']


    - name: "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg"
      become_method: sudo
      become: yes
      ansible.builtin.apt_key:
        url: "https://download.docker.com/linux/ubuntu/gpg"
        keyring: /usr/share/keyrings/docker-archive-keyring.gpg
        state: present

    - name: "echo deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null"
      become_method: sudo
      become: yes
      ansible.builtin.apt_repository:
        repo: "deb [arch={{ architecture_map[ansible_architecture] }} signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
        state: present
      vars:
        architecture_map:
          "x86_64": "amd64"
          "aarch64": "arm64"
          "aarch": "arm64"
          "armhf": "armhf"
          "armv7l": "armhf"

    - name: sudo apt-get update
      become_method: sudo
      become: yes
      ansible.builtin.apt:
        update_cache: yes

    - name: sudo apt-get install docker-ce docker-ce-cli containerd.io
      become_method: sudo
      become: yes
      ansible.builtin.apt: name={{ item }} state=latest update_cache=yes
      loop: [ 'docker-ce', 'docker-ce-cli', 'containerd.io']

  • Test for Device name = u200402, ip = 192.168.100.13
ansible-playbook instdockerpb.yml -l 192.168.100.133 -K
  • Here is a resul of playbook execution:
Click to show the picture

picture

  • Here is a test of installed docker:
Click to show the picture

picture

Creating /etc/docker/daemon.json

  • Note: daemon.json file has not been created yet

    • let's add additional tasks to instdockerpb.yml-file
  • Source

cat <<EOF | sudo tee /etc/docker/daemon.json
{
  "exec-opts": ["native.cgroupdriver=systemd"],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m"
  },
  "storage-driver": "overlay2"
}
EOF

sudo systemctl enable docker
sudo systemctl daemon-reload
sudo systemctl restart docker
  • Ansible task
  tasks:
    - name: "Creating /etc/docker/daemon.json file with content"
      become_method: sudo
      become: yes
      ansible.builtin.copy:
        dest: "/etc/docker/daemon.json"
        content: |
          {
           "exec-opts": ["native.cgroupdriver=systemd"],
           "log-driver": "json-file",
           "log-opts": {
             "max-size": "100m"
           },
           "storage-driver": "overlay2"
          }

    - name: "sudo systemctl enable docker"
      become_method: sudo
      become: yes
      ansible.builtin.systemd:
        enabled: yes
        name: docker

    - name: "sudo systemctl daemon-reload   sudo systemctl restart docker"
      become_method: sudo
      become: yes
      ansible.builtin.systemd:
        state: restarted
        daemon_reload: yes
        name: docker

Final instdockerpb.yml playbook file

---
- hosts: all

  tasks:
    - name: Update apt packages
      become_method: sudo
      become: true
      ansible.builtin.apt:
        update_cache: yes

    - name: sudo apt-get install ca-certificates curl gnupg lsb-release
      become_method: sudo
      become: yes
      ansible.builtin.apt: name={{ item }} state=latest update_cache=yes
      loop: [ 'ca-certificates', 'curl', 'gnupg', 'lsb-release']


    - name: "curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg"
      become_method: sudo
      become: yes
      ansible.builtin.apt_key:
        url: "https://download.docker.com/linux/ubuntu/gpg"
        keyring: /usr/share/keyrings/docker-archive-keyring.gpg
        state: present

    - name: "echo deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null"
      become_method: sudo
      become: yes
      ansible.builtin.apt_repository:
        repo: "deb [arch={{ architecture_map[ansible_architecture] }} signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable"
        state: present
      vars:
        architecture_map:
          "x86_64": "amd64"
          "aarch64": "arm64"
          "aarch": "arm64"
          "armhf": "armhf"
          "armv7l": "armhf"

    - name: sudo apt-get update
      become_method: sudo
      become: yes
      ansible.builtin.apt:
        update_cache: yes

    - name: sudo apt-get install docker-ce docker-ce-cli containerd.io
      become_method: sudo
      become: yes
      ansible.builtin.apt: name={{ item }} state=latest update_cache=yes
      loop: [ 'docker-ce', 'docker-ce-cli', 'containerd.io']

    - name: "Creating /etc/docker/daemon.json file with content"
      become_method: sudo
      become: yes
      ansible.builtin.copy:
        dest: "/etc/docker/daemon.json"
        content: |
          {
           "exec-opts": ["native.cgroupdriver=systemd"],
           "log-driver": "json-file",
           "log-opts": {
             "max-size": "100m"
           },
           "storage-driver": "overlay2"
          }

    - name: "sudo systemctl enable docker"
      become_method: sudo
      become: yes
      ansible.builtin.systemd:
        enabled: yes
        name: docker

    - name: "sudo systemctl daemon-reload   sudo systemctl restart docker"
      become_method: sudo
      become: yes
      ansible.builtin.systemd:
        state: restarted
        daemon_reload: yes
        name: docker

Additional Resources

Installing docker with geerlingguy.docker

Starting with

  • Deploy three Ubuntu 20.04 TLS virtual machines. Consult the articles U1.01 and U1.02

    • Device name = UbuntuAnsible, ip = 192.168.100.26
    • Device name = U200403, ip = 192.168.100.17
  • For each machine UbuntuAnsible, u200403

    • run the command python3 --version
      • In our case it returns : Python 3.8.10
    • we have sudo-enabled user = yury with identical password for each machine
    • run the command
sudo apt install openssh-server
  • For the machine UbuntuAnsible
ssh-keygen
ssh-copy-id 192.168.100.17
sudo apt-get install python3-pip
python3 -m pip install --user ansible
python -m site --user-base
export PATH="$PATH:YOUR_OUTPUT_WITH_BIN"

Installation

  • Step 01: install geerlingguy.docker
    • For the machine UbuntuAnsible
      • run the command (no sudo prefix)
ansible-galaxy install geerlingguy.docker
  • Step 02: create the folder
    • For the machine UbuntuAnsible
      • run the commands (no sudo prefix)
        • we chose apbs-name for "Ansible PlayBoks"
mkdir apbs
cd apbs
  • Step 03: create inventory-file
    • For the machine UbuntuAnsible
      • create and save apbs/grdocker_inv.ini file with the context
[all]
192.168.100.17

[geerlingguydocker]
192.168.100.17
  • Step 04: create playbook-file
    • For the machine UbuntuAnsible
      • create and save apbs/grdocker_pb.yml file with the context
---
- hosts: geerlingguydocker
  roles:
    - geerlingguy.docker
  • Step 05: Execute playbook-file
    • For the machine UbuntuAnsible
      • run the command
ansible-playbook -i grdocker_inv.ini grdocker_pb.yml --become --become-user root -K
  • Here is a responce
Click to show responce
PLAY [geerlingguydocker] *******************************************************

TASK [Gathering Facts] *********************************************************
ok: [192.168.100.17]

TASK [geerlingguy.docker : include_tasks] **************************************
skipping: [192.168.100.17]

TASK [geerlingguy.docker : include_tasks] **************************************
included: /home/yury/.ansible/roles/geerlingguy.docker/tasks/setup-Debian.yml for 192.168.100.17

TASK [geerlingguy.docker : Ensure old versions of Docker are not installed.] ***
ok: [192.168.100.17]

TASK [geerlingguy.docker : Ensure dependencies are installed.] *****************
changed: [192.168.100.17]

TASK [geerlingguy.docker : Ensure additional dependencies are installed (on Ubuntu < 20.04 and any other systems).] ***
skipping: [192.168.100.17]

TASK [geerlingguy.docker : Ensure additional dependencies are installed (on Ubuntu >= 20.04).] ***
ok: [192.168.100.17]

TASK [geerlingguy.docker : Add Docker apt key.] ********************************
changed: [192.168.100.17]

TASK [geerlingguy.docker : Ensure curl is present (on older systems without SNI).] ***
skipping: [192.168.100.17]

TASK [geerlingguy.docker : Add Docker apt key (alternative for older systems without SNI).] ***
skipping: [192.168.100.17]

TASK [geerlingguy.docker : Add Docker repository.] *****************************
changed: [192.168.100.17]

TASK [geerlingguy.docker : Install Docker.] ************************************
changed: [192.168.100.17]

TASK [geerlingguy.docker : Ensure /etc/docker/ directory exists.] **************
skipping: [192.168.100.17]

TASK [geerlingguy.docker : Configure Docker daemon options.] *******************
skipping: [192.168.100.17]

TASK [geerlingguy.docker : Ensure Docker is started and enabled at boot.] ******
ok: [192.168.100.17]

TASK [geerlingguy.docker : Ensure handlers are notified now to avoid firewall conflicts.] ***

RUNNING HANDLER [geerlingguy.docker : restart docker] **************************
changed: [192.168.100.17]

TASK [geerlingguy.docker : include_tasks] **************************************
included: /home/yury/.ansible/roles/geerlingguy.docker/tasks/docker-compose.yml for 192.168.100.17

TASK [geerlingguy.docker : Check current docker-compose version.] **************
ok: [192.168.100.17]

TASK [geerlingguy.docker : Delete existing docker-compose version if it's different.] ***
ok: [192.168.100.17]

TASK [geerlingguy.docker : Install Docker Compose (if configured).] ************
changed: [192.168.100.17]

TASK [geerlingguy.docker : include_tasks] **************************************
skipping: [192.168.100.17]

PLAY RECAP *********************************************************************
192.168.100.17             : ok=14   changed=6    unreachable=0    failed=0    skipped=7    rescued=0    ignored=0  
  • Step 06: make a test
    • For the machine U200403
      • run the command
sudo docker version
  • Here is a responce
Click to show responce
Client: Docker Engine - Community
 Version:           20.10.11
 API version:       1.41
 Go version:        go1.16.9
 Git commit:        dea9396
 Built:             Thu Nov 18 00:37:06 2021
 OS/Arch:           linux/amd64
 Context:           default
 Experimental:      true

Server: Docker Engine - Community
 Engine:
  Version:          20.10.11
  API version:      1.41 (minimum version 1.12)
  Go version:       go1.16.9
  Git commit:       847da18
  Built:            Thu Nov 18 00:35:15 2021
  OS/Arch:          linux/amd64
  Experimental:     false
 containerd:
  Version:          1.4.12
  GitCommit:        7b11cfaabd73bb80907dd23182b9347b4245eb5d
 runc:
  Version:          1.0.2
  GitCommit:        v1.0.2-0-g52b36a2
 docker-init:
  Version:          0.19.0
  GitCommit:        de40ad0

  • Step 06: make a test
    • For the machine U200403
      • run the command
sudo docker run hello-world
  • Here is a responce
Click to show responce
Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
2db29710123e: Pull complete 
Digest: sha256:cc15c5b292d8525effc0f89cb299f1804f3a725c8d05e158653a563f15e4f685
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

To generate this message, Docker took the following steps:
 1. The Docker client contacted the Docker daemon.
 2. The Docker daemon pulled the "hello-world" image from the Docker Hub.
    (amd64)
 3. The Docker daemon created a new container from that image which runs the
    executable that produces the output you are currently reading.
 4. The Docker daemon streamed that output to the Docker client, which sent it
    to your terminal.

To try something more ambitious, you can run an Ubuntu container with:
 $ docker run -it ubuntu bash

Share images, automate workflows, and more with a free Docker ID:
 https://hub.docker.com/

For more examples and ideas, visit:
 https://docs.docker.com/get-started/

Custom dockerd options (/etc/docker/daemon.json)

  • To change dockerd options
    • we use docker_daemon_options variable
      • here is an example
docker_daemon_options:
  storage-driver: "overlay2"
  exec-opts: ["native.cgroupdriver=systemd"]
  log-driver: "json-file"
  log-opts:
    max-size: "100m"
  • Step 07: modify playbook
    • For the machine UbuntuAnsible
      • here is the new version of apbs/grdocker_pb.yml-file
---
- hosts: geerlingguydocker
  vars:
    docker_daemon_options:
      storage-driver: "overlay2"
      exec-opts: ["native.cgroupdriver=systemd"]
      log-driver: "json-file"
      log-opts:
        max-size: "100m"
  roles:
    - geerlingguy.docker
  • Step 08: create a new (fresh) copy of the virtual machine U200403

    • immediately after creating the virtual machine and before installing docker, we made a copy of the vhdx file
    • using Hyper-V checkpoints is another way
  • Step 09: Execute new version of the playbook-file

    • For the machine UbuntuAnsible
      • run the command as before
ansible-playbook -i grdocker_inv.ini grdocker_pb.yml --become --become-user root -K
  • Step 10: Check /etc/docker/daemon.json
    • For the machine u200403
      • open /etc/docker/daemon.json file
        • here is a result
{
    "exec-opts": [
        "native.cgroupdriver=systemd"
    ],
    "log-driver": "json-file",
    "log-opts": {
        "max-size": "100m"
    },
    "storage-driver": "overlay2"
}

Docker users

  • To change Docker users
    • we use docker_users variable
      • here is an example
docker_users:
  - user1
  - user2
  • Note: please take a look at the code to understand how it works ansible-role-docker/tasks/docker-users.yml

    • it creates new users or changes existing ones
    • adds users to the docker-group
  • Step 11: modify playbook

    • For the machine UbuntuAnsible
      • here is the new version of apbs/grdocker_pb.yml-file
        • Note: we have not tested this playbook, but we think it works
---
- hosts: geerlingguydocker
  vars:
    docker_daemon_options:
      storage-driver: "overlay2"
      exec-opts: ["native.cgroupdriver=systemd"]
      log-driver: "json-file"
      log-opts:
        max-size: "100m"
    docker_users:
      - user1
      - user2
  roles:
    - geerlingguy.docker
⚠️ **GitHub.com Fallback** ⚠️