手动构筑单机版K8s(七)部署node节点

7.部署node节点

kubernetes node 节点包含的组件:

  • Flanneld(本次为单节点部署,所以此步骤跳过)
  • docker
  • kubelet
  • kube-proxy

下载docker

确认下载源:

[root@k8s bin]#yum list docker --showduplicates | sort -r
docker.x86_64             2:1.13.1-208.git7d71120.el7_9                  @extras
docker.x86_64             2:1.13.1-206.git7d71120.el7_9                  extras
docker.x86_64             2:1.13.1-205.git7d71120.el7.centos             extras
docker.x86_64             2:1.13.1-204.git0be3e21.el7                    extras
docker.x86_64             2:1.13.1-203.git0be3e21.el7.centos             extras
docker.x86_64             2:1.13.1-162.git64e9980.el7.centos             extras
docker.x86_64             2:1.13.1-161.git64e9980.el7_8                  extras
docker.x86_64             2:1.13.1-109.gitcccb291.el7.centos             extras
docker.x86_64             2:1.13.1-108.git4ef4b30.el7.centos             extras
docker.x86_64             2:1.13.1-103.git7f2769b.el7.centos             extras
docker.x86_64             2:1.13.1-102.git7f2769b.el7.centos             extras

下载:

yum install docker.x86_64

确认版本:

[root@k8s-master k8s]# docker -v
Docker version 1.13.1, build 7d71120/1.13.1

配置和启动 kubelet

对于kuberentes1.18集群,必须关闭swap,否则kubelet启动将失败。

我们在准备阶段已经将其关闭。可以再次确认:

[root@k8s bin]# free -g
              total        used        free      shared  buff/cache   available
Mem:              7           0           6           0           0           6
Swap:             0           0           0

swap部分都为0即可。

kubelet 启动时向 kube-apiserver 发送 TLS bootstrapping 请求,需要先将 bootstrap token 文件中的 kubelet-bootstrap 用户赋予 system:node-bootstrapper cluster 角色(role), 然后 kubelet 才能有权限创建认证请求(certificate signing requests):

cd /etc/kubernetes
kubectl create clusterrolebinding kubelet-bootstrap \
  --clusterrole=system:node-bootstrapper \
  --user=kubelet-bootstrap
  • --user=kubelet-bootstrap 是在 /etc/kubernetes/token.csv 文件中指定的用户名,同时也写入了 /etc/kubernetes/bootstrap.kubeconfig 文件;

kubelet 通过认证后向 kube-apiserver 发送 register node 请求,需要先将 kubelet-nodes 用户赋予 system:node cluster角色(role) 和 system:nodes 组(group), 然后 kubelet 才能有权限创建节点请求:

kubectl create clusterrolebinding kubelet-nodes \
  --clusterrole=system:node \
  --group=system:nodes

下载最新的kubelet和kube-proxy二进制文件

wget https://dl.k8s.io/v1.18.0/kubernetes-server-linux-amd64.tar.gz
tar -xzvf kubernetes-server-linux-amd64.tar.gz
cd kubernetes
tar -xzvf  kubernetes-src.tar.gz
cp -r ./server/bin/{kube-proxy,kubelet} /usr/local/bin/

创建kubelet的service配置文件

service配置文件/usr/lib/systemd/system/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
EnvironmentFile=-/etc/kubernetes/kubelet
ExecStart=/usr/local/bin/kubelet \
            $KUBE_LOGTOSTDERR \
            $KUBE_LOG_LEVEL \
            $KUBELET_ADDRESS \
            $KUBELET_POD_INFRA_CONTAINER \
            $KUBELET_ARGS
Restart=on-failure

[Install]
WantedBy=multi-user.target
  • 注意:在启动kubelet之前,需要先手动创建/var/lib/kubelet目录。

配置文件/etc/kubernetes/kubelet

###
## 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="--address=0.0.0.0"
#
## The port for the info server to serve on
# KUBELET_PORT="--port=10250"
#
## You may leave this blank to use the actual hostname
#KUBELET_HOSTNAME="--hostname-override=127.0.0.1"
#
## location of the api-server
## COMMENT THIS ON KUBERNETES 1.8+
# KUBELET_API_SERVER="--api-servers=http://127.0.0.1:8080"
#
## pod infrastructure container
KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=k8s.gcr.io/pause:3.2"
#
## Add your own!
KUBELET_ARGS="--cgroup-driver=systemd --cluster-dns=10.254.0.2 --experimental-bootstrap-kubeconfig=/etc/kubernetes/bootstrap.kubeconfig --kubeconfig=/etc/kubernetes/kubelet.kubeconfig --cert-dir=/etc/kubernetes/ssl --cluster-domain=cluster.local --hairpin-mode=promiscuous-bridge --serialize-image-pulls=false"
  • 请特别注意--cgroup-driver=systemd部分,如果你的docker.service启动的cgroup drivercgroupfs,那么配置文件中就不要这句话(默认--cgroup-driver=cgroupfs)。总之,必须保持docker与kubelet的cgroup driver一致。
  • kubelet会根据--pod-infra-container-image设定来下载pause,国内的同志会因为某种原因无法成功下载。这时可以采用以下两种方式解决(任选一种即可)
  1. --pod-infra-container-image改为docker.io/anjia0532/google-containers.pause:3.1
  2. 手动下载docker.io/anjia0532/google-containers.pause:3.1,执行如下指令,给k8s.gcr.io/pause:3.2打一个tag:
docker tag docker.io/anjia0532/google-containers.pause:3.1 k8s.gcr.io/pause:3.2

启动kublet

systemctl daemon-reload
systemctl enable kubelet
systemctl start kubelet
systemctl status kubelet

通过kublet的TLS证书请求

kubelet 首次启动时向 kube-apiserver 发送证书签名请求,必须通过后 kubernetes 系统才会将该 Node 加入到集群。

查看未授权的 CSR 请求

[root@k8s bin]# kubectl get csr
NAME        AGE       REQUESTOR           CONDITION
csr-2b308   4m        kubelet-bootstrap   Pending
[root@k8s bin]# kubectl get nodes
No resources found.

通过 CSR 请求(用你的crs name替换csr-2b308)

[root@k8s bin]# kubectl certificate approve csr-2b308
certificatesigningrequest "csr-2b308" approved
[root@k8s bin]# kubectl get nodes
NAME        STATUS    AGE       VERSION
10.64.3.7   Ready     49m       v1.6.1

自动生成了 kubelet kubeconfig 文件和公私钥

[root@k8s bin]# ls -l /etc/kubernetes/kubelet.kubeconfig
-rw------- 1 root root 2292  6月 29 15:11 /etc/kubernetes/kubelet.kubeconfig
[root@k8s bin]# ls -l /etc/kubernetes/ssl/kubelet*
-rw------- 1 root root 1232  6月 29 15:11 /etc/kubernetes/ssl/kubelet-client-2021-06-29-15-11-30.pem
lrwxrwxrwx 1 root root   58  6月 29 15:11 /etc/kubernetes/ssl/kubelet-client-current.pem -> /etc/kubernetes/ssl/kubelet-client-2021-06-29-15-11-30.pem
-rw-r--r-- 1 root root 2213  6月 29 15:10 /etc/kubernetes/ssl/kubelet.crt
-rw------- 1 root root 1679  6月 29 15:10 /etc/kubernetes/ssl/kubelet.key
  • 假如你更新kubernetes的证书,只要没有更新token.csv,当重启kubelet后,该node就会自动加入到kuberentes集群中,而不会重新发送certificaterequest,也不需要在master节点上执行kubectl certificate approve操作。前提是不要删除node节点上的/etc/kubernetes/ssl/kubelet*/etc/kubernetes/kubelet.kubeconfig文件。否则kubelet启动时会提示找不到证书而失败。

配置和启动 kubelet

安装conntrack

yum install -y conntrack-tools

创建kube-proxy的service配置文件

service配置文件/usr/lib/systemd/system/kube-proxy.service内容:

[Unit]
Description=Kubernetes Kube-Proxy Server
Documentation=https://github.com/GoogleCloudPlatform/kubernetes
After=network.target

[Service]
EnvironmentFile=-/etc/kubernetes/config
EnvironmentFile=-/etc/kubernetes/proxy
ExecStart=/usr/local/bin/kube-proxy \
        $KUBE_LOGTOSTDERR \
        $KUBE_LOG_LEVEL \
        $KUBE_MASTER \
        $KUBE_PROXY_ARGS
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

配置文件/etc/kubernetes/proxy

###
# kubernetes proxy config

# default config should be adequate

# Add your own!
KUBE_PROXY_ARGS="--bind-address=127.0.0.1 --hostname-override=127.0.0.1 --kubeconfig=/etc/kubernetes/kube-proxy.kubeconfig --cluster-cidr=10.254.0.0/16"
  • --hostname-override 参数值必须与 kubelet 的值一致,否则 kube-proxy 启动后会找不到该 Node,从而不会创建任何 iptables 规则;
  • --kubeconfig 指定的配置文件嵌入了 kube-apiserver 的地址、用户名、证书、秘钥等请求和认证信息;
    预定义的 RoleBinding cluster-admin 将User system:kube-proxy 与 Role system:node-proxier 绑定,该 Role 授予了调用 kube-apiserver Proxy 相关 API 的权限;

启动 kube-proxy

systemctl daemon-reload
systemctl enable kube-proxy
systemctl start kube-proxy
systemctl status kube-proxy

验证node节点

[root@k8s bin]# kubectl get nodes
NAME              STATUS   ROLES    AGE   VERSION
k8s.localdomain   Ready       25h   v1.18.0

安装步骤

  1. 事前准备
  2. 创建TLS证书和秘钥
  3. 安装kubectl命令行工具
  4. 创建kubeconfig文件
  5. 创建高可用etcd集群
  6. 部署master节点
  7. 部署node节点
  8. 全局验证

你可能感兴趣的:(手动构筑单机版K8s(七)部署node节点)