Kubernetes学习之路(五)之Node节点二进制部署
_____egon新书来袭请看:https://egonlin.com/book.html
K8S Node节点部署
-
1、在node01、node02、node03上部署kubelet
(1)准备好二进制包(在node01、node02、node3上都执行)
cd /usr/local/src/kubernetes/server/bin/ cp kubelet kube-proxy /opt/kubernetes/bin/
(2)执行下述命令,创建角色绑定(在任意一个安装有kubectl的节点执行即可,重复创建会报错)
kubelet启动时会向kube-apiserver发送tsl bootstrap请求,所以需要将bootstrap的token设置成对应的角色,这样kubectl才有权限创建该请求。
kubectl create clusterrolebinding kubelet-bootstrap --clusterrole=system:node-bootstrapper --user=kubelet-bootstrap # 查看token awk -F ',' '{print $1}' /opt/kubernetes/ssl/bootstrap-token.csv # 结果为 d149190dacf50968d58b069745dda2a2
(3)创建 kubelet bootstrapping kubeconfig 文件 设置集群参数(在三个节点上都执行)
mkdir -p /usr/local/src/ssl
cd /usr/local/src/ssl
kubectl config set-cluster kubernetes \ --certificate-authority=/opt/kubernetes/ssl/ca.pem \ --embed-certs=true \ --server=https://10.1.1.200:8000 \ --kubeconfig=bootstrap.kubeconfig
(4)设置客户端认证参数(在三个节点上都执行)
kubectl config set-credentials kubelet-bootstrap \ --token=d149190dacf50968d58b069745dda2a2 \ --kubeconfig=bootstrap.kubeconfig
(5)设置上下文参数(在三个节点上都执行)
kubectl config set-context default \ --cluster=kubernetes \ --user=kubelet-bootstrap \ --kubeconfig=bootstrap.kubeconfig
(6)选择默认上下文(在三个节点都执行)
kubectl config use-context default --kubeconfig=bootstrap.kubeconfig cp bootstrap.kubeconfig /opt/kubernetes/cfg
-
2、部署kubelet 1.设置CNI支持
node01、node02、node03三个节点均执行下述操作
(1)配置CNI
mkdir -p /etc/cni/net.d cat > /etc/cni/net.d/10-default.conf << EOF { "cniVersion":"0.3.0", "name": "flannel", "type": "flannel","delegate": { "hairpinMode": true, "isDefaultGateway": true } } EOF
# ps:cniVersion不加,kubelet无法检识别插件,cniVersion的版本号不能超过0.3.0,此乃实验结果,后续有待进一步研究
阅读:https://www.cnblogs.com/rexcheny/p/10960233.html
下载cni插件
# 所有node节点执行 wget https://github.com/containernetworking/plugins/releases/download/v0.9.0/cni-plugins-linux-amd64-v0.9.0.tgz mkdir -p /opt/kubernetes/bin/cni/ tar xvf cni-plugins-linux-amd64-v0.9.0.tgz -C /opt/kubernetes/bin/cni/
(2)创建kubelet数据存储目录
mkdir /var/lib/kubelet
(3)创建kubelet服务配置
cat > /usr/lib/systemd/system/kubelet.service << EOF [Unit] Description=Kubernetes Kubelet Documentation=https://github.com/GoogleCloudPlatform/kubernetes After=docker.service Requires=docker.service [Service] WorkingDirectory=/var/lib/kubelet ExecStart=/opt/kubernetes/bin/kubelet --address=10.1.1.103 --hostname-override=10.1.1.103 --pod-infra-container-image=mirrorgooglecontainers/pause-amd64:3.0 --experimental-bootstrap-kubeconfig=/opt/kubernetes/cfg/bootstrap.kubeconfig --kubeconfig=/opt/kubernetes/cfg/kubelet.kubeconfig --cert-dir=/opt/kubernetes/ssl --network-plugin=cni --cni-conf-dir=/etc/cni/net.d --cni-bin-dir=/opt/kubernetes/bin/cni --cluster-dns=10.0.0.2 --cluster-domain=cluster.local. --hairpin-mode hairpin-veth --fail-swap-on=false --logtostderr=true --v=2 --logtostderr=false --log-dir=/opt/kubernetes/log Restart=on-failure RestartSec=5 EOF # node02、node03上修改下述参数为自己的即可 --address=节点自己的ip地址--hostname-override=同上
(4)启动Kubelet
systemctl daemon-reload systemctl enable kubelet systemctl start kubelet systemctl status kubelet
kubel启动失败1
发现有时候运行systemctl status kubelet 发现kubelet服务是正常的,一会再看又失败了的灵异事件。 journalctl -xefu kubelet 命令查看systemd日志才发现,真正的错误是 failed to run Kubelet: misconfiguration: kubelet cgroup driver: "cgroupfs" is different from docker cgroup driver: "systemd" #解决方案一: 将/etc/docker/daemon.json中的对应参数修改为cgroupfs,原来为systemd [root@node01 ~]# cat /etc/docker/daemon.json { "exec-opts": ["native.cgroupdriver=cgroupfs"], } 重启docker systemctl restart docker systemctl status kubelet.service 现在kubelet正常运行了。 # 解决方案二 也就是修改/var/lib/kubelet/kubeadm-flags.env文件,将其中的–cgroup-driver=cgroupfs 修改为 –cgroup-driver=systemd 办法二 将/var/lib/kubelet/kubeadm-flags.env文件中的–cgroup-driver=cgroupfs 修改为 –cgroup-driver=systemd 也就是docker配置项和kubelet的一致都是systemd。
kubel启动失败2
在查看kubelet的状态,如果发现有如下报错Failed to get system container stats for "/system.slice/kubelet.service": failed to...此时需要调整kubelet的启动参数。 解决方法: 在/usr/lib/systemd/system/kubelet.service的[service]新增: Environment="KUBELET_MY_ARGS=--runtime-cgroups=/systemd/system.slice --kubelet-cgroups=/systemd/system.slice" 修改ExecStart: 在末尾新增$KUBELET_MY_ARGS
(5)查看csr请求 ,都处于Pending状态
[root@master01 ssl]# kubectl get csr
NAME AGE SIGNERNAME REQUESTOR CONDITION
node-csr-DxFo3mdPFfaJ_XEU1j_aWOScaVcmT39RU6buAzQfLPM 3m21s kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap Pending
node-csr-aVtRrSAlQQgXQsI1nbOmvnV7Xysv5DtyuLKFFbogkTo 3m36s kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap Pending
node-csr-qKSnHikCVLvXZTnqJn-F6pwE4oQsQv59A4HGrKRA14M 16m kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap Pending
(6)批准kubelet 的 TLS 证书请求
[root@node01 ~]# kubectl get csr|grep 'Pending' | awk 'NR>0{print $1}'| xargs kubectl certificate approve certificatesigningrequest.certificates.k8s.io/node-csr-DxFo3mdPFfaJ_XEU1j_aWOScaVcmT39RU6buAzQfLPM approved certificatesigningrequest.certificates.k8s.io/node-csr-aVtRrSAlQQgXQsI1nbOmvnV7Xysv5DtyuLKFFbogkTo approved certificatesigningrequest.certificates.k8s.io/node-csr-qKSnHikCVLvXZTnqJn-F6pwE4oQsQv59A4HGrKRA14M approved [root@node01 ~]# [root@master01 ssl]# kubectl get csr NAME AGE SIGNERNAME REQUESTOR CONDITION node-csr-DxFo3mdPFfaJ_XEU1j_aWOScaVcmT39RU6buAzQfLPM 3m53s kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap Approved,Issued node-csr-aVtRrSAlQQgXQsI1nbOmvnV7Xysv5DtyuLKFFbogkTo 4m8s kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap Approved,Issued node-csr-qKSnHikCVLvXZTnqJn-F6pwE4oQsQv59A4HGrKRA14M 17m kubernetes.io/kube-apiserver-client-kubelet kubelet-bootstrap Approved,Issued 执行完毕后,查看节点状态已经是Ready的状态了 [root@linux-node1 ssl]# kubectl get node NAME STATUS ROLES AGE VERSION 192.168.56.120 Ready <none> 50m v1.10.1 192.168.56.130 Ready <none> 46m v1.10.1
-
3、部署Kubernetes Proxy
在node01、node02、node03部署
(1)配置kube-proxy使用LVS
yum install -y ipvsadm ipset conntrack
(2)创建 kube-proxy 证书请求
cd /usr/local/src/ssl/
cat > kube-proxy-csr.json << EOF { "CN": "system:kube-proxy", "hosts": [], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "BeiJing", "L": "BeiJing", "O": "k8s", "OU": "ops" } ] }
EOF
(3)生成证书
cfssl gencert -ca=/opt/kubernetes/ssl/ca.pem \ -ca-key=/opt/kubernetes/ssl/ca-key.pem \ -config=/opt/kubernetes/ssl/ca-config.json \ -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy
(4)分发证书到所有Node节点
cp kube-proxy*.pem /opt/kubernetes/ssl/
scp kube-proxy*.pem node02:/opt/kubernetes/ssl/
scp kube-proxy*.pem node03:/opt/kubernetes/ssl/
(5)创建kube-proxy配置文件(在三个节点上都执行)
# 步骤1:先切换目录,因为我们后面产生的配置文件都用的相对路径,方便 cd /opt/kubernetes/cfg/ # 步骤2 kubectl config set-cluster kubernetes \ --certificate-authority=/opt/kubernetes/ssl/ca.pem \ --embed-certs=true \ --server=https://10.1.1.200:8000 \ --kubeconfig=kube-proxy.kubeconfig kubectl config set-credentials kube-proxy \ --client-certificate=/opt/kubernetes/ssl/kube-proxy.pem \ --client-key=/opt/kubernetes/ssl/kube-proxy-key.pem \ --embed-certs=true \ --kubeconfig=kube-proxy.kubeconfig kubectl config set-context default \ --cluster=kubernetes \ --user=kube-proxy \ --kubeconfig=kube-proxy.kubeconfig # 步骤3 kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig
(6)分发kubeconfig配置文件
cp kube-proxy.kubeconfig /opt/kubernetes/cfg/ scp kube-proxy.kubeconfig node02:/opt/kubernetes/cfg/ scp kube-proxy.kubeconfig node03:/opt/kubernetes/cfg/
(7)创建kube-proxy服务配置
[root@linux-node1 ssl]# mkdir /var/lib/kube-proxy cat > /usr/lib/systemd/system/kube-proxy.service << EOF [Unit] Description=Kubernetes Kube-Proxy Server Documentation=https://github.com/GoogleCloudPlatform/kubernetes After=network.target [Service] WorkingDirectory=/var/lib/kube-proxy ExecStart=/opt/kubernetes/bin/kube-proxy \ --bind-address=10.1.1.103 \ --hostname-override=10.1.1.103 \ --kubeconfig=/opt/kubernetes/cfg/kube-proxy.kubeconfig \ --masquerade-all \ --feature-gates=SupportIPVSProxyMode=true \ --proxy-mode=ipvs \ --ipvs-min-sync-period=5s \ --ipvs-sync-period=5s \ --ipvs-scheduler=rr \ --logtostderr=true \ --v=2 \ --logtostderr=false \ --log-dir=/opt/kubernetes/log Restart=on-failure RestartSec=5 LimitNOFILE=65536 [Install] WantedBy=multi-user.target
EOF 其余两个节点也做修改
(8)启动Kubernetes Proxy
systemctl daemon-reload systemctl enable kube-proxy systemctl restart kube-proxy systemctl status kube-proxy 检查LVS状态,可以看到已经创建了一个LVS集群,将来自10.0.0.1:443的请求转到三地址上,而6443就是api-server的端口 [root@node01 ssl]# ipvsadm -Ln IP Virtual Server version 1.2.1 (size=4096) Prot LocalAddress:Port Scheduler Flags -> RemoteAddress:Port Forward Weight ActiveConn InActConn TCP 10.0.0.1:443 rr -> 10.1.1.100:6443 Masq 1 0 0 -> 10.1.1.101:6443 Masq 1 0 0 -> 10.1.1.102:6443 Masq 1 0 0
到此,K8S的集群就部署完毕,由于K8S本身不支持网络,需要借助第三方网络才能进行创建Pod,将在下一节学习Flannel网络为K8S提供网络支持。
(9)遇到的问题:kubelet无法启动,kubectl get node 提示:no resource found
[root@linux-node1 ssl]# kubectl get node No resources found. [root@linux-node3 ~]# systemctl status kubelet ● kubelet.service - Kubernetes Kubelet Loaded: loaded (/usr/lib/systemd/system/kubelet.service; static; vendor preset: disabled) Active: activating (auto-restart) (Result: exit-code) since Wed 2018-05-30 04:48:29 EDT; 1s ago Docs: https://github.com/GoogleCloudPlatform/kubernetes Process: 16995 ExecStart=/opt/kubernetes/bin/kubelet --address=192.168.56.130 --hostname-override=192.168.56.130 --pod-infra-container-image=mirrorgooglecontainers/pause-amd64:3.0 --experimental-bootstrap-kubeconfig=/opt/kubernetes/cfg/bootstrap.kubeconfig --kubeconfig=/opt/kubernetes/cfg/kubelet.kubeconfig --cert-dir=/opt/kubernetes/ssl --network-plugin=cni --cni-conf-dir=/etc/cni/net.d --cni-bin-dir=/opt/kubernetes/bin/cni --cluster-dns=10.1.0.2 --cluster-domain=cluster.local. --hairpin-mode hairpin-veth --allow-privileged=true --fail-swap-on=false --logtostderr=true --v=2 --logtostderr=false --log-dir=/opt/kubernetes/log (code=exited, status=255) Main PID: 16995 (code=exited, status=255) May 30 04:48:29 linux-node3.example.com systemd[1]: Unit kubelet.service entered failed state. May 30 04:48:29 linux-node3.example.com systemd[1]: kubelet.service failed. [root@linux-node3 ~]# tailf /var/log/messages ...... May 30 04:46:24 linux-node3 kubelet: F0530 04:46:24.134612 16207 server.go:233] failed to run Kubelet: failed to create kubelet: misconfiguration: kubelet cgroup driver: "cgroupfs" is different from docker cgroup driver: "systemd" 提示kubelet使用的cgroup驱动类型和docker的cgroup驱动类型不一致。进行查看docker.service [Unit] Description=Docker Application Container Engine Documentation=http://docs.docker.com After=network.target Wants=docker-storage-setup.service Requires=docker-cleanup.timer [Service] Type=notify NotifyAccess=all KillMode=process EnvironmentFile=-/etc/sysconfig/docker EnvironmentFile=-/etc/sysconfig/docker-storage EnvironmentFile=-/etc/sysconfig/docker-network Environment=GOTRACEBACK=crash Environment=DOCKER_HTTP_HOST_COMPAT=1 Environment=PATH=/usr/libexec/docker:/usr/bin:/usr/sbin ExecStart=/usr/bin/dockerd-current \ --add-runtime docker-runc=/usr/libexec/docker/docker-runc-current \ --default-runtime=docker-runc \ --exec-opt native.cgroupdriver=systemd \ ###修改此处"systemd"为"cgroupfs" --userland-proxy-path=/usr/libexec/docker/docker-proxy-current \ $OPTIONS \ $DOCKER_STORAGE_OPTIONS \ $DOCKER_NETWORK_OPTIONS \ $ADD_REGISTRY \ $BLOCK_REGISTRY \ $INSECURE_REGISTRY ExecReload=/bin/kill -s HUP $MAINPID LimitNOFILE=1048576 LimitNPROC=1048576 LimitCORE=infinity TimeoutStartSec=0 Restart=on-abnormal MountFlags=slave [Install] WantedBy=multi-user.target [root@linux-node3 ~]# systemctl daemon-reload [root@linux-node3 ~]# systemctl restart docker.service [root@linux-node3 ~]# systemctl restart kubelet