k8s-hpa

Kubernetes定义了三种不同的监控数据接口,分别是Resource Metric,Custom Metric以及External Metric。

一般来说Resource Metric是通过metrics-server采集;

Custom Metric是通过prometheus来实现自定义扩容。

External Metric就是针对云场景的了,比方说通过获取slb最大连接数来实现自动扩容。

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: alibaba-cloud-metrics-adapter
  name: alibaba-cloud-metrics-adapter
  namespace: kube-system
spec:
  replicas: 1
  selector:
    matchLabels:
      app: alibaba-cloud-metrics-adapter
  template:
    metadata:
      labels:
        app: alibaba-cloud-metrics-adapter
      name: alibaba-cloud-metrics-adapter
    spec:
      serviceAccountName: admin
      containers:
      - name: alibaba-cloud-metrics-adapter
        image: registry.cn-beijing.aliyuncs.com/acs/alibaba-cloud-metrics-adapter-amd64:v0.2.0-alpha-e8f8c17f
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 443
          name: https
        - containerPort: 8080
          name: http
        volumeMounts:
        - mountPath: /tmp
          name: temp-vol
        - name: tz-config
          mountPath: /etc/localtime
          readOnly: true
      volumes:
      - name: temp-vol
        emptyDir: {}
      - name: tz-config
        hostPath:
          path: /etc/localtime
---
apiVersion: v1
kind: Service
metadata:
  name: alibaba-cloud-metrics-adapter
  namespace: kube-system
spec:
  ports:
  - name: https
    port: 443
    targetPort: 443
  - name: http
    port: 80
    targetPort: 8080
  selector:
    app: alibaba-cloud-metrics-adapter
---
apiVersion: apiregistration.k8s.io/v1beta1
kind: APIService
metadata:
  name: v1beta1.external.metrics.k8s.io
spec:
  service:
    name: alibaba-cloud-metrics-adapter
    namespace: kube-system
  group: external.metrics.k8s.io
  version: v1beta1
  insecureSkipTLSVerify: true
  groupPriorityMinimum: 100
  versionPriority: 100
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: external-metrics-server-resources
rules:
- apiGroups:
  - external.metrics.k8s.io
  resources: ["*"]
  verbs: ["*"]
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
  name: external-metrics-resource-reader
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: external-metrics-server-resources
subjects:
- kind: ServiceAccount
  name: horizontal-pod-autoscaler
  namespace: kube-system

安装alibaba-cloud-metrics-adapter

kubectl get --raw="/apis/external.metrics.k8s.io/v1beta1" | jq

简单说下各个指标的含义,方便之后去选择哪个指标去自动扩容。

slb_l4_traffic_rx 每秒流入

slb_l4_packet_tx 每秒流入的数据包数

slb_l4_active_connection 活动连接

slb_l4_max_connection 最大连接数

slb_l7_qps QPS

slb_l7_status_2xx 2xx个请求(每秒)

slb_l7_upstream_4xx 上游服务4xx请求(每秒)

sls_ingress_qps 特定入口路由的QPS

sls_ingress_inflow 入口流入带宽

k8s_workload_memory_usage 内存使用情况

k8s_workload_memory_rss rss

根据slb_l4_active_connection这个指标,实现自动扩容

apiVersion: apps/v1beta2 # for versions before 1.8.0 use apps/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment-basic
  labels:
    app: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.7.9 # replace it with your exactly <image_name:tags>
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: default
spec:
  externalTrafficPolicy: Local
  ports:
    - port: 80
      protocol: TCP
      targetPort: 80
  selector:
    app: nginx
  sessionAffinity: None
  type: LoadBalancer
---
apiVersion: autoscaling/v2beta2
kind: HorizontalPodAutoscaler
metadata:
  name: slb-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1beta2
    kind: Deployment
    name: nginx-deployment-basic
  minReplicas: 5
  maxReplicas: 10
  metrics:
    - type: External
      external:
        metric:
          name: slb_l4_active_connection
          selector:
            matchLabels:
              # slb.instance.id: "lb-2ze2locy5fk8at1cfx47y"
              slb.instance.id: ""
              # slb.instance.port: "80"
              slb.instance.port: ""
        target:
          type: Value
          value: 100
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值