tags: worker, kubelet

07-2.部署 kubelet 组件

kublet 运行在每个 worker 节点上,接收 kube-apiserver 发送的请求,管理 Pod 容器,执行交互式命令,如 exec、run、logs 等。

kublet 启动时自动向 kube-apiserver 注册节点信息,内置的 cadvisor 统计和监控节点的资源使用情况。

为确保安全,本文档只开启接收 https 请求的安全端口,对请求进行认证和授权,拒绝未授权的访问(如 apiserver、heapster)。

下载和分发 kubelet 二进制文件

参考 06-0.部署master节点.md

安装依赖包

参考 07-0.部署worker节点.md

创建 kubelet bootstrap kubeconfig 文件

  1. source /opt/k8s/bin/environment.sh
  2. for node_name in ${NODE_NAMES[@]}
  3. do
  4. echo ">>> ${node_name}"
  5. # 创建 token
  6. export BOOTSTRAP_TOKEN=$(kubeadm token create \
  7. --description kubelet-bootstrap-token \
  8. --groups system:bootstrappers:${node_name} \
  9. --kubeconfig ~/.kube/config)
  10. # 设置集群参数
  11. kubectl config set-cluster kubernetes \
  12. --certificate-authority=/etc/kubernetes/cert/ca.pem \
  13. --embed-certs=true \
  14. --server=${KUBE_APISERVER} \
  15. --kubeconfig=kubelet-bootstrap-${node_name}.kubeconfig
  16. # 设置客户端认证参数
  17. kubectl config set-credentials kubelet-bootstrap \
  18. --token=${BOOTSTRAP_TOKEN} \
  19. --kubeconfig=kubelet-bootstrap-${node_name}.kubeconfig
  20. # 设置上下文参数
  21. kubectl config set-context default \
  22. --cluster=kubernetes \
  23. --user=kubelet-bootstrap \
  24. --kubeconfig=kubelet-bootstrap-${node_name}.kubeconfig
  25. # 设置默认上下文
  26. kubectl config use-context default --kubeconfig=kubelet-bootstrap-${node_name}.kubeconfig
  27. done
  • 证书中写入 Token 而非证书,证书后续由 controller-manager 创建。

查看 kubeadm 为各节点创建的 token:

  1. $ kubeadm token list --kubeconfig ~/.kube/config
  2. TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA GROUPS
  3. k0s2bj.7nvw1zi1nalyz4gz 23h 2018-06-14T15:14:31+08:00 authentication,signing kubelet-bootstrap-token system:bootstrappers:kube-node1
  4. mkus5s.vilnjk3kutei600l 23h 2018-06-14T15:14:32+08:00 authentication,signing kubelet-bootstrap-token system:bootstrappers:kube-node3
  5. zkiem5.0m4xhw0jc8r466nk 23h 2018-06-14T15:14:32+08:00 authentication,signing kubelet-bootstrap-token system:bootstrappers:kube-node2
  • 创建的 token 有效期为 1 天,超期后将不能再被使用,且会被 kube-controller-manager 的 tokencleaner 清理(如果启用该 controller 的话);
  • kube-apiserver 接收 kubelet 的 bootstrap token 后,将请求的 user 设置为 system:bootstrap:,group 设置为 system:bootstrappers;

各 token 关联的 Secret:

  1. $ kubectl get secrets -n kube-system
  2. NAME TYPE DATA AGE
  3. bootstrap-token-k0s2bj bootstrap.kubernetes.io/token 7 1m
  4. bootstrap-token-mkus5s bootstrap.kubernetes.io/token 7 1m
  5. bootstrap-token-zkiem5 bootstrap.kubernetes.io/token 7 1m
  6. default-token-99st7 kubernetes.io/service-account-token 3 2d

分发 bootstrap kubeconfig 文件到所有 worker 节点

  1. source /opt/k8s/bin/environment.sh
  2. for node_name in ${NODE_NAMES[@]}
  3. do
  4. echo ">>> ${node_name}"
  5. scp kubelet-bootstrap-${node_name}.kubeconfig k8s@${node_name}:/etc/kubernetes/kubelet-bootstrap.kubeconfig
  6. done

创建和分发 kubelet 参数配置文件

从 v1.10 开始,kubelet 部分参数需在配置文件中配置,kubelet --help 会提示:

  1. DEPRECATED: This parameter should be set via the config file specified by the Kubelet's --config flag

