Install Kubernetes on Ubuntu
Tags: kubernetes, ubuntu, devops, devjournal
Published: 2022-07-07 08:48:23

<!-- wp:paragraph {"fontSize":"large"} -->
<p class="has-large-font-size"><strong>Step 1: Install Docker</strong></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>curl -fsSL https://get.docker.com -o get-docker.sh
sh get-docker.sh</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph {"fontSize":"large"} -->
<p class="has-large-font-size"><strong>Step 2: Make sure containerd dosen't have CRI disabled</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Edit the file <code>/etc/containerd/config.toml</code> and make sure that the disabled_plugins setting is blank </p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>nano /etc/containerd/config.toml</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Then restart the <code>containerd</code> service:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>service containerd restart</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph {"fontSize":"large"} -->
<p class="has-large-font-size"><strong>Step 3: Install Kubernetes</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You will start by installing the&nbsp;<code>apt-transport-https</code>&nbsp;package which enables working with&nbsp;<code>http</code>&nbsp;and&nbsp;<code>https</code>&nbsp;in Ubuntu’s repositories. Also, install&nbsp;<code>curl</code>&nbsp;as it will be necessary for the next steps. Execute the following command:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sudo apt install apt-transport-https curl</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Then, add the&nbsp;Kubernetes&nbsp;signing&nbsp;key&nbsp;to both nodes by executing the command:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" &gt;&gt; ~/kubernetes.list
sudo mv ~/kubernetes.list /etc/apt/sources.list.d
curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>After that, update the nodes:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sudo apt update</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Once the update completes, we will install Kubernetes. This involves installing the various tools that make up Kubernetes:&nbsp;kubeadm,&nbsp;kubelet,&nbsp;kubectl, and&nbsp;kubernetes-cni</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sudo apt-get install -y kubelet kubeadm kubectl kubernetes-cni</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph {"fontSize":"large"} -->
<p class="has-large-font-size"><strong>Step 4: Disabling Swap Memory</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Kubernetes fails to function in a system that is using&nbsp;swap&nbsp;memory. Hence, it must be disabled in the master node and all worker nodes. Execute the following command to disable swap memory:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sudo swapoff -a</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph {"fontSize":"large"} -->
<p class="has-large-font-size"><strong>Step 5: Setting Unique Hostnames</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Your nodes must have unique hostnames for easier identification. If you are deploying a cluster with many nodes, you can set it to identify names for your worker nodes such as node-1, node-2, etc. As we had mentioned earlier, we have named our nodes as&nbsp;kubernetes-master&nbsp;and&nbsp;kubernetes-worker. We have set them at the time of creating the server. However, you can adjust or set yours if you had not already done so from the command line. To adjust the hostname on the master node, run the following command:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sudo hostnamectl set-hostname kubernetes-master</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph {"fontSize":"large"} -->
<p class="has-large-font-size"><strong>Step 6: Changing Docker Cgroup Driver</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>On both master and worker nodes, update the&nbsp;<code>cgroupdriver</code>&nbsp;with the following commands:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sudo mkdir /etc/docker
cat &lt;&lt;EOF | sudo tee /etc/docker/daemon.json
{ "exec-opts": &#91;"native.cgroupdriver=systemd"],
"log-driver": "json-file",
"log-opts":
{ "max-size": "100m" },
"storage-driver": "overlay2"
}
EOF</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Then, execute the following commands to&nbsp;restart&nbsp;and&nbsp;enable&nbsp;Docker on system boot-up:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sudo systemctl enable docker
sudo systemctl daemon-reload
sudo systemctl restart docker</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph {"fontSize":"large"} -->
<p class="has-large-font-size"><strong>Step 7: Initializing the Kubernetes Master Node</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The first step in deploying a Kubernetes cluster is to fire up the master node. While on the terminal of your master node, execute the following command to initialize the&nbsp;kubernetes-master:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>kubeadm init --pod-network-cidr=10.244.0.0/16 --control-plane-endpoint &lt;EXTERNAL-IP&gt;</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>In the output, Kubernetes also displays some additional commands that you should run as a regular user on the master node before you start to use the cluster. Let’s run these commands:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>We have now initialized the master node. However, we also have to set up the pod network on the master node before we join the worker nodes.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph {"fontSize":"large"} -->
<p class="has-large-font-size"><strong>Step 8: Deploying a Pod Network</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>A pod network facilitates communication between servers and it’s necessary for the proper functioning of the Kubernetes cluster. You can read more about&nbsp;<a rel="noreferrer noopener" href="https://kubernetes.io/docs/concepts/cluster-administration/networking/" target="_blank">Kubernetes Cluster Networking</a>&nbsp;from the official docs. We will be using the&nbsp;<a rel="noreferrer noopener" href="https://kubernetes.io/docs/concepts/cluster-administration/networking/#flannel" target="_blank">Flannel</a>&nbsp;pod network for this tutorial. Flannel is a simple overlay network that satisfies the Kubernetes requirements.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/k8s-manifests/kube-flannel-rbac.yml</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph {"fontSize":"large"} -->
<p class="has-large-font-size"><strong>Step 9: Joining Worker Nodes to the Kubernetes Cluster</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>First, log into your worker node on a separate terminal session. You will use your&nbsp;<code>kubeadm</code>&nbsp;join&nbsp;command that was shown in your terminal when we initialized the master node in&nbsp;Step 7, execute the command indicated there, it will look something like the following:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p></p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>kubeadm join XXXX:6443 --token XXX \
	--discovery-token-ca-cert-hash sha256:XXXX</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Once the joining process completes, switch the master node terminal and execute the following command to confirm that your worker node has joined the cluster:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>kubectl get nodes</code></pre>
<!-- /wp:code -->
Originaly from https://blog.facha.dev/install-kubernetes-on-ubuntu/