Deploy Kubernetes definitions
This document provides information about deploying and updating the microservices application on K0s, a lightweight Kubernetes distribution.
Overview
Namespace: A dedicated namespace
microservices
for all resourcesConfigMap: For non-sensitive configuration values
Secret: For sensitive configuration values (passwords, API keys, etc.)
PersistentVolumes and PersistentVolumeClaims: For database storage
Deployments and Services: For each microservice
Ingress: For external access to the gateway service
Prerequisites
TLS certificate for the domain (stored as a Kubernetes Secret)
Directory Structure
namespace.yaml
: Defines the microservices namespaceconfigmap.yaml
: Contains non-sensitive configurationsecrets.yaml
: Contains sensitive configuration (passwords, API keys)storage.yaml
: Defines PersistentVolumes and PersistentVolumeClaims for databasesregistry-service.yaml
: Service registry (Eureka)zipkin.yaml
: Distributed tracing (Zipkin, Zipkin Storage, Zipkin Dependencies)monitoring.yaml
: Monitoring tools (Prometheus, Grafana)traefik-middleware.yaml
: Defines Traefik middleware for HTTP to HTTPS redirectionauthentication-db.yaml
: Authentication database (MariaDB)usermanagement-db.yaml
: User Management database (MariaDB)chess-db.yaml
: Chess database (MariaDB)fitness-db.yaml
: Fitness database (MariaDB)music-db.yaml
: Music database (MariaDB)admin-service.yaml
: Admin dashboardauthentication-service.yaml
: Authentication serviceusermanagement-service.yaml
: User management servicegateway-service.yaml
: API gateway with Ingress configurationwebsite-service.yaml
: Website servicechess-service.yaml
: Chess servicefitness-service.yaml
: Fitness servicemusic-service.yaml
: Music service
Deployment Instructions
Prepare metallb:
Create the namespace:
kubectl apply -f namespace.yamlCreate local-storage clas
kubectl apply -n microservices -f local-storageclass.yamlCreate ConfigMap and Secret:
kubectl apply -n microservices -f configmap.yaml kubectl apply -n microservices -f secrets.yamlCreate storage resources:
kubectl apply -n microservices -f storage.yamlDeploy infrastructure services:
kubectl apply -n microservices -f zipkin.yaml kubectl apply -n microservices -f registry-service.yamlDeploy database services:
kubectl apply -n microservices -f authentication-db.yaml kubectl apply -n microservices -f usermanagement-db.yaml kubectl apply -n microservices -f chess-db.yaml kubectl apply -n microservices -f fitness-db.yaml kubectl apply -n microservices -f music-db.yamlDeploy application services:
kubectl apply -n microservices -f admin-service.yaml kubectl apply -n microservices -f authentication-service.yaml kubectl apply -n microservices -f usermanagement-service.yaml kubectl apply -n microservices -f website-service.yaml kubectl apply -n microservices -f chess-service.yaml kubectl apply -n microservices -f fitness-service.yaml kubectl apply -n microservices -f music-service.yaml
Deploy gateway service with Ingress:
TLS Certificate
Before deploying the gateway service, ensure you have created a TLS Secret named
michibaum-tls
in themicroservices
namespace:k0s kubectl create secret tls michibaum-tls --cert=/data/ssl/fullchain.pem --key=/data/ssl/privkey.pem -n microserviceskubectl apply -n microservices -f gateway-service.yamlDeploy DB backups
kubectl apply -n microservices -f db-backup.yaml
Ingress Configuration
Notes
The configuration uses hostPath volumes for simplicity. In a production environment, you should use a more robust storage solution like a cloud provider's persistent disk.
Secrets contain placeholder values. Replace them with actual values before deployment.
Port Forward
Updating the Kubernetes Deployment
Update Types
Updates to the microservices application can be categorized into several types:
Service Image Updates: Deploying new versions of service containers
Configuration Updates: Changes to ConfigMaps or Secrets
Database Schema Changes: Updates that require database migrations
Infrastructure Updates: Changes to the underlying Kubernetes resources
Updating Service Images
Using Specific Tags
While the current configuration uses the latest
tag, it's recommended to use specific version tags for production deployments. This allows for better control over which version is deployed and makes rollbacks easier.
To update a service image:
Update the image tag in the service's deployment YAML file:
Apply the updated deployment:
Using kubectl set image
For quick updates without modifying YAML files, you can use the kubectl set image
command:
Monitoring the Update
Monitor the rollout status to ensure the update is proceeding as expected:
Updating Configuration
ConfigMap Updates
To update configuration in ConfigMaps:
Edit the
configmap.yaml
file with the new configuration valuesApply the updated ConfigMap:
Restart the affected deployments to pick up the new configuration:
Secret Updates
To update sensitive configuration in Secrets:
Edit the
secrets.yaml
file with the new secret valuesApply the updated Secret:
Restart the affected deployments:
Database Schema Changes
Database schema changes require careful handling to avoid data loss or service disruptions.
Recommended Approach
Ensure Backward Compatibility: Design schema changes to be backward compatible when possible
Create a Migration Plan: Document the steps required for the migration
Backup Data: Always backup the database before schema changes
Implementation Steps
Create a database backup:
Apply schema changes using a migration tool or script
Update the application to use the new schema
Verify the application works correctly with the new schema
If issues occur, be prepared to rollback to the previous schema
Rolling Updates
Kubernetes performs rolling updates by default when you apply changes to a deployment. This ensures that there is no downtime during the update process.
Customizing Rolling Update Strategy
You can customize the rolling update strategy in the deployment YAML:
maxUnavailable
: Maximum number of pods that can be unavailable during the updatemaxSurge
: Maximum number of pods that can be created over the desired number of pods
Canary Deployments
For critical updates, consider using a canary deployment approach:
Deploy a single instance of the new version alongside the existing version
Route a small percentage of traffic to the new version
Monitor for any issues
Gradually increase traffic to the new version
Once confident, complete the rollout of the new version
Rollback Procedures
If an update causes issues, you can rollback to a previous version.
Rolling Back a Deployment
To rollback to a specific revision:
Rolling Back Configuration Changes
If a ConfigMap or Secret update causes issues:
Reapply the previous version of the ConfigMap or Secret
Restart the affected deployments:
Best Practices
Use Specific Image Tags: Avoid using
latest
tag in productionTest Updates in Staging: Always test updates in a staging environment before applying to production
Implement Health Checks: Ensure all services have proper readiness and liveness probes
Monitor During Updates: Closely monitor application metrics and logs during updates
Automate Updates: Use CI/CD pipelines to automate the update process
Document Changes: Keep a changelog of all updates applied to the cluster
Regular Backups: Maintain regular backups of all databases and configuration
Update Strategy: Define an update strategy for each service based on its criticality
Communication: Inform all stakeholders before performing updates that might affect service availability