Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Deploying to Kubernetes

Introduction

Kubernetes is an open-source platform designed to automate deploying, scaling, and operating application containers. In this tutorial, we will walk through the steps required to deploy an application to a Kubernetes cluster. This includes setting up your environment, writing Kubernetes manifests, and using kubectl to manage your cluster.

Prerequisites

Before you start, ensure you have the following:

  • Basic understanding of containers and Docker.
  • Kubectl command-line tool installed.
  • Access to a Kubernetes cluster. You can use Minikube for local testing.

Setting Up Your Environment

First, let's set up our environment. Install kubectl if you haven't already:

curl -LO "https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl"
chmod +x ./kubectl
sudo mv ./kubectl /usr/local/bin/kubectl

Verify the installation:

kubectl version --client
Client Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.0", GitCommit:"af46c47", GitTreeState:"clean", BuildDate:"2020-12-08T16:56:12Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"}

Creating Kubernetes Manifests

Kubernetes uses YAML files, called manifests, to define the state of your application. Let's create a simple deployment manifest for a Docker container running Nginx.

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

Save this as nginx-deployment.yaml.

Deploying the Application

Use kubectl to create the deployment and verify its status:

kubectl apply -f nginx-deployment.yaml
kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 1m

Exposing the Deployment

To allow external traffic to access your deployment, you need to expose it as a service:

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

Check the service details:

kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 443/TCP 1h
nginx-service LoadBalancer 10.0.0.254 80:30686/TCP 1m

Scaling the Deployment

You can easily scale your deployment to handle more traffic:

kubectl scale deployment nginx-deployment --replicas=5

Verify the number of pods:

kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-6f79f9fc4f-abcde 1/1 Running 0 1m
nginx-deployment-6f79f9fc4f-fghij 1/1 Running 0 1m
nginx-deployment-6f79f9fc4f-klmno 1/1 Running 0 1m
nginx-deployment-6f79f9fc4f-pqrst 1/1 Running 0 1m
nginx-deployment-6f79f9fc4f-uvwxyz 1/1 Running 0 1m

Cleaning Up

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

kubectl delete deployment nginx-deployment
kubectl delete service nginx-service

Conclusion

In this tutorial, we covered the basics of deploying an application to a Kubernetes cluster. You learned how to set up your environment, create Kubernetes manifests, deploy and expose your application, scale the deployment, and clean up resources. Kubernetes is a powerful tool for managing containerized applications at scale, and mastering its features will greatly enhance your DevOps skills.