创建 kubelet 参数配置模板文件:

  1. source /opt/k8s/bin/environment.sh
  2. cat > kubelet.config.json.template <<EOF
  3. {
  4. "kind": "KubeletConfiguration",
  5. "apiVersion": "kubelet.config.k8s.io/v1beta1",
  6. "authentication": {
  7. "x509": {
  8. "clientCAFile": "/etc/kubernetes/cert/ca.pem"
  9. },
  10. "webhook": {
  11. "enabled": true,
  12. "cacheTTL": "2m0s"
  13. },
  14. "anonymous": {
  15. "enabled": false
  16. }
  17. },
  18. "authorization": {
  19. "mode": "Webhook",
  20. "webhook": {
  21. "cacheAuthorizedTTL": "5m0s",
  22. "cacheUnauthorizedTTL": "30s"
  23. }
  24. },
  25. "address": "##NODE_IP##",
  26. "port": 10250,
  27. "readOnlyPort": 0,
  28. "cgroupDriver": "cgroupfs",
  29. "hairpinMode": "promiscuous-bridge",
  30. "serializeImagePulls": false,
  31. "featureGates": {
  32. "RotateKubeletClientCertificate": true,
  33. "RotateKubeletServerCertificate": true
  34. },
  35. "clusterDomain": "${CLUSTER_DNS_DOMAIN}",
  36. "clusterDNS": ["${CLUSTER_DNS_SVC_IP}"]
  37. }
  38. EOF
  • address:API 监听地址,不能为 127.0.0.1,否则 kube-apiserver、heapster 等不能调用 kubelet 的 API;
  • readOnlyPort=0:关闭只读端口(默认 10255),等效为未指定;
  • authentication.anonymous.enabled:设置为 false,不允许匿名访问 10250 端口;
  • authentication.x509.clientCAFile:指定签名客户端证书的 CA 证书,开启 HTTP 证书认证;
  • authentication.webhook.enabled=true:开启 HTTPs bearer token 认证;
  • 对于未通过 x509 证书和 webhook 认证的请求(kube-apiserver 或其他客户端),将被拒绝,提示 Unauthorized;
  • authroization.mode=Webhook:kubelet 使用 SubjectAccessReview API 查询 kube-apiserver 某 user、group 是否具有操作资源的权限(RBAC);
  • featureGates.RotateKubeletClientCertificate、featureGates.RotateKubeletServerCertificate:自动 rotate 证书,证书的有效期取决于 kube-controller-manager 的 —experimental-cluster-signing-duration 参数;
  • 需要 root 账户运行;

为各节点创建和分发 kubelet 配置文件:

  1. source /opt/k8s/bin/environment.sh
  2. for node_ip in ${NODE_IPS[@]}
  3. do
  4. echo ">>> ${node_ip}"
  5. sed -e "s/##NODE_IP##/${node_ip}/" kubelet.config.json.template > kubelet.config-${node_ip}.json
  6. scp kubelet.config-${node_ip}.json root@${node_ip}:/etc/kubernetes/kubelet.config.json
  7. done

替换后的 kubelet.config.json 文件: kubelet.config.json

创建和分发 kubelet systemd unit 文件

创建 kubelet systemd unit 文件模板:

  1. cat > kubelet.service.template <<EOF
  2. [Unit]
  3. Description=Kubernetes Kubelet
  4. Documentation=https://github.com/GoogleCloudPlatform/kubernetes
  5. After=docker.service
  6. Requires=docker.service
  7. [Service]
  8. WorkingDirectory=/var/lib/kubelet
  9. ExecStart=/opt/k8s/bin/kubelet \\
  10. --bootstrap-kubeconfig=/etc/kubernetes/kubelet-bootstrap.kubeconfig \\
  11. --cert-dir=/etc/kubernetes/cert \\
  12. --kubeconfig=/etc/kubernetes/kubelet.kubeconfig \\
  13. --config=/etc/kubernetes/kubelet.config.json \\
  14. --hostname-override=##NODE_NAME## \\
  15. --pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest \\
  16. --allow-privileged=true \\
  17. --alsologtostderr=true \\
  18. --logtostderr=false \\
  19. --log-dir=/var/log/kubernetes \\
  20. --v=2
  21. Restart=on-failure
  22. RestartSec=5
  23. [Install]
  24. WantedBy=multi-user.target
  25. EOF
  • 如果设置了 --hostname-override 选项,则 kube-proxy 也需要设置该选项,否则会出现找不到 Node 的情况;
  • --bootstrap-kubeconfig:指向 bootstrap kubeconfig 文件,kubelet 使用该文件中的用户名和 token 向 kube-apiserver 发送 TLS Bootstrapping 请求;
  • K8S approve kubelet 的 csr 请求后,在 --cert-dir 目录创建证书和私钥文件,然后写入 --kubeconfig 文件;

