Automating Kubernetes Pod Deployment and Exposure Using Jenkins
In a DevOps pipeline, managing Kubernetes deployments efficiently is critical, and Jenkins offers a great way to automate these tasks. In this blog, we’ll set up a Jenkins job that automates the deployment of a Kubernetes pod and exposes it to the network. This approach simplifies the CI/CD process, providing an efficient and scalable workflow for deploying containerized applications.
Prerequisites
- Jenkins Installed: Jenkins should be installed and accessible. Ensure you have administrator privileges.
- Kubernetes Cluster: A Kubernetes cluster (local or cloud) with access configured.
- kubectl Configured on Jenkins Server: Ensure the
kubectl
CLI tool is configured on the Jenkins server and that Jenkins has permissions to runkubectl
commands. - Kubernetes Plugin for Jenkins: The Kubernetes plugin is useful for connecting Jenkins and Kubernetes seamlessly.
Step-by-Step Guide to Create a Jenkins Job for Kubernetes Pod Deployment
Step 1: Install the Required Jenkins Plugins
- Navigate to Manage Jenkins → Manage Plugins.
- In the “Available” tab, search for Kubernetes CLI and install it.
- Restart Jenkins to activate the plugin.
Step 2: Create a New Jenkins Job
- From the Jenkins dashboard, click on New Item.
- Name the job (e.g., “K8s-Pod-Deployment”) and select Freestyle Project, then click OK.
- In the configuration screen, scroll to the Build Environment section and check Use Kubernetes CLI Plugin if available. Otherwise, we will use a shell script to execute
kubectl
commands.
Step 3: Set Up Build Steps for Kubernetes Pod Deployment
- Scroll down to the Build section and click on
Add Build Step
→Execute shell
. - In the shell command box, add the following script to deploy a pod to Kubernetes.
#!/bin/bash
# Define the pod configuration in YAML format
cat <<EOF > my-k8s-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: my-k8s-pod
labels:
app: my-app
spec:
containers:
- name: nginx-container
image: nginx
ports:
- containerPort: 80
EOF
# Apply the pod configuration
echo "Deploying the Kubernetes Pod..."
kubectl apply -f my-k8s-pod.yaml
# Check if the pod is running
kubectl get pods -l app=my-app
This script creates a basic YAML file to define a pod configuration and applies it to the Kubernetes cluster. The configuration includes:
- Pod name:
my-k8s-pod
- Container: Runs the
nginx
image. - Port: Exposes port 80 within the pod.
Step 4: Expose the Pod as a Service
To make the pod accessible outside the cluster, expose it as a Kubernetes service.
- Append the following code to the previous shell script to create a service.
# Define the service configuration in YAML format
cat <<EOF > my-k8s-service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-k8s-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
EOF
# Apply the service configuration
echo "Exposing the Kubernetes Pod via Service..."
kubectl apply -f my-k8s-service.yaml
# Confirm the service is created and running
kubectl get services my-k8s-service
This configuration will:
- Expose the pod as a LoadBalancer service (use NodePort if running locally).
- Route requests sent to the service on port 80 to the pod’s container port 80.
Step 5: Save and Run the Job
- Click Save to store your job configuration.
- Now, go back to the project’s main page and click Build Now to execute the job.
Step 6: Monitor the Jenkins Job Output
Click on Console Output to monitor the job’s progress. You should see outputs confirming the following:
- The pod configuration is created and applied.
- The service is set up and available with an external IP if using a LoadBalancer type.
After a successful run, your application will be accessible via the IP and port provided by the kubectl get services
command.
Step 7: Clean Up Resources (Optional)
For development purposes, it’s good practice to delete resources once you’re done. Add the following optional script to the end of the shell command to clean up:
# Clean up resources
echo "Cleaning up Kubernetes resources..."
kubectl delete -f my-k8s-pod.yaml
kubectl delete -f my-k8s-service.yaml
This will delete the pod and service configurations once they are no longer needed.
Extending This Setup with Jenkins Pipelines
This job can be extended by creating a Jenkins pipeline using a Jenkinsfile. This approach is especially useful for complex multi-stage deployment processes and can help create reusable and scalable CI/CD pipelines.
Benefits of Using Jenkins for Kubernetes Deployments
- Automation: Jenkins automates Kubernetes deployments, minimizing human error and speeding up release cycles.
- Consistency: Automated deployments ensure consistent configurations across environments.
- Scalability: Jenkins pipelines make it easy to scale deployments and manage environments.
Conclusion
By creating this Jenkins job, you’ve automated Kubernetes pod deployment and exposure, which is essential for streamlined and efficient CI/CD workflows. Jenkins provides a flexible, reliable way to manage Kubernetes resources, making it an indispensable tool for modern DevOps teams.