1. 程式人生 > >Kubernetes部署(七):Node節點部署

Kubernetes部署(七):Node節點部署

node只需安裝kubelet服務,所有節點都需要安裝kube-proxy

部署kubelet

1.二進位制包準備
將軟體包從複製到所有node中去。

[[email protected] ~]# cd /usr/local/src/kubernetes/server/bin/
[[email protected] bin]# for n in `seq 204 206`;do scp kubelet 10.31.90.$n:/data/kubernetes/bin/ ;done
[[email protected] bin]# for n in `seq 201 206`;do scp  kube-proxy 10.31.90.$n:/data/kubernetes/bin/ ;done

2.建立角色繫結

[[email protected] ~]# kubectl create clusterrolebinding kubelet-bootstrap --clusterrole=system:node-bootstrapper --user=kubelet-bootstrap
clusterrolebinding.rbac.authorization.k8s.io/kubelet-bootstrap created

3.建立 kubelet bootstrapping kubeconfig 檔案
設定叢集引數

[[email protected] ~]# kubectl config set-cluster kubernetes \
   --certificate-authority=/data/kubernetes/ssl/ca.pem \
   --embed-certs=true \
   --server=https://10.31.90.200:6443 \
   --kubeconfig=bootstrap.kubeconfig
Cluster "kubernetes" set.

設定客戶端認證引數,token要使用之前那個

[[email protected] ~]# kubectl config set-credentials kubelet-bootstrap \
   --token=cf25becebf64e3fffd7f3890a60ac16d \
   --kubeconfig=bootstrap.kubeconfig   
User "kubelet-bootstrap" set.

設定上下文引數

[[email protected] ~]# kubectl config set-context default \
   --cluster=kubernetes \
   --user=kubelet-bootstrap \
   --kubeconfig=bootstrap.kubeconfig
Context "default" created.

選擇預設上下文

[[email protected] ~]# kubectl config use-context default --kubeconfig=bootstrap.kubeconfig
Switched to context "default".
#拷貝到3個node節點
for n in `seq 204 206`;do scp bootstrap.kubeconfig 10.31.90.$n:/data/kubernetes/cfg/;done

部署kubelet(node節點)

  1. 設定CNI支援(所有節點)
    [[email protected] ~]# mkdir -p /etc/cni/net.d
    [[email protected] ~]# cat >>/etc/cni/net.d/10-default.conf<<EOF
    {
        "name": "flannel",
        "type": "flannel",
        "delegate": {
            "bridge": "docker0",
            "isDefaultGateway": true,
            "mtu": 1400
        }
    }
    EOF

2.建立kubelet目錄

[[email protected] ~]# mkdir /var/lib/kubelet

3.建立kubelet服務配置

[[email protected] ~]# vim /usr/lib/systemd/system/kubelet.service
[Unit]
Description=Kubernetes Kubelet
Documentation=https://github.com/GoogleCloudPlatform/kubernetes
After=docker.service
Requires=docker.service

[Service]
WorkingDirectory=/var/lib/kubelet
ExecStart=/data/kubernetes/bin/kubelet \
  --address=10.31.90.204 \
  --hostname-override=10.31.90.204 \
  --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
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

4.啟動Kubelet

[[email protected] ~]# systemctl daemon-reload
[[email protected] ~]# systemctl enable kubelet
[[email protected] ~]# systemctl start kubelet

5.檢視服務狀態

[[email protected] kubernetes]# systemctl status kubelet

6.檢視csr請求
注意是在linux-node1上執行。

[[email protected] ssl]# kubectl get csr
NAME                                                   AGE       REQUESTOR           CONDITION
node-csr-NqH7J1OuDM_jGtQM_VuABiBcAqgDlT_MfJiVS_qWCbg   1m        kubelet-bootstrap   Pending
node-csr-jy10bYvow5hYQ2sKWfCuBlUNIPit54dhQfzRUd5E6dc   4m        kubelet-bootstrap   Pending
node-csr-zPPE5g4d1PtbKo-lQWNNC0bbngttC2bdZtwqBBvjrVM   1m        kubelet-bootstrap   Pending

7.批准kubelet 的 TLS 證書請求

[[email protected] ~]# kubectl get csr|grep 'Pending' | awk 'NR>0{print $1}'| xargs kubectl certificate approve

執行完畢後,檢視節點狀態已經是Ready的狀態了

