二进制安装kubernetes v1.11.2 (第十三章 kubelet部署)
继续前一章的部署。
十三、kubelet 部署
kubelet 运行在 node 节点上,接收 kube-apiserver 发送的请求,管理pod容器,执行交互式命令,如exec、run、logs等。
kubelet 启动时向 kube-apiserver 注册信息,内置的 cadvisor 统计和监控节点的资源使用情况。
为确保安全,本文档只开启接收 https请求的安全端口,对请求进行认证和授权,拒绝未授权的访问(比如apiserver、heapster)。
13.1 下载和分发二进制文件,参考 第三章
分发到各节点
source /opt/k8s/bin/environment.sh for node_name in ${NODE_NAMES[@]} do echo ">>> ${node_name}" scp k8s/v1.11.2/server/kubernetes/server/bin/{kube-proxy,kubelet} k8s@${node_name}:/opt/k8s/bin/ ssh k8s@${node_name} "chmod 755 /opt/k8s/bin/{kube-proxy,kubelet}" done
13.2 安装依赖包,参考 第十一章
13.3 创建 kubelet bootstrap kubeconfig 文件
source /opt/k8s/bin/environment.sh
for node_name in ${NODE_NAMES[@]}
do echo ">>> ${node_name}" # 创建 token export BOOTSTRAP_TOKEN=$(kubeadm token create \ --description kubelet-bootstrap-token \ --groups system:bootstrappers:${node_name} \ --kubeconfig ~/.kube/config) # 设置集群参数 kubectl config set-cluster kubernetes \ --certificate-authority=/etc/kubernetes/cert/ca.pem \ --embed-certs=true \ --server=${KUBE_APISERVER} \ --kubeconfig=kubelet-bootstrap-${node_name}.kubeconfig # 设置客户端认证参数 kubectl config set-credentials kubelet-bootstrap \ --token=${BOOTSTRAP_TOKEN} \ --kubeconfig=kubelet-bootstrap-${node_name}.kubeconfig # 设置上下文参数 kubectl config set-context default \ --cluster=kubernetes \ --user=kubelet-bootstrap \ --kubeconfig=kubelet-bootstrap-${node_name}.kubeconfig # 设置默认上下文 kubectl config use-context default --kubeconfig=kubelet-bootstrap-${node_name}.kubeconfig done
- 证书中写入 token 而非证书,证书后续由 controller-manager 创建
查看 kubeadm 为各节点创建的token:
[k8s@k8s-m1 ~]$ kubeadm token list --kubeconfig ~/.kube/config TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA GROUPS 7lvdb6.afnskmo3ei3spwcm 23h 2018-11-02T14:55:32+08:00 authentication,signing kubelet-bootstrap-token system:bootstrappers:k8s-m1 fgzg0j.moacwq8c71gchq9d 23h 2018-11-02T14:55:33+08:00 authentication,signing kubelet-bootstrap-token system:bootstrappers:k8s-n2 n5sq59.2c3zgwyzukrxg9qm 23h 2018-11-02T14:55:32+08:00 authentication,signing kubelet-bootstrap-token system:bootstrappers:k8s-n1 ncgv6p.6eitzxvw2hrivbsz 23h 2018-11-02T14:55:32+08:00 authentication,signing kubelet-bootstrap-token system:bootstrappers:k8s-m2
- 创建的 token 有效期为1天,超气候将不能再被使用,且会被 kube-controller 的tokencleaner 清理(如果启用该 controller )的话
- kube-apiserver 接收 kubelet 的 bootstrap token 后,将请求的 user 设置为 system:bootstrap; group 设置为 system:bootstrappers
各 token 关联的 secret:
kubectl get secrets -n kube-system
分发 bootstrap kubeconfig 文件到所有 node 节点
source /opt/k8s/bin/environment.sh for node_name in ${NODE_NAMES[@]} do echo ">>> ${node_name}" scp kubelet-bootstrap-${node_name}.kubeconfig k8s@${node_name}:/etc/kubernetes/kubelet-bootstrap.kubeconfig done
13.4 创建和分发 kubelet 参数配置文件
source /opt/k8s/bin/environment.sh cat > kubelet.config.json.template <<EOF { "kind": "KubeletConfiguration", "apiVersion": "kubelet.config.k8s.io/v1beta1", "authentication": { "x509": { "clientCAFile": "/etc/kubernetes/cert/ca.pem" }, "webhook": { "enabled": true, "cacheTTL": "2m0s" }, "anonymous": { "enabled": false } }, "authorization": { "mode": "Webhook", "webhook": { "cacheAuthorizedTTL": "5m0s", "cacheUnauthorizedTTL": "30s" } }, "address": "##NODE_IP##", "port": 10250, "readOnlyPort": 0, "cgroupDriver": "cgroupfs", "hairpinMode": "promiscuous-bridge", "serializeImagePulls": false, "featureGates": { "RotateKubeletClientCertificate": true, "RotateKubeletServerCertificate": true }, "clusterDomain": "${CLUSTER_DNS_DOMAIN}", "clusterDNS": ["${CLUSTER_DNS_SVC_IP}"] } 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 配置文件:
source /opt/k8s/bin/environment.sh for node_ip in ${NODE_IPS[@]} do echo ">>> ${node_ip}" sed -e "s/##NODE_IP##/${node_ip}/" kubelet.config.json.template > kubelet.config-${node_ip}.json scp kubelet.config-${node_ip}.json root@${node_ip}:/etc/kubernetes/kubelet.config.json done
13.5 创建和分发 kubelet systemd unit 文件
cat > kubelet.service.template <<EOF [Unit] Description=Kubernetes Kubelet Documentation=https://github.com/GoogleCloudPlatform/kubernetes After=docker.service Requires=docker.service [Service] WorkingDirectory=/var/lib/kubelet ExecStart=/opt/k8s/bin/kubelet \\ --bootstrap-kubeconfig=/etc/kubernetes/kubelet-bootstrap.kubeconfig \\ --cert-dir=/etc/kubernetes/cert \\ --kubeconfig=/etc/kubernetes/kubelet.kubeconfig \\ --config=/etc/kubernetes/kubelet.config.json \\ --hostname-override=##NODE_NAME## \\ --pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest \\ --allow-privileged=true \\ --alsologtostderr=true \\ --logtostderr=false \\ --log-dir=/var/log/kubernetes \\ --v=2 Restart=on-failure RestartSec=5 [Install] WantedBy=multi-user.target EOF
- 如果设置了 --hostname-override 选项,则 kube-proxy 也需要设置该选项,否则会出现找不到 Node 的情况;
- --bootstrap-kubeconfig:指向 bootstrap kubeconfig 文件,kubelet 使用该文件中的用户名和 token 向 kube-apiserver 发送 TLS Bootstrapping 请求;
- K8S approve kubelet 的 csr 请求后,在 --cert-dir 目录创建证书和私钥文件,然后写入 --kubeconfig 文件;
为各节点创建和分发 kubelet systemd unit 文件
source /opt/k8s/bin/environment.sh for node_name in ${NODE_NAMES[@]} do echo ">>> ${node_name}" sed -e "s/##NODE_NAME##/${node_name}/" kubelet.service.template > kubelet-${node_name}.service scp kubelet-${node_name}.service root@${node_name}:/etc/systemd/system/kubelet.service done
13.6 bootstrap token auth 和授予权限
kubelet 启动时查找配置的 --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 启动失败,错误日志如下:
$ sudo journalctl -u kubelet -a |grep -A 2 'certificatesigningrequests' 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 May 06 06:42:36 kube-node1 systemd[1]: kubelet.service: Main process exited, code=exited, status=255/n/a May 06 06:42:36 kube-node1 systemd[1]: kubelet.service: Failed with result 'exit-code'.
解决办法是:创建一个 clusterrolebinding, 将 group system:bootstrappers 和 clusterrole system:node-bootstrape 绑定
kubectl create clusterrolebinding kubelet-bootstrap --clusterrole=system:node-bootstrapper --group=system:bootstrappers
13.7 启动 kubelet 服务
source /opt/k8s/bin/environment.sh for node_ip in ${NODE_IPS[@]} do echo ">>> ${node_ip}" ssh root@${node_ip} "mkdir -p /var/lib/kubelet" ssh root@${node_ip} "/usr/sbin/swapoff -a" ssh root@${node_ip} "mkdir -p /var/log/kubernetes && chown -R k8s /var/log/kubernetes" ssh root@${node_ip} "systemctl daemon-reload && systemctl enable kubelet && systemctl restart kubelet" done
- 关闭 swap 分区,否则 kubelet 会启动失败
- 必须创建工作和日志目录
- 查看日志: journalctl -u kubelet |tail
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 创建证书和私钥。
[k8s@k8s-m1 ~]$ kubectl get csr NAME AGE REQUESTOR CONDITION node-csr-aou6js9kpfqLw9Ap5PhEuVcOicXtlNuKhjqneAsJR3w 24m system:bootstrap:ncgv6p Pending node-csr-x8IqY1rLVCgG9uXwzQUI1pgyi6AJLH3pUba0ftSKYAk 24m system:bootstrap:7lvdb6 Pending [k8s@k8s-m1 ~]$ kubectl get nodes No resources found. [k8s@k8s-m1 ~]$
- 两个node的 csr 均处于 pending 状态,另外两台token不正确,还没加入进来(后查询原因是没有关闭swap)
13.8 approve kubelet csr 请求
可以通过手动和自动两种方式 approve csr 请求,推荐使用自动的方式。从1.8版本后,可以自动approve csr请求,生成证书。
手动 approve csr 请求
# 查看 csr 请求 [root@k8s-m1 node2]# kubectl get csr NAME AGE REQUESTOR CONDITION node-csr-3fwI6KAvaCo7woy_BNmqXuqalmpP1X7ck7qUpKKxOPI 10s system:bootstrap:00h95y Pending # approve csr [root@k8s-m1 node2]# kubectl certificate approve node-csr-3fwI6KAvaCo7woy_BNmqXuqalmpP1X7ck7qUpKKxOPI NAME AGE REQUESTOR CONDITION node-csr-3fwI6KAvaCo7woy_BNmqXuqalmpP1X7ck7qUpKKxOPI 2m system:bootstrap:00h95y Approved,Issued # 查看 approve 结果 [root@k8s-m1 node2]# kubectl describe csr node-csr-3fwI6KAvaCo7woy_BNmqXuqalmpP1X7ck7qUpKKxOPI Name: node-csr-3fwI6KAvaCo7woy_BNmqXuqalmpP1X7ck7qUpKKxOPI Labels: <none> Annotations: <none> CreationTimestamp: Tue, 06 Nov 2018 16:03:45 +0800 Requesting User: system:bootstrap:00h95y Status: Approved,Issued Subject: Common Name: system:node:k8s-m1 Serial Number: Organization: system:nodes Events: <none>
- Requesting User:请求 CSR 的用户,kube-apiserver 对它进行认证和授权;
- Subject:请求签名的证书信息;
- 证书的 CN 是 system:node:k8s-m1, Organization 是 system:nodes,kube-apiserver 的 Node 授权模式会授予该证书的相关权限;
approve csr 后,kube-controller-manager 为各 node 生成了 kubeconfig 文件和公私钥
[root@k8s-m1 template]# ls -l /etc/kubernetes/kubelet.kubeconfig -rw------- 1 root root 2309 Nov 6 16:04 /etc/kubernetes/kubelet.kubeconfig [root@k8s-m1 template]# ls -l /etc/kubernetes/cert/kubelet* -rw------- 1 root root 1269 Nov 6 16:04 /etc/kubernetes/cert/kubelet-client-2018-11-06-16-04-09.pem lrwxrwxrwx 1 root root 59 Nov 6 16:04 /etc/kubernetes/cert/kubelet-client-current.pem -> /etc/kubernetes/cert/kubelet-client-2018-11-06-16-04-09.pem -rw-r--r-- 1 root root 2153 Nov 6 15:33 /etc/kubernetes/cert/kubelet.crt -rw------- 1 root root 1675 Nov 6 15:33 /etc/kubernetes/cert/kubelet.key
自动 approve csr 请求
cat > csr-crb.yaml <<EOF # Approve all CSRs for the group "system:bootstrappers" kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: auto-approve-csrs-for-group subjects: - kind: Group name: system:bootstrappers apiGroup: rbac.authorization.k8s.io roleRef: kind: ClusterRole name: system:certificates.k8s.io:certificatesigningrequests:nodeclient apiGroup: rbac.authorization.k8s.io --- # To let a node of the group "system:nodes" renew its own credentials kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: node-client-cert-renewal subjects: - kind: Group name: system:nodes apiGroup: rbac.authorization.k8s.io roleRef: kind: ClusterRole name: system:certificates.k8s.io:certificatesigningrequests:selfnodeclient apiGroup: rbac.authorization.k8s.io --- # A ClusterRole which instructs the CSR approver to approve a node requesting a # serving cert matching its client cert. kind: ClusterRole apiVersion: rbac.authorization.k8s.io/v1 metadata: name: approve-node-server-renewal-csr rules: - apiGroups: ["certificates.k8s.io"] resources: ["certificatesigningrequests/selfnodeserver"] verbs: ["create"] --- # To let a node of the group "system:nodes" renew its own server credentials kind: ClusterRoleBinding apiVersion: rbac.authorization.k8s.io/v1 metadata: name: node-server-cert-renewal subjects: - kind: Group name: system:nodes apiGroup: rbac.authorization.k8s.io roleRef: kind: ClusterRole name: approve-node-server-renewal-csr apiGroup: rbac.authorization.k8s.io 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;
- 使用自动 approve csr 方式后,kubelet server 证书会周期轮转
生成配置:
kubectl apply -f csr-crb.yaml
---
启动 kubelet 时,需要关闭swap