Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Integrating with Kubernetes

Introduction

Kubernetes is a powerful orchestration system for managing containerized applications across a cluster of machines. This tutorial will guide you through the process of integrating your applications with Kubernetes, from setting up your environment to deploying and managing your applications.

Prerequisites

Before you begin, ensure you have the following installed on your system:

  • Docker
  • kubectl
  • Minikube (for local development)

Setting Up Kubernetes Cluster

To start using Kubernetes, you first need to set up a cluster. For local development, Minikube is a great tool to create a single-node Kubernetes cluster on your local machine.

minikube start

😄  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 ...
...
🏄  Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
                    

Once Minikube starts, you can verify the cluster status using:

kubectl cluster-info

Kubernetes control plane is running at https://127.0.0.1:32768
CoreDNS is running at https://127.0.0.1:32768/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy
                    

Creating a Deployment

Deployments are used to manage stateless applications. Below is a sample deployment YAML configuration for a simple Nginx application:

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
                

To apply this configuration, save it to a file (e.g., nginx-deployment.yaml) and run:

kubectl apply -f nginx-deployment.yaml

deployment.apps/nginx-deployment created
                    

Exposing the Deployment

To access the Nginx application from outside the cluster, you need to expose the deployment as a service:

kubectl expose deployment nginx-deployment --type=LoadBalancer --port=80

service/nginx-deployment exposed
                    

To get the URL for the service, you can use:

minikube service nginx-deployment --url

http://192.168.99.100:32500
                    

Scaling the Deployment

To scale the number of pod replicas in your deployment, use the following command:

kubectl scale deployment nginx-deployment --replicas=5

deployment.apps/nginx-deployment scaled
                    

Verify the scaling by listing the pods:

kubectl get pods

NAME                               READY   STATUS    RESTARTS   AGE
nginx-deployment-5b6b47c446-abcde  1/1     Running   0          2m
nginx-deployment-5b6b47c446-fghij  1/1     Running   0          2m
nginx-deployment-5b6b47c446-klmno  1/1     Running   0          2m
nginx-deployment-5b6b47c446-pqrst  1/1     Running   0          2m
nginx-deployment-5b6b47c446-uvwxy  1/1     Running   0          2m
                    

Cleaning Up

To delete the deployment and service, use the following commands:

kubectl delete service nginx-deployment

service "nginx-deployment" deleted
                    

kubectl delete deployment nginx-deployment

deployment.apps "nginx-deployment" deleted
                    

Conclusion

Integrating with Kubernetes involves setting up a cluster, creating deployments, exposing services, and scaling applications. This tutorial provided a step-by-step guide to get you started. Kubernetes offers many more features and capabilities, which you can explore further in the official documentation.