Blog by Yan

Install Kubernetes with Kubespray and ansible

April 09, 2019

Requirements

  • 3 Computers with Ubuntu OS
  • One for Operation of Ansible execution - 10.0.0.0
  • One for K8s Master Host - 10.0.0.1
  • One for K8s Node Host - 10.0.0.2

Preparation (On Operation Computer)

Preparation of Hosts for passwordless sudoers

  • Login to K8s Master Host with ssh
  • ssh masterhostuser@10.0.0.1
  • Set root passsword if you haven’t yet sudo passwd root
  • sudo vim /etc/ssh/sshd_config
  • Set PermitRootLogin=yes
  • Restart ssh sudo systemctl restart ssh
  • Logout with logout
  • Do the same for K8s Node Host
  • Make root passwords the same

Preparation for Ansible Environemnt

  • Create private key for ansible execution on hosts
  • ssh-keygen -t rsa -b 4096 -C "your_email@example.com" &% default values are fine.
  • Copy the key to hosts
  • ssh-copy-id root@10.0.0.1
  • ssh-copy-id root@10.0.0.2
  • Check if you can login without password to both hosts with ssh root@10.0.0.1 & ssh root@10.0.0.2

Preparation of Ansible Configuration

  • Clone the Ansible Config for Kubespray
  • git clone git@github.com:kubernetes-sigs/kubespray.git
  • cd kubespray
  • Install python-pip sudo apt install python-pip python3
  • Install dependencies from requirements.txt
  • cp -rfp inventory/sample inventory/mycluster
  • declare -a IPS=(10.0.0.1 10.0.0.2)
  • CONFIG_FILE=inventory/mycluster/hosts.ini sudo python3 contrib/inventory_builder/inventory.py ${IPS[@]}
  • Copy the file to inventory folder
  • cp inventory/sample/hosts.yaml inventory/mycluster/hosts.yaml
  • Change as the following config
[all]
node1  ansible_host=10.0.0.1 ip=10.0.0.1
node2  ansible_host=10.0.0.2 ip=10.0.0.2

[kube-master]
node1

[etcd]
node1

[kube-node]
node2

[k8s-cluster:children]
kube-master
kube-node

[calico-rr]

[vault]
node1
node2

RUN Ansible Cookbook

  • Run Ansible with this command
  • ansible-playbook -i inventory/mycluster/hosts.yaml --become --become-user=root cluster.yml -K

Yan Lin Aung

Written by Yan Lin Aung who lives and works in Yangon building useful things. You should follow him on Twitter