K8S从入门到放弃系列-(9)kubernetes集群之kubelet部署
摘要:
Kubelet组件运行在Node节点上,维持运行中的Pods以及提供kuberntes运行时环境,主要完成以下使命:
1.监视分配给该Node节点的pods
2.挂载pod所需要的volumes
3.下载pod的secret
4.通过docker/rkt来运行pod中的容器
5.周期的执行pod中为容器定义的liveness探针
6.上报pod的状态给系统的其他组件
7.上报Node的状态
1、以下操作属于node节点上组件的部署,在master节点上只是进行文件配置,然后发布至各node节点。
2、若是需要master也作为node节点加入集群,也需要在master节点部署docker、kubelet、kube-proxy。
1)创建角色绑定
kubelet 启动时向 kube-apiserver 发送 TLS bootstrapping 请求,需要先将 bootstrap token 文件中的 kubelet-bootstrap 用户赋予 system:node-bootstrapper cluster 角色(role), 然后 kubelet 才能有权限创建认证请求(certificate signing requests):
[root@k8s-master01 ~]# kubectl create clusterrolebinding kubelet-bootstrap --clusterrole=system:node-bootstrapper --user=kubelet-bootstrap
--user=kubelet-bootstrap 是部署kube-apiserver时创建bootstrap-token.csv文件中指定的用户,同时也需要写入bootstrap.kubeconfig 文件
2)创建kubelet kubeconfig文件,设置集群参数
## 设置集群参数 [root@k8s-master01 ~]# kubectl config set-cluster kubernetes \ --certificate-authority=/etc/kubernetes/ssl/ca.pem \ --embed-certs=true \ --server=https://127.0.0.1:6443 \ --kubeconfig=bootstrap.kubeconfig Cluster "kubernetes" set. ## 设置客户端认证参数 ### tocker是前文提到的bootstrap-token.csv文件中token值 [root@k8s-master01 ~]# kubectl config set-credentials kubelet-bootstrap \ --token=fb8f04963e38858eab0867e8d2296d6b \ --kubeconfig=bootstrap.kubeconfig User "kubelet-bootstrap" set. ## 设置上下文参数 [root@k8s-master01 ~]# kubectl config set-context default \ --cluster=kubernetes \ --user=kubelet-bootstrap \ --kubeconfig=bootstrap.kubeconfig Context "default" created. ## 设置默认上下问参数 [root@k8s-master01 ~]# kubectl config use-context default --kubeconfig=bootstrap.kubeconfig Switched to context "default". ## 分发生成的集群配置文件到各node节点 [root@k8s-master01 ~]# ansible k8s-node -m copy -a 'src=/root/bootstrap.kubeconfig dest=/etc/kubernetes/config/'
3)创建系统核心配置文件服务
我们先在master节点配置好,然后用ansible分发至2各node节点,然后修改对应主机名及IP即可
[root@k8s-master01 ~]# vim /opt/k8s/cfg/kubelet.conf
###
# kubernetes kubelet (minion) config
# kubernetes kubelet (minion) config
# The address for the info server to serve on (set to 0.0.0.0 or "" for all interfaces)
KUBELET_ADDRESS="--node-ip=10.10.0.17"
KUBELET_ADDRESS="--node-ip=10.10.0.17"
# The port for the info server to serve on
# KUBELET_PORT="--port=10250"
# KUBELET_PORT="--port=10250"
# You may leave this blank to use the actual hostname
KUBELET_HOSTNAME="--hostname-override=k8s-node01"
KUBELET_HOSTNAME="--hostname-override=k8s-node01"
# location of the api-server
# KUBELET_API_SERVER=""
# KUBELET_API_SERVER=""
# Add your own!
KUBELET_ARGS=" --address=0.0.0.0 \
--allow-privileged \
--anonymous-auth=false \
--authentication-token-webhook=true \
--authorization-mode=Webhook \
--bootstrap-kubeconfig=/etc/kubernetes/config/bootstrap.kubeconfig \
--client-ca-file=/etc/kubernetes/ssl/ca.pem \
--network-plugin=cni \
--cgroup-driver=cgroupfs \
--cert-dir=/etc/kubernetes/ssl \
--cluster-dns=10.254.0.2 \
--cluster-domain=cluster.local \
--cni-conf-dir=/etc/cni/net.d \
--eviction-max-pod-grace-period=30 \
--image-gc-high-threshold=80 \
--image-gc-low-threshold=70 \
--image-pull-progress-deadline=30s \
--kubeconfig=/etc/kubernetes/config/kubelet.kubeconfig \
--max-pods=100 \
--minimum-image-ttl-duration=720h0m0s \
--node-labels=node.kubernetes.io/k8s-node=true \
--pod-infra-container-image=registry.aliyuncs.com/google_containers/pause-amd64:3.1 \
--rotate-certificates \
--rotate-server-certificates \
--fail-swap-on=false \
--v=2"
--allow-privileged \
--anonymous-auth=false \
--authentication-token-webhook=true \
--authorization-mode=Webhook \
--bootstrap-kubeconfig=/etc/kubernetes/config/bootstrap.kubeconfig \
--client-ca-file=/etc/kubernetes/ssl/ca.pem \
--network-plugin=cni \
--cgroup-driver=cgroupfs \
--cert-dir=/etc/kubernetes/ssl \
--cluster-dns=10.254.0.2 \
--cluster-domain=cluster.local \
--cni-conf-dir=/etc/cni/net.d \
--eviction-max-pod-grace-period=30 \
--image-gc-high-threshold=80 \
--image-gc-low-threshold=70 \
--image-pull-progress-deadline=30s \
--kubeconfig=/etc/kubernetes/config/kubelet.kubeconfig \
--max-pods=100 \
--minimum-image-ttl-duration=720h0m0s \
--node-labels=node.kubernetes.io/k8s-node=true \
--pod-infra-container-image=registry.aliyuncs.com/google_containers/pause-amd64:3.1 \
--rotate-certificates \
--rotate-server-certificates \
--fail-swap-on=false \
--v=2"
## 分发至node节点(别忘了修改参数中对应的主机名、IP地址) [root@k8s-master01 ~]# ansible k8s-node -m copy -a 'src=/opt/k8s/cfg/kubelet.conf dest=/etc/kubernetes/config/'
参数解释:
- authorization-mode:kubelet认证模式
- network-plugin:网络插件名称
- cert-dir:TLS证书所在的目录
- eviction-max-pod-grace-period:终止pod最大宽限时间
- pod-infra-container-image:每个pod的network/ipc namespace容器使用的镜像
- rotate-certificates:当证书到期时,通过从kube-apiserver请求新的证书,自动旋转kubelet客户机证书
- hostname-override:设置node在集群中的主机名,默认使用主机hostname;如果设置了此项参数,kube-proxy服务也需要设置此项参数
4)创建kubelet系统脚本
[root@k8s-master01 ~]# vim /opt/k8s/unit/kubelet.service [Unit] Description=Kubernetes Kubelet Server Documentation=https://github.com/GoogleCloudPlatform/kubernetes After=docker.service Requires=docker.service [Service] WorkingDirectory=/var/lib/kubelet EnvironmentFile=-/etc/kubernetes/config/kubelet.conf ExecStart=/usr/local/bin/kubelet $KUBELET_ARGS Restart=on-failure KillMode=process [Install] WantedBy=multi-user.target
## 分发脚本配置文件 [root@k8s-master01 ~]# ansible k8s-node -m copy -a 'src=/opt/k8s/unit/kubelet.service dest=/usr/lib/systemd/system/' ## 创建kubelet数据目录 [root@k8s-master01 ~]# ansible k8s-node -m file -a 'path=/var/lib/kubelet state=directory'
5)启动服务
[root@k8s-master01 ~]# ansible k8s-node -m shell -a 'systemctl daemon-reload' [root@k8s-master01 ~]# ansible k8s-node -m shell -a 'systemctl enable kubelet' [root@k8s-master01 ~]# ansible k8s-node -m shell -a 'systemctl start kubelet'
6)查看csr请求
查看未授权的csr请求,处于”Pending”状态
[root@k8s-master01 ~]# kubectl get csr NAME AGE REQUESTOR CONDITION csr-5m922 100s kubelet-bootstrap Pending csr-k4v2g 99s kubelet-bootstrap Pending
7)批准kubelet 的 TLS 证书请求
kubelet 首次启动向 kube-apiserver 发送证书签名请求,必须由 kubernetes 系统允许通过后,才会将该 node 加入到集群。
## 批准后 node节点就加入集群了 [root@k8s-master01 ~]# kubectl certificate approve csr-5m922 [root@k8s-master01 ~]# kubectl certificate approve csr-k4v2g ## 查看node节点就绪状态 ### 由于我们还没有安装网络,所以node节点还处于NotReady状态 [root@k8s-master01 ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-node01 NotReady <none> 49m v1.14.1 k8s-node02 NotReady <none> 6m15s v1.14.1