替换后的 unit 文件:kubelet.service

为各节点创建和分发 kubelet systemd unit 文件:

  1. source /opt/k8s/bin/environment.sh
  2. for node_name in ${NODE_NAMES[@]}
  3. do
  4. echo ">>> ${node_name}"
  5. sed -e "s/##NODE_NAME##/${node_name}/" kubelet.service.template > kubelet-${node_name}.service
  6. scp kubelet-${node_name}.service root@${node_name}:/etc/systemd/system/kubelet.service
  7. done

Bootstrap Token Auth 和授予权限

kublet 启动时查找配置的 —kubeletconfig 文件是否存在,如果不存在则使用 —bootstrap-kubeconfig 向 kube-apiserver 发送证书签名请求 (CSR)。

kube-apiserver 收到 CSR 请求后,对其中的 Token 进行认证(事先使用 kubeadm 创建的 token),认证通过后将请求的 user 设置为 system:bootstrap:,group 设置为 system:bootstrappers,这一过程称为 Bootstrap Token Auth。

默认情况下,这个 user 和 group 没有创建 CSR 的权限,kubelet 启动失败,错误日志如下:

  1. $ sudo journalctl -u kubelet -a |grep -A 2 'certificatesigningrequests'
  2. May 06 06:42:36 kube-node1 kubelet[26986]: F0506 06:42:36.314378 26986 server.go:233] failed to run Kubelet: cannot create certificate signing request: certificatesigningrequests.certificates.k8s.io is forbidden: User "system:bootstrap:lemy40" cannot create certificatesigningrequests.certificates.k8s.io at the cluster scope
  3. May 06 06:42:36 kube-node1 systemd[1]: kubelet.service: Main process exited, code=exited, status=255/n/a
  4. May 06 06:42:36 kube-node1 systemd[1]: kubelet.service: Failed with result 'exit-code'.

解决办法是:创建一个 clusterrolebinding,将 group system:bootstrappers 和 clusterrole system:node-bootstrapper 绑定:

  1. $ kubectl create clusterrolebinding kubelet-bootstrap --clusterrole=system:node-bootstrapper --group=system:bootstrappers

启动 kubelet 服务

  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 /var/lib/kubelet"
  6. ssh root@${node_ip} "/usr/sbin/swapoff -a"
  7. ssh root@${node_ip} "mkdir -p /var/log/kubernetes && chown -R k8s /var/log/kubernetes"
  8. ssh root@${node_ip} "systemctl daemon-reload && systemctl enable kubelet && systemctl restart kubelet"
  9. done
  • 关闭 swap 分区,否则 kubelet 会启动失败;
  • 必须先创建工作和日志目录;
  1. $ journalctl -u kubelet |tail
  2. Jun 13 16:05:40 kube-node2 kubelet[22343]: I0613 16:05:40.388242 22343 feature_gate.go:226] feature gates: &{{} map[RotateKubeletServerCertificate:true RotateKubeletClientCertificate:true]}
  3. Jun 13 16:05:40 kube-node2 kubelet[22343]: I0613 16:05:40.394342 22343 mount_linux.go:211] Detected OS with systemd
  4. Jun 13 16:05:40 kube-node2 kubelet[22343]: W0613 16:05:40.394494 22343 cni.go:171] Unable to update cni config: No networks found in /etc/cni/net.d
  5. Jun 13 16:05:40 kube-node2 kubelet[22343]: I0613 16:05:40.399508 22343 server.go:376] Version: v1.10.4
  6. Jun 13 16:05:40 kube-node2 kubelet[22343]: I0613 16:05:40.399583 22343 feature_gate.go:226] feature gates: &{{} map[RotateKubeletServerCertificate:true RotateKubeletClientCertificate:true]}
  7. Jun 13 16:05:40 kube-node2 kubelet[22343]: I0613 16:05:40.399736 22343 plugins.go:89] No cloud provider specified.
  8. Jun 13 16:05:40 kube-node2 kubelet[22343]: I0613 16:05:40.399752 22343 server.go:492] No cloud provider specified: "" from the config file: ""
  9. Jun 13 16:05:40 kube-node2 kubelet[22343]: I0613 16:05:40.399777 22343 bootstrap.go:58] Using bootstrap kubeconfig to generate TLS client cert, key and kubeconfig file
  10. Jun 13 16:05:40 kube-node2 kubelet[22343]: I0613 16:05:40.446068 22343 csr.go:105] csr for this node already exists, reusing
  11. Jun 13 16:05:40 kube-node2 kubelet[22343]: I0613 16:05:40.453761 22343 csr.go:113] csr for this node is still valid

