Skip to main content

Chapter 12.3 - Environment Management

Learning Objectives​

By the end of this chapter, you will be able to:

  • Understand environment management strategies
  • Use namespaces to isolate environments
  • Manage separate clusters
  • Configure per-environment values
  • Implement promotion between environments
  • Apply best practices

Introduction​

Environment management allows separating development, staging, and production for better stability and security.


Strategies​

1. Namespaces per Environment​

Advantages:

  • Simple to manage
  • Resource sharing
  • Cost-effective

Disadvantages:

  • Limited isolation
  • Risk of conflicts

2. Separate Clusters​

Advantages:

  • Complete isolation
  • Maximum security
  • No conflicts

Disadvantages:

  • More complex
  • Higher cost

Namespaces per Environment​

Creation​

# Create namespaces
kubectl create namespace dev
kubectl create namespace staging
kubectl create namespace production

# Add labels
kubectl label namespace dev environment=development
kubectl label namespace staging environment=staging
kubectl label namespace production environment=production

Deployment​

# Deploy to each environment
kubectl apply -f deployment.yaml -n dev
kubectl apply -f deployment.yaml -n staging
kubectl apply -f deployment.yaml -n production

Resource Quotas per Environment​

# dev-quota.yaml
apiVersion: v1
kind: ResourceQuota
metadata:
name: dev-quota
namespace: dev
spec:
hard:
requests.cpu: "5"
requests.memory: 10Gi
limits.cpu: "10"
limits.memory: 20Gi

Separate Clusters​

Architecture​

Advantages:

  • Complete isolation
  • Maximum security
  • No risk of contamination

Per-Environment Configuration​

Values Files​

# values-dev.yaml
replicaCount: 1
image:
tag: "dev"
resources:
requests:
cpu: 100m
memory: 128Mi

# values-staging.yaml
replicaCount: 2
image:
tag: "staging"
resources:
requests:
cpu: 250m
memory: 256Mi

# values-prod.yaml
replicaCount: 5
image:
tag: "1.0.0"
resources:
requests:
cpu: 500m
memory: 512Mi

Deployment with Helm​

# Dev
helm install my-app ./my-chart -f values-dev.yaml -n dev

# Staging
helm install my-app ./my-chart -f values-staging.yaml -n staging

# Production
helm install my-app ./my-chart -f values-prod.yaml -n production

Promotion Between Environments​

Process​

Automation​

# GitHub Actions workflow
name: Promote to Production
on:
workflow_dispatch:
inputs:
version:
description: 'Version to promote'
required: true

jobs:
promote:
runs-on: ubuntu-latest
steps:
- name: Deploy to Production
run: |
helm upgrade my-app ./my-chart \
-f values-prod.yaml \
--set image.tag=${{ github.event.inputs.version }} \
-n production

Best Practices​

1. Clear Separation​

Maintain a clear separation between environments.

2. Externalized Configuration​

Use ConfigMaps and Secrets for configuration.

3. Automated Tests​

Run tests in each environment.

4. Documentation​

Document the differences between environments.

5. Restricted Access​

Limit access to production with RBAC.


Summary​

In this chapter, you learned:

Strategies: Namespaces or separate clusters
Namespaces: Simple, cost-effective, limited isolation
Clusters: Complete isolation, more complex
Configuration: Values files per environment
Promotion: Process for promoting between environments
Best practices: Separation, externalized configuration, tests


Next Steps​

Lab 12.4: Complete Final Project
Course Recap


Chapter created on: December 2024