Configuring and Scaling the Application
Configuring the Application
Kubernetes provides various ways to configure your application, such as:
- Environment Variables: You can set environment variables in your Deployment manifest to pass configuration data to your application.
spec:
containers:
- name: my-web-app
env:
- name: DATABASE_URL
value: postgres://user:password@db/myapp
- ConfigMaps: You can use ConfigMaps to store configuration data separately from your application code, making it easier to manage and update.
apiVersion: v1
kind: ConfigMap
metadata:
name: my-web-app-config
data:
app.settings: |
DEBUG=False
ALLOWED_HOSTS=["*"]
SECRET_KEY=supersecretkey
- Secrets: Secrets are used to store sensitive information, such as database credentials or API keys, in a secure way.
apiVersion: v1
kind: Secret
metadata:
name: my-web-app-secret
type: Opaque
data:
db-password: c3VwZXJzZWNyZXRwYXNzd29yZA==
Scaling the Application
Kubernetes provides several ways to scale your application:
- Horizontal Pod Autoscaling (HPA): HPA automatically scales the number of pods based on CPU or memory utilization.
apiVersion: autoscaling/v2beta1
kind: HorizontalPodAutoscaler
metadata:
name: my-web-app
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-web-app
minReplicas: 3
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
targetAverageUtilization: 50
- Vertical Pod Autoscaling (VPA): VPA automatically adjusts the CPU and memory requests and limits of your pods based on their usage.
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: my-web-app-vpa
spec:
targetRef:
apiVersion: apps/v1
kind: Deployment
name: my-web-app
updatePolicy:
updateMode: "Auto"
- Manual Scaling: You can manually scale your application by updating the
replicas
field in your Deployment manifest and applying the changes.
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-web-app
spec:
replicas: 5
## other Deployment configuration
By using these Kubernetes features, you can ensure your web application is properly configured and can scale to handle increased traffic and load.