https://cloudresumechallenge.dev/docs/extensions/kubernetes-challenge/
Imagine you are going to deploy an e-commerce website. It's crucial to consider the challenges of modern web application deployment and how containerization and Kubernetes (K8s) offer compelling solutions:
- Scalability: How can your application automatically adjust to fluctuating traffic?
- Consistency: How do you ensure your application runs the same across all environments?
- Availability: How can you update your application with zero downtime?
Containerization, using Docker, encapsulates your application and its environment, ensuring it runs consistently everywhere. Kubernetes, a container orchestration platform, automates deployment, scaling, and management, offering:
- Dynamic Scaling: Adjusts application resources based on demand
- Self-healing: Restarts failed containers and reschedules them on healthy nodes
- Seamless Updates & Rollbacks: Enables zero-downtime updates and easy rollbacks
By leveraging Kubernetes and containerization for your challenge, you embrace a scalable, consistent, and resilient deployment strategy. This not only demonstrates your technical acumen but aligns with modern DevOps practices.
Dive into KodeKloud's Docker Learning Path and Kubernetes Learning Path by KodeKloud to master these technologies, enhancing your project and preparing you for a future in cloud computing and DevOps.
Before you embark on this journey, ensure you are equipped with:
- Docker and Kubernetes CLI Tools: Essential for building, pushing Docker images, and managing Kubernetes resources
- Cloud Provider Account: Access to AWS, Azure, or GCP for creating a Kubernetes cluster
- GitHub Account: For version control and implementing CI/CD pipelines
- Kubernetes Crash Course: This free course from KodeKloud contains a number of helpful labs to get you familiar with K8s basics
- E-commerce Application Source Code and DB Scripts: Available at kodekloudhub/learning-app-ecommerce. Familiarize yourself with the application structure and database scripts provided
KodeKloud CKAD Course: To ensure you have a solid understanding of Kubernetes concepts and practical experience, complete the Certified Kubernetes Application Developer (CKAD) course by KodeKloud. This course will equip you with the knowledge and skills needed to tackle this challenge effectively.
-
Create a Dockerfile: Navigate to the root of the e-commerce application and create a Dockerfile. This file should instruct Docker to:
- Use php:7.4-apache as the base image
- Install mysqli extension for PHP
- Copy the application source code to /var/www/html/
- Update database connection strings to point to a Kubernetes service named mysql-service
- Expose port 80 to allow traffic to the web server
-
Build and Push the Docker Image:
- Execute
docker build -t yourdockerhubusername/ecom-web:v1 .
to build your image - Push it to Docker Hub with
docker push yourdockerhubusername/ecom-web:v1
- Outcome: Your web application Docker image is now available on Docker Hub
- Execute
Database Preparation: Instead of containerizing the database yourself, you'll use the official MariaDB image. Prepare the database initialization script (db-load-script.sql) to be used with Kubernetes ConfigMaps or as an entrypoint script.
- Cluster Creation: Choose AWS (EKS), Azure (AKS), or GCP (GKE) and follow their documentation to create a Kubernetes cluster. Ensure you have kubectl configured to interact with your cluster.
- Outcome: A fully operational Kubernetes cluster ready for deployment.
- Kubernetes Deployment: Create a website-deployment.yaml defining a Deployment that uses the Docker image created in Step 1A. Ensure the Deployment specifies the necessary environment variables and mounts for the database connection.
- Outcome: The e-commerce web application is running on Kubernetes, with pods managed by the Deployment.
- Service Creation: Define a website-service.yaml to create a Service of type LoadBalancer. This Service exposes your Deployment to the internet.
- Outcome: An accessible URL or IP address for your web application.
Task: Add a feature toggle to the web application to enable a "dark mode" for the website.
- Modify the Web Application: Add a simple feature toggle in the application code (e.g., an environment variable FEATURE_DARK_MODE that enables a CSS dark theme)
- Use ConfigMaps: Create a ConfigMap named feature-toggle-config with the data FEATURE_DARK_MODE=true
- Deploy ConfigMap: Apply the ConfigMap to your Kubernetes cluster
- Update Deployment: Modify the website-deployment.yaml to include the environment variable from the ConfigMap
- Outcome: Your website should now render in dark mode, demonstrating how ConfigMaps manage application features
Task: Prepare for a marketing campaign expected to triple traffic.
- Evaluate Current Load: Use
kubectl get pods
to assess the current number of running pods - Scale Up: Increase replicas in your deployment or use
kubectl scale deployment/ecom-web --replicas=6
to handle the increased load - Monitor Scaling: Observe the deployment scaling up with
kubectl get pods
- Outcome: The application scales up to handle increased traffic, showcasing Kubernetes' ability to manage application scalability dynamically
Task: Update the website to include a new promotional banner for the marketing campaign.
- Update Application: Modify the web application's code to include the promotional banner
- Build and Push New Image: Build the updated Docker image as yourdockerhubusername/ecom-web:v2 and push it to Docker Hub
- Rolling Update: Update website-deployment.yaml with the new image version and apply the changes
- Monitor Update: Use
kubectl rollout status deployment/ecom-web
to watch the rolling update process - Outcome: The website updates with zero downtime, demonstrating rolling updates' effectiveness in maintaining service availability
Task: Suppose the new banner introduced a bug. Roll back to the previous version.
- Identify Issue: After deployment, monitoring tools indicate a problem affecting user experience
- Roll Back: Execute
kubectl rollout undo deployment/ecom-web
to revert to the previous deployment state - Verify Rollback: Ensure the website returns to its pre-update state without the promotional banner
- Outcome: The application's stability is quickly restored, highlighting the importance of rollbacks in deployment strategies
Task: Automate scaling based on CPU usage to handle unpredictable traffic spikes.
- Implement HPA: Create a Horizontal Pod Autoscaler targeting 50% CPU utilization, with a minimum of 2 and a maximum of 10 pods
- Apply HPA: Execute
kubectl autoscale deployment ecom-web --cpu-percent=50 --min=2 --max=10
- Simulate Load: Use a tool like Apache Bench to generate traffic and increase CPU load
- Monitor Autoscaling: Observe the HPA in action with
kubectl get hpa
- Outcome: The deployment automatically adjusts the number of pods based on CPU load, showcasing Kubernetes' capability to maintain performance under varying loads
Task: Ensure the web application is restarted if it becomes unresponsive and doesn't receive traffic until ready.
- Define Probes: Add liveness and readiness probes to website-deployment.yaml, targeting an endpoint in your application that confirms its operational status
- Apply Changes: Update your deployment with the new configuration
- Test Probes: Simulate failure scenarios (e.g., manually stopping the application) and observe Kubernetes' response
- Outcome: Kubernetes automatically restarts unresponsive pods and delays traffic to newly started pods until they're ready, enhancing the application's reliability and availability
Task: Securely manage the database connection string and feature toggles without hardcoding them in the application.
- Create Secret and ConfigMap: For sensitive data like DB credentials, use a Secret. For non-sensitive data like feature toggles, use a ConfigMap
- Update Deployment: Reference the Secret and ConfigMap in the deployment to inject these values into the application environment
- Outcome: Application configuration is externalized and securely managed, demonstrating best practices in configuration and secret management
- Finalize Your Project Code: Ensure your project is complete and functioning as expected. Test all features locally and document all dependencies clearly
- Create a Git Repository: Create a new repository on your preferred git hosting service (e.g., GitHub, GitLab, Bitbucket)
- Push Your Code to the Remote Repository
- Write Documentation: Create a README.md or a blog post detailing each step, decisions made, and how challenges were overcome
Task: Utilize Helm to package your application, making deployment and management on Kubernetes clusters more efficient and scalable.
- Create Helm Chart: Start by creating a Helm chart for your application. This involves setting up a chart directory with the necessary templates for your Kubernetes resources
- Define Values: Customize your application deployment by defining variables in the values.yaml file. This allows for flexibility and reusability of your Helm chart across different environments or configurations
- Package and Deploy: Use Helm commands to package your application into a chart and deploy it to your Kubernetes cluster. Ensure to test your chart to verify that all components are correctly configured and working as expected
- Outcome: Your application is now packaged as a Helm chart, simplifying deployment processes and enabling easy versioning and rollback capabilities
For more details, follow KodeKloud Helm Course.
Task: Ensure data persistence for the MariaDB database across pod restarts and redeployments.
- Create a PVC: Define a PersistentVolumeClaim for MariaDB storage needs
- Update MariaDB Deployment: Modify the deployment to use the PVC for storing database data
- Outcome: Database data persists beyond the lifecycle of MariaDB pods, ensuring data durability
Task: Automate the build and deployment process using GitHub Actions.
- GitHub Actions Workflow: Create a .github/workflows/deploy.yml file to build the Docker image, push it to Docker Hub, and update the Kubernetes deployment upon push to the main branch
- Outcome: Changes to the application are automatically built and deployed, showcasing an efficient CI/CD pipeline