您的位置 首页 k8s

k8s pod 根据cpu 内存使用 自动扩容

Kubernetes 中的自动扩容主要通过 Horizontal Pod Autoscaler (HPA) 和 Vertical Pod Autoscaler (VPA) 来实现。以下是详细的配置方案:

## 1. Horizontal Pod Autoscaler (HPA) - 水平扩容

### 启用 Metrics Server

首先需要安装 Metrics Server 来收集资源使用指标:

```bash
# 安装 Metrics Server
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml

# 验证安装
kubectl get deployment metrics-server -n kube-system
```

如果遇到证书问题,可以编辑部署配置:
```bash
kubectl edit deployment metrics-server -n kube-system
# 添加 --kubelet-insecure-tls 参数到容器启动命令
```

### 基于 CPU 的自动扩容

```yaml
# hpa-cpu-example.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: app-hpa-cpu
namespace: default
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: your-app-deployment
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70 # 当 CPU 平均使用率达到 70% 时触发扩容
behavior:
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 10
periodSeconds: 60
scaleUp:
stabilizationWindowSeconds: 60
policies:
- type: Percent
value: 50
periodSeconds: 60
```

### 基于内存的自动扩容

```yaml
# hpa-memory-example.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: app-hpa-memory
namespace: default
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: your-app-deployment
minReplicas: 1
maxReplicas: 5
metrics:
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80 # 当内存平均使用率达到 80% 时触发扩容
```

### 基于自定义指标的自动扩容

```yaml
# hpa-custom-metrics.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: app-hpa-custom
namespace: default
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: your-app-deployment
minReplicas: 1
maxReplicas: 20
metrics:
- type: Pods
pods:
metric:
name: http_requests_per_second
target:
type: AverageValue
averageValue: "100" # 每个 Pod 处理的请求数超过 100 时扩容
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60
```

### 多指标组合扩容

```yaml
# hpa-multi-metrics.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: app-hpa-multi
namespace: default
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: your-app-deployment
minReplicas: 2
maxReplicas: 15
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
- type: External
external:
metric:
name: queue_length
target:
type: Value
value: "30"
behavior:
scaleDown:
stabilizationWindowSeconds: 300
selectPolicy: Min
policies:
- type: Percent
value: 10
periodSeconds: 60
- type: Pods
value: 2
periodSeconds: 60
scaleUp:
stabilizationWindowSeconds: 60
selectPolicy: Max
policies:
- type: Percent
value: 100
periodSeconds: 15
- type: Pods
value: 4
periodSeconds: 15
```

## 2. Vertical Pod Autoscaler (VPA) - 垂直扩容

### 安装 VPA

```bash
# 克隆 VPA 仓库
git clone https://github.com/kubernetes/autoscaler.git
cd autoscaler/vertical-pod-autoscaler

# 安装 VPA
./hack/vpa-up.sh
```

### VPA 配置示例

```yaml
# vpa-example.yaml
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: app-vpa
namespace: default
spec:
targetRef:
apiVersion: "apps/v1"
kind: Deployment
name: your-app-deployment
updatePolicy:
updateMode: "Auto" # Auto: 自动更新, Initial: 首次更新, Off: 不更新
resourcePolicy:
containerPolicies:
- containerName: app-container
maxAllowed:
cpu: 2
memory: 2Gi
minAllowed:
cpu: 100m
memory: 128Mi
```

### VPA 更新模式

```yaml
# vpa-off-mode.yaml - 仅推荐模式,不自动更新
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
name: app-vpa-recommend
namespace: default
spec:
targetRef:
apiVersion: "apps/v1"
kind: Deployment
name: your-app-deployment
updatePolicy:
updateMode: "Off" # 仅提供推荐值,不自动更新
```

## 3. 完整应用示例

### 应用部署配置

```yaml
# app-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
namespace: default
labels:
app: web-app
spec:
replicas: 2
selector:
matchLabels:
app: web-app
template:
metadata:
labels:
app: web-app
spec:
containers:
- name: web-container
image: nginx:latest
ports:
- containerPort: 80
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 512Mi
---
apiVersion: v1
kind: Service
metadata:
name: web-app-service
namespace: default
spec:
selector:
app: web-app
ports:
- port: 80
targetPort: 80
type: ClusterIP
```

### HPA 配置

```yaml
# app-hpa.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: web-app-hpa
namespace: default
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: web-app
minReplicas: 2
maxReplicas: 20
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 60
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 75
behavior:
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 10
periodSeconds: 60
scaleUp:
stabilizationWindowSeconds: 60
policies:
- type: Percent
value: 50
periodSeconds: 60
- type: Pods
value: 2
periodSeconds: 60
```

## 4. 监控和调试

### 查看 HPA 状态

```bash
# 查看 HPA 状态
kubectl get hpa

# 查看详细信息
kubectl describe hpa web-app-hpa

# 查看 Metrics Server 指标
kubectl top nodes
kubectl top pods
```

### 查看 VPA 状态

```bash
# 查看 VPA 状态
kubectl get vpa

# 查看详细信息
kubectl describe vpa app-vpa

# 查看推荐资源
kubectl get vparecommendation
```

### 自定义指标监控

如果使用自定义指标,需要安装 Prometheus Adapter:

```yaml
# prometheus-adapter-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: adapter-config
namespace: custom-metrics
data:
config.yaml: |
rules:
- seriesQuery: 'http_requests_total{namespace!="",pod!=""}'
resources:
overrides:
namespace: {resource: "namespace"}
pod: {resource: "pod"}
name:
matches: "^(.*)_total"
as: "${1}_per_second"
metricsQuery: 'sum(rate(<<.Series>>{<<.LabelMatchers>>}[2m])) by (<<.GroupBy>>)'
```

## 5. 最佳实践

### HPA 配置建议

```yaml
# production-hpa.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: production-app-hpa
namespace: production
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: production-app
minReplicas: 3
maxReplicas: 50
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 65
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
behavior:
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 10
periodSeconds: 120
- type: Pods
value: 2
periodSeconds: 120
scaleUp:
stabilizationWindowSeconds: 60
policies:
- type: Percent
value: 50
periodSeconds: 60
- type: Pods
value: 5
periodSeconds: 60
selectPolicy: Max
```

### 注意事项

1. **资源请求设置**:确保 Pod 设置了合理的资源 requests
2. **监控延迟**:Metrics Server 有约 15-60 秒的延迟
3. **冷却时间**:设置合适的 scaleUp 和 scaleDown 行为
4. **最大副本数**:根据集群资源合理设置 maxReplicas
5. **指标选择**:选择最能反映应用负载的指标

通过这些配置,您可以实现基于 CPU 和内存使用率的自动扩容,确保应用在不同负载下都能获得合适的资源分配。

欢迎来撩 : 汇总all

白眉大叔

关于白眉大叔linux云计算: 白眉大叔

热门文章