kubelet 启动后使用 —bootstrap-kubeconfig 向 kube-apiserver 发送 CSR 请求,当这个 CSR 被 approve 后,kube-controller-manager 为 kubelet 创建 TLS 客户端证书、私钥和 —kubeletconfig 文件。

注意:kube-controller-manager 需要配置 --cluster-signing-cert-file--cluster-signing-key-file 参数,才会为 TLS Bootstrap 创建证书和私钥。

  1. $ kubectl get csr
  2. NAME AGE REQUESTOR CONDITION
  3. node-csr-QzuuQiuUfcSdp3j5W4B2UOuvQ_n9aTNHAlrLzVFiqrk 43s system:bootstrap:zkiem5 Pending
  4. node-csr-oVbPmU-ikVknpynwu0Ckz_MvkAO_F1j0hmbcDa__sGA 27s system:bootstrap:mkus5s Pending
  5. node-csr-u0E1-ugxgotO_9FiGXo8DkD6a7-ew8sX2qPE6KPS2IY 13m system:bootstrap:k0s2bj Pending
  6. $ kubectl get nodes
  7. No resources found.
  • 三个 work 节点的 csr 均处于 pending 状态;

approve kubelet CSR 请求

可以手动或自动 approve CSR 请求。推荐使用自动的方式,因为从 v1.8 版本开始,可以自动轮转approve csr 后生成的证书。

手动 approve CSR 请求

查看 CSR 列表:

  1. $ kubectl get csr
  2. NAME AGE REQUESTOR CONDITION
  3. node-csr-QzuuQiuUfcSdp3j5W4B2UOuvQ_n9aTNHAlrLzVFiqrk 43s system:bootstrap:zkiem5 Pending
  4. node-csr-oVbPmU-ikVknpynwu0Ckz_MvkAO_F1j0hmbcDa__sGA 27s system:bootstrap:mkus5s Pending
  5. node-csr-u0E1-ugxgotO_9FiGXo8DkD6a7-ew8sX2qPE6KPS2IY 13m system:bootstrap:k0s2bj Pending

approve CSR:

  1. $ kubectl certificate approve node-csr-QzuuQiuUfcSdp3j5W4B2UOuvQ_n9aTNHAlrLzVFiqrk
  2. certificatesigningrequest.certificates.k8s.io "node-csr-QzuuQiuUfcSdp3j5W4B2UOuvQ_n9aTNHAlrLzVFiqrk" approved

查看 Approve 结果:

  1. $ kubectl describe csr node-csr-QzuuQiuUfcSdp3j5W4B2UOuvQ_n9aTNHAlrLzVFiqrk
  2. Name: node-csr-QzuuQiuUfcSdp3j5W4B2UOuvQ_n9aTNHAlrLzVFiqrk
  3. Labels: <none>
  4. Annotations: <none>
  5. CreationTimestamp: Wed, 13 Jun 2018 16:05:04 +0800
  6. Requesting User: system:bootstrap:zkiem5
  7. Status: Approved
  8. Subject:
  9. Common Name: system:node:kube-node2
  10. Serial Number:
  11. Organization: system:nodes
  12. Events: <none>
  • Requesting User:请求 CSR 的用户,kube-apiserver 对它进行认证和授权;
  • Subject:请求签名的证书信息;
  • 证书的 CN 是 system:node:kube-node2, Organization 是 system:nodes,kube-apiserver 的 Node 授权模式会授予该证书的相关权限;

自动 approve CSR 请求

