06-4.部署高可用 kube-scheduler 集群

本文档介绍部署高可用 kube-scheduler 集群的步骤。

该集群包含 3 个节点,启动后将通过竞争选举机制产生一个 leader 节点,其它节点为阻塞状态。当 leader 节点不可用后,剩余节点将再次进行选举产生新的 leader 节点,从而保证服务的可用性。

为保证通信安全,本文档先生成 x509 证书和私钥,kube-scheduler 在于 kube-apiserver 的安全端口通信时使用该证书。

准备工作

下载最新版本的二进制文件、安装和配置 flanneld 参考:06-0.部署master节点.md

创建 kube-scheduler 证书和私钥

创建证书签名请求:

  1. cd /opt/k8s/work
  2. cat > kube-scheduler-csr.json <<EOF
  3. {
  4. "CN": "system:kube-scheduler",
  5. "hosts": [
  6. "127.0.0.1",
  7. "172.27.136.1",
  8. "172.27.136.2",
  9. "172.27.136.3"
  10. ],
  11. "key": {
  12. "algo": "rsa",
  13. "size": 2048
  14. },
  15. "names": [
  16. {
  17. "C": "CN",
  18. "ST": "BeiJing",
  19. "L": "BeiJing",
  20. "O": "system:kube-scheduler",
  21. "OU": "4Paradigm"
  22. }
  23. ]
  24. }
  25. EOF
  • hosts 列表包含所有 kube-scheduler 节点 IP;
  • CN 为 system:kube-scheduler、O 为 system:kube-scheduler,kubernetes 内置的 ClusterRoleBindings system:kube-scheduler 将赋予 kube-scheduler 工作所需的权限。

生成证书和私钥:

  1. cd /opt/k8s/work
  2. cfssl gencert -ca=/opt/k8s/work/ca.pem \
  3. -ca-key=/opt/k8s/work/ca-key.pem \
  4. -config=/opt/k8s/work/ca-config.json \
  5. -profile=kubernetes kube-scheduler-csr.json | cfssljson -bare kube-scheduler
  6. ls kube-scheduler*pem

创建和分发 kubeconfig 文件

kubeconfig 文件包含访问 apiserver 的所有信息,如 apiserver 地址、CA 证书和自身使用的证书;

  1. cd /opt/k8s/work
  2. source /opt/k8s/bin/environment.sh
  3. kubectl config set-cluster kubernetes \
  4. --certificate-authority=/opt/k8s/work/ca.pem \
  5. --embed-certs=true \
  6. --server=${KUBE_APISERVER} \
  7. --kubeconfig=kube-scheduler.kubeconfig
  8. kubectl config set-credentials system:kube-scheduler \
  9. --client-certificate=/opt/k8s/work/kube-scheduler.pem \
  10. --client-key=/opt/k8s/work/kube-scheduler-key.pem \
  11. --embed-certs=true \
  12. --kubeconfig=kube-scheduler.kubeconfig
  13. kubectl config set-context system:kube-scheduler \
  14. --cluster=kubernetes \
  15. --user=system:kube-scheduler \
  16. --kubeconfig=kube-scheduler.kubeconfig
  17. kubectl config use-context system:kube-scheduler --kubeconfig=kube-scheduler.kubeconfig
  • 上一步创建的证书、私钥以及 kube-apiserver 地址被写入到 kubeconfig 文件中;

分发 kubeconfig 到所有 master 节点:

  1. cd /opt/k8s/work
  2. source /opt/k8s/bin/environment.sh
  3. for node_ip in ${NODE_IPS[@]}
  4. do
  5. echo ">>> ${node_ip}"
  6. scp kube-scheduler.kubeconfig root@${node_ip}:/etc/kubernetes/
  7. done

