本文目录导读:

我来详细说明 PHP 应用的 HPA(Horizontal Pod Autoscaler)配置方法。
基础 HPA 配置
CPU 使用率 HPA
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: php-app-hpa
namespace: production
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
内存使用率 HPA
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: php-app-memory-hpa
namespace: production
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
综合指标 HPA
多指标配置
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: php-app-comprehensive-hpa
namespace: production
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
behavior:
scaleUp:
stabilizationWindowSeconds: 60
policies:
- type: Percent
value: 100
periodSeconds: 60
- type: Pods
value: 4
periodSeconds: 60
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 20
periodSeconds: 60
基于自定义指标
Prometheus Adapter 配置
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: php-app-custom-hpa
namespace: production
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Pods
pods:
metric:
name: php_requests_per_second
target:
type: AverageValue
averageValue: 100
- type: Pods
pods:
metric:
name: php_response_time_ms
target:
type: AverageValue
averageValue: 200
部署配置优化
Deployment 资源请求
apiVersion: apps/v1
kind: Deployment
metadata:
name: php-app
namespace: production
spec:
replicas: 2
selector:
matchLabels:
app: php-app
template:
metadata:
labels:
app: php-app
spec:
containers:
- name: php-app
image: php:8.2-fpm
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 512Mi
readinessProbe:
httpGet:
path: /health
port: 9000
initialDelaySeconds: 5
periodSeconds: 10
livenessProbe:
httpGet:
path: /health
port: 9000
initialDelaySeconds: 15
periodSeconds: 20
PHP-FPM 配置优化
; php-fpm.conf pm = dynamic pm.max_children = 50 pm.start_servers = 5 pm.min_spare_servers = 5 pm.max_spare_servers = 35 pm.max_requests = 500 ; 调优参数 pm.status_path = /status ping.path = /ping
Nginx 配置并发优化
# nginx.conf
upstream php-backend {
server php-service:9000;
keepalive 32;
}
server {
listen 80;
server_name example.com;
location ~ \.php$ {
fastcgi_pass php-backend;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# 增加超时设置
fastcgi_connect_timeout 5s;
fastcgi_read_timeout 30s;
fastcgi_send_timeout 30s;
# 缓冲优化
fastcgi_buffers 16 16k;
fastcgi_buffer_size 32k;
}
}
完整部署示例
# php-hpa-complete.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: php-config
data:
php.ini: |
memory_limit = 256M
max_execution_time = 30
opcache.enable = 1
opcache.memory_consumption = 128
opcache.max_accelerated_files = 4000
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: php-app
namespace: production
spec:
replicas: 2
selector:
matchLabels:
app: php-app
template:
metadata:
labels:
app: php-app
spec:
containers:
- name: php-app
image: php:8.2-fpm
ports:
- containerPort: 9000
resources:
requests:
cpu: 100m
memory: 128Mi
limits:
cpu: 500m
memory: 512Mi
env:
- name: PHP_MEMORY_LIMIT
value: "256M"
- name: PHP_MAX_EXECUTION_TIME
value: "30"
volumeMounts:
- name: php-config
mountPath: /usr/local/etc/php/conf.d/custom.ini
subPath: php.ini
volumes:
- name: php-config
configMap:
name: php-config
---
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: php-app-hpa
namespace: production
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: php-app
minReplicas: 2
maxReplicas: 10
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 70
- type: Resource
resource:
name: memory
target:
type: Utilization
averageUtilization: 80
behavior:
scaleUp:
stabilizationWindowSeconds: 60
policies:
- type: Percent
value: 100
periodSeconds: 60
scaleDown:
stabilizationWindowSeconds: 300
policies:
- type: Percent
value: 20
periodSeconds: 60
监控和调试
# 查看 HPA 状态 kubectl get hpa -n production # 查看详细状态 kubectl describe hpa php-app-hpa -n production # 查看当前指标 kubectl top pods -n production # 查看扩容事件 kubectl get events -n production --sort-by='.lastTimestamp'
最佳实践
- 合理设置资源请求:资源请求要基于实际使用情况
- 配置多个指标:同时监控 CPU 和内存
- 设置扩缩容策略:避免频繁扩缩容
- 优化 PHP 配置:调整 FPM 和 opcache 参数
- 添加就绪探针:确保服务可用性
- 监控指标:使用 Prometheus 等工具监控
扩展:使用 KEDA
apiVersion: keda.sh/v1alpha1
kind: ScaledObject
metadata:
name: php-app-scaledobject
namespace: production
spec:
scaleTargetRef:
name: php-app
minReplicaCount: 2
maxReplicaCount: 20
triggers:
- type: prometheus
metadata:
serverAddress: http://prometheus.monitoring.svc:9090
metricName: php_requests_total
query: |
sum(rate(php_requests_total{namespace="production"}[2m]))
threshold: "100"
- type: cron
metadata:
timezone: Asia/Shanghai
start: "0 9 * * *"
end: "0 18 * * *"
desiredReplicas: "5"
这套配置可以根据业务需求灵活调整,确保 PHP 应用在负载波动时能够自动扩展或缩减实例数量。