创建三个 ClusterRoleBinding,分别用于自动 approve client、renew client、renew server 证书:

  1. cat > csr-crb.yaml <<EOF
  2. # Approve all CSRs for the group "system:bootstrappers"
  3. kind: ClusterRoleBinding
  4. apiVersion: rbac.authorization.k8s.io/v1
  5. metadata:
  6. name: auto-approve-csrs-for-group
  7. subjects:
  8. - kind: Group
  9. name: system:bootstrappers
  10. apiGroup: rbac.authorization.k8s.io
  11. roleRef:
  12. kind: ClusterRole
  13. name: system:certificates.k8s.io:certificatesigningrequests:nodeclient
  14. apiGroup: rbac.authorization.k8s.io
  15. ---
  16. # To let a node of the group "system:nodes" renew its own credentials
  17. kind: ClusterRoleBinding
  18. apiVersion: rbac.authorization.k8s.io/v1
  19. metadata:
  20. name: node-client-cert-renewal
  21. subjects:
  22. - kind: Group
  23. name: system:nodes
  24. apiGroup: rbac.authorization.k8s.io
  25. roleRef:
  26. kind: ClusterRole
  27. name: system:certificates.k8s.io:certificatesigningrequests:selfnodeclient
  28. apiGroup: rbac.authorization.k8s.io
  29. ---
  30. # A ClusterRole which instructs the CSR approver to approve a node requesting a
  31. # serving cert matching its client cert.
  32. kind: ClusterRole
  33. apiVersion: rbac.authorization.k8s.io/v1
  34. metadata:
  35. name: approve-node-server-renewal-csr
  36. rules:
  37. - apiGroups: ["certificates.k8s.io"]
  38. resources: ["certificatesigningrequests/selfnodeserver"]
  39. verbs: ["create"]
  40. ---
  41. # To let a node of the group "system:nodes" renew its own server credentials
  42. kind: ClusterRoleBinding
  43. apiVersion: rbac.authorization.k8s.io/v1
  44. metadata:
  45. name: node-server-cert-renewal
  46. subjects:
  47. - kind: Group
  48. name: system:nodes
  49. apiGroup: rbac.authorization.k8s.io
  50. roleRef:
  51. kind: ClusterRole
  52. name: approve-node-server-renewal-csr
  53. apiGroup: rbac.authorization.k8s.io
  54. EOF
  • auto-approve-csrs-for-group:自动 approve node 的第一次 CSR; 注意第一次 CSR 时,请求的 Group 为 system:bootstrappers;
  • node-client-cert-renewal:自动 approve node 后续过期的 client 证书,自动生成的证书 Group 为 system:nodes;
  • node-server-cert-renewal:自动 approve node 后续过期的 server 证书,自动生成的证书 Group 为 system:nodes;

生效配置:

  1. $ kubectl apply -f csr-crb.yaml

查看 kublet 的情况

等待一段时间(1-10 分钟),三个节点的 CSR 都被自动 approve:

  1. $ kubectl get csr
  2. NAME AGE REQUESTOR CONDITION
  3. csr-98h25 6m system:node:kube-node2 Approved,Issued
  4. csr-lb5c9 7m system:node:kube-node3 Approved,Issued
  5. csr-m2hn4 14m system:node:kube-node1 Approved,Issued
  6. node-csr-7q7i0q4MF_K2TSEJj16At4CJFLlJkHIqei6nMIAaJCU 28m system:bootstrap:k0s2bj Approved,Issued
  7. node-csr-ND77wk2P8k2lHBtgBaObiyYw0uz1Um7g2pRvveMF-c4 35m system:bootstrap:mkus5s Approved,Issued
  8. node-csr-Nysmrw55nnM48NKwEJuiuCGmZoxouK4N8jiEHBtLQso 6m system:bootstrap:zkiem5 Approved,Issued
  9. node-csr-QzuuQiuUfcSdp3j5W4B2UOuvQ_n9aTNHAlrLzVFiqrk 1h system:bootstrap:zkiem5 Approved,Issued
  10. node-csr-oVbPmU-ikVknpynwu0Ckz_MvkAO_F1j0hmbcDa__sGA 1h system:bootstrap:mkus5s Approved,Issued
  11. node-csr-u0E1-ugxgotO_9FiGXo8DkD6a7-ew8sX2qPE6KPS2IY 1h system:bootstrap:k0s2bj Approved,Issued

