[DOCKER] Building a kubernetes environment with ansible 1

Introduction

Install k8s on node and automate it with ansible as much as possible.
Ansible executor and k8s master use KVM of MAAS server

Goal

Build up to the state where k8s can be installed with ansible

Environment

MBP OS Sierra
MAAS server (192.168.100.152)
k8s-master server (KVM: 192.168.100.191)
ansible server (KVM: 192.168.100.192)

ansible Ver.2.5.1
kubernetes Ver.1.10.3

Install KVM on MAAS server

Install KVM to create ansible server and k8s server on the base MAAS server
After installation, join the libvirt group so that it can be run without sudo

$ sudo apt install -y qemu-kvm libvirt0 libvirt-bin virt-manager bridge-utils
$ sudo systemctl enable libvirt-bin
$ sudo gpasswd libvirtd -a <username>

Install the desktop on the MAAS server as it is more convenient to use the desktop environment to create KVM

$ sudo apt -y install ubuntu-desktop

Desktop installation takes time, so wait for a while
The desktop will be displayed automatically when the installation is completed and restarted.

Starting KVM in the terminal on the desktop will launch the creation window

$ virt-manager
0001_New-Virtual-Machine.png

Here, create a new KVM with the following specifications

  Hostname: ansible
 Memory: 4GB
CPU:2
 Storage: 30GB

 Hostname: k8s-master
 Memory: 8GB
CPU:4
 Storage: 40GB  



 

Building ansible

Actually install ansible on the ansible server created by KVM

$ sudo apt-get update
$ sudo apt-get install software-properties-common
$ sudo apt-add-repository ppa:ansible/ansible
$ sudo apt-get update
$ sudo apt-get install ansible

This time, create ansible folder directly under home and manage various files

$ sudo mkdir ansible

Create a playbook to install k8s on k8s-master

$ sudo vi k8s-master.yaml

---
- hosts: k8s-master
 remote_user: $ user name
  become: yes
  tasks:
 --name: Install prerequisites and Docker.io #docker install
      become: yes
      apt: name={{item}} update_cache=yes
      with_items:
        - apt-transport-https
        - ca-certificates
        - curl
        - software-properties-common
        - docker.io
    - name: user add to docker group
      user: name=gauss group=docker append=yes
 --name: Add K8S GPG key # k8s Preparation for installation
      apt_key:
        url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
    - name: Add K8S APT repository
      apt_repository:
        repo: deb http://apt.kubernetes.io/ kubernetes-xenial main
    - name: Install K8S
      apt: name={{item}} update_cache=yes
      with_items:
        - kubelet
        - kubeadm
        - kubectl
 --name: Remove swapfile from / etc / fstab #swap must be deleted or it will fail
      mount:
        name: swap
        fstype: swap
        state: absent
    - name: Disable swap
      command: swapoff -a
      when: ansible_swaptotal_mb > 0
 --name: Set docker service to start on boot. # Automatically start docker even after rebooting
      service: name=docker enabled=yes
 --name: Set kubelet service to start on boot. # Automatically start k8s even after rebooting
      service: name=kubelet enabled=yes
 --name: Init k8s-master # k8sm initialization
      become: yes
      shell: kubeadm init --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=192.168.100.191
    - name: Make Directory .kube
      file:
        path: /.kube
        state: directory
 owner: $ owner
        group: docker
        mode: 0755
    - name: Copy the .kube config
      become: yes
      file:
 src: /home/$username/ansible/admin.conf
        dest: ~/.kube/config
 owner: $ owner
        group: docker
        mode: 0600
    - name: Export Kubernetes
      lineinfile:
 path: /home/$username/.kube/config
        state: absent
        regexp: '^%KUBECONFIG'
 --name: Apply Flannel $ Create a network of flannel
      sudo: yes
      shell: kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.10.0/Documentation/kube-flannel.yml

Next, add ansible hosts

$ sudo vi /etc/ansible/hosts

[master]

k8s-master

$ sudo vi /etc/hosts 192.168.100.191 k8s-master

Run ansible playbook when ready
If you want to use python3, you need the option after "-e"

~/ansible$ sudo ansible-playbook --private-key=id_rsa_common k8s-master.yml -e 'ansible_python_interpreter=/usr/bin/python3'

Complete if no error occurs

Conclusion

The version of k8s changes quickly, and ubuntu also has a lot of errors with the new version of 18.04, but I was relieved for the success of initialization

I stumbled

docker version

When initializing k8s, if the latest docker (18.03) is installed, the supported version is up to 17.03, so I got a message asking me to downgrade, so I re-installed 17.03
However, it still failed, so I added docker.io and it succeeded.

kube-dns didn't work

In the kubeadm init command executed in the ansible file, the network notation of --pod-network-cidr was initially set to "10.0.0.0", but with that setting, kube- in the package dns didn't work
When I checked each page, I found that "10.244.0.0" seems to be correct for this network, and when I executed the command with that setting, kube-dns worked normally.

Reference page

Ubuntu 16.04 : Install KVM and start virtual machine
Introduction to Ansible. [Introduction]
kubernetes official page
Introduction to Docker container management with kubernetes
Install kubernetes v1.8 + Flannel with kubeadm

Recommended Posts