Kubernetes, often abbreviated as K8s, is an open-source platform designed to automate deploying, scaling, and operating application containers. Originally developed by Google, Kubernetes is now maintained by the Cloud Native Computing Foundation (CNCF). It has become the de facto standard for container orchestration.

Key Concepts

  1. Cluster

A Kubernetes cluster is a set of nodes (machines) that run containerized applications. A cluster consists of at least one master node and multiple worker nodes.

  1. Node

A node is a single machine in the Kubernetes cluster, which can be either a physical machine or a virtual machine. There are two types of nodes:

  • Master Node: Manages the Kubernetes cluster and coordinates all activities.
  • Worker Node: Runs the containerized applications.

  1. Pod

A pod is the smallest and simplest Kubernetes object. It represents a single instance of a running process in your cluster. A pod can contain one or more containers.

  1. Service

A service in Kubernetes is an abstraction that defines a logical set of pods and a policy by which to access them. Services enable communication between different parts of an application.

  1. Deployment

A deployment provides declarative updates to applications. It describes the desired state of an application and the Kubernetes system ensures that the current state matches the desired state.

  1. Namespace

Namespaces are a way to divide cluster resources between multiple users. They provide a scope for names, allowing for resource isolation.

Kubernetes Architecture

Master Node Components

  • API Server: Exposes the Kubernetes API. It is the front end of the Kubernetes control plane.
  • etcd: A consistent and highly-available key-value store used as Kubernetes' backing store for all cluster data.
  • Controller Manager: Runs controller processes that handle routine tasks in the cluster.
  • Scheduler: Assigns workloads to specific nodes based on resource availability.

Worker Node Components

  • Kubelet: An agent that runs on each node in the cluster. It ensures that containers are running in a pod.
  • Kube-proxy: Maintains network rules on nodes. These rules allow network communication to your pods from network sessions inside or outside of your cluster.
  • Container Runtime: The software that is responsible for running containers. Docker is a popular container runtime, but Kubernetes also supports others like containerd and CRI-O.

Practical Example: Deploying a Simple Application

Step 1: Create a Deployment

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

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

Step 2: Apply the Deployment

Use the kubectl command to apply the deployment:

kubectl apply -f nginx-deployment.yaml

Step 3: Verify the Deployment

Check the status of the deployment:

kubectl get deployments

You should see an output similar to:

NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           1m

Step 4: Expose the Deployment as a Service

Create a service to expose the deployment:

kubectl expose deployment nginx-deployment --type=LoadBalancer --name=nginx-service

Step 5: Verify the Service

Check the status of the service:

kubectl get services

You should see an output similar to:

NAME            TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
nginx-service   LoadBalancer   10.0.0.1        <pending>     80:30685/TCP   1m

Exercises

Exercise 1: Create a Deployment

Create a deployment for a simple web application using the httpd image. Ensure that the deployment has 2 replicas.

Solution:

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

apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: httpd
  template:
    metadata:
      labels:
        app: httpd
    spec:
      containers:
      - name: httpd
        image: httpd:2.4
        ports:
        - containerPort: 80

Apply the deployment:

kubectl apply -f httpd-deployment.yaml

Exercise 2: Expose the Deployment

Expose the httpd deployment as a service of type NodePort.

Solution:

Expose the deployment:

kubectl expose deployment httpd-deployment --type=NodePort --name=httpd-service

Summary

In this section, we introduced Kubernetes and its key concepts, including clusters, nodes, pods, services, deployments, and namespaces. We also covered the architecture of Kubernetes, detailing the components of both master and worker nodes. Finally, we provided a practical example of deploying a simple application and exposing it as a service, along with exercises to reinforce the learned concepts. In the next section, we will delve deeper into deploying Docker containers in Kubernetes.

© Copyright 2024. All rights reserved