Kubernetes is a container orchestration system that manages containers at scale. Initially developed by Google based on its experience running containers in production, Kubernetes is open source and actively developed by a community around the world.
Note: This tutorial uses version 1.22 of Kubernetes, the official supported version at the time of this article’s publication. For up-to-date information on the latest version, please see the current release notes in the official Kubernetes documentation.
Kubeadm automates the installation and configuration of Kubernetes components such as the API server, Controller Manager, and Kube DNS. It does not, however, create users or handle the installation of operating-system-level dependencies and their configuration. For these preliminary tasks, it is possible to use a configuration management tool like Ansible or SaltStack. Using these tools makes creating additional clusters or recreating existing clusters much simpler and less error prone.
In this guide, you will set up a Kubernetes cluster from scratch using Ansible and Kubeadm, and then deploy a containerized Nginx application to it. If you’re looking for a managed Kubernetes hosting service, check out our simple, managed Kubernetes service built for growth.
Your cluster will include the following physical resources:
The control plane node (a node in Kubernetes refers to a server) is responsible for managing the state of the cluster. It runs Etcd, which stores cluster data among components that schedule workloads to worker nodes.
Worker nodes are the servers where your workloads (i.e. containerized applications and services) will run. A worker will continue to run your workload once they’re assigned to it, even if the control plane goes down once scheduling is complete. A cluster’s capacity can be increased by adding workers.
After completing this guide, you will have a cluster ready to run containerized applications, provided that the servers in the cluster have sufficient CPU and RAM resources for your applications to consume. Almost any traditional Unix application including web applications, databases, daemons, and command line tools can be containerized and made to run on the cluster. The cluster itself will consume around 300-500MB of memory and 10% of CPU on each node.
Once the cluster is set up, you will deploy the web server Nginx to it to ensure that it is running workloads correctly.
An SSH key pair on your local Linux/macOS/BSD machine. If you haven’t used SSH keys before, you can learn how to set them up by following this explanation of how to set up SSH keys on your local machine.
Three servers running Ubuntu 20.04 with at least 2GB RAM and 2 vCPUs each. You should be able to SSH into each server as the root user with your SSH key pair.
Note: If you haven’t SSH’d into each of these servers at least once prior to following this tutorial, you may be prompted to accept their host fingerprints at an inconvenient time later on. You should do this now, or as an alternative, you can disable host key checking.
Ansible installed on your local machine. If you’re running Ubuntu 20.04 as your OS, follow the “Step 1 - Installing Ansible” section in How to Install and Configure Ansible on Ubuntu 20.04 to install Ansible. For installation instructions on other platforms like macOS or Rocky Linux, follow the official Ansible installation documentation.
Familiarity with Ansible playbooks. For review, check out Configuration Management 101: Writing Ansible Playbooks.
Knowledge of how to launch a container from a Docker image. Look at “Step 5 — Running a Docker Container” in How To Install and Use Docker on Ubuntu 20.04 if you need a refresher.
In this section, you will create a directory on your local machine that will serve as your workspace. You will configure Ansible locally so that it can communicate with and execute commands on your remote servers. Once that’s done, you will create a hosts
file containing inventory information such as the IP addresses of your servers and the groups that each server belongs to.
Out of your three servers, one will be the control plane with an IP displayed as control_plane_ip
. The other two servers will be workers and will have the IPs worker_1_ip
and worker_2_ip
.
Create a directory named ~/kube-cluster
in the home directory of your local machine and cd
into it:
- mkdir ~/kube-cluster
- cd ~/kube-cluster
This directory will be your workspace for the rest of the tutorial and will contain all of your Ansible playbooks. It will also be the directory inside which you will run all local commands.
Create a file named ~/kube-cluster/hosts
using nano
or your favorite text editor:
- nano ~/kube-cluster/hosts
Add the following text to the file, which will specify information about the logical structure of your cluster:
[control_plane]
control1 ansible_host=control_plane_ip ansible_user=root
[workers]
worker1 ansible_host=worker_1_ip ansible_user=root
worker2 ansible_host=worker_2_ip ansible_user=root
[all:vars]
ansible_python_interpreter=/usr/bin/python3
You may recall that inventory files in Ansible are used to specify server information such as IP addresses, remote users, and groupings of servers to target as a single unit for executing commands. ~/kube-cluster/hosts
will be your inventory file and you’ve added two Ansible groups (control plane and workers) to it specifying the logical structure of your cluster.
In the control plane group, there is a server entry named “control1” that lists the control plane’s IP (control_plane_ip
) and specifies that Ansible should run remote commands as the root user.
Similarly, in the workers group, there are two entries for the worker servers (worker_1_ip
and worker_2_ip
) that also specify the ansible_user
as root.
The last line of the file tells Ansible to use the remote servers’ Python 3 interpreters for its management operations.
Save and close the file after you’ve added the text. If you are using nano
, press Ctrl+X
, then when prompted, Y
and Enter
.
Having set up the server inventory with groups, let’s move on to installing operating system level dependencies and creating configuration settings.
In this section you will create a non-root user with sudo privileges on all servers so that you can SSH into them manually as an unprivileged user. This can be useful if, for example, you would like to see system information with commands such as top/htop
, view a list of running containers, or change configuration files owned by root. These operations are routinely performed during the maintenance of a cluster, and using a non-root user for such tasks minimizes the risk of modifying or deleting important files or unintentionally performing other dangerous operations.
Create a file named ~/kube-cluster/initial.yml
in the workspace:
- nano ~/kube-cluster/initial.yml
Next, add the following play to the file to create a non-root user with sudo privileges on all of the servers. A play in Ansible is a collection of steps to be performed that target specific servers and groups. The following play will create a non-root sudo user:
---
- hosts: all
become: yes
tasks:
- name: create the 'ubuntu' user
user: name=ubuntu append=yes state=present createhome=yes shell=/bin/bash
- name: allow 'ubuntu' to have passwordless sudo
lineinfile:
dest: /etc/sudoers
line: 'ubuntu ALL=(ALL) NOPASSWD: ALL'
validate: 'visudo -cf %s'
- name: set up authorized keys for the ubuntu user
authorized_key: user=ubuntu key="{{item}}"
with_file:
- ~/.ssh/id_rsa.pub
Here’s a breakdown of what this playbook does:
Creates the non-root user ubuntu
.
Configures the sudoers
file to allow the ubuntu
user to run sudo
commands without a password prompt.
Adds the public key in your local machine (usually ~/.ssh/id_rsa.pub
) to the remote ubuntu
user’s authorized key list. This will allow you to SSH into each server as the ubuntu
user.
Save and close the file after you’ve added the text.
Next, run the playbook locally:
- ansible-playbook -i hosts ~/kube-cluster/initial.yml
The command will complete within two to five minutes. On completion, you will see output similar to the following:
OutputPLAY [all] ****
TASK [Gathering Facts] ****
ok: [control1]
ok: [worker1]
ok: [worker2]
TASK [create the 'ubuntu' user] ****
changed: [control1]
changed: [worker1]
changed: [worker2]
TASK [allow 'ubuntu' user to have passwordless sudo] ****
changed: [control1]
changed: [worker1]
changed: [worker2]
TASK [set up authorized keys for the ubuntu user] ****
changed: [worker1] => (item=ssh-rsa AAAAB3...)
changed: [worker2] => (item=ssh-rsa AAAAB3...)
changed: [control1] => (item=ssh-rsa AAAAB3...)
PLAY RECAP ****
control1 : ok=4 changed=3 unreachable=0 failed=0
worker1 : ok=4 changed=3 unreachable=0 failed=0
worker2 : ok=4 changed=3 unreachable=0 failed=0
Now that the preliminary setup is complete, you can move on to installing Kubernetes-specific dependencies.
In this section, you will install the operating-system-level packages required by Kubernetes with Ubuntu’s package manager. These packages are:
Docker - a container runtime. It is the component that runs your containers. Kubernetes supports other runtimes, but Docker is still a popular and straightforward choice.
kubeadm
- a CLI tool that will install and configure the various components of a cluster in a standard way.
kubelet
- a system service/program that runs on all nodes and handles node-level operations.
kubectl
- a CLI tool used for issuing commands to the cluster through its API Server.
Create a file named ~/kube-cluster/kube-dependencies.yml
in the workspace:
- nano ~/kube-cluster/kube-dependencies.yml
Add the following plays to the file to install these packages to your servers:
---
- hosts: all
become: yes
tasks:
- name: create Docker config directory
file: path=/etc/docker state=directory
- name: changing Docker to systemd driver
copy:
dest: "/etc/docker/daemon.json"
content: |
{
"exec-opts": ["native.cgroupdriver=systemd"]
}
- name: install Docker
apt:
name: docker.io
state: present
update_cache: true
- name: install APT Transport HTTPS
apt:
name: apt-transport-https
state: present
- name: add Kubernetes apt-key
apt_key:
url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
state: present
- name: add Kubernetes' APT repository
apt_repository:
repo: deb http://apt.kubernetes.io/ kubernetes-xenial main
state: present
filename: 'kubernetes'
- name: install kubelet
apt:
name: kubelet=1.22.4-00
state: present
update_cache: true
- name: install kubeadm
apt:
name: kubeadm=1.22.4-00
state: present
- hosts: control_plane
become: yes
tasks:
- name: install kubectl
apt:
name: kubectl=1.22.4-00
state: present
force: yes
The first play in the playbook does the following:
Installs Docker, the container runtime, and configures a compatibility setting.
Installs apt-transport-https
, allowing you to add external HTTPS sources to your APT sources list.
Adds the Kubernetes APT repository’s apt-key for key verification.
Adds the Kubernetes APT repository to your remote servers’ APT sources list.
Installs kubelet
and kubeadm
.
The second play consists of a single task that installs kubectl
on your control plane node.
Note: While the Kubernetes documentation recommends you use the latest stable release of Kubernetes for your environment, this tutorial uses a specific version. This will ensure that you can follow the steps successfully, as Kubernetes changes rapidly and the latest version may not work with this tutorial. Although “xenial” is the name of Ubuntu 16.04, and this tutorial is for Ubuntu 20.04, Kubernetes is still referring to Ubuntu 16.04 package sources by default, and they are supported on 20.04 in this case.
Save and close the file when you are finished.
Next, run the playbook locally with the following command:
- ansible-playbook -i hosts ~/kube-cluster/kube-dependencies.yml
On completion, you will receive output similar to the following:
OutputPLAY [all] ****
TASK [Gathering Facts] ****
ok: [worker1]
ok: [worker2]
ok: [control1]
TASK [create Docker config directory] ****
changed: [control1]
changed: [worker1]
changed: [worker2]
TASK [changing Docker to systemd driver] ****
changed: [control1]
changed: [worker1]
changed: [worker2]
TASK [install Docker] ****
changed: [control1]
changed: [worker1]
changed: [worker2]
TASK [install APT Transport HTTPS] *****
ok: [control1]
ok: [worker1]
changed: [worker2]
TASK [add Kubernetes apt-key] *****
changed: [control1]
changed: [worker1]
changed: [worker2]
TASK [add Kubernetes' APT repository] *****
changed: [control1]
changed: [worker1]
changed: [worker2]
TASK [install kubelet] *****
changed: [control1]
changed: [worker1]
changed: [worker2]
TASK [install kubeadm] *****
changed: [control1]
changed: [worker1]
changed: [worker2]
PLAY [control1] *****
TASK [Gathering Facts] *****
ok: [control1]
TASK [install kubectl] ******
changed: [control1]
PLAY RECAP ****
control1 : ok=11 changed=9 unreachable=0 failed=0
worker1 : ok=9 changed=8 unreachable=0 failed=0
worker2 : ok=9 changed=8 unreachable=0 failed=0
After running this playbook, Docker, kubeadm
, and kubelet
will be installed on all of the remote servers. kubectl
is not a required component and is only needed for executing cluster commands. Installing it only on the control plane node makes sense in this context, since you will run kubectl
commands only from the control plane. Note, however, that kubectl
commands can be run from any of the worker nodes or from any machine where it can be installed and configured to point to a cluster.
All system dependencies are now installed. Let’s set up the control plane node and initialize the cluster.
In this section, you will set up the control plane node. Before creating any playbooks, however, it’s worth covering a few concepts such as Pods and Pod Network Plugins, since your cluster will include both.
A pod is an atomic unit that runs one or more containers. These containers share resources such as file volumes and network interfaces in common. Pods are the basic unit of scheduling in Kubernetes: all containers in a pod are guaranteed to run on the same node that the pod is scheduled on.
Each pod has its own IP address, and a pod on one node should be able to access a pod on another node using the pod’s IP. Containers on a single node can communicate easily through a local interface. Communication between pods is more complicated, however, and requires a separate networking component that can transparently route traffic from a pod on one node to a pod on another.
This functionality is provided by pod network plugins. For this cluster, you will use Flannel, a stable and performant option.
Create an Ansible playbook named control-plane.yml
on your local machine:
- nano ~/kube-cluster/control-plane.yml
Add the following play to the file to initialize the cluster and install Flannel:
---
- hosts: control_plane
become: yes
tasks:
- name: initialize the cluster
shell: kubeadm init --pod-network-cidr=10.244.0.0/16 >> cluster_initialized.txt
args:
chdir: $HOME
creates: cluster_initialized.txt
- name: create .kube directory
become: yes
become_user: ubuntu
file:
path: $HOME/.kube
state: directory
mode: 0755
- name: copy admin.conf to user's kube config
copy:
src: /etc/kubernetes/admin.conf
dest: /home/ubuntu/.kube/config
remote_src: yes
owner: ubuntu
- name: install Pod network
become: yes
become_user: ubuntu
shell: kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml >> pod_network_setup.txt
args:
chdir: $HOME
creates: pod_network_setup.txt
Here’s a breakdown of this play:
The first task initializes the cluster by running kubeadm init
. Passing the argument --pod-network-cidr=10.244.0.0/16
specifies the private subnet that the pod IPs will be assigned from. Flannel uses the above subnet by default; we’re telling kubeadm
to use the same subnet.
The second task creates a .kube
directory at /home/ubuntu
. This directory will hold configuration information such as the admin key files, which are required to connect to the cluster, and the cluster’s API address.
The third task copies the /etc/kubernetes/admin.conf
file that was generated from kubeadm init
to your non-root user’s home directory. This will allow you to use kubectl
to access the newly-created cluster.
The last task runs kubectl apply
to install Flannel
. kubectl apply -f descriptor.[yml|json]
is the syntax for telling kubectl
to create the objects described in the descriptor.[yml|json]
file. The kube-flannel.yml
file contains the descriptions of objects required for setting up Flannel
in the cluster.
Save and close the file when you are finished.
Run the playbook locally with the following command:
- ansible-playbook -i hosts ~/kube-cluster/control-plane.yml
On completion, you will see output similar to the following:
Output
PLAY [control1] ****
TASK [Gathering Facts] ****
ok: [control1]
TASK [initialize the cluster] ****
changed: [control1]
TASK [create .kube directory] ****
changed: [control1]
TASK [copy admin.conf to user's kube config] *****
changed: [control1]
TASK [install Pod network] *****
changed: [control1]
PLAY RECAP ****
control1 : ok=5 changed=4 unreachable=0 failed=0
To check the status of the control plane node, SSH into it with the following command:
- ssh ubuntu@control_plane_ip
Once inside the control plane node, execute:
- kubectl get nodes
You will now see the following output:
OutputNAME STATUS ROLES AGE VERSION
control1 Ready control-plane,master 51s v1.22.4
Note: As of Ubuntu 20.04, kubernetes is in the process of updating their old terminology. The node we’ve referred to as control-plane
throughout this tutorial used to be called the master
node, and occasionally you’ll see kubernetes assigning both roles simultaneously for compatibility reasons.
The output states that the control-plane
node has completed all initialization tasks and is in a Ready
state from which it can start accepting worker nodes and executing tasks sent to the API Server. You can now add the workers from your local machine.
Adding workers to the cluster involves executing a single command on each. This command includes the necessary cluster information, such as the IP address and port of the control plane’s API Server, and a secure token. Only nodes that pass in the secure token will be able join the cluster.
Navigate back to your workspace and create a playbook named workers.yml
:
- nano ~/kube-cluster/workers.yml
Add the following text to the file to add the workers to the cluster:
---
- hosts: control_plane
become: yes
gather_facts: false
tasks:
- name: get join command
shell: kubeadm token create --print-join-command
register: join_command_raw
- name: set join command
set_fact:
join_command: "{{ join_command_raw.stdout_lines[0] }}"
- hosts: workers
become: yes
tasks:
- name: join cluster
shell: "{{ hostvars['control1'].join_command }} >> node_joined.txt"
args:
chdir: $HOME
creates: node_joined.txt
Here’s what the playbook does:
The first play gets the join command that needs to be run on the worker nodes. This command will be in the following format:kubeadm join --token <token> <control-plane-ip>:<control-plane-port> --discovery-token-ca-cert-hash sha256:<hash>
. Once it gets the actual command with the proper token and hash values, the task sets it as a fact so that the next play will be able to access that info.
The second play has a single task that runs the join command on all worker nodes. On completion of this task, the two worker nodes will be part of the cluster.
Save and close the file when you are finished.
Run the playbook by locally with the following command:
- ansible-playbook -i hosts ~/kube-cluster/workers.yml
On completion, you will see output similar to the following:
OutputPLAY [control1] ****
TASK [get join command] ****
changed: [control1]
TASK [set join command] *****
ok: [control1]
PLAY [workers] *****
TASK [Gathering Facts] *****
ok: [worker1]
ok: [worker2]
TASK [join cluster] *****
changed: [worker1]
changed: [worker2]
PLAY RECAP *****
control1 : ok=2 changed=1 unreachable=0 failed=0
worker1 : ok=2 changed=1 unreachable=0 failed=0
worker2 : ok=2 changed=1 unreachable=0 failed=0
With the addition of the worker nodes, your cluster is now fully set up and functional, with workers ready to run workloads. Before scheduling applications, let’s verify that the cluster is working as intended.
A cluster can sometimes fail during setup because a node is down or network connectivity between the control plane and workers is not working correctly. Let’s verify the cluster and ensure that the nodes are operating correctly.
You will need to check the current state of the cluster from the control plane node to ensure that the nodes are ready. If you disconnected from the control plane node, you can SSH back into it with the following command:
- ssh ubuntu@control_plane_ip
Then execute the following command to get the status of the cluster:
- kubectl get nodes
You will see output similar to the following:
OutputNAME STATUS ROLES AGE VERSION
control1 Ready control-plane,master 3m21s v1.22.0
worker1 Ready <none> 32s v1.22.0
worker2 Ready <none> 32s v1.22.0
If all of your nodes have the value Ready
for STATUS
, it means that they’re part of the cluster and ready to run workloads.
If, however, a few of the nodes have NotReady
as the STATUS
, it could mean that the worker nodes haven’t finished their setup yet. Wait for around five to ten minutes before re-running kubectl get nodes
and inspecting the new output. If a few nodes still have NotReady
as the status, you might have to verify and re-run the commands in the previous steps.
Now that your cluster is verified successfully, let’s schedule an example Nginx application on the cluster.
You can now deploy any containerized application to your cluster. To keep things familiar, let’s deploy Nginx using Deployments and Services to explore how this application can be deployed to the cluster. You can use the commands below for other containerized applications as well, provided you change the Docker image name and any relevant flags (such as ports
and volumes
).
Ensure that you are logged into the control plane node and then and then run the following command to create a deployment named nginx
:
- kubectl create deployment nginx --image=nginx
A deployment is a type of Kubernetes object that ensures there’s always a specified number of pods running based on a defined template, even if the pod crashes during the cluster’s lifetime. The above deployment will create a pod with one container from the Docker registry’s Nginx Docker Image.
Next, run the following command to create a service named nginx
that will expose the app publicly. It will do so through a NodePort, a scheme that will make the pod accessible through an arbitrary port opened on each node of the cluster:
- kubectl expose deploy nginx --port 80 --target-port 80 --type NodePort
Services are another type of Kubernetes object that expose cluster internal services to clients, both internal and external. They are also capable of load balancing requests to multiple pods, and are an integral component in Kubernetes, frequently interacting with other components.
Run the following command:
- kubectl get services
This command will output text similar to the following:
OutputNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 1d
nginx NodePort 10.109.228.209 <none> 80:nginx_port/TCP 40m
From the highlighted line of the above output, you can retrieve the port that Nginx is running on. Kubernetes will assign a random port that is greater than 30000
automatically, while ensuring that the port is not already bound by another service.
To test that everything is working, visit http://worker_1_ip:nginx_port
or http://worker_2_ip:nginx_port
through a browser on your local machine. You will see Nginx’s familiar welcome page.
If you would like to remove the Nginx application, first delete the nginx
service from the control plane node:
- kubectl delete service nginx
Run the following to ensure that the service has been deleted:
- kubectl get services
You will see the following output:
OutputNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 1d
Then delete the deployment:
- kubectl delete deployment nginx
Run the following to confirm that this worked:
- kubectl get deployments
OutputNo resources found.
In this guide, you’ve successfully set up a Kubernetes cluster on Ubuntu 20.04 using Kubeadm and Ansible for automation.
If you’re wondering what to do with the cluster now that it’s set up, a good next step would be to get comfortable deploying your own applications and services onto the cluster. Here’s a list of links with further information that can guide you in the process:
Dockerizing applications - lists examples that detail how to containerize applications using Docker.
Pod Overview - describes in detail how Pods work and their relationship with other Kubernetes objects. Pods are ubiquitous in Kubernetes, so understanding them will facilitate your work.
Deployments Overview - provides an overview of deployments. It is useful to understand how controllers such as deployments work since they are used frequently in stateless applications for scaling and the automated healing of unhealthy applications.
Services Overview - covers services, another frequently used object in Kubernetes clusters. Understanding the types of services and the options they have is essential for running both stateless and stateful applications.
Other important concepts that you can look into are Volumes, Ingresses and Secrets, all of which come in handy when deploying production applications.
Kubernetes has a lot of functionality and features to offer. The Kubernetes Official Documentation is the best place to learn about concepts, find task-specific guides, and look up API references for various objects. You can also review our Kubernetes for Full-Stack Developers curriculum.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Hi,
One of the dependencies isn’t working. (Add Kubernetes Key) https://packages.cloud.google.com/apt/doc/apt-key.gpg The above link has expired. Anyone knows how to remediate this?
www.digitalocean.com
I have created a bunch of scripts based on this tutorial which work with Ubuntu 22.04 and the latest version of Kubernetes.
Link to my repo k8s-cluster
Thanks for the tutorial. It seems the tutorial needs an update. some things are not working again. kubeadm init command not create the admin.conf file and also there is no handler to enable or start docker. Thanks
thanks for saving my whole day awesome document :)
Great guide. A small thing that I encountered is an error of ansible being unprivileged.
The master.yml need to change
Adding hostname would be a good idea:
Playbook:
This comment has been deleted
Oof, ansible, kill me. Learn kubeadm from the documentation. Don’t abstract with ansible until you know kubeadm.
Brilliant guide, super helpful, just one Q on it, does K8s no longer require Swap to be disabled? I remember this was a requirement on their docs a few months ago
The file “~/kube-cluster/kube-dependencies.yml” has syntax errors.
It should look like this: https://pastebin.com/aym9rKEj