在集群中自动缩放 DNS 服务
本页面介绍如何在 Kubernetes 集群中启用和配置 DNS 服务的自动扩缩容。
准备工作
您需要有一个 Kubernetes 集群,并且 kubectl 命令行工具必须配置为与您的集群通信。建议您在至少有两个节点且这些节点不充当控制平面主机的集群上运行本教程。如果您还没有集群,则可以使用 minikube 创建一个集群,或者可以使用以下 Kubernetes 游乐场之一
要检查版本,请输入kubectl version
。本指南假设您的节点使用 AMD64 或 Intel 64 CPU 架构。
确保 Kubernetes DNS 已启用。
确定是否已启用 DNS 水平自动扩缩容
kubectl get deployment --namespace=kube-system
输出类似于以下内容
NAME READY UP-TO-DATE AVAILABLE AGE
...
kube-dns-autoscaler 1/1 1 1 ...
...
如果在输出中看到“kube-dns-autoscaler”,则表示已启用 DNS 水平自动扩缩容,您可以跳至 调整自动扩缩容参数。
获取 DNS 部署的名称
列出 kube-system 命名空间中集群中的 DNS 部署
kubectl get deployment -l k8s-app=kube-dns --namespace=kube-system
输出类似于以下内容
NAME READY UP-TO-DATE AVAILABLE AGE
...
coredns 2/2 2 2 ...
...
如果看不到 DNS 服务的部署,您也可以按名称查找
kubectl get deployment --namespace=kube-system
并查找名为 coredns
或 kube-dns
的部署。
您的扩缩目标是
Deployment/<your-deployment-name>
其中 <your-deployment-name>
是您的 DNS 部署的名称。例如,如果您的 DNS 部署名称为 coredns,则您的扩缩目标为 Deployment/coredns。
注意
CoreDNS 是 Kubernetes 的默认 DNS 服务。CoreDNS 设置标签k8s-app=kube-dns
,以便它可以在最初使用 kube-dns 的集群中工作。启用 DNS 水平自动扩缩容
在本节中,您将创建一个新的部署。部署中的 Pod 运行基于 cluster-proportional-autoscaler-amd64
镜像的容器。
使用以下内容创建一个名为 dns-horizontal-autoscaler.yaml
的文件
kind: ServiceAccount
apiVersion: v1
metadata:
name: kube-dns-autoscaler
namespace: kube-system
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: system:kube-dns-autoscaler
rules:
- apiGroups: [""]
resources: ["nodes"]
verbs: ["list", "watch"]
- apiGroups: [""]
resources: ["replicationcontrollers/scale"]
verbs: ["get", "update"]
- apiGroups: ["apps"]
resources: ["deployments/scale", "replicasets/scale"]
verbs: ["get", "update"]
# Remove the configmaps rule once below issue is fixed:
# kubernetes-incubator/cluster-proportional-autoscaler#16
- apiGroups: [""]
resources: ["configmaps"]
verbs: ["get", "create"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: system:kube-dns-autoscaler
subjects:
- kind: ServiceAccount
name: kube-dns-autoscaler
namespace: kube-system
roleRef:
kind: ClusterRole
name: system:kube-dns-autoscaler
apiGroup: rbac.authorization.k8s.io
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: kube-dns-autoscaler
namespace: kube-system
labels:
k8s-app: kube-dns-autoscaler
kubernetes.io/cluster-service: "true"
spec:
selector:
matchLabels:
k8s-app: kube-dns-autoscaler
template:
metadata:
labels:
k8s-app: kube-dns-autoscaler
spec:
priorityClassName: system-cluster-critical
securityContext:
seccompProfile:
type: RuntimeDefault
supplementalGroups: [ 65534 ]
fsGroup: 65534
nodeSelector:
kubernetes.io/os: linux
containers:
- name: autoscaler
image: registry.k8s.io/cpa/cluster-proportional-autoscaler:1.8.4
resources:
requests:
cpu: "20m"
memory: "10Mi"
command:
- /cluster-proportional-autoscaler
- --namespace=kube-system
- --configmap=kube-dns-autoscaler
# Should keep target in sync with cluster/addons/dns/kube-dns.yaml.base
- --target=<SCALE_TARGET>
# When cluster is using large nodes(with more cores), "coresPerReplica" should dominate.
# If using small nodes, "nodesPerReplica" should dominate.
- --default-params={"linear":{"coresPerReplica":256,"nodesPerReplica":16,"preventSinglePointFailure":true,"includeUnschedulableNodes":true}}
- --logtostderr=true
- --v=2
tolerations:
- key: "CriticalAddonsOnly"
operator: "Exists"
serviceAccountName: kube-dns-autoscaler
在文件中,将 <SCALE_TARGET>
替换为您的扩缩目标。
转到包含您的配置文件的目录,然后输入以下命令以创建部署
kubectl apply -f dns-horizontal-autoscaler.yaml
成功执行命令的输出为
deployment.apps/kube-dns-autoscaler created
DNS 水平自动扩缩容现已启用。
调整 DNS 自动扩缩容参数
验证 kube-dns-autoscaler 配置映射 是否存在
kubectl get configmap --namespace=kube-system
输出类似于以下内容
NAME DATA AGE
...
kube-dns-autoscaler 1 ...
...
修改配置映射中的数据
kubectl edit configmap kube-dns-autoscaler --namespace=kube-system
查找以下行
linear: '{"coresPerReplica":256,"min":1,"nodesPerReplica":16}'
根据您的需要修改字段。“min”字段表示 DNS 后端的最小数量。实际的后端数量使用以下公式计算
replicas = max( ceil( cores × 1/coresPerReplica ) , ceil( nodes × 1/nodesPerReplica ) )
请注意,coresPerReplica
和 nodesPerReplica
的值均为浮点数。
其理念是,当集群使用具有许多核心的节点时,coresPerReplica
占主导地位。当集群使用核心较少的节点时,nodesPerReplica
占主导地位。
还有其他受支持的扩缩模式。有关详细信息,请参阅 cluster-proportional-autoscaler。
禁用 DNS 水平自动扩缩容
有几种调整 DNS 水平自动扩缩容的选项。使用哪个选项取决于不同的情况。
选项 1:将 kube-dns-autoscaler 部署缩减为 0 个副本
此选项适用于所有情况。输入以下命令
kubectl scale deployment --replicas=0 kube-dns-autoscaler --namespace=kube-system
输出为
deployment.apps/kube-dns-autoscaler scaled
验证副本计数是否为零
kubectl get rs --namespace=kube-system
输出在 DESIRED 和 CURRENT 列中显示 0
NAME DESIRED CURRENT READY AGE
...
kube-dns-autoscaler-6b59789fc8 0 0 0 ...
...
选项 2:删除 kube-dns-autoscaler 部署
如果 kube-dns-autoscaler 在您自己的控制之下,则此选项有效,这意味着没有人会重新创建它
kubectl delete deployment kube-dns-autoscaler --namespace=kube-system
输出为
deployment.apps "kube-dns-autoscaler" deleted
选项 3:从主节点删除 kube-dns-autoscaler 清单文件
如果 kube-dns-autoscaler 由(已弃用的)插件管理器 控制,并且您对主节点具有写入权限,则此选项有效。
登录到主节点并删除相应的清单文件。此 kube-dns-autoscaler 的常用路径为
/etc/kubernetes/addons/dns-horizontal-autoscaler/dns-horizontal-autoscaler.yaml
删除清单文件后,插件管理器将删除 kube-dns-autoscaler 部署。
了解 DNS 水平自动扩缩容的工作原理
cluster-proportional-autoscaler 应用程序与 DNS 服务分开部署。
自动扩缩器 Pod 运行一个客户端,该客户端轮询 Kubernetes API 服务器以获取集群中的节点和核心数量。
根据当前可调度的节点和核心以及给定的扩缩参数,计算所需副本计数并将其应用于 DNS 后端。
扩缩参数和数据点通过配置映射提供给自动扩缩器,它会在每个轮询间隔刷新其参数表,以与最新的所需扩缩参数保持同步。
允许更改扩缩参数,而无需重建或重新启动自动扩缩器 Pod。
自动扩缩器提供了一个控制器接口来支持两种控制模式:_线性_ 和 _阶梯_。
后续步骤
- 阅读有关 关键插件 Pod 的保证调度 的信息。
- 详细了解 cluster-proportional-autoscaler 的实现。