[[email protected] ~]# kubectl get csr
NAME                                                   AGE       REQUESTOR           CONDITION
node-csr-NqH7J1OuDM_jGtQM_VuABiBcAqgDlT_MfJiVS_qWCbg   1m        kubelet-bootstrap   Approved,Issued
node-csr-jy10bYvow5hYQ2sKWfCuBlUNIPit54dhQfzRUd5E6dc   5m        kubelet-bootstrap   Approved,Issued
node-csr-zPPE5g4d1PtbKo-lQWNNC0bbngttC2bdZtwqBBvjrVM   2m        kubelet-bootstrap   Approved,Issued

部署Kubernetes Proxy

kube-proxy master和node節點都安裝
1.配置kube-proxy使用LVS

[[email protected] ~]# yum install -y ipvsadm ipset conntrack

2.建立 kube-proxy 證書請求

[[email protected] ssl]# cd /usr/local/src/ssl/
[[email protected] ssl]# vim kube-proxy-csr.json
{
  "CN": "system:kube-proxy",
  "hosts": [],
  "key": {
    "algo": "rsa",
    "size": 2048
  },
  "names": [
    {
      "C": "CN",
      "ST": "BeiJing",
      "L": "BeiJing",
      "O": "k8s",
      "OU": "System"
    }
  ]
}

3.生成證書

[[email protected] ssl]# cfssl gencert -ca=/data/kubernetes/ssl/ca.pem \
   -ca-key=/data/kubernetes/ssl/ca-key.pem \
   -config=/data/kubernetes/ssl/ca-config.json \
   -profile=kubernetes  kube-proxy-csr.json | cfssljson -bare kube-proxy

4.分發證書到所有node節點

[[email protected] ssl]# for n in `seq 201 206`;do scp kube-proxy*.pem [email protected]$n:/data/kubernetes/ssl;done

5.建立kube-proxy配置檔案

[[email protected] ~]# kubectl config set-cluster kubernetes \
   --certificate-authority=/data/kubernetes/ssl/ca.pem \
   --embed-certs=true \
   --server=https://10.31.90.200:6443 \
   --kubeconfig=kube-proxy.kubeconfig
Cluster "kubernetes" set.

[[email protected] ~]# kubectl config set-credentials kube-proxy \
   --client-certificate=/data/kubernetes/ssl/kube-proxy.pem \
   --client-key=/data/kubernetes/ssl/kube-proxy-key.pem \
   --embed-certs=true \
   --kubeconfig=kube-proxy.kubeconfig
User "kube-proxy" set.

[[email protected] ~]# kubectl config set-context default \
   --cluster=kubernetes \
   --user=kube-proxy \
   --kubeconfig=kube-proxy.kubeconfig
Context "default" created.

[[email protected] ~]# kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig
Switched to context "default".

6.分發kubeconfig配置檔案

[[email protected] ssl]# for n in `seq 201 206 `;do scp kube-proxy.kubeconfig 10.31.90.$n:/data/kubernetes/cfg/;done   

7.建立kube-proxy服務配置

[[email protected] ~]# mkdir /var/lib/kube-proxy

[[email protected] ~]# vim /usr/lib/systemd/system/kube-proxy.service
[Unit]
Description=Kubernetes Kube-Proxy Server
Documentation=https://github.com/GoogleCloudPlatform/kubernetes
After=network.target

[Service]
WorkingDirectory=/var/lib/kube-proxy
ExecStart=/data/kubernetes/bin/kube-proxy \
  --bind-address=10.31.90.204 \
  --hostname-override=10.31.90.204 \
  --kubeconfig=/data/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=/data/kubernetes/log

Restart=on-failure
RestartSec=5
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

8.啟動Kubernetes Proxy
[[email protected] ~]# systemctl daemon-reload
[[email protected] ~]# systemctl enable kube-proxy
[[email protected] ~]# systemctl start kube-proxy

9.檢視服務狀態
檢視kube-proxy服務狀態

[[email protected] scripts]# systemctl status kube-proxy

檢查LVS狀態
[[email protected] ~]# ipvsadm -L -n
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  10.1.0.1:443 rr
  -> 10.31.90.201:6443            Masq    1      0          0  

如果都安裝了kubelet和proxy服務,使用下面的命令可以檢查狀態:

[[email protected] ~]# kubectl get node
NAME           STATUS    ROLES     AGE       VERSION
10.31.90.204   Ready     <none>    40m       v1.11.5
10.31.90.205   Ready     <none>    40m       v1.11.5
10.31.90.206   Ready     <none>    40m       v1.11.5

node-05,node-06節點請自行部署。
後續會陸續更新所有的安裝文件,如果你覺得我寫的不錯,希望大家多多關注點贊,非常感謝!