A Kubernetes cluster will consist of one master and one worker node. For this, we will be using Vagrant and VirtualBox to quickly provision virtual machines.
Create a dedicated SSH key pair to connect to the VMs:
ssh-keygen -b 4096
vagrant_key
is used as a key name.
-
There will be 2 VMs. One for the master node and another for the worker node.
-
Run the following command on the root folder of the project:
cd vagrant-provision
vagrant up
If Vms are ok then the following command should return SUCCESS like below:
cd ../kubeadm-ansible
ansible -i hosts all -m ping
worker1 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
master | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python3"
},
"changed": false,
"ping": "pong"
}
- The hostnames and IP addresses of the VMs are below:
- master : 192.168.56.15
- worker1 : 192.168.56.16
sudo ansible-playbook -i hosts node-init.yml
Once the playbook is finished, we need to see content below:
PLAY RECAP ***********************************************************************************
master : ok=14 changed=9 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
worker1 : ok=14 changed=9 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
sudo ansible-playbook -i hosts master-node-init.yml
sudo ansible-playbook -i hosts worker-node-init.yml
NOTE: Instead of the commands under 1. and 2. above, you can simply run sh install.sh
on the root folder.
3.1 ssh vagrant@192.168.56.15 -i $HOME/.ssh/vagrant_key
3.2 kubectl get nodes
or set the KUBECONFIG environment variable like below:
export KUBECONFIG=<folder_of_this_project>/kubeadm-ansible/config
i.e if your project is under the ~/try/k8s/create-k8s-cluster-with-ansible/
then set the KUBECONFIG like below:
export KUBECONFIG=~/try/k8s/create-k8s-cluster-with-ansible/kubeadm-ansible/config
kubectl get nodes
cd vagrant-provision
vagrant destroy
rm -rf .vagrant
Resources: