Kubernetes is an open-source container orchestration platform used for deploying, scaling, and managing containerized applications. It is a powerful tool for automating the deployment and scaling of containerized applications. In this article, we will guide you through the process of setting up a basic Kubernetes environment and configuration.

Before we start, let’s first take a look at some of the terms that are commonly used in Kubernetes:

  • Cluster: A group of nodes that work together to run containerized applications.
  • Node: A physical or virtual machine that runs containerized applications.
  • Pod: The smallest deployable unit in Kubernetes. A pod can contain one or more containers.
  • Deployment: A Kubernetes resource that defines how many replicas of a pod should be running at any given time.
  • Service: A Kubernetes resource that provides a stable IP address and DNS name for a set of pods.
  • Container: A lightweight, standalone executable package that includes everything needed to run an application, including code, libraries, and dependencies.

Now that we know the basic terminology, let’s move on to setting up our Kubernetes environment.

Step 1: Installing Kubernetes

The first step in setting up a Kubernetes environment is to install the necessary software. There are several ways to install Kubernetes, but we will be using kubeadm, which is the official tool for setting up a Kubernetes cluster.

Here are the steps to install Kubernetes using kubeadm:

  1. Update your system:
sudo apt-get update
sudo apt-get upgrade
  1. Install Docker:
sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin -y
  1. Add the Kubernetes apt repository:
sudo apt-get update && sudo apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
cat <<EOF | sudo tee /etc/apt/sources.list.d/kubernetes.list
deb https://apt.kubernetes.io/ kubernetes-xenial main
EOF
  1. Install Kubernetes:
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl

Step 2: Initializing the Master Node

After installing Kubernetes, the next step is to initialize the master node. The master node is responsible for managing the cluster and its resources.

Here are the steps to initialize the master node:

  1. Disable swap:
sudo swapoff -a
  1. Initialize the master node:
sudo kubeadm init

This command will generate a command that you can use to join other nodes to the cluster. Make sure to save this command as you will need it later.

  1. Set up the kubeconfig file:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

Step 3: Joining the Nodes

Now that the master node is set up, the next step is to join the worker nodes to the cluster.

Here are the steps to join a node to the cluster:

  1. Run the command that was generated when initializing the master node on the worker node:
sudo kubeadm join <master-node-ip>:<master-node-port> --token <token> --discovery-token-ca-cert-hash <hash>
  1. Verify that the node has been added to the cluster:
kubectl get nodes

This command should show all the nodes in the cluster, including the newly added node.

Step 4: Deploying an Application

Now that the cluster is set up and the nodes are joined, the next step is to deploy an application to the

cluster. In Kubernetes, an application is typically deployed as a set of pods, which are managed by a deployment.

Here are the steps to deploy an application:

  1. Create a deployment file:
nano deployment.yaml
  1. Paste the following code into the file:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp-container
        image: nginx
        ports:
        - containerPort: 80

This code defines a deployment with three replicas of a pod that runs the Nginx web server.

  1. Deploy the application:
kubectl apply -f deployment.yaml
  1. Verify that the deployment is running:
kubectl get deployments

This command should show the myapp-deployment deployment and its status.

  1. Create a service for the deployment:
nano service.yaml
  1. Paste the following code into the file:
apiVersion: v1
kind: Service
metadata:
  name: myapp-service
spec:
  selector:
    app: myapp
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer

This code defines a service that provides a stable IP address and DNS name for the pods in the myapp-deployment deployment.

  1. Deploy the service:
kubectl apply -f service.yaml
  1. Verify that the service is running:
kubectl get services

This command should show the myapp-service service and its IP address.

Step 5: Scaling the Application

One of the main benefits of using Kubernetes is the ability to easily scale applications up or down based on demand. In Kubernetes, scaling is done by adjusting the number of replicas in a deployment.

Here are the steps to scale the application:

  1. Scale the deployment:
kubectl scale deployment myapp-deployment --replicas=5

This command will increase the number of replicas in the myapp-deployment deployment to five.

  1. Verify that the deployment has been scaled:
kubectl get deployments

This command should show the myapp-deployment deployment with five replicas.

Step 6: Updating the Application

Another benefit of using Kubernetes is the ability to easily update applications without downtime. In Kubernetes, updates are done by creating a new deployment with the updated configuration and then gradually rolling out the new deployment while rolling down the old deployment.

Here are the steps to update the application:

  1. Update the deployment:
nano deployment.yaml
  1. Change the image version to a new version:
apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deployment
spec:
  replicas: 5
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp-container
        image: nginx:1.19
        ports:
        - containerPort: 80

This code updates the myapp-deployment deployment to use version 1.19 of the Nginx image.

  1. Deploy the updated deployment:
kubectl apply -f deployment.yaml
  1. Verify that the updated deployment is running:
kubectl get deployments

This command should show the myapp-deployment deployment with the updated image version

  1. Verify that the pods are being rolled out:
kubectl get pods

This command should show both the old and new versions of the myapp-deployment pods.

  1. Verify that the rollout is complete:
kubectl rollout status deployment myapp-deployment

This command should show that the rollout is complete and that all pods are running the updated version.

Step 7: Monitoring the Application

Kubernetes provides a number of tools for monitoring the health and performance of applications running on the cluster. One of these tools is the Kubernetes Dashboard, which provides a graphical user interface for viewing and managing Kubernetes resources.

Here are the steps to install and use the Kubernetes Dashboard:

  1. Install the Kubernetes Dashboard:
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.4.0/aio/deploy/recommended.yaml

This command will install the Kubernetes Dashboard in the cluster.

  1. Create an administrative user:
nano admin-user.yaml
  1. Paste the following code into the file:
apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin-user
  namespace: kubernetes-dashboard
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: admin-user
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: admin-user
  namespace: kubernetes-dashboard

This code creates a new service account and a cluster role binding that grants the account cluster-admin privileges.

  1. Deploy the administrative user:
kubectl apply -f admin-user.yaml
  1. Start a proxy to the Kubernetes API server:
kubectl proxy
  1. Open the Kubernetes Dashboard:
http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/
  1. Sign in to the Kubernetes Dashboard using the token of the admin user:
kubectl -n kubernetes-dashboard describe secret $(kubectl -n kubernetes-dashboard get secret | grep admin-user | awk '{print $1}')

This command will display the token for the admin user. Copy the token and use it to sign in to the Kubernetes Dashboard.

The Kubernetes Dashboard provides a number of features for monitoring and managing Kubernetes resources, including:

  • Overview of the cluster and its resources
  • Metrics for the cluster and its resources
  • Logs for the cluster and its resources
  • Deployment and scaling of applications
  • Management of Kubernetes resources

Conclusion

In conclusion, setting up and configuring a basic Kubernetes environment involves installing and configuring a Kubernetes cluster, deploying an application, scaling the application, updating the application, and monitoring the application. While these steps may seem complex, Kubernetes provides a number of tools and features that make it easy to manage and scale applications running on a cluster. With a basic understanding of Kubernetes concepts and commands, anyone can set up and manage a Kubernetes cluster and deploy and scale applications with ease.