WindowsMacSoftwareSettingsSecurityProductivityLinuxAndroidPerformanceConfigurationApple All

How to Install and Use Kubernetes on Linux

Edited 4 weeks ago by ExtremeHow Editorial Team

KubernetesContainersDevOpsClustersOrchestrationApplication ManagementScalabilityConfigurationDeploymentServer Management

How to Install and Use Kubernetes on Linux

This content is available in 7 different language

Kubernetes, often referred to as K8s, is an open-source platform designed to automate the deployment, scaling, and operation of application containers. It provides a framework that enables you to run distributed systems flexibly. Kubernetes manages the work your applications perform based on the resources available on your cluster, optimally distributes load, and manages failure recovery.

Prerequisites

Before starting the installation of Kubernetes on Linux, make sure you have the following prerequisites:

Setting up Docker

Docker is the runtime that Kubernetes uses to manage containers. It forms the basis on which Kubernetes works. Follow these steps to install Docker on your Linux system:

  1. First, update your package list:
  2. sudo apt-get update
  3. Install the required prerequisite packages for Docker:
  4. sudo apt-get install apt-transport-https ca-certificates curl software-properties-common
  5. Add Docker's GPG key to ensure the authenticity of Docker packages:
  6. curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
  7. Add the Docker repository to your sources list:
  8. sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/$(lsb_release -cs) stable"
  9. Re-update the package database, and install Docker:
  10. sudo apt-get update sudo apt-get install docker-ce
  11. Make sure Docker is running:
  12. sudo systemctl start docker sudo systemctl enable docker

Verify the Docker installation by checking its version:

docker --version

Installing Kubernetes components

Step 1: Adding Kubernetes repositories

To install Kubernetes, you need to add its official repository to your package manager:

  1. Import Google Cloud's public GPG key:
  2. curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
  3. Add the Kubernetes repository to the sources list:
  4. echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list

Step 2: Installing kubelet, kubeadm, and kubectl

The three fundamental components of Kubernetes are kubelet, kubeadm and kubectl. Here's how to install them:

  1. Update package list:
  2. sudo apt-get update
  3. Install the components:
  4. sudo apt-get install -y kubelet kubeadm kubectl
  5. Prevent packages from being automatically updated by locking their versions:
  6. sudo apt-mark hold kubelet kubeadm kubectl

Configuring Kubernetes

Once Kubernetes is installed, it's time to configure the system appropriately:

Step 3: Disabling Swap Memory

Kubernetes does not work when swap memory is enabled. Use the following to disable swap on your system:

sudo swapoff -a

To make the change permanent across reboots, comment out the swap line in /etc/fstab.

Step 4: Network settings

Make sure these network settings are configured correctly:

  1. Enable IP forwarding by setting net.bridge.bridge-nf-call-iptables=1:
  2. sudo sysctl net.bridge.bridge-nf-call-iptables=1
  3. Verify the settings by running sysctl -p.

Step 5: Initializing Kubernetes with kubeadm

We use kubeadm to set up a master node - the control plane for your Kubernetes cluster:

  1. Run kubeadm init to initialize your environment:
  2. sudo kubeadm init
  3. Follow the output instructions to set up your kubectl configuration. This usually includes:
  4. mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
  5. If using root, replace $HOME with /root or /home/<your-username>.

Step 6: Setting up the Pod Network

Install the Pod Network plugin to allow communication between pods. Here is an example using Flannel:

kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

Check the status of your nodes:

kubectl get nodes

Managing your Kubernetes cluster

Step 7: Deploying the Application

Deploying your first application is simple. Let's deploy a simple nginx server:

kubectl run nginx --image=nginx --port=80

To expose the deployment as a service accessible from outside the cluster:

kubectl expose deployment nginx --type=NodePort

Get details about your deployment and service:

kubectl get deployments kubectl get services

NodePort of the service will give you a port to access the nginx server, which you can access using your server's IP address and the given port.

Scaling applications

One of the significant advantages of Kubernetes is its ability to easily scale applications. If your nginx server needs to handle more load, you can scale it:

kubectl scale deployment nginx --replicas=3

Check Status:

kubectl get pods -o wide

Cleaning

After trying out Kubernetes features, you may want to clean up the demo environment:

kubectl delete service nginx kubectl delete deployment nginx

Conclusion

With this guide, you've gotten started installing and using Kubernetes on Linux. This setup gives you a basic understanding of Kubernetes, focusing on essential components like kubeadm, kubelet, and the ability to deploy and manage simple applications. From here, you can explore more advanced Kubernetes concepts, configuration, and management techniques to harness the full power of Kubernetes in your infrastructure.

If you find anything wrong with the article content, you can


Comments