Automating Kubernetes Cluster Deployment with Jenkins and Terraform: A Complete Guide to CI/CD for Infrastructure as Code
Avesh
Posted on October 31, 2024
Deploying a Kubernetes cluster using Jenkins and Infrastructure as Code (IaC) through Terraform can be an efficient, automated way to set up and manage your infrastructure. This process enables version-controlled infrastructure and makes deployment repeatable, scalable, and less error-prone. Below is a comprehensive guide on setting up a Jenkins pipeline for deploying a Kubernetes cluster on a cloud platform (AWS, for example) using Terraform.
Prerequisites
- Jenkins Setup: Jenkins installed and configured.
- Terraform Setup: Terraform installed on the Jenkins server.
- Kubernetes CLI (kubectl): To interact with the Kubernetes cluster.
- AWS Credentials: Access keys set up for AWS or any other cloud provider.
- Git Repository: Code repository with your Terraform and Kubernetes files.
Step 1: Set Up Your Infrastructure with Terraform
Terraform scripts will define and provision a Kubernetes cluster infrastructure on AWS.
-
Define Provider: Create a
main.tf
file for AWS as the provider.
# main.tf
provider "aws" {
region = "us-west-2"
}
resource "aws_vpc" "my_vpc" {
cidr_block = "10.0.0.0/16"
}
resource "aws_subnet" "my_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-west-2a"
}
-
Configure EKS Cluster: In
eks.tf
, define the Elastic Kubernetes Service (EKS) cluster.
# eks.tf
module "eks" {
source = "terraform-aws-modules/eks/aws"
cluster_name = "my-cluster"
cluster_version = "1.21"
subnets = [aws_subnet.my_subnet.id]
vpc_id = aws_vpc.my_vpc.id
}
-
Add Output: In
outputs.tf
, output essential information such as the cluster endpoint.
# outputs.tf
output "cluster_endpoint" {
value = module.eks.cluster_endpoint
}
output "kubeconfig" {
value = module.eks.kubeconfig
}
- Initialize and Test Terraform: Run the following commands to test your Terraform configuration:
terraform init
terraform apply -auto-approve
Once your Terraform files are ready, commit them to your Git repository.
Step 2: Configure Jenkins Pipeline
In Jenkins, we will create a pipeline script to automate the deployment of the Kubernetes cluster using the Terraform code.
-
Create a New Pipeline Job:
- Go to Jenkins Dashboard -> New Item -> Enter an Item name.
- Select “Pipeline” and click OK.
-
Set Up Pipeline Script:
- In the pipeline configuration, set the pipeline definition to “Pipeline script from SCM.”
- Choose Git, and add the repository URL with the Terraform and Kubernetes files.
Define the Jenkinsfile: Create a
Jenkinsfile
in your repository with the pipeline stages.
pipeline {
agent any
environment {
AWS_REGION = 'us-west-2'
TF_VAR_region = "${AWS_REGION}"
}
stages {
stage('Checkout') {
steps {
checkout scm
}
}
stage('Terraform Init') {
steps {
script {
sh 'terraform init'
}
}
}
stage('Terraform Apply') {
steps {
script {
sh 'terraform apply -auto-approve'
}
}
}
stage('Configure kubectl') {
steps {
script {
def kubeconfig = sh(returnStdout: true, script: 'terraform output -raw kubeconfig')
writeFile file: 'kubeconfig.yaml', text: kubeconfig
sh 'export KUBECONFIG=$WORKSPACE/kubeconfig.yaml'
}
}
}
stage('Deploy Application') {
steps {
script {
sh 'kubectl apply -f your_k8s_manifest.yaml'
}
}
}
}
post {
always {
cleanWs()
}
}
}
In this Jenkinsfile
, we define the following stages:
- Checkout: Clones the repository containing Terraform files.
- Terraform Init: Initializes the Terraform environment.
- Terraform Apply: Deploys the infrastructure defined in Terraform.
-
Configure kubectl: Retrieves the Kubernetes config and sets up
kubectl
for cluster interaction. - Deploy Application: Deploys Kubernetes manifests to the cluster.
Step 3: Add Kubernetes Manifest Files
Create a your_k8s_manifest.yaml
file with Kubernetes resources, such as a Deployment and a Service.
# your_k8s_manifest.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
This file defines:
- Deployment: Deploys an NGINX application with two replicas.
- Service: Exposes the application with a LoadBalancer for external access.
Step 4: Run the Pipeline
-
Trigger the Pipeline:
- In Jenkins, go to the Pipeline job you created and click “Build Now.”
- Monitor the console output to see the progress of each stage.
-
Verify the Deployment:
- Once complete, verify that the Kubernetes cluster is running by checking the load balancer's external IP in AWS.
- Access the NGINX application using the load balancer's IP address.
Step 5: Cleanup Resources (Optional)
Once you’re done with the Kubernetes cluster, you can clean up resources by destroying the infrastructure.
Add an additional stage in the Jenkinsfile if you want automatic teardown after testing:
stage('Terraform Destroy') {
steps {
script {
sh 'terraform destroy -auto-approve'
}
}
}
Conclusion
This setup demonstrates how to deploy a Kubernetes cluster using Terraform and Jenkins. By leveraging Infrastructure as Code (IaC) and CI/CD pipelines, you create a streamlined, reproducible way to manage cloud resources and Kubernetes applications.
Automating deployment not only saves time but also reduces the likelihood of manual errors, enabling you to focus on application development and innovation. Adjust this configuration as needed for other cloud providers, security policies, or custom applications.
Posted on October 31, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.