This repository contains a demonstration of deploying and managing applications using Kubernetes (k8s). It is designed to help users understand the basics of Kubernetes, including deployments, services, scaling, and other core concepts. Whether you're new to Kubernetes or looking for a practical example to experiment with, this project provides a hands-on approach to working with k8s.
- Overview
- Prerequisites
- Getting Started
- Project Structure
- Deploying the Application
- Scaling and Managing the Application
- Cleaning Up
- Contributing
- License
This project demonstrates how to:
- Deploy a sample application to a Kubernetes cluster.
- Expose the application using Kubernetes Services.
- Scale the application using ReplicaSets.
- Manage configurations using ConfigMaps and Secrets.
- Clean up resources after deployment.
- Implement GitOps practices using ArgoCD
- Manage multiple environments (dev, staging, prod)
- Automate deployments using Helm charts
The demo application is a simple web server that serves a static webpage or API endpoint.
Before you begin, ensure you have the following installed:
- Kubernetes Cluster: You can use a local cluster like Minikube, Kind, or a cloud-based cluster (e.g., GKE, EKS, AKS).
- kubectl: The Kubernetes command-line tool. Installation guide.
- Docker: To build and manage container images. Installation guide.
-
Clone this repository:
git clone https://github.com/JawherKl/k8s-demo.git cd k8s-demo
-
Ensure your Kubernetes cluster is running:
kubectl cluster-info
-
Build the Docker image (if applicable):
docker build -t your-username/k8s-demo-app:latest .
-
Deploy the application to your cluster (see Deploying the Application).
k8s-demo/
├── manifests/ # Kubernetes YAML files
│ ├── deployment.yaml # Deployment configuration
│ ├── service.yaml # Service configuration
│ └── configmap.yaml # ConfigMap for environment variables
├── helm/ # Helm chart directory
│ └── webapp/
│ ├── Chart.yaml # Helm chart definition
│ ├── values.yaml # Default values
│ ├── values-dev.yaml # Development values
│ └── templates/ # Helm templates
├── argocd/ # ArgoCD configurations
│ └── projects/
│ └── webapp.yaml # ArgoCD application definitions
├── src/ # Application source code
├── Dockerfile # Dockerfile for the application
└── README.md # This file
-
Apply the Kubernetes manifests:
kubectl apply -f manifests/
-
Verify the deployment:
kubectl get pods kubectl get services
-
Access the application:
- If using a local cluster (e.g., Minikube), run:
minikube service <service-name>
- For cloud clusters, use the external IP or LoadBalancer endpoint provided by the service.
- If using a local cluster (e.g., Minikube), run:
-
Scale the application:
kubectl scale deployment <deployment-name> --replicas=3
-
Update the application:
- Modify the
deployment.yaml
file or other manifests. - Reapply the changes:
kubectl apply -f manifests/
- Modify the
-
View logs:
kubectl logs <pod-name>
To delete the deployed resources:
kubectl delete -f manifests/
This project uses Helm for package management and deployment. The Helm chart structure is organized as follows:
helm/webapp/
├── Chart.yaml # Chart metadata
├── values.yaml # Default values
├── values-dev.yaml # Development values
├── values-staging.yaml # Staging values
├── values-prod.yaml # Production values
└── templates/ # Helm templates
├── deployment.yaml
├── service.yaml
├── ingress.yaml
├── configmap.yaml
├── secret.yaml
├── hpa.yaml
└── _helpers.tpl
-
Install Helm (if not already installed):
curl https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
-
Add dependencies:
helm repo add bitnami https://charts.bitnami.com/bitnami helm repo update
-
Deploy to different environments:
# Development helm install webapp-dev ./helm/webapp -f helm/webapp/values-dev.yaml -n development # Staging helm install webapp-staging ./helm/webapp -f helm/webapp/values-staging.yaml -n staging # Production helm install webapp-prod ./helm/webapp -f helm/webapp/values-prod.yaml -n production
Key configurations available in values files:
webapp:
replicaCount: 1
image:
repository: nanajanashia/k8s-demo-app
tag: v1.0
pullPolicy: IfNotPresent
resources:
requests:
cpu: 250m
memory: 256Mi
limits:
cpu: 500m
memory: 512Mi
service:
type: NodePort
port: 3000
nodePort: 30100
mongodb:
enabled: true
auth:
rootPassword: mongopassword
username: mongouser
password: mongopassword
-
Upgrade a release:
helm upgrade webapp-dev ./helm/webapp -f helm/webapp/values-dev.yaml -n development
-
Roll back to a previous release:
# List release history helm history webapp-dev -n development # Roll back to a specific revision helm rollback webapp-dev 1 -n development
# Validate chart
helm lint ./helm/webapp
# See what will be installed
helm template webapp-dev ./helm/webapp -f values-dev.yaml
# List installed releases
helm list -n development
# Uninstall a release
helm uninstall webapp-dev -n development
This project uses ArgoCD for GitOps-based deployments. The setup includes:
-
Install ArgoCD:
kubectl create namespace argocd kubectl apply -n argocd -f https://raw.githubusercontent.com/argoproj/argo-cd/stable/manifests/install.yaml
-
Access ArgoCD UI:
kubectl port-forward svc/argocd-server -n argocd 8080:443
Get the initial admin password:
kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d
-
Deploy Applications:
kubectl apply -f argocd/projects/webapp.yaml
The project supports three environments:
- Development: Automatic sync with pruning enabled
- Staging: Automatic sync with pruning enabled
- Production: Manual sync for safety
Each environment is configured in:
helm/webapp/values-{env}.yaml
: Environment-specific configurationsargocd/projects/webapp.yaml
: ArgoCD application definitions
- Make changes to the Helm chart or values
- Commit and push to the repository
- ArgoCD automatically detects changes
- Changes are applied based on environment sync policies
Contributions are welcome! If you'd like to improve this project, please follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature/your-feature
). - Commit your changes (
git commit -m 'Add some feature'
). - Push to the branch (
git push origin feature/your-feature
). - Open a Pull Request.
This project is licensed under the MIT License. See the LICENSE file for details.