Kubernetes

Kubernetes, often referred to as K8s, is an open-source platform designed to automate the deployment, scaling, and operation of application containers. It groups containers into logical units for easy management and discovery. Originally designed by Google and now maintained by the Cloud Native Computing Foundation (CNCF), Kubernetes is the leading container orchestration tool used in the industry.

### Key Concepts

1. **Cluster**: A set of nodes that run containerized applications managed by Kubernetes.

2. **Node**: A single machine (virtual or physical) in a Kubernetes cluster. Each node runs containerized applications.

3. **Pod**: The smallest and simplest Kubernetes object. A pod represents a single instance of a running process in a cluster and can contain one or more containers.

4. **Deployment**: A Kubernetes resource that provides declarative updates to applications. It manages the lifecycle of pods and ensures that a specified number of pods are running.

5. **Service**: An abstraction that defines a logical set of pods and a policy by which to access them. Services enable network access to the pods.

6. **ConfigMap** and **Secret**: Objects used to manage configuration data and sensitive information, respectively.

## Setting Up Kubernetes

### Prerequisites

1. A machine with a supported operating system (Linux, macOS, or Windows).

2. Admin/root privileges on the machine to install Kubernetes and its dependencies.

### Installation Steps

#### Minikube (for Local Development)

Minikube is a tool that lets you run Kubernetes locally. It creates a single-node Kubernetes cluster on your machine, making it ideal for development and testing.

1. **Install Minikube**:

  – **Windows**:

   “`sh

   choco install minikube

   “`

  – **macOS**:

   “`sh

   brew install minikube

   “`

  – **Linux**:

   “`sh

   curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64

   sudo install minikube-linux-amd64 /usr/local/bin/minikube

   “`

2. **Start Minikube**:

  “`sh

  minikube start

  “`

3. **Verify Installation**:

  “`sh

  kubectl get nodes

  “`

#### kubectl (Kubernetes Command-Line Tool)

`kubectl` is the command-line interface for running commands against Kubernetes clusters.

1. **Install kubectl**:

  – **Windows**:

   “`sh

   choco install kubernetes-cli

   “`

  – **macOS**:

   “`sh

   brew install kubectl

   “`

  – **Linux**:

   “`sh

   sudo apt-get update && sudo apt-get install -y apt-transport-https gnupg2

   curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add –

   echo “deb https://apt.kubernetes.io/ kubernetes-xenial main” | sudo tee -a /etc/apt/sources.list.d/kubernetes.list

   sudo apt-get update

   sudo apt-get install -y kubectl

   “`

2. **Verify Installation**:

  “`sh

  kubectl version –client

  “`

### Creating and Managing a Kubernetes Cluster

#### Deploying an Application

1. **Create a Deployment**:

  “`sh

  kubectl create deployment hello-world –image=k8s.gcr.io/echoserver:1.4

  “`

2. **Expose the Deployment as a Service**:

  “`sh

  kubectl expose deployment hello-world –type=LoadBalancer –port=8080

  “`

3. **Get the URL to Access the Service**:

  “`sh

  minikube service hello-world –url

  “`

#### Scaling the Application

1. **Scale the Deployment**:

  “`sh

  kubectl scale deployment hello-world –replicas=3

  “`

2. **Verify the Number of Pods**:

  “`sh

  kubectl get pods

  “`

#### Updating the Application

1. **Update the Deployment**:

  “`sh

  kubectl set image deployment/hello-world echoserver=k8s.gcr.io/echoserver:1.5

  “`

2. **Verify the Update**:

  “`sh

  kubectl rollout status deployment/hello-world

  “`

3. **Rollback if Needed**:

  “`sh

  kubectl rollout undo deployment/hello-world

  “`

### Example: Running a Simple Web Application

1. **Create a YAML Configuration File**:

  Create a file named `k8s-deployment.yaml` with the following content:

  “`yaml

  apiVersion: apps/v1

  kind: Deployment

  metadata:

   name: web-app

  spec:

   replicas: 2

   selector:

    matchLabels:

     app: web-app

   template:

    metadata:

     labels:

      app: web-app

    spec:

     containers:

     – name: web-app

      image: nginx:latest

      ports:

      – containerPort: 80

  —

  apiVersion: v1

  kind: Service

  metadata:

   name: web-app-service

  spec:

   selector:

    app: web-app

   ports:

    – protocol: TCP

     port: 80

     targetPort: 80

   type: LoadBalancer

  “`

2. **Apply the Configuration**:

  “`sh

  kubectl apply -f k8s-deployment.yaml

  “`

3. **Verify the Deployment and Service**:

  “`sh

  kubectl get deployments

  kubectl get services

  “`

4. **Access the Web Application**:

  Use the URL provided by Minikube to access the service:

  “`sh

  minikube service web-app-service –url

 

Conclusion

Kubernetes simplifies the management of containerized applications at scale, providing robust features for deployment, scaling, and maintenance. By setting up Kubernetes using Minikube for local development and using `kubectl` for cluster management, you can efficiently deploy and manage applications. The YAML configuration allows for declarative management of the cluster resources, making it easier to version control and automate deployments.

Leave a comment

Your email address will not be published. Required fields are marked *