Kubernetes and CI/CD: The Complete Beginner’s Guide
Kubernetes and CI/CD: The Complete Beginner’s Guide to Container Orchestration
☸️ What is Kubernetes? Why It’s a Game-Changer
In the evolving world of DevOps and cloud-native applications, Kubernetes has emerged as a revolutionary tool that transforms how we deploy and manage applications. Kubernetes, also known as K8s, is an open-source container orchestration platform originally developed by Google. It automates the deployment, scaling, and operation of application containers across clusters of hosts.
Why Kubernetes?
- Scalability: Automatically scale applications up or down based on traffic or load.
- Self-Healing: Detects and replaces failed containers, ensuring high availability.
- Portability: Works across public cloud, private cloud, and on-premise environments.
- Resource Optimization: Efficiently utilizes system resources by packing containers effectively.
- Declarative Configuration: Define desired state using YAML files, and Kubernetes ensures the actual state matches it.
Real-World Example:
Imagine running an online learning platform. During exam season, the number of users spikes. Kubernetes can automatically scale up backend services to handle the load, then scale them down when traffic decreases, saving resources and cost.
⚙️ Kubernetes Architecture and Key Concepts (Pods, Services, Deployments)
It has a master-worker architecture:
- Control Plane (Master): Manages the cluster, schedules workloads, handles API requests.
- Node: A worker machine where containers run, managed by the control plane.
1. Pod
- A Pod is the smallest deployable unit.
- Contains one or more tightly coupled containers.
- Shares the same network namespace and storage.
2. Service
- Abstracts access to a group of Pods.
- ClusterIP: Default, internal access only.
- NodePort: Exposes app on static port on each node.
- LoadBalancer: Integrates with cloud load balancer for external access.
3. Deployment
- Defines how to deploy and manage Pods.
- Supports zero-downtime rolling updates.
- Enables version control and rollback.
Additional Concepts:
- ReplicaSet: Ensures a specified number of Pods are running.
- Ingress: Manages external HTTP/HTTPS access to services.
- Namespace: Organizes resources within the cluster.
Real-World Scenario:
A food delivery app might use multiple Deployments: one for APIs, another for background jobs, and another for payment processing—all isolated yet coordinated.
Also Read,
DevOps Tools & Technologies: From Beginner to Intermediate |
📦 Deploying Your First App to Kubernetes
Prerequisites:
- Install Minikube (local Kubernetes)
- Install kubectl (Kubernetes CLI)
Step-by-Step Guide:
Step-1: Start Minikube
1 |
minikube start |
Step-2: Create a Deployment
1 |
kubectl create deployment hello-world --image=nginx |
Step-3: Expose the Deployment
1 |
kubectl expose deployment hello-world --type=NodePort --port=80 |
Step-4: Access the App
1 |
minikube service hello-world |
Tips:
- Use
kubectl get pods
to check Pod status. - Use
kubectl describe pod <pod-name>
for logs and debugging. - Use
kubectl delete
to clean up resources.
📋 Kubernetes YAML Files – Explained Simply
YAML files allow you to declare your application infrastructure.
Anatomy of a Deployment YAML
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
apiVersion: apps/v1 kind: Deployment metadata: name: web-app spec: replicas: 2 selector: matchLabels: app: web template: metadata: labels: app: web spec: containers: - name: nginx-container image: nginx:latest ports: - containerPort: 80 |
Anatomy of a Service YAML
1 2 3 4 5 6 7 8 9 10 11 |
apiVersion: v1 kind: Service metadata: name: web-service spec: selector: app: web ports: - port: 80 targetPort: 80 type: NodePort |
Why YAML?
- Human-readable and version-controlled.
- Easily shareable and reusable.
- Declarative — tells Kubernetes what to do, not how.
Tips:
- Validate YAML with online linters.
- Use ConfigMaps and Secrets to decouple config.
- Use Helm for managing complex apps.
🔄 CI/CD Pipeline with Jenkins and Kubernetes
Continuous Integration (CI) and Continuous Deployment (CD) automate the process of building, testing, and deploying applications.
Jenkins Overview:
- Jenkins is an open-source automation server.
- Works well with Kubernetes to automate build, test, and deployment pipelines.
Setup:
- Install Jenkins on Kubernetes using Helm or a YAML file.
- Configure webhooks from GitHub/GitLab.
- Use Jenkins agents inside Kubernetes.
CI/CD Flow:
- Developer pushes code to GitHub.
- Jenkins triggers build via webhook.
- Jenkins builds Docker image and pushes it to Docker Hub.
- Jenkins applies updated Kubernetes YAML using
kubectl
.
Jenkinsfile Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
pipeline { agent any stages { stage('Build') { steps { sh 'docker build -t user/app .' } } stage('Push') { steps { withCredentials([usernamePassword(credentialsId: 'dockerhub', usernameVariable: 'USER', passwordVariable: 'PASS')]) { sh 'docker login -u $USER -p $PASS' sh 'docker push user/app' } } } stage('Deploy') { steps { sh 'kubectl apply -f k8s/deployment.yaml' } } } } |
Real-World Use Case:
An e-commerce company uses Jenkins to automate testing and roll out new features in minutes using it.
🔐 Secrets and ConfigMaps in Kubernetes
Secure and manage configuration outside your application code.
ConfigMaps:
- Stores non-sensitive data like environment variables or config files.
1 2 3 4 5 6 7 |
apiVersion: v1 kind: ConfigMap metadata: name: app-config data: LOG_LEVEL: info MODE: production |
Secrets:
- Encodes sensitive data like passwords, API keys.
1 2 3 4 5 6 7 8 |
apiVersion: v1 kind: Secret metadata: name: db-secret type: Opaque data: username: YWRtaW4= password: cGFzc3dvcmQ= |
How to Use in Pods:
1 2 3 4 5 6 |
env: - name: DB_USER valueFrom: secretKeyRef: name: db-secret key: username |
Best Practices:
- Don’t hardcode secrets.
- Use RBAC to restrict access.
- Regularly rotate credentials.
Conclusion
Kubernetes and CI/CD offer a robust, scalable, and automated way to build and manage modern applications. From setting up a cluster, understanding YAML, managing secrets, to deploying through Jenkins pipelines — you’ve now seen the full lifecycle of a cloud-native application.
Embrace these tools to become proficient in deploying reliable and maintainable software at scale.
📤 Stay Updated with NextGen Careers Hub
📱 Follow us on Instagram
📺 Subscribe us on YouTube
Please share our website with others: NextGenCareersHub.in
Comments are closed.