所有节点均 ready:

  1. $ kubectl get nodes
  2. NAME STATUS ROLES AGE VERSION
  3. kube-node1 Ready <none> 18m v1.10.4
  4. kube-node2 Ready <none> 10m v1.10.4
  5. kube-node3 Ready <none> 11m v1.10.4

kube-controller-manager 为各 node 生成了 kubeconfig 文件和公私钥:

  1. $ ls -l /etc/kubernetes/kubelet.kubeconfig
  2. -rw------- 1 root root 2293 Jun 13 17:07 /etc/kubernetes/kubelet.kubeconfig
  3. $ ls -l /etc/kubernetes/cert/|grep kubelet
  4. -rw-r--r-- 1 root root 1046 Jun 13 17:07 kubelet-client.crt
  5. -rw------- 1 root root 227 Jun 13 17:07 kubelet-client.key
  6. -rw------- 1 root root 1334 Jun 13 17:07 kubelet-server-2018-06-13-17-07-45.pem
  7. lrwxrwxrwx 1 root root 58 Jun 13 17:07 kubelet-server-current.pem -> /etc/kubernetes/cert/kubelet-server-2018-06-13-17-07-45.pem
  • kubelet-server 证书会周期轮转;

kubelet 提供的 API 接口

kublet 启动后监听多个端口,用于接收 kube-apiserver 或其它组件发送的请求:

  1. $ sudo netstat -lnpt|grep kubelet
  2. tcp 0 0 172.27.129.111:4194 0.0.0.0:* LISTEN 2490/kubelet
  3. tcp 0 0 127.0.0.1:10248 0.0.0.0:* LISTEN 2490/kubelet
  4. tcp 0 0 172.27.129.111:10250 0.0.0.0:* LISTEN 2490/kubelet
  • 4194: cadvisor http 服务;
  • 10248: healthz http 服务;
  • 10250: https API 服务;注意:未开启只读端口 10255;

例如执行 kubectl ec -it nginx-ds-5rmws -- sh 命令时,kube-apiserver 会向 kubelet 发送如下请求:

  1. POST /exec/default/nginx-ds-5rmws/my-nginx?command=sh&input=1&output=1&tty=1

kubelet 接收 10250 端口的 https 请求:

  • /pods、/runningpods
  • /metrics、/metrics/cadvisor、/metrics/probes
  • /spec
  • /stats、/stats/container
  • /logs
  • /run/、”/exec/“, “/attach/“, “/portForward/“, “/containerLogs/“ 等管理;

详情参考:https://github.com/kubernetes/kubernetes/blob/master/pkg/kubelet/server/server.go#L434:3

由于关闭了匿名认证,同时开启了 webhook 授权,所有访问 10250 端口 https API 的请求都需要被认证和授权。

预定义的 ClusterRole system:kubelet-api-admin 授予访问 kubelet 所有 API 的权限:

  1. $ kubectl describe clusterrole system:kubelet-api-admin
  2. Name: system:kubelet-api-admin
  3. Labels: kubernetes.io/bootstrapping=rbac-defaults
  4. Annotations: rbac.authorization.kubernetes.io/autoupdate=true
  5. PolicyRule:
  6. Resources Non-Resource URLs Resource Names Verbs
  7. --------- ----------------- -------------- -----
  8. nodes [] [] [get list watch proxy]
  9. nodes/log [] [] [*]
  10. nodes/metrics [] [] [*]
  11. nodes/proxy [] [] [*]
  12. nodes/spec [] [] [*]
  13. nodes/stats [] [] [*]

kublet api 认证和授权

kublet 配置了如下认证参数:

  • authentication.anonymous.enabled:设置为 false,不允许匿名访问 10250 端口;
  • authentication.x509.clientCAFile:指定签名客户端证书的 CA 证书,开启 HTTPs 证书认证;
  • authentication.webhook.enabled=true:开启 HTTPs bearer token 认证;

同时配置了如下授权参数:

  • authroization.mode=Webhook:开启 RBAC 授权;

kubelet 收到请求后,使用 clientCAFile 对证书签名进行认证,或者查询 bearer token 是否有效。如果两者都没通过,则拒绝请求,提示 Unauthorized:

  1. $ curl -s --cacert /etc/kubernetes/cert/ca.pem https://172.27.129.111:10250/metrics
  2. Unauthorized
  3. $ curl -s --cacert /etc/kubernetes/cert/ca.pem -H "Authorization: Bearer 123456" https://172.27.129.111:10250/metrics
  4. Unauthorized

