在 Kubernetes 1.23 中,可以使用 Horizontal Pod Autoscaler(HPA)来实现 Pod 的自动扩容和缩容。HPA 根据 CPU 使用率或其他指标(例如内存使用率或自定义指标)自动调整副本数量。以下是如何实现 Pod 自动扩容和缩容的步骤:
1. 安装 Metrics Server
首先,确保你已经安装了 Metrics Server,这是 HPA 的依赖组件,用于收集资源使用数据。
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
2. 创建 Deployment
创建一个 Deployment,用于演示自动扩容和缩容。
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
resources:
requests:
cpu: "100m"
memory: "128Mi"
limits:
cpu: "500m"
memory: "512Mi"
保存上述 YAML 文件为 nginx-deployment.yaml
,然后应用它:</