To create a Kubernetes configuration file that includes both a deployment of an Nginx server and a Horizontal Pod Autoscaler (HPA), you can define the necessary resources (CPU and memory) and set limits within the YAML file. Below, I'll provide an example of such a YAML file, named nginx-hpa.yaml
. This example will set up a basic Nginx deployment and configure HPA to automatically scale the number of pods based on CPU utilization.
Contents of nginx-hpa.yaml
:
vi nginx-hpa.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
resources:
requests:
cpu: "100m" # Minimum necessary CPU to run
memory: "128Mi" # Minimum necessary memory to run
limits:
cpu: "200m" # Maximum CPU allowed to use
memory: "256Mi" # Maximum memory allowed to use
---
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: nginx-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx-deployment
minReplicas: 1
maxReplicas: 5
targetCPUUtilizationPercentage: 50
Breakdown of the nginx-hpa.yaml
:
Deployment Section:
- API Version and Kind: Specifies the resource type (Deployment).
- Metadata: Defines the name and labels used to identify the deployment.
- Spec:
- Replicas: Sets the initial number of pods to 2.
- Selector and Template Labels: Ensures the deployment manages pods labeled as
app: nginx
. - Containers:
- Name and Image: Uses the latest official Nginx image from Docker Hub.
- Ports: Exposes port 80 on the container.
- Resources:
- Requests: The minimum resources necessary for running each pod.
- Limits: The maximum resources each pod is allowed to use.
Horizontal Pod Autoscaler (HPA) Section:
- API Version and Kind: Specifies the resource type (HorizontalPodAutoscaler).
- Metadata: Names the HPA (
nginx-hpa
). - Spec:
- scaleTargetRef: Points the HPA to the Nginx deployment.
- minReplicas and maxReplicas: Sets the minimum and maximum number of pods.
- targetCPUUtilizationPercentage: The target CPU utilization percentage that triggers scaling.
How to Use:
- Save the file: Copy the contents above into a file named
nginx-hpa.yaml
. - Deploy to Kubernetes:
kubectl apply -f nginx-hpa.yaml
Verify Deployment and HPA:
- Check deployment:
kubectl get deployment nginx-deployment
Check HPA:
kubectl get hpa nginx-hpa
This setup will provide an automatically scaling Nginx server deployment that adjusts the number of pods based on the CPU usage, ensuring efficient resource use based on load.
No comments:
Post a Comment