通过认证后,kubelet 使用 SubjectAccessReview API 向 kube-apiserver 发送请求,查询证书或 token 对应的 user、group 是否有操作资源的权限(RBAC);

证书认证和授权:

  1. $ # 权限不足的证书;
  2. $ curl -s --cacert /etc/kubernetes/cert/ca.pem --cert /etc/kubernetes/cert/kube-controller-manager.pem --key /etc/kubernetes/cert/kube-controller-manager-key.pem https://172.27.129.111:10250/metrics
  3. Forbidden (user=system:kube-controller-manager, verb=get, resource=nodes, subresource=metrics)
  4. $ # 使用部署 kubectl 命令行工具时创建的、具有最高权限的 admin 证书;
  5. $ curl -s --cacert /etc/kubernetes/cert/ca.pem --cert ./admin.pem --key ./admin-key.pem https://172.27.129.111:10250/metrics|head
  6. # HELP apiserver_client_certificate_expiration_seconds Distribution of the remaining lifetime on the certificate used to authenticate a request.
  7. # TYPE apiserver_client_certificate_expiration_seconds histogram
  8. apiserver_client_certificate_expiration_seconds_bucket{le="0"} 0
  9. apiserver_client_certificate_expiration_seconds_bucket{le="21600"} 0
  10. apiserver_client_certificate_expiration_seconds_bucket{le="43200"} 0
  11. apiserver_client_certificate_expiration_seconds_bucket{le="86400"} 0
  12. apiserver_client_certificate_expiration_seconds_bucket{le="172800"} 0
  13. apiserver_client_certificate_expiration_seconds_bucket{le="345600"} 0
  14. apiserver_client_certificate_expiration_seconds_bucket{le="604800"} 0
  15. apiserver_client_certificate_expiration_seconds_bucket{le="2.592e+06"} 0
  • --cacert--cert--key 的参数值必须是文件路径,如上面的 ./admin.pem 不能省略 ./,否则返回 401 Unauthorized

bear token 认证和授权:

创建一个 ServiceAccount,将它和 ClusterRole system:kubelet-api-admin 绑定,从而具有调用 kubelet API 的权限:

  1. kubectl create sa kubelet-api-test
  2. kubectl create clusterrolebinding kubelet-api-test --clusterrole=system:kubelet-api-admin --serviceaccount=default:kubelet-api-test
  3. SECRET=$(kubectl get secrets | grep kubelet-api-test | awk '{print $1}')
  4. TOKEN=$(kubectl describe secret ${SECRET} | grep -E '^token' | awk '{print $2}')
  5. echo ${TOKEN}
  6. $ curl -s --cacert /etc/kubernetes/cert/ca.pem -H "Authorization: Bearer ${TOKEN}" https://172.27.129.111:10250/metrics|head
  7. # HELP apiserver_client_certificate_expiration_seconds Distribution of the remaining lifetime on the certificate used to authenticate a request.
  8. # TYPE apiserver_client_certificate_expiration_seconds histogram
  9. apiserver_client_certificate_expiration_seconds_bucket{le="0"} 0
  10. apiserver_client_certificate_expiration_seconds_bucket{le="21600"} 0
  11. apiserver_client_certificate_expiration_seconds_bucket{le="43200"} 0
  12. apiserver_client_certificate_expiration_seconds_bucket{le="86400"} 0
  13. apiserver_client_certificate_expiration_seconds_bucket{le="172800"} 0
  14. apiserver_client_certificate_expiration_seconds_bucket{le="345600"} 0
  15. apiserver_client_certificate_expiration_seconds_bucket{le="604800"} 0
  16. apiserver_client_certificate_expiration_seconds_bucket{le="2.592e+06"} 0

cadvisor 和 metrics

cadvisor 统计所在节点各容器的资源(CPU、内存、磁盘、网卡)使用情况,分别在自己的 http web 页面(4194 端口)和 10250 以 promehteus metrics 的形式输出。

浏览器访问 http://172.27.129.105:4194/containers/ 可以查看到 cadvisor 的监控页面:

cadvisor-home

浏览器访问 https://172.27.129.80:10250/metricshttps://172.27.129.80:10250/metrics/cadvisor 分别返回 kublet 和 cadvisor 的 metrics。

cadvisor-metrics

