DEV Community

Cover image for Getting Started with Kubernetes for Container Orchestration: A Guide with Coding Examples
Nitin Rachabathuni
Nitin Rachabathuni

Posted on

Getting Started with Kubernetes for Container Orchestration: A Guide with Coding Examples

In today’s fast-paced software development environment, containerization has become a cornerstone for building scalable, portable, and consistent applications. Among the various tools available for managing containers, Kubernetes has emerged as the industry standard for container orchestration. Whether you’re a developer, DevOps engineer, or IT professional, understanding Kubernetes is crucial for modern software deployment and management.

This article will provide you with a basic understanding of Kubernetes, along with coding examples to help you get started on your container orchestration journey.

What is Kubernetes?
Kubernetes, often abbreviated as K8s, is an open-source platform designed to automate deploying, scaling, and operating containerized applications. It groups containers that make up an application into logical units for easy management and discovery. Kubernetes is highly extensible, enabling you to manage containerized applications across multiple hosts, providing the foundation for cloud-native applications.

Key Features of Kubernetes:
Automated Rollouts and Rollbacks: Kubernetes manages updates to your application or its configuration and monitors the status of your application to ensure that all instances don't go down simultaneously.

Service Discovery and Load Balancing: K8s can expose a container using the DNS name or using their IP address. If traffic to a container is high, Kubernetes can load balance and distribute the network traffic to keep the deployment stable.

Self-healing: Restarts containers that fail, replaces containers, kills containers that don’t respond to user-defined health checks, and doesn’t advertise them to clients until they are ready to serve.

Horizontal Scaling: Automatically scale up or down your application based on CPU utilization or other metrics.
Setting Up Your Kubernetes Environment
Before diving into Kubernetes, ensure that you have Docker installed, as Kubernetes uses Docker containers to run applications.

Step 1: Install Minikube
Minikube is a tool that makes it easy to run Kubernetes locally. Minikube runs a single-node Kubernetes cluster on your personal computer.

# Install Minikube on macOS using Homebrew
brew install minikube

# Start Minikube
minikube start

# Check Minikube status
minikube status

Enter fullscreen mode Exit fullscreen mode

Step 2: Install Kubectl
kubectl is a command-line tool that interacts with the Kubernetes cluster. You can use it to deploy applications, inspect and manage cluster resources, and view logs.

# Install kubectl on macOS using Homebrew
brew install kubectl

# Check version
kubectl version --client

Enter fullscreen mode Exit fullscreen mode

Deploying Your First Application
With your environment set up, let's deploy a simple application on Kubernetes.

Step 1: Create a Deployment
A Deployment in Kubernetes manages a set of identical pods, ensuring that the specified number of pods are running at any given time.

# Create a file called 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

Enter fullscreen mode Exit fullscreen mode

Save the file and apply it using kubectl:

kubectl apply -f deployment.yaml

Enter fullscreen mode Exit fullscreen mode

Step 2: Expose the Deployment
Now that your deployment is up and running, you’ll want to expose it to the outside world.

kubectl expose deployment nginx-deployment --type=NodePort --port=80
Enter fullscreen mode Exit fullscreen mode

Step 3: Access the Application
You can now access the application using the Minikube service command:


minikube service nginx-deployment
Enter fullscreen mode Exit fullscreen mode

This command will open the NGINX application in your default web browser.

Scaling Your Application
One of the significant advantages of Kubernetes is its ability to scale applications up or down based on demand.

# Scale the application to 5 replicas
kubectl scale deployment/nginx-deployment --replicas=5

# Check the status of the pods
kubectl get pods

Enter fullscreen mode Exit fullscreen mode

With this command, Kubernetes will automatically create the necessary pods to meet the specified replica count.

Rolling Updates and Rollbacks
Kubernetes makes it easy to update your applications without downtime.

Rolling Update
To update the NGINX image to a new version:

kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1
Enter fullscreen mode Exit fullscreen mode

Kubernetes will perform a rolling update, updating each pod one at a time, ensuring that your application remains available throughout the process.

Rollback
If something goes wrong during the update, you can roll back to the previous version:


kubectl rollout undo deployment/nginx-deployment
Enter fullscreen mode Exit fullscreen mode

Conclusion
Kubernetes is a powerful tool that abstracts away much of the complexity associated with container orchestration. With features like automated rollouts, scaling, self-healing, and service discovery, Kubernetes enables you to build resilient and scalable applications effortlessly.

By following the steps outlined in this article, you’ve successfully deployed your first application on Kubernetes, scaled it, and even performed a rolling update. As you continue to explore Kubernetes, you’ll discover many more features and patterns that will help you manage your containerized applications efficiently.

For more advanced use cases, consider exploring Kubernetes services, ConfigMaps, Secrets, and StatefulSets. Happy orchestrating!


Thank you for reading my article! For more updates and useful information, feel free to connect with me on LinkedIn and follow me on Twitter. I look forward to engaging with more like-minded professionals and sharing valuable insights.

Top comments (0)