k8s 实现自动伸缩扩容
创建pod副本
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: nginx
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
imagePullSecrets:
- name: login
volumes:
- name: master-nginx-vhost
hostPath:
path: /public/nginx/vhost
type: DirectoryOrCreate
- name: master-nginx-ssl
hostPath:
path: /public/nginx/ssl
- name: nginx-config-volume
configMap:
name: nginx-config
containers:
- name: nginx
image: private.yd-data.com/zjj/tengine:latest
imagePullPolicy: Always
ports:
- containerPort: 443
volumeMounts:
- name: master-nginx-vhost
mountPath: /etc/nginx/vhost
- name: master-nginx-ssl
mountPath: /etc/nginx/ssl
- name: nginx-config-volume
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
resources:
requests:
cpu: "1000m" # 最少可获得1000 毫核= 1核
limits:
cpu: "2000m" # 最多可获得2000 毫核= 2核
securityContext:
capabilities:
add:
- SYS_RESOURCE # 添加 SYS_RESOURCE 能力,允许设置资源限制
---
apiVersion: v1 # service api版本
kind: Service # 使用service 模块
metadata: # 元数据
name: nginx # 新建自定义service 模块的名字
namespace: nginx
spec: # service 信息
type: NodePort # 使用Nodeport协议,对外提供开放端口
ports: # 端口信息
- port: 443 # service 的端口
targetPort: 443 # 容器tomcat的端口
nodePort: 443 # 自定义对外开发的端口
selector: # 选择标签器
app: nginx # app nginx 此名字要匹配deployment 的app: nginx
创建rbac授权
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: hpa-metrics-reader
namespace: nginx
rules:
- apiGroups:
- metrics.k8s.io
resources:
- pods
- nodes
verbs:
- get
- list
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: hpa-metrics-reader-binding
namespace: nginx
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: hpa-metrics-reader
subjects:
- kind: ServiceAccount
name: default # 假设使用默认的 ServiceAccount
namespace: nginx
最后创建hpa
apiVersion: autoscaling/v2beta2
#apiVersion: autoscaling/v2 #Kubernetes 1.23 版本以上支持
kind: HorizontalPodAutoscaler
metadata:
name: nginx-hpa
namespace: nginx
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: nginx-deployment
minReplicas: 2
maxReplicas: 5
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
#Kubernetes 1.23版本以上支持一下配置
#behavior:
# scaleDown:
# stabilizationWindowSeconds: 300 # 缩容冷却时间,单位为秒
# policies:
# - type: Percent #表示按照百分比来缩容,value: 10 意味着每次最多减少 10% 的 Pod 副本数量
# value: 100
# periodSeconds: 15 #表示检查周期为 15 秒,即 HPA 每 15 秒检查一次是否需要进行缩容操作
# - type: Pods #表示按照固定数量的 Pod 来缩容,value: 1 表示每次至少减少 1 个 Pod
# value: 1
# periodSeconds: 15
查看hpa状态
kubectl get hpa -n nginx
压力测试pod检查是否能自动伸缩
kubectl exec -it -n nginx nginx-deployment-7f45f46888-prd6b -- stress-ng --cpu 1 --timeout 300s
kubectl top pod -n nginx