k8s對接雲實現自動擴容pod

公衆號關注 「SY技術小站

設爲「星標」,每天帶你分享技術與生活!

介紹

在之前的文章我介紹了下 Custom Metric 怎麼實現自動擴容的。k8s基於自定義指標實現自動擴容

實際上Kubernetes定義了三種不同的監控數據接口,分別是Resource MetricCustom Metric以及External Metric

一般來說Resource Metric是通過metrics-server採集;

Custom Metric是通過prometheus來實現自定義擴容。

External Metric就是針對雲場景的了,比方說通過獲取slb最大連接數來實現自動擴容。

下面我來說下具體怎麼實現的。

部署

安裝alibaba-cloud-metrics-adapter,以下是yaml文件

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

可以使用下面的命令來檢測是否生效了:

# kubectl get --raw="/apis/external.metrics.k8s.io/v1beta1" | jq
{
  "kind": "APIResourceList",
  "apiVersion": "v1",
  "groupVersion": "external.metrics.k8s.io/v1beta1",
  "resources": [
    {
      "name": "sls_ingress_qps",
      "singularName": "",
      "namespaced": true,
      "kind": "ExternalMetricValueList",
      "verbs": [
        "get"
      ]
    },
    {
      "name": "slb_l4_connection_utilization",
      "singularName": "",
      "namespaced": true,
      "kind": "ExternalMetricValueList",
      "verbs": [
        "get"
      ]
    },
    {
      "name": "slb_l7_qps",
      "singularName": "",
      "namespaced": true,
      "kind": "ExternalMetricValueList",
      "verbs": [
        "get"
      ]
    },
    {
      "name": "ahas_sentinel_total_qps",
      "singularName": "",
      "namespaced": true,
      "kind": "ExternalMetricValueList",
      "verbs": [
        "get"
      ]
    },
    {
      "name": "ahas_sentinel_avg_rt",
      "singularName": "",
      "namespaced": true,
      "kind": "ExternalMetricValueList",
      "verbs": [
        "get"
      ]
    },
    {
      "name": "k8s_workload_cpu_util",
      "singularName": "",
      "namespaced": true,
      "kind": "ExternalMetricValueList",
      "verbs": [
        "get"
      ]
    },
    {
      "name": "k8s_workload_memory_request",
      "singularName": "",
      "namespaced": true,
      "kind": "ExternalMetricValueList",
      "verbs": [
        "get"
      ]
    },
    {
      "name": "k8s_workload_memory_cache",
      "singularName": "",
      "namespaced": true,
      "kind": "ExternalMetricValueList",
      "verbs": [
        "get"
      ]
    },
    。。。。。。。。。

簡單說下各個指標的含義,方便之後去選擇哪個指標去自動擴容。

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

這樣就實現了通過External Metric自動擴容。

參考鏈接

https://github.com/AliyunContainerService/alibaba-cloud-metrics-adapter

往期精彩文章 

  微信羣加入方式 

  歡迎關注作者公衆號

點個在看少個 bug ????

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章