> For the complete documentation index, see [llms.txt](https://docs.plenit.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.plenit.com/docs-en/support/tutorials/linux/how-to-set-up-kubernetes.md).

# How to set up a Kubernetes environment on Plenit

Keep reading if you want to discover **how to set up a Kubernetes environment in Plenit** and get the most out of it.

Since Google released the Kubernetes project almost 10 years ago (2014), this technology has been gaining more and more prominence until it has become an **essential element** in microservices management.

Kubernetes is an **extensible, portable and flexible platform** based on open software (open source) that allows us to manage workloads of different sizes and that includes variable sizing, allowing all kinds of services to be deployed, facilitating, among other things, automation and response to workload spikes and other types of changing workloads.

The way Kubernetes operates makes it an ideal platform for PaaS (Platform as a Service) or IaaS (Infrastructure as a Service)-based management.

### **How to set up a Kubernetes environment in Plenit?**

#### **Prerequisites and recommendations:**

To successfully complete this tutorial and be able to **set up a Kubernetes environment in Plenit** the following will be necessary:

* On the one hand, be **registered on the Plenit Platform** with an organization and be registered in it after doing [**Log-in**](https://admin.jotelulu.com/).
* On the other hand, [**have registered a Server subscription**](https://jotelulu.com/soporte/tutoriales/como-desplegar-servidor)
* Have three **servers** Ubuntu 22.04 deployed within the subscription.

```{=html}
kubernetes-nacho-1 = Master.
kubernetes-nacho-2 = Worker 1.
kubernetes-nacho-3 = Worker 2.
```

* A user with **administrator privileges** either through "root" or using the "sudo" command.
* According to Kubernetes specifications, the provisioned virtual machines must have at least 2GB of RAM, 2 CPUs, and at least 20GB of hard disk space to work, although depending on the use they are going to be put to, it may be necessary to increase these requirements.

#### **Step 1. Preparing the machine before deploying Kubernetes**

The first thing to do **before starting the deployment** of Kubernetes is **update the packages and the distribution** to make sure we have the most up-to-date packages and the latest updated sources.

To do this, you must **run the following commands**.

```
# sudo apt update
# sudo apt upgrade
```

Let's remember that **the "sudo" command** is used in **Ubuntu distributions** to **run commands with elevated privileges**. It would be something like a "Super User Do" (Do as Super User), the equivalent of a "RunAs" in Microsoft environments.

<figure><img src="https://3894148717-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxksPMyYRDCe4qKkhbpcY%2Fuploads%2Fp5M2ZytSynQZjp8u5ZQc%2F01_Paso-1.-Lanzamos-la-actualizacion-de-los-paquetes-y-de-la-distribucion.png?alt=media&amp;token=25fe3806-b273-4437-a704-19a5168e95ca" alt=""><figcaption><p>Step 1. We launch the update of the packages and the distribution</p></figcaption></figure>

One of the points that **we must control** when working with Kubernetes is the use of **swap memory**. Sometimes problems have occurred during scaling of the PODS, since the system does not manage them correctly if the system SWAP is active.

To avoid erratic behavior, the best option is **to disable this option** using the "swapoff" command.

```
# sudo swapoff -a
```

To **check whether it has been disabled,** several methods can be used.

```
# free -h
# swapon -s
# cat /proc/swaps
```

So that it **is not activated on reboot** and cannot be used in any way **it must be removed from the file systems table**, that is, from **"/etc/fstab"** so it must be edited and the lines where the word "swap" appears commented out.

```
# sudo vim /etc/fstab
```

Once that is done, you can **run the query** with a "cat" (if you are a cat lover you can use any other viewing command) to see if it was saved correctly.

```
# cat /etc/fstab
```

This cleanup process of the "/etc/fstab" file could also be done with the following command:

```
# sudo sed -i '/ swap / s/^/#/' /etc/fstab
```

This command based on "sed" searches for the string "swap", without applying typing, and places a hash "#", which is the symbol used to comment out text.

<figure><img src="https://3894148717-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxksPMyYRDCe4qKkhbpcY%2Fuploads%2Fwm0NKAh6CeHud5YTlwe8%2F02_Paso-1.-Deshabilitamos-la-SWAP-para-que-no-haya-problemas-con-los-PODS-de-Kubernetes.png?alt=media&amp;token=efe7b5f6-ae76-4024-ae12-1e4175a1a9be" alt=""><figcaption><p>Step 1. We disable SWAP so there are no problems with Kubernetes PODS</p></figcaption></figure>

It is possible that you may want to **change the machine name** to identify it within our network or for any other purpose. This process can be carried out before the entire Kubernetes setup process, which is why we mention it before starting the actual procedure.

It is **the name is queried** current through the **command "hostname"**, although it should actually be visible as part of the Bash prompt.

```
# hostname
```

Next, to **change the machine name** the "sudo hostnamectl set-hostname" command is run followed by the new name and the "exec bash" command is run so that the changes take effect.

An example of all this could be:

```
# sudo hostnamectl set-hostname <New_Name>
# exec bash
```

Where:

* \<New\_Name>: Will be the name received by the machine from that moment on.

An example could be:

```
# sudo hostnamectl set-hostname kubernetes-nacho-1
# exec bash
```

Where:

* "kubernetes-nacho-1" will be the name received by the machine from that moment on.

![Step 1. We change the machine name to identify it on the network as a Kubernetes server](https://3894148717-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxksPMyYRDCe4qKkhbpcY%2Fuploads%2FF8sUgu8pcbprvypX8Stx%2F03_Paso-1.-Cambiamos-el-nombre-de-la-maquina-para-identificarla-en-la-red-como-servidor-de-Kubernetes.png?alt=media\&token=f70a58d5-2627-43f6-9af3-f9da27bfad9f)

The next step is **configure the kernel** (system core) with some **additional parameters so** that **Kubernetes** can work correctly.

To do this, you must **edit the file "/etc/sysctl.conf"** by running the following commands:

```
 sudo tee /etc/modules-load.d/containerd.conf <<EOF
overlay
br_netfilter
EOF
# sudo modprobe overlay
# sudo modprobe br_netfilter
```

And the following sequence:

```
# sudo tee /etc/sysctl.d/99-kubernetes-cri.conf <<EOF
net.bridge.bridge-nf-call-iptables  = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward                 = 1
EOF
```

<figure><img src="https://3894148717-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxksPMyYRDCe4qKkhbpcY%2Fuploads%2FAVZEs5ZqQXJvQk989ACy%2F04_Paso-1.-Configuramos-parametros-del-Kernel-para-el-uso-de-Kubernetes.png?alt=media&amp;token=61491b13-660a-4f08-a8c8-28bbc587df81" alt=""><figcaption><p>Step 1. We configure Kernel parameters for use with Kubernetes</p></figcaption></figure>

The next step will be to run the “sysctl –system” command so that **reloads the configuration** **of the kernel** with the changes we have made.

```
# sudo sysctl –system
```

We will need to review the architecture we want to set up and the IPs and names of the servers involved, which in this case would be something like this:

| Server             | Name in Plenit | Internal IP |
| ------------------ | -------------- | ----------- |
| kubernetes-nacho-1 | Ubuntu22Nacho1 | 10.0.0.96   |
| kubernetes-nacho-2 | Ubuntu22Nacho2 | 10.0.0.33   |
| kubernetes-nacho-3 | Ubuntu22Nacho3 | 10.0.0.92   |

With that we generate the data that the “/etc/hosts” file should contain, which should be:

```
10.0.0.96: kubernetes-nacho-1
10.0.0.33: kubernetes-nacho-2
10.0.0.92: kubernetes-nacho-3
```

#### **Step 2. Installing Kubernetes on our Ubuntu system**

At this point we are going to **start with the configuration of Kubernetes** itself, so we must **download and install the packages securely**. To do this, the "apt" command is used again, adding the use of certificates.

```
# sudo apt-get install -y apt-transport-https ca-certificates curl
```

<figure><img src="https://3894148717-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxksPMyYRDCe4qKkhbpcY%2Fuploads%2FtJmbXLKCs73gOOsVpCxQ%2F05_Paso-2.-Aseguramos-la-descarga-e-instalacion-de-los-paquetes-de-manera-segura.png?alt=media&amp;token=18712b00-4242-4f2f-9abe-5921c78c827c" alt=""><figcaption><p>Step 2. We ensure the download and installation of the packages securely</p></figcaption></figure>

For the correct operation of Kubernetes, you must **create a directory** in which the authenticity of the Kubernetes packages will be verified.

```
# sudo mkdir /etc/apt/keyrings
```

At this point you must **search for the public key and store it** in the folder just created. In this way, Kubernetes packages will be validated to prevent us from being sold a dummy and **make sure all packages are legitimate**.

```
# curl -fsSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo gpg –dearmor -o /etc/apt/keyrings/kubernetes-archive-keyring.gpg
```

The Kubernetes download source is added.

```
# echo «deb [signed-by=/etc/apt/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main» | sudo tee /etc/apt/sources.list.d/kubernetes.list
```

**Let's update the "apt" package index** to see new items by running the "sudo apt-get update" command again.

```
# sudo apt-get update
```

Next, you must **install the kubelet, kubeadm and kubectl packages** by running the "apt install" command with the different Kubernetes versions.

For information purposes, let's explain what each of the kubelet, kubeadm and kubectl packages is for.

* **Kubelet**: It is the service that runs on all nodes belonging to the Kubernetes cluster and is responsible for running PODS and containers.
* **Kubeadm**: It is used to create the Kubernetes cluster.
* **Kubectl**: It is used for the day-to-day operation of the Kubernetes cluster.

The command to run is the following, although there may be variations due to version changes:

```
# sudo apt install -y kubelet=1.26.5-00 kubeadm=1.26.5-00 kubectl=1.26.5-00 command
```

**NOTE:** *If it fails, you may need to add the command to include “–allow-downgrades”.*

```
# sudo apt install -y –allow-downgrades kubelet=1.26.5-00 kubeadm=1.26.5-00 kubectl=1.26.5-00
```

<figure><img src="https://3894148717-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxksPMyYRDCe4qKkhbpcY%2Fuploads%2FxfjbeuHAiv9rvSSzMMYC%2F06_Paso-2.-Lanzamos-la-instalacion-de-los-paquetes-de-Kubernetes-en-Ubuntu.png?alt=media&amp;token=c2397834-262f-4a47-8724-efbd792f81ad" alt=""><figcaption><p>Step 2. We launch the installation of the Kubernetes packages on Ubuntu</p></figcaption></figure>

The next step is to **deploy Docker to serve Kubernetes**. Docker will be the **platform selected for creating the containers** used by Kubernetes, this will allow us to create, distribute and run applications inside containers easily.

The reason containers are used instead of virtual machines is because they offer us a much lighter, portable and elastic environment, thus guaranteeing better performance in their different configurations.

For the **installation we use the command “sudo apt install docker.io”** which installs the Docker package.

```
# sudo apt install docker.io
```

<figure><img src="https://3894148717-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxksPMyYRDCe4qKkhbpcY%2Fuploads%2F6HJe1SvQNstk7ZoGw4T6%2F07_Paso-2.-Instalamos-Docker-para-dar-servicio-a-Kubernetes.png?alt=media&amp;token=7e0cea7c-3eac-47d3-8de3-3f9e2b5d7fd5" alt=""><figcaption><p>Step 2. We install Docker to serve Kubernetes</p></figcaption></figure>

The next step is the **configuration of “Containerd”** on all cluster nodes to ensure compatibility with Kubernetes.

To do this, the first thing to do is **create a folder for the configuration file**.

```
# sudo mkdir /etc/containerd
```

Once this is done, you must **create a default configuration file** for containerd and save it with the **name “config.toml”.**

```
# sudo sh -c «containerd config default > /etc/containerd/config.toml»
```

After running these commands, you must **modify the file "config.toml"** to locate the entry that sets «SystemdCgroup = false" and **change its value to «SystemdCgroup = true"**.

```
# sudo sed -i 's/ SystemdCgroup = false/ SystemdCgroup = true/' /etc/containerd/config.toml
```

To finish this part you must **restart the "contenedord" and "kubelet" services** so that the configuration is reread and the changes are saved.

```
# sudo systemctl restart containerd.service
# sudo systemctl restart kubelet.service
```

The normal thing is to want to **start Kubernetes** and therefore the Kubelet service **every time the machine starts,** so you must run the following command:

```
# sudo systemctl enable kubelet.service.
```

<figure><img src="https://3894148717-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxksPMyYRDCe4qKkhbpcY%2Fuploads%2FuGQcIy4ctNuDJ8wg8aaA%2F08_Paso-2.-Preparamos-el-sistema-para-el-uso-de-Containered.png?alt=media&amp;token=db5f56b2-0070-42df-b6fb-a92bec90458d" alt=""><figcaption><p>Step 2. We prepare the system for the use of Containered</p></figcaption></figure>

The next step is **start the Kubernetes cluster** on the master node, so that when a Kubernetes control plane is started using kubeadm, different components will be deployed to manage and orchestrate the cluster.

The following command will **download the component images** (kube-apiserver, kube-controller-manager, kube-scheduler, etcd, kube-proxy).

```
# sudo kubeadm config images pull
```

We initialize the master node by passing "--pod-network-cidr" with the range of IP addresses reserved for the POD network.

```
# sudo kubeadm init --pod-network-cidr=10.0.0.0/16
```

<figure><img src="https://3894148717-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxksPMyYRDCe4qKkhbpcY%2Fuploads%2FjxE5WsyVMqwSORrCNc5X%2F09_Paso-2.-Arrancamos-el-cluster-de-Kubernetes.png?alt=media&amp;token=680a8a89-92b8-440c-96fb-9a5366cd72fb" alt=""><figcaption><p>Step 2. We start the Kubernetes cluster</p></figcaption></figure>

At this point we are going to **configure kubectl,** which is the tool used to manage the Kubernetes cluster. To do this, you must start by **creating the .kube directory** in HOME and copying the cluster management configuration.

```
# mkdir -p $HOME/.kube
# sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
```

After this you must **change the ownership of the file** of the configuration that has been copied in order to **give the user permission** to use the configuration file.

```
# sudo chown $(id -u):$(id -g) $HOME/.kube/config
```

Next we are going to **configure both kubectl and Calico,** which will be necessary for the operation of our Kubernetes service. To do this, it will be necessary to **run the following commands on the master node** so that the Calico operator is deployed.

```
# kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/tigera-operator.yaml
```

<figure><img src="https://3894148717-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxksPMyYRDCe4qKkhbpcY%2Fuploads%2FL5osZLtZ59pqu83Qzdm0%2F10_Paso-2.-Configuramos-Kubctl-y-Calico-en-nuestra-Ubuntu.png?alt=media&amp;token=38364947-a885-408d-a0ec-5a8d4a0d215f" alt=""><figcaption><p>Step 2. We configure Kubectl and Calico on our Ubuntu</p></figcaption></figure>

The next step to take is to **download the custom resource file** for **Calico,** which contains different configurations necessary for using this service.

```
# curl https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/custom-resources.yaml -O
```

<figure><img src="https://3894148717-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxksPMyYRDCe4qKkhbpcY%2Fuploads%2F93CxpzkzHrvicBtluwR0%2F11_Paso-2.-Descargamos-los-recursos-de-Calico-y-configuramos-el-CDIR-de-nuestro-cluster-de-Kubernetes.png?alt=media&amp;token=d30dbca6-8118-4f14-8110-b1729add51d2" alt=""><figcaption><p>Step 2. We download the Calico resources and configure the CIDR of our Kubernetes cluster</p></figcaption></figure>

At this point you must **modify the CIDR in the "custom-resources.yaml" configuration file** so that the resources match the POD network that will be provisioned in our case.

```
# sed -i 's/cidr: 192.168.0.0/16/cidr: 10.0.0.0/16/g' custom-resources.yaml
```

At this point **kubectl is told to read the contents of the "custom-resources.yaml" file** and to **create the resources** defined in that file, for which the following command must be run:

```
# kubectl create -f custom-resources.yaml
```

<figure><img src="https://3894148717-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FxksPMyYRDCe4qKkhbpcY%2Fuploads%2Fm4Nx4HCr68EPkmPsa84Z%2F12_Paso-2.-Configuramos-el-CDIR-de-nuestro-cluster-de-Kubernetes.png?alt=media&amp;token=0a979518-c966-48a0-b18f-5686c8d87936" alt=""><figcaption><p>Step 2. We configure the CIDR of our Kubernetes cluster</p></figcaption></figure>

With this, everything should already be configured within our master, so we move on to configure the workers.

#### **Step 3. Add workers to the Kubernetes cluster**

Now that the master node of our Kubernetes cluster is configured, it is time to **add the worker nodes to the cluster, which will be the ones doing the actual work**, leaving the master the task of coordinating.

When the Kubeadm of the master node is started, a token will be provided that will be used to add those worker nodes, which will be added using the "kubeadm join" command.

```
# sudo kubeadm join <MASTER_NODE_IP >:<API_SERVER_PORT> –token <TOKEN> –discovery-token-ca-cert-hash <CERTIFICATE_HASH>
```

Where:

* **\<MASTER\_NODE\_IP>**: IP address of the master node of the Kubernetes cluster.
* **\<API\_SERVER\_PORT>**: Communication port, usually "6443".
* **\<TOKEN>**: We pass the validation token.
* **\<CERTIFICATE\_HASH>**: We pass the HASH of the token in order to make the mathematical checks.

With that we would have already added the workers to the Kubernetes structure and can start operating the platform.

**NOTE:** *If you encounter any problem during this procedure, you can consult the* [*Kubernetes installation help*](https://kubernetes.io/es/docs/tasks/tools/included/install-kubectl-linux/)

### **Conclusions:**

As you can see, **set up a Kubernetes environment in Plenit** it is very easy to do by following the steps we have described.

In this case we have seen how to deploy the Kubernetes service on our GNU/Linux servers, **Ubuntu 22.04** (the latest version available as of this date) but the same procedure can be used for other versions with slight changes.

Thanks for joining us!


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.plenit.com/docs-en/support/tutorials/linux/how-to-set-up-kubernetes.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
