W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
本頁展示了如何在集群中啟用和配置 DNS 服務的自動擴縮功能。
要檢查版本,請輸入 ?kubectl version
?。
在 kube-system 命名空間中列出集群中的 Deployments :
kubectl get deployment --namespace=kube-system
輸出類似如下這樣:
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
...
dns-autoscaler 1 1 1 1 ...
...
如果在輸出中看到 “dns-autoscaler”,說明 DNS 水平自動擴縮已經(jīng)啟用,可以跳到 調優(yōu)自動擴縮參數(shù)。
列出集群內(nèi) kube-system 名字空間中的 DNS Deployment:
kubectl get deployment -l k8s-app=kube-dns --namespace=kube-system
輸出類似如下這樣:
NAME READY UP-TO-DATE AVAILABLE AGE
...
coredns 2/2 2 2 ...
...
如果看不到 DNS 服務的 Deployment,你也可以通過名字來查找:
kubectl get deployment --namespace=kube-system
并在輸出中尋找名稱為 ?coredns
?或 ?kube-dns
? 的 Deployment。
你的擴縮目標為:
Deployment/<your-deployment-name>
其中 ?<your-deployment-name>
? 是 DNS Deployment 的名稱。 例如,如果你的 DNS Deployment 名稱是 ?coredns
?,則你的擴展目標是 Deployment/coredns。
Note: CoreDNS 是 Kubernetes 的默認 DNS 服務。CoreDNS 設置標簽 ?
k8s-app=kube-dns
?, 以便能夠在原來使用 ?kube-dns
? 的集群中工作。
在本節(jié),我們創(chuàng)建一個 Deployment。Deployment 中的 Pod 運行一個基于 ?cluster-proportional-autoscaler-amd64
? 鏡像的容器。
創(chuàng)建文件 ?dns-horizontal-autoscaler.yaml
?,內(nèi)容如下所示:
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"]
# 待以下 issue 修復后,請刪除 Configmaps
# 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: k8s.gcr.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
# 應該保持目標與 cluster/addons/dns/kube-dns.yaml.base 同步
- --target=<SCALE_TARGET>
#當集群使用大節(jié)點(有更多核)時,“coresPerReplica”應該占主導地位。
#如果使用小節(jié)點,“nodesPerReplica“ 應該占主導地位。
- --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>
? 替換成擴縮目標。
進入到包含配置文件的目錄中,輸入如下命令創(chuàng)建 Deployment:
kubectl apply -f dns-horizontal-autoscaler.yaml
一個成功的命令輸出是:
deployment.apps/dns-autoscaler created
DNS 水平自動擴縮在已經(jīng)啟用了。
驗證 dns-autoscaler ConfigMap 是否存在:
kubectl get configmap --namespace=kube-system
輸出類似于:
NAME DATA AGE
...
dns-autoscaler 1 ...
...
修改該 ConfigMap 中的數(shù)據(jù):
kubectl edit configmap dns-autoscaler --namespace=kube-system
找到如下這行內(nèi)容:
linear: '{"coresPerReplica":256,"min":1,"nodesPerReplica":16}'
根據(jù)需要修改對應的字段?!癿in” 字段表明 DNS 后端的最小數(shù)量。 實際后端的數(shù)量通過使用如下公式來計算:
replicas = max( ceil( cores * 1/coresPerReplica ) , ceil( nodes * 1/nodesPerReplica ) )
注意 ?coresPerReplica
?和 ?nodesPerReplica
?的值都是整數(shù)。
背后的思想是,當一個集群使用具有很多核心的節(jié)點時,由 ?coresPerReplica
?來控制。 當一個集群使用具有較少核心的節(jié)點時,由 ?nodesPerReplica
?來控制。
其它的擴縮模式也是支持的,詳情查看 cluster-proportional-autoscaler。
有幾個可供調優(yōu)的 DNS 水平自動擴縮選項。具體使用哪個選項因環(huán)境而異。
該選項適用于所有場景。運行如下命令:
kubectl scale deployment --replicas=0 dns-autoscaler --namespace=kube-system
輸出如下所示:
deployment.apps/dns-autoscaler scaled
驗證當前副本數(shù)為 0:
kubectl get rs --namespace=kube-system
輸出內(nèi)容中,在 DESIRED 和 CURRENT 列顯示為 0:
NAME DESIRED CURRENT READY AGE
...
dns-autoscaler-6b59789fc8 0 0 0 ...
...
如果 dns-autoscaler 為你所控制,也就說沒有人會去重新創(chuàng)建它,可以選擇此選項:
kubectl delete deployment dns-autoscaler --namespace=kube-system
輸出內(nèi)容如下所示:
deployment.apps "dns-autoscaler" deleted
如果 dns-autoscaler 在插件管理器 的控制之下,并且具有操作 master 節(jié)點的寫權限,可以使用此選項。
登錄到主控節(jié)點,刪除對應的清單文件。 dns-autoscaler 對應的路徑一般為:
/etc/kubernetes/addons/dns-horizontal-autoscaler/dns-horizontal-autoscaler.yaml
當清單文件被刪除后,插件管理器將刪除 dns-autoscaler Deployment。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: