技术分享之 CoreDNS 集群层粒度 autoscaling

CoreDNS 集群层粒度 autoscaling

Posted by 董江 on Monday, November 11, 2024

CoreDNS DNS集群粒度 autoscaling

第一篇: 使用 NodeLocal DNSCache 提高 clusterDNS 性能和可靠性

一、什么是集群比例自动扩缩器 (Cluster Proportional AutoscalerCPA) ?

集群比例自动扩缩器 (CPA) 是一种特殊水平 pod 自动扩缩器. 和通用的HPA的自动扩缩器一样,通过策略将 pod 数达到预想设定的副本数。

那为什么有了通用的HPA(并支持custom metric HPA,还需要这种CPA呢? 答: 处于架构依赖关系,避免组件循环依赖。本身集群内组件包括:cert-manager, coredns, metrics-server 以及 kube-state-metrics

应用application的HPA,核心指标来自与: *单容器:流量bytes、cpu微核m、内存Mi等 来源于 cAdvisor metrics *容器组:来源于coredns、ingress provider metrics *容器组和节点:来源于 metrics-serverkube-state-metrics 等 *HTTP细粒度 tsl, 证书授权,开源于 cert-manager

由于 coredns 再依赖hpa方式,整个组件依赖形成了循环依赖。 因此才有CPA的出现.

CPA只根据集群中的节点数/CPU数扩缩副本,调整副本数量。

此功能适用于需要根据集群大小自动扩缩的应用程序,例如 CoreDNSmetrics-server 和 其他根据集群中的节点/pod 数量扩缩的服务。

二、Cluster Proportional Autoscaler 原理

CPA 不依赖于 Metrics API,更不需要 Metrics Server。 不使用 Kubernetes 资源进行扩展,而是使用标志来识别目标工作负载和 ConfigMap 来扩展配置。CPA 提供了一个简单的控制循环,用于监视集群大小并扩展目标控制器。CPA 的输入是集群中可调度的核心和节点的数量。

CPA 的一些主要用例包括:

  • 规划配置configmap
  • 横向扩展目标服务target

由于不需要指标服务器或 Prometheus 适配器,因此可以简单轻松地扩展工作负载

三、使用方式

3.1 部署CPA, 规划伸缩配置策略

apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns-linear
data:
  linear: |-
    {
      "coresPerReplica": 512,  // 按比例, 每增加512核, 增加一个coredns实例
      "nodesPerReplica": 4,    // 按比例, 每增加4台node, 增加一个coredns实例
      "min": 2,                // 最小2个 coredns实例数
      "max": 100,              // 最大100个 coredns实例数
      "preventSinglePointFailure": true, // 如果有多个Node节点,控制器会确保至少 2 个副本
      "includeUnschedulableNodes": true  // coredns实例副本将仅根据可调度节点的数量进行扩展
    }    

计算方式:

replicas = max( ceil( cores * 1/coresPerReplica ) , ceil( nodes * 1/nodesPerReplica ) )
replicas = min(replicas, max)
replicas = max(replicas, min)

3.1 部署方式

# 创建RBAC和SA,以便组件能够正常请求集群
kind: ServiceAccount
apiVersion: v1
metadata:
  name: cluster-proportional-autoscaler
  namespace: kube-system
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: cluster-proportional-autoscaler
rules:
  - apiGroups: [""]
    resources: ["nodes"]
    verbs: ["list", "watch"]
  - apiGroups: [""]
    resources: ["replicationcontrollers/scale"]
    verbs: ["get", "update"]
  - apiGroups: ["extensions","apps"]
    resources: ["deployments/scale", "replicasets/scale"]
    verbs: ["get", "update"]
  - apiGroups: [""]
    resources: ["configmaps"]
    verbs: ["get", "create"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: cluster-proportional-autoscaler
subjects:
  - kind: ServiceAccount
    name: cluster-proportional-autoscaler
    namespace: kube-system
roleRef:
  kind: ClusterRole
  name: cluster-proportional-autoscaler
  apiGroup: rbac.authorization.k8s.io
  
#创建configmap,写明期望的扩缩容规则
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: coredns-autoscaler
  namespace: kube-system
data:
  linear: |-
    {
      "coresPerReplica": 512,
      "nodesPerReplica": 4,
      "min": 2,
      "max": 100,
      "preventSinglePointFailure": true,
      "includeUnschedulableNodes": true
    }    

# 创建autoscaler监控组件CoreDNS
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: coredns-autoscaler
  namespace: kube-system
  labels:
    k8s-app: kube-dns  #coredns label
spec:
  selector:
    matchLabels:
      k8s-app: kube-dns
  replicas: 1
  template:
    metadata:
      labels:
        k8s-app: kube-dns
    spec:
      containers:
        - image: dongjiang1989/cluster-proportional-autoscaler:v1.8.9
          name: autoscaler
          command:
            - /cluster-proportional-autoscaler
            - --namespace=kube-system  # 监控的命名空间
            - --configmap=coredns-autoscaler  # 使用的configmap配置
            - --target=deployment/coredns  # 监控的资源
            - --logtostderr=true
            - --v=2
      serviceAccountName: cluster-proportional-autoscaler  # 使用的SA

3.3 验证

$ kubectl apply -f cpa.yaml 
serviceaccount/cluster-proportional-autoscaler created
clusterrole.rbac.authorization.k8s.io/cluster-proportional-autoscaler created
clusterrolebinding.rbac.authorization.k8s.io/cluster-proportional-autoscaler created
configmap/coredns-autoscaler created
deployment.apps/coredns-autoscaler created

$ kubectl get pod -n kube-system
NAME                                         READY   STATUS    RESTARTS        AGE
coredns-5d78c9869d-5lg77                     1/1     Running   7 (3d5h ago)    54d
coredns-5d78c9869d-jbsfl                     1/1     Running   5 (3d5h ago)    54d
coredns-autoscaler-f9448b7fd-rdd5g           1/1     Running   0               40s
etcd-kind-control-plane                      1/1     Running   4 (3d5h ago)    54d
kindnet-25nll                                1/1     Running   13 (11d ago)    54d
kube-apiserver-kind-control-plane            1/1     Running   11 (3d5h ago)   54d
kube-controller-manager-kind-control-plane   1/1     Running   28 (10h ago)    54d
kube-proxy-mszwz                             1/1     Running   4 (3d5h ago)    54d
kube-scheduler-kind-control-plane            1/1     Running   27 (10h ago)    54d

四、其他

「如果这篇文章对你有用,请随意打赏」

Kubeservice博客

如果这篇文章对你有用,请随意打赏

使用微信扫描二维码完成支付