Installing 3 machine Kubernetes cluster on bare metal with the help of Ansible
Based on Digital Ocean Tutorial
- Ansible installed on your local machine
- SSH access to 3 machines (check that connecting works with root user)
- Ubuntu 20.04 installed
- At least 2 vCPUs and 2GB RAM for each machine
- Make a copy of the
hosts.example
file and rename it to `hosts - Edit the
hosts
file and add the IP addresses of the machines - Also add path to your SSH public key, which you are using for connecting to the machines
We will create ubuntu
user and add sudo privileges on all machines. Also the public keys will be added, so you can
SSH with same keys
Run Ansible playbook:
ansible-playbook -i hosts 01-initial.yaml
Now we are able to SSH with the ubuntu
user
Now we install tools needed by Kubernetes like Docker, kubelet
and kubeadm
Run Ansible playbook:
ansible-playbook -i hosts 02-kube-dependencies.yaml
We will initialize the cluster with kubeadm
and install networking and some configurations. Also the kubectl
tool will be installed to control-plane node.
Run Ansible playbook:
ansible-playbook -i hosts 03-control-plane.yaml
Now we first get the kubeadm join
command from control plane, and then we will join the worker nodes to the cluster
Run Ansible playbook:
ansible-playbook -i hosts 04-workers.yaml
At the control-plane setup stage we copied the KUBECONFIG
file, so we can use kubectl
from local machine to
control cluster.
Run: export KUBECONFIG=$(pwd)KUBECONFIG
Now check that you are connected to correct cluster: kubectl cluster-info
and/or kubectl get nodes
In example-app
folder you can find two files: deployment.yaml
and service.yaml
deployment.yaml
will deploy very basic nginx containerservice.yaml
will create NodePort service, so you can connect to application with worker node ip and specific port
Deploy nginx by running:
kubectl apply -f example-app/deployment.yaml
Check that it's deployed:
kubectl get deployments
Deploy Service by running:
kubectl apply -f example-app/service.yaml
Check that service is created and also port is assigned:
kubectl get services
Now you can open up the application in browser with http://<worker-node-ip>:<port>
Assigned port range is usually 30000-32767