A Quick Technical Guide to Automating Kubernetes Deployments

Get a quick technical guide to automating Kubernetes deployments. Learn to use kubectl and YAML for consistent application setup, updates, and rollbacks in your cluster.

Morgan Perry

Morgan Perry

April 15, 2024 · 2 min read
A Quick Technical Guide to Automating Kubernetes Deployments - Qovery

Automating Kubernetes deployments streamlines your development workflows and ensures consistency across environments.

This guide focuses on using native Kubernetes tools such as kubectl and YAML configuration files to automate your deployment processes.

#Prerequisites

  • A working Kubernetes cluster
  • kubectl command-line tool installed
  • Basic understanding of Kubernetes concepts and YAML syntax

#Step 1: Creating a Deployment YAML File

Start by creating a YAML file for your deployment. This file will define the desired state of your application in the Kubernetes cluster. Here’s a simple example of a YAML file for deploying a basic nginx container:

apiVersion: apps/v1
kind: Deployment
metadata:
 name: nginx-deployment
spec:
 replicas: 3
 selector:
 matchLabels:
 app: nginx
 template:
 metadata:
 labels:
 app: nginx

This configuration creates a deployment named nginx-deployment, starting three replicas of the nginx container.

#Step 2: Deploying with kubectl

To deploy the application to your Kubernetes cluster, run the following command:

kubectl apply -f nginx-deployment.yaml

This command instructs Kubernetes to set up the deployment as described in your YAML file.

#Step 3: Automating Deployment Updates and Testing Automation

Automate with CI/CD Pipeline: Configure a CI/CD pipeline (e.g., Jenkins, GitLab CI, GitHub Actions) to automatically run kubectl apply -f <configuration-file>.yaml whenever changes are pushed to the main branch. Here is an example using GitLab CI:

deploy:
 stage: deploy
 script:
 - kubectl apply -f nginx-deployment.yaml
 only:
 - main

#Step 4:Testing the Automation

  • Make a change (e.g., update the nginx image version in the YAML file).
  • Commit and push the change; watch the CI/CD pipeline trigger and update the deployment.
  • Verify updates using kubectl rollout status deployment/nginx-deployment.
  • Optionally, test rollback by deploying a faulty configuration and then executing kubectl rollout undo deployment/nginx-deployment.

#Step 5: Verifying the Deployment

After deploying, verify the status of your deployment with:

kubectl get deployments

This command provides details about the current deployments, including their desired and actual states.

#Ready to Streamline Your Deployments?

For a deeper dive into how these and other tools can transform your CI/CD pipelines, read our Kubernetes automation tools article here.

Your Favorite DevOps Automation Platform

Qovery is a DevOps Automation Platform Helping 200+ Organizations To Ship Faster and Eliminate DevOps Hiring Needs,

Try it out now!
Your Favorite DevOps Automation Platform
Qovery white logo

Your Favorite DevOps Automation Platform

Qovery is a DevOps Automation Platform Helping 200+ Organizations To Ship Faster and Eliminate DevOps Hiring Needs,

Try it out now!
KubernetesDevOpsPlatform Engineering