Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Container Orchestration with Kubernetes

Introduction

Kubernetes is an open-source platform designed to automate deploying, scaling, and operating application containers. It groups containers that make up an application into logical units for easy management and discovery. This tutorial will guide you through the basics of Kubernetes, from setting up a cluster to deploying applications.

Setting Up a Kubernetes Cluster

To start with Kubernetes, you need to set up a cluster. There are several ways to set up a Kubernetes cluster, including Minikube for local development and managed Kubernetes services like Google Kubernetes Engine (GKE), Amazon EKS, and Azure Kubernetes Service (AKS).

Example: Setting up a local Kubernetes cluster with Minikube.

minikube start

This command starts a local Kubernetes cluster.

😄  minikube v1.23.2 on Ubuntu 20.04
✨  Using the docker driver based on user configuration
👍  Starting control plane node minikube in cluster minikube
🚜  Pulling base image ...
🔄  Restarting existing docker container for "minikube" ...
🐳  Preparing Kubernetes v1.21.2 on Docker 20.10.8 ...
    ▪ Generating certificates and keys ...
    ▪ Booting up control plane ...
    ▪ Configuring RBAC rules ...
🔎  Verifying Kubernetes components...
    ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5
🌟  Enabled addons: storage-provisioner, default-storageclass
🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default

Basic Kubernetes Concepts

Before diving into more advanced topics, you should understand some basic Kubernetes concepts:

  • Pod: The smallest and simplest Kubernetes object. A Pod represents a single instance of a running process in your cluster.
  • Node: A worker machine (virtual or physical) in Kubernetes, which runs Pods.
  • Cluster: A set of nodes grouped together and managed by Kubernetes.

Deploying an Application

To deploy an application in Kubernetes, you need to create a deployment. Let's deploy a simple Nginx web server.

Create a deployment YAML 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

Apply the deployment using the following command:

kubectl apply -f nginx-deployment.yaml
deployment.apps/nginx-deployment created

Exposing Your Application

After deploying your application, you need to expose it to the outside world. This can be done using a Service. Let's expose the Nginx deployment using a LoadBalancer service.

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

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: LoadBalancer
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80

Apply the service using the following command:

kubectl apply -f nginx-service.yaml
service/nginx-service created

Scaling Your Application

One of the key features of Kubernetes is the ability to scale your applications up or down. Let's scale our Nginx deployment to 5 replicas.

Use the following command to scale the deployment:

kubectl scale deployment/nginx-deployment --replicas=5
deployment.apps/nginx-deployment scaled

Monitoring and Logging

Kubernetes provides several tools for monitoring and logging. You can use kubectl logs to view the logs of a particular pod and kubectl top to see the resource usage of nodes and pods.

Get the logs of a pod:

kubectl logs [pod-name]

Get the resource usage of nodes:

kubectl top nodes

Get the resource usage of pods:

kubectl top pods

Conclusion

In this tutorial, we've covered the basics of Kubernetes, including setting up a cluster, deploying applications, exposing services, scaling applications, and monitoring/logging. Kubernetes is a powerful tool for container orchestration and can greatly simplify the management of containerized applications.

Continue exploring the Kubernetes documentation and other resources to deepen your understanding and make the most of its capabilities.