352.4 Container Orchestration Platforms (weight: 3)

Tech Tutorial: 352.4 Container Orchestration Platforms (weight: 3) #

Introduction #

In the world of software development, managing individual containers can become cumbersome and inefficient as applications scale. Container orchestration platforms play a crucial role in automating the deployment, scaling, and operation of containerized applications. In this tutorial, we will delve into Kubernetes, one of the most popular container orchestration platforms, and explore its key components and utilities.

Exam Objective #

The objective of this section is to understand and utilize container orchestration platforms, with a focus on Kubernetes.

Key Knowledge Areas:

  • Understanding Kubernetes architecture
  • Deployment configurations
  • Networking in Kubernetes
  • Service discovery and load balancing
  • Persistent storage management
  • Autoscaling applications
  • Health checking and self-healing

Utilities:

  • kubectl
  • minikube
  • helm

Step-by-Step Guide #

1. Setting Up Kubernetes with Minikube #

Minikube is a tool that allows you to run Kubernetes locally. It simplifies the process of learning Kubernetes by creating a single-node cluster inside a virtual machine on your computer.

# Install Minikube
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
chmod +x minikube
sudo mv minikube /usr/local/bin/

# Start Minikube
minikube start

2. Exploring Kubernetes with kubectl #

kubectl is the command-line tool for interacting with the Kubernetes API. You use kubectl to deploy applications, inspect and manage cluster resources, and view logs.

# Get information about the cluster
kubectl cluster-info

# List all nodes in the cluster
kubectl get nodes

# Create a deployment
kubectl create deployment nginx --image=nginx

# List deployments
kubectl get deployments

# Expose the deployment as a service
kubectl expose deployment nginx --type=NodePort --port=80

# Find the service URL
minikube service nginx --url

3. Managing Deployments #

Deployments in Kubernetes provide declarative updates to applications. You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate.

# nginx-deployment.yaml
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
# Create a deployment using the above YAML file
kubectl apply -f nginx-deployment.yaml

# List the pods to see if they are running
kubectl get pods

# Update the deployment
kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1

# Watch the rolling update
kubectl rollout status deployment/nginx-deployment

4. Service Discovery and Load Balancing #

Kubernetes Services are an abstract way to expose an application running on a set of Pods as a network service.

# nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
  type: LoadBalancer
# Create a service using the above YAML file
kubectl apply -f nginx-service.yaml

# Get the list of services
kubectl get services

5. Persistent Storage Management #

Kubernetes supports several types of persistent volumes, but here we’ll use a simple hostPath volume that mounts a directory from the host node’s filesystem into your Pod.

# nginx-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nginx-pv
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
# Create the persistent volume
kubectl apply -f nginx-pv.yaml

# Check the persistent volume
kubectl get pv

6. Autoscaling Applications #

Horizontal Pod Autoscaler automatically scales the number of Pods in a replication controller, deployment, or replica set based on observed CPU utilization.

# Create an autoscaler that maintains between 1 and 10 replicas of the Pods
kubectl autoscale deployment nginx-deployment --cpu-percent=50 --min=1 --max=10

# Get the current status of autoscaler
kubectl get hpa

7. Health Checking and Self-Healing #

Kubernetes supports liveness and readiness probes to manage the health of your applications.

# Add liveness probe to nginx-deployment.yaml
livenessProbe:
  httpGet:
    path: /healthz
    port: 80
  initialDelaySeconds: 30
  timeoutSeconds: 10
# Update the deployment with health check
kubectl apply -f nginx-deployment.yaml

Conclusion #

In this tutorial, we covered the basics of setting up and managing a Kubernetes cluster using Minikube and kubectl. We explored how to manage deployments, configure service discovery and load balancing, manage persistent storage, autoscale applications, and set up health checks. Container orchestration with Kubernetes can significantly streamline the process of deploying and managing scalable applications in production environments.