注意:

  • kublet.config.json 设置 authentication.anonymous.enabled 为 false,不允许匿名证书访问 10250 的 https 服务;
  • 参考A.浏览器访问kube-apiserver安全端口.md,创建和导入相关证书,然后访问上面的 10250 端口;

获取 kublet 的配置

从 kube-apiserver 获取各 node 的配置:

  1. $ source /opt/k8s/bin/environment.sh
  2. $ # 使用部署 kubectl 命令行工具时创建的、具有最高权限的 admin 证书;
  3. $ curl -sSL --cacert /etc/kubernetes/cert/ca.pem --cert ./admin.pem --key ./admin-key.pem ${KUBE_APISERVER}/api/v1/nodes/kube-node1/proxy/configz | jq \
  4. '.kubeletconfig|.kind="KubeletConfiguration"|.apiVersion="kubelet.config.k8s.io/v1beta1"'
  5. {
  6. "syncFrequency": "1m0s",
  7. "fileCheckFrequency": "20s",
  8. "httpCheckFrequency": "20s",
  9. "address": "172.27.129.80",
  10. "port": 10250,
  11. "readOnlyPort": 10255,
  12. "authentication": {
  13. "x509": {},
  14. "webhook": {
  15. "enabled": false,
  16. "cacheTTL": "2m0s"
  17. },
  18. "anonymous": {
  19. "enabled": true
  20. }
  21. },
  22. "authorization": {
  23. "mode": "AlwaysAllow",
  24. "webhook": {
  25. "cacheAuthorizedTTL": "5m0s",
  26. "cacheUnauthorizedTTL": "30s"
  27. }
  28. },
  29. "registryPullQPS": 5,
  30. "registryBurst": 10,
  31. "eventRecordQPS": 5,
  32. "eventBurst": 10,
  33. "enableDebuggingHandlers": true,
  34. "healthzPort": 10248,
  35. "healthzBindAddress": "127.0.0.1",
  36. "oomScoreAdj": -999,
  37. "clusterDomain": "cluster.local.",
  38. "clusterDNS": [
  39. "10.254.0.2"
  40. ],
  41. "streamingConnectionIdleTimeout": "4h0m0s",
  42. "nodeStatusUpdateFrequency": "10s",
  43. "imageMinimumGCAge": "2m0s",
  44. "imageGCHighThresholdPercent": 85,
  45. "imageGCLowThresholdPercent": 80,
  46. "volumeStatsAggPeriod": "1m0s",
  47. "cgroupsPerQOS": true,
  48. "cgroupDriver": "cgroupfs",
  49. "cpuManagerPolicy": "none",
  50. "cpuManagerReconcilePeriod": "10s",
  51. "runtimeRequestTimeout": "2m0s",
  52. "hairpinMode": "promiscuous-bridge",
  53. "maxPods": 110,
  54. "podPidsLimit": -1,
  55. "resolvConf": "/etc/resolv.conf",
  56. "cpuCFSQuota": true,
  57. "maxOpenFiles": 1000000,
  58. "contentType": "application/vnd.kubernetes.protobuf",
  59. "kubeAPIQPS": 5,
  60. "kubeAPIBurst": 10,
  61. "serializeImagePulls": false,
  62. "evictionHard": {
  63. "imagefs.available": "15%",
  64. "memory.available": "100Mi",
  65. "nodefs.available": "10%",
  66. "nodefs.inodesFree": "5%"
  67. },
  68. "evictionPressureTransitionPeriod": "5m0s",
  69. "enableControllerAttachDetach": true,
  70. "makeIPTablesUtilChains": true,
  71. "iptablesMasqueradeBit": 14,
  72. "iptablesDropBit": 15,
  73. "featureGates": {
  74. "RotateKubeletClientCertificate": true,
  75. "RotateKubeletServerCertificate": true
  76. },
  77. "failSwapOn": true,
  78. "containerLogMaxSize": "10Mi",
  79. "containerLogMaxFiles": 5,
  80. "enforceNodeAllocatable": [
  81. "pods"
  82. ],
  83. "kind": "KubeletConfiguration",
  84. "apiVersion": "kubelet.config.k8s.io/v1beta1"
  85. }

或者参考代码中的注释:https://github.com/kubernetes/kubernetes/blob/master/pkg/kubelet/apis/kubeletconfig/v1beta1/types.go

参考

  1. kubelet 认证和授权:https://kubernetes.io/docs/reference/command-line-tools-reference/kubelet-authentication-authorization/