创建和分发 kube-scheduler systemd unit 文件

  1. cd /opt/k8s/work
  2. cat > kube-scheduler.service <<EOF
  3. [Unit]
  4. Description=Kubernetes Scheduler
  5. Documentation=https://github.com/GoogleCloudPlatform/kubernetes
  6. [Service]
  7. WorkingDirectory=${K8S_DIR}/kube-scheduler
  8. ExecStart=/opt/k8s/bin/kube-scheduler \\
  9. --address=127.0.0.1 \\
  10. --kubeconfig=/etc/kubernetes/kube-scheduler.kubeconfig \\
  11. --leader-elect=true \\
  12. --kube-api-qps=100 \\
  13. --logtostderr=true \\
  14. --v=2
  15. Restart=always
  16. RestartSec=5
  17. StartLimitInterval=0
  18. [Install]
  19. WantedBy=multi-user.target
  20. EOF
  • --address:在 127.0.0.1:10251 端口接收 http /metrics 请求;kube-scheduler 目前还不支持接收 https 请求;
  • --kubeconfig:指定 kubeconfig 文件路径,kube-scheduler 使用它连接和验证 kube-apiserver;
  • --leader-elect=true:集群运行模式,启用选举功能;被选为 leader 的节点负责处理工作,其它节点为阻塞状态;

完整 unit 见 kube-scheduler.service

分发 systemd unit 文件到所有 master 节点:

  1. cd /opt/k8s/work
  2. source /opt/k8s/bin/environment.sh
  3. for node_ip in ${NODE_IPS[@]}
  4. do
  5. echo ">>> ${node_ip}"
  6. scp kube-scheduler.service root@${node_ip}:/etc/systemd/system/
  7. done

启动 kube-scheduler 服务

  1. source /opt/k8s/bin/environment.sh
  2. for node_ip in ${NODE_IPS[@]}
  3. do
  4. echo ">>> ${node_ip}"
  5. ssh root@${node_ip} "mkdir -p ${K8S_DIR}/kube-scheduler"
  6. ssh root@${node_ip} "systemctl daemon-reload && systemctl enable kube-scheduler && systemctl restart kube-scheduler"
  7. done
  • 必须先创建日志目录;

检查服务运行状态

  1. source /opt/k8s/bin/environment.sh
  2. for node_ip in ${NODE_IPS[@]}
  3. do
  4. echo ">>> ${node_ip}"
  5. ssh root@${node_ip} "systemctl status kube-scheduler|grep Active"
  6. done

确保状态为 active (running),否则查看日志,确认原因:

  1. journalctl -u kube-scheduler

查看当前的 leader

  1. kubectl get endpoints kube-scheduler --namespace=kube-system -o yaml

输出:

  1. apiVersion: v1
  2. kind: Endpoints
  3. metadata:
  4. annotations:
  5. control-plane.alpha.kubernetes.io/leader: '{"holderIdentity":"m7-demo-136001","leaseDurationSeconds":15,"acquireTime":"2018-08-15T14:22:00Z","renewTime":"2018-08-15T14:22:13Z","leaderTransitions":0}'
  6. creationTimestamp: 2018-08-15T14:22:01Z
  7. name: kube-scheduler
  8. namespace: kube-system
  9. resourceVersion: "286"
  10. selfLink: /api/v1/namespaces/kube-system/endpoints/kube-scheduler
  11. uid: 93348ee8-a096-11e8-aa45-ac1f6b83a952
  12. subsets: null

可见,当前的 leader 为 m7-demo-136001 节点。

测试 kube-scheduler 集群的高可用

随便找一个或两个 master 节点,停掉 kube-scheduler 服务,看其它节点是否获取了 leader 权限(systemd 日志)。

查看输出的 metrics

注意:以下命令在 kube-scheduler 节点上执行。

kube-scheduler 监听 10251 端口,接收 http 请求:

  1. $ sudo netstat -lnpt|grep kube-sche
  2. tcp 0 0 127.0.0.1:10251 0.0.0.0:* LISTEN 15143/kube-schedule
  1. $ curl -s http://127.0.0.1:10251/metrics | head
  2. # HELP apiserver_audit_event_total Counter of audit events generated and sent to the audit backend.
  3. # TYPE apiserver_audit_event_total counter
  4. apiserver_audit_event_total 0
  5. # HELP go_gc_duration_seconds A summary of the GC invocation durations.
  6. # TYPE go_gc_duration_seconds summary
  7. go_gc_duration_seconds{quantile="0"} 9.7715e-05
  8. go_gc_duration_seconds{quantile="0.25"} 0.000107676
  9. go_gc_duration_seconds{quantile="0.5"} 0.00017868
  10. go_gc_duration_seconds{quantile="0.75"} 0.000262444
  11. go_gc_duration_seconds{quantile="1"} 0.001205223