Deploying Applications on Kubernetes: A Step‑by‑Step Guide

Deploying applications on Kubernetes can seem daunting at first, but with declarative YAML files, you can manage your deployments with ease and precision. In this guide, we’ll walk you through deploying a sample application using YAML files and share best practices for versioning and updating your applications.


1. Introduction

Kubernetes uses YAML files to define the desired state of your application components. By specifying configurations for Deployments, Services, and other resources in YAML, you can easily deploy, update, and maintain your applications in a consistent and repeatable manner.


2. Deploying a Sample Application with YAML

A. Creating a Deployment

A Deployment manages a ReplicaSet, ensuring that a specified number of Pods are always running. Here’s a simple YAML example for deploying an NGINX application:

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
  • apiVersion & kind: Defines the resource type (Deployment in this case).
  • metadata: Provides the name and labels for the Deployment.
  • spec: Specifies the desired state, such as the number of replicas and container details.

B. Exposing the Application with a Service

To make your application accessible, create a Service that exposes your Deployment. Below is an example of a Service YAML:

apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: LoadBalancer
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
  • selector: Matches the Pods labeled with app: nginx in your Deployment.
  • type: LoadBalancer type makes the service externally accessible.

C. Applying the YAML Files

  1. Save the YAML files:
    Save the Deployment configuration as nginx-deployment.yaml and the Service configuration as nginx-service.yaml.
  2. Deploy Using kubectl: kubectl apply -f nginx-deployment.yaml kubectl apply -f nginx-service.yaml
  3. Verify the Deployment: kubectl get deployments kubectl get pods kubectl get services

3. Best Practices for Versioning and Updating Applications

A. Use Immutable Container Images

  • Tagging:
    Always tag your container images with version numbers (e.g., nginx:1.21.0) instead of using latest. This ensures consistency and easier rollbacks.
  • Immutable Deployments:
    Deploy immutable images to avoid unexpected changes.

B. Rolling Updates and Rollbacks

  • Rolling Updates:
    Use Kubernetes Deployments to perform rolling updates. Modify the image tag in your Deployment YAML and apply the update: kubectl set image deployment/nginx-deployment nginx=nginx:1.21.0 Kubernetes will gradually update your Pods with the new image, ensuring minimal downtime.
  • Rollbacks:
    If an update causes issues, you can easily roll back to a previous version: kubectl rollout undo deployment/nginx-deployment

C. Version Control for YAML Files

  • Use Git:
    Store your YAML configuration files in a Git repository. This practice allows you to track changes over time, collaborate with your team, and revert to previous versions if needed.
  • Automate Deployments:
    Integrate your YAML files with CI/CD pipelines (using tools like Jenkins, GitLab CI, or ArgoCD) to automate testing and deployment.

D. Monitor and Test

  • Health Checks:
    Configure liveness and readiness probes in your Deployment to automatically detect and handle unhealthy Pods.
  • Continuous Monitoring:
    Use monitoring tools (like Prometheus, Grafana, or Kubernetes Dashboard) to track application performance and resource usage.

4. Visual Overview

Below is a simplified diagram that summarizes the deployment process:

flowchart TD
A[Write YAML Files]
B[Apply with kubectl]
C[Deployment Created]
D[Service Exposed]
E[Rolling Updates & Rollbacks]

Diagram: The flow from writing YAML files to deploying and updating your application in Kubernetes.


5. 🤝 Connect With Us

Are you looking for certified professionals or need expert guidance on managing your Kubernetes deployments? We’re here to help!

🔹 Get Certified Candidates: Hire skilled professionals with deep Kubernetes expertise.
🔹 Project Consultation: Receive hands‑on support and best practices tailored to your environment.

📞 Contact Us Now
💼 Discuss Your Project

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top