php

k8s 利用 HPA自动缩容

定义 Deployment apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: nginx spec: #replicas: 2 # 指定 3 个副本 selector: matchLabels: app: nginx template: met…

4 阅读

定义 Deployment

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  #replicas: 2  # 指定 3 个副本
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:1.25.3  # 使用官方稳定版本
          ports:
            - containerPort: 80  # 容器监听的端口
          resources:
            requests:
              memory: "64Mi"
              cpu: "250m"
            limits:
              memory: "128Mi"
              cpu: "500m"
          # 健康检查(可选)
          livenessProbe:
            httpGet:
              path: /
              port: 80
            initialDelaySeconds: 5
            periodSeconds: 10
          readinessProbe:
            httpGet:
              path: /
              port: 80
            initialDelaySeconds: 2
            periodSeconds: 5
---
---
# 定义 Service
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: NodePort  # 通过节点端口暴露服务
  selector:
    app: nginx  # 必须与 Deployment 的标签匹配
  ports:
    - protocol: TCP
      port: 80    # Service 的端口
      targetPort: 80  # 容器的端口
      nodePort: 30080  # 节点的端口(范围 30000-32767)
---
# nginx-hpa.yaml
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: nginx-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: nginx-deployment
  minReplicas: 2
  maxReplicas: 10
  metrics:
    - type: Resource
      resource:
        name: cpu
        target:
          type: Utilization
          averageUtilization: 10  # 目标 CPU 使用率 50%
#    - type: Pods
#      pods:
#        metric:
#          name: requests-per-second
#        target:
#          type: AverageValue
#          averageValue: 20
  behavior:
    scaleDown:
      stabilizationWindowSeconds: 20
      selectPolicy: Max
      policies:
        - type: Percent
          value: 50
          periodSeconds: 10
    scaleUp:
      stabilizationWindowSeconds: 20
      selectPolicy: Max
      policies:
        - type: Percent
          value: 50
          periodSeconds: 10

Deployment apiVersion: apps/v1:指定 API 版本。 kind: Deployment:指定资源类型为 Deployment。 metadata:元数据部分。 name: nginx-deployment:Deployment 的名称。 labels:标签,用于标识 Deployment。 app: nginx:标签键值对,键为 app,值为 nginx。 spec:规范部分。 replicas: 1:指定副本数为 1。 selector:选择器,用于选择要管理的 Pods。 matchLabels:匹配标签。 app: nginx:匹配标签键值对,键为 app,值为 nginx。 template:Pod 模板。 metadata:Pod 的元数据。 labels:Pod 的标签。 app: nginx:标签键值对,键为 app,值为 nginx。 spec:Pod 的规范。 containers:容器列表。 name: nginx:容器名称。 image: nginx:1.25.3:使用的镜像。 ports:端口列表。 containerPort: 80:容器监听的端口。 resources:资源请求和限制。 requests:资源请求。 memory: "64Mi":请求的内存。 cpu: "250m":请求的 CPU。 limits:资源限制。 memory: "128Mi":限制的内存。 cpu: "500m":限制的 CPU。 livenessProbe:存活性探针。 httpGet:HTTP GET 请求。 path: /:请求路径。 port: 80:请求端口。 initialDelaySeconds: 5:初始延迟。 periodSeconds: 10:检查间隔。 readinessProbe:就绪性探针。 httpGet:HTTP GET 请求。 path: /:请求路径。 port: 80:请求端口。 initialDelaySeconds: 2:初始延迟。 periodSeconds: 5:检查间隔。 Service apiVersion: v1:指定 API 版本。 kind: Service:指定资源类型为 Service。 metadata:元数据部分。 name: nginx-service:Service 的名称。 spec:规范部分。 type: NodePort:通过节点端口暴露服务。 selector:选择器,用于选择要暴露的 Pods。 app: nginx:选择标签键值对,键为 app,值为 nginx。 ports:端口列表。 protocol: TCP:协议类型。 port: 80:Service 的端口。 targetPort: 80:容器的端口。 nodePort: 30080:节点的端口(范围 30000-32767)。 Horizontal Pod Autoscaler (HPA) apiVersion: autoscaling/v2:指定 API 版本。 kind: HorizontalPodAutoscaler:指定资源类型为 HPA。 metadata:元数据部分。 name: nginx-hpa:HPA 的名称。 spec:规范部分。 scaleTargetRef:扩展目标引用。 apiVersion: apps/v1:目标 API 版本。 kind: Deployment:目标资源类型。 name: nginx-deployment:目标 Deployment 的名称。 minReplicas: 2:最小副本数。 maxReplicas: 10:最大副本数。 metrics:指标列表。 type: Resource:资源类型指标。 resource:资源名称。 name: cpu:CPU 资源。 target:目标设置。 type: Utilization:使用率类型。 averageUtilization: 50:目标 CPU 使用率为 50%。 type: Pods:Pods 类型指标。 pods:Pods 指标。 metric:指标名称。 name: requests-per-second:每秒请求数。 target:目标设置。 type: AverageValue:平均值类型。 averageValue: 20:目标每秒平均请求数为 20。 behavior:行为设置。 scaleDown:缩减设置。 stabilizationWindowSeconds: 300:稳定窗口时间(秒)。 selectPolicy: Max:选择策略为最大值。 policies:策略列表。 type: Percent:百分比类型。 value: 50:缩减百分比为 50%。 periodSeconds: 60:策略周期(秒)。 scaleUp:扩展设置。 stabilizationWindowSeconds: 600:稳定窗口时间(秒)。 selectPolicy: Max:选择策略为最大值。 policies:策略列表。 type: Percent:百分比类型。 value: 100:扩展百分比为 100%。 periodSeconds: 60:策略周期(秒)。

正在加载评论…