Kubernetes (k8s) 叢集部署(四) 完整版

最閒的碼農發表於2020-06-09

第四步

Kubernetes Node

Node 是主要執行容器例項的節點,可視為工作節點。在這步驟我們會下載 Kubernetes binary 檔案,並建立 node 的 certificate 來提供給節點註冊認證用。Kubernetes 使用Node Authorizer來提供Authorization mode,這種授權模式會替 Kubelet 生成 API request。

  • 在開始前,我們先在master1將需要的 ca 與 cert 複製到 Node 節點上:
$ cd ~/kubernetes/server/bin
$ scp kubelet kube-proxy 192.168.184.29:/usr/local/bin/
$ scp kubelet kube-proxy 192.168.184.30:/usr/local/bin/
$ cd /etc/etcd/ssl
  • 建立角色繫結
$ kubectl create clusterrolebinding kubelet-bootstrap --clusterrole=system:node-bootstrapper --user=kubelet-bootstrap
  • 建立 kubelet bootstrap.kubeconfig 檔案 設定叢集引數
$ kubectl config set-cluster kubernetes --certificate-authority=/etc/etcd/ssl/ca.pem --embed-certs=true --server=https://192.168.184.28:6443 --kubeconfig=bootstrap.kubeconfig
  • 設定客戶端認證引數,token值為之前生成的
$ cat /etc/etcd/ssl/bootstrap-token.csv
$ kubectl config set-credentials kubelet-bootstrap --token=05c645cec943aef73c8b1f54464120c0 --kubeconfig=bootstrap.kubeconfig // --token 之前存的token
  • 設定上下文引數
$ kubectl config set-context default --cluster=kubernetes --user=kubelet-bootstrap --kubeconfig=bootstrap.kubeconfig
  • 選擇預設上下文並向node節點分發在master端生成的bootstrap.kubeconfig檔案
$ kubectl config use-context default --kubeconfig=bootstrap.kubeconfig
$ mv bootstrap.kubeconfig /etc/etcd/cfg/
$ scp /etc/etcd/cfg/bootstrap.kubeconfig 192.168.184.29:/etc/etcd/cfg/
$ scp /etc/etcd/cfg/bootstrap.kubeconfig 192.168.184.30:/etc/etcd/cfg/

部署kubelet(work01和work02節點操作)

  • 設定CNI支援
$ mkdir -p /etc/etcd/cni/net.d //192.168.184.29
$ mkdir -p /etc/etcd/cni/net.d //192.168.184.30
$ cat > /etc/etcd/cni/net.d/10-default.conf <<EOF
{
        "name": "flannel",
        "type": "flannel",
        "delegate": {
            "bridge": "docker0",
            "isDefaultGateway": true,
            "mtu": 1400
        }
}
EOF

$ scp -r /etc/etcd/cni/net.d/10-default.conf 192.168.184.26:/etc/etcd/cni/net.d/10-default.conf
  • 建立kubelet目錄和.建立kubelet服務配置
$ mkdir /var/lib/kubelet //192.168.184.29
$ mkdir /var/lib/kubelet //192.168.184.30
$ 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=/usr/local/bin/kubelet \
  --address=192.168.184.29 \
  --hostname-override=192.168.184.29 \
  --pod-infra-container-image=mirrorgooglecontainers/pause-amd64:3.0 \
  --experimental-bootstrap-kubeconfig=/etc/etcd/cfg/bootstrap.kubeconfig \
  --kubeconfig=/etc/etcd/cfg/kubelet.kubeconfig \
  --cert-dir=/etc/etcd/ssl \
  --network-plugin=cni \
  --cni-conf-dir=/etc/etcd/cni/net.d \
  --cni-bin-dir=/usr/local/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=/etc/etcd/log
Restart=on-failure
RestartSec=5

EOF
$ scp -r  /usr/lib/systemd/system/kubelet.service 192.168.184.30: /usr/lib/systemd/system/kubelet.service
  • 啟動
$ systemctl daemon-reload
$ systemctl enable  kubelet.service &&  systemctl start kubelet.service
$ systemctl status  kubelet.service
  • 檢視csr請求 注意是在master01上執行
$ kubectl get csr

Docker+Kubernetes (k8s) 叢集部署(四) 完整版

  • 批准kubelet 的 TLS 證照請求
$ kubectl get csr|grep 'Pending' | awk 'NR>0{print $1}'| xargs kubectl certificate approve

Docker+Kubernetes (k8s) 叢集部署(四) 完整版

  • 執行完畢後,檢視節點狀態如果是Ready的狀態就說明一切正常
$  kubectl get node

Docker+Kubernetes (k8s) 叢集部署(四) 完整版

部署Kubernetes Proxy (work01和work02節點操作)

  • 配置kube-proxy使用LVS
$ yum install -y ipvsadm ipset conntrack
  • 建立 kube-proxy 證照請求 (master01操作)
$ cd /etc/etcd/ssl_tmp
$  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": "System"
    }
  ]
}

EOF
  • 生成證照,並分發至節點.
$ cfssl gencert -ca=/etc/etcd/ssl/ca.pem -ca-key=/etc/etcd/ssl/ca-key.pem -config=/etc/etcd/ssl/ca-config.json -profile=kubernetes  kube-proxy-csr.json | cfssljson -bare kube-proxy
$ cp kube-proxy*.pem ../ssl/
$ scp kube-proxy*.pem 192.168.184.29:/etc/etcd/ssl/
$ scp kube-proxy*.pem 192.168.184.30:/etc/etcd/ssl/
  • 建立kube-proxy配置檔案
$ kubectl config set-cluster kubernetes --certificate-authority=/etc/etcd/ssl/ca.pem --embed-certs=true --server=https://192.168.184.28:6443 --kubeconfig=kube-proxy.kubeconfig
$ kubectl config set-credentials kube-proxy --client-certificate=/etc/etcd/ssl/kube-proxy.pem --client-key=/etc/etcd/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
$ kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig
$ scp kube-proxy.kubeconfig 192.168.184.29:/etc/etcd/cfg/
$ scp kube-proxy.kubeconfig 192.168.184.30:/etc/etcd/cfg/
$ mv kube-proxy.kubeconfig /etc/etcd/cfg/
  • 建立kube-proxy服務配置 (work01和work02節點操作)
$ mkdir /var/lib/kube-proxy //192.168.184.29 和 192.168.184.30
$ 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=/usr/local/bin/kube-proxy \
  --bind-address=192.168.184.29 \
  --hostname-override=192.168.184.29 \
  --kubeconfig=/etc/etcd/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=/etc/etcd/log

Restart=on-failure
RestartSec=5
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target

EOF

$ //注意修改ip
$ systemctl enable kube-proxy && systemctl start kube-proxy
$ systemctl status kube-proxy
$ ipvsadm -L -n
  • 檢查LVS狀態
$ ipvsadm -L -n

Docker+Kubernetes (k8s) 叢集部署(四) 完整版

  • 檢查狀態:(master01操作)
$ kubectl get nodes

Docker+Kubernetes (k8s) 叢集部署(四) 完整版

-

Flannel網路部署

  • Flannel生成證照
    $ cd /etc/etcd/ssl_tmp/
    $ cat > /etc/etcd/ssl_tmp/flanneld-csr.json <<EOF
    {
    "CN": "flanneld",
    "hosts": [],
    "key": {
      "algo": "rsa",
      "size": 2048
    },
    "names": [
      {
        "C": "CN",
        "ST": "BeiJing",
        "L": "BeiJing",
        "O": "k8s",
        "OU": "System"
      }
    ]
    }
    EOF
  • 生成證照和分發證照
$ cfssl gencert -ca=/etc/etcd/ssl/ca.pem  -ca-key=/etc/etcd/ssl/ca-key.pem -config=/etc/etcd/ssl/ca-config.json -profile=kubernetes flanneld-csr.json | cfssljson -bare flanneld
$ cp flanneld*.pem /etc/etcd/ssl/
$ scp  /etc/etcd/ssl/flanneld*.pem 192.168.184.29:/etc/etcd/ssl/
$ scp  /etc/etcd/ssl/flanneld*.pem 192.168.184.30:/etc/etcd/ssl/
  • 下載Flannel軟體包
$ cd && wget https://github.com/coreos/flannel/releases/download/v0.10.0/flannel-v0.10.0-linux-amd64.tar.gz
$ tar zxf flannel-v0.10.0-linux-amd64.tar.gz
$ cp flanneld mk-docker-opts.sh /usr/local/bin/
$ scp flanneld mk-docker-opts.sh 192.168.184.29:/usr/local/bin/
$ scp flanneld mk-docker-opts.sh 192.168.184.30:/usr/local/bin/
  • 分發對應指令碼到/usr/local/bin目錄下
$ cd kubernetes/cluster/centos/node/bin/
$ cp remove-docker0.sh /usr/local/bin/
$ scp remove-docker0.sh 192.168.184.29:/usr/local/bin/
$ scp remove-docker0.sh 192.168.184.30:/usr/local/bin/
  • 配置Flannel
$ cat > /etc/etcd/cfg/flannel  <<EOF
FLANNEL_ETCD="-etcd-endpoints=https://192.168.184.28:2379,https://192.168.184.29:2379,https://192.168.184.30:2379"
FLANNEL_ETCD_KEY="-etcd-prefix=/etc/etcd/network"
FLANNEL_ETCD_CAFILE="--etcd-cafile=/etc/etcd/ssl/ca.pem"
FLANNEL_ETCD_CERTFILE="--etcd-certfile=/etc/etcd/ssl/flanneld.pem"
FLANNEL_ETCD_KEYFILE="--etcd-keyfile=/etc/etcd/ssl/flanneld-key.pem"

EOF
$ scp /etc/etcd/cfg/flannel 192.168.184.29:/etc/etcd/cfg/
$ scp /etc/etcd/cfg/flannel 192.168.184.30:/etc/etcd/cfg/
  • 設定Flannel系統服務
$ vim /usr/lib/systemd/system/flannel.service  
[Unit]
Description=Flanneld overlay address etcd agent
After=network.target
Before=docker.service

[Service]
EnvironmentFile=-/etc/etcd/cfg/flannel
ExecStartPre=/usr/local/bin/remove-docker0.sh
ExecStart=/usr/local/bin/flanneld ${FLANNEL_ETCD} ${FLANNEL_ETCD_KEY} ${FLANNEL_ETCD_CAFILE} ${FLANNEL_ETCD_CERTFILE} ${FLANNEL_ETCD_KEYFILE}
ExecStartPost=/usr/local/bin/mk-docker-opts.sh -d /run/flannel/docker

Type=notify

[Install]
WantedBy=multi-user.target
RequiredBy=docker.service
$ scp /usr/lib/systemd/system/flannel.service 192.168.184.29:/usr/lib/systemd/system/
$ scp /usr/lib/systemd/system/flannel.service 192.168.184.30:/usr/lib/systemd/system/
  • 在master節點建立Etcd的key
$ etcdctl --ca-file /etc/etcd/ssl/ca.pem --cert-file /etc/etcd/ssl/flanneld.pem --key-file /etc/etcd/ssl/flanneld-key.pem --no-sync -C  https://192.168.184.28:2379,https://192.168.184.29:2379,https://192.168.184.30:2379 set  /etc/etcd/network/config '{ "Network": "10.2.0.0/16","SubnetLen": 24, "Backend": { "Type": "vxlan", "VNI": 1 }}'
  • 啟動
$ systemctl daemon-reload
$ systemctl enable flannel && systemctl start flannel
$ systemctl status flannel
  • 配置Docker使用Flannel

    在Unit段中的After後面新增flannel.service引數,在Wants下面新增Requires=flannel.service.
    [Service]段中Type後面新增EnvironmentFile=-/run/flannel/docker段,在ExecStart後面新增$DOCKER_OPTS引數.

 $ vim /usr/lib/systemd/system/docker.service 
[Unit]
Description=Docker Application Container Engine
Documentation=https://docs.docker.com
After=network-online.target firewalld.service flannel.service
Wants=network-online.target
Requires=flannel.service

[Service]
Type=notify
EnvironmentFile=-/run/flannel/docker
ExecStart=/usr/bin/dockerd $DOCKER_OPTS
...
  • 將配置分發到另外兩個節點中
$ rsync -av  /usr/lib/systemd/system/docker.service  192.168.184.29:/usr/lib/systemd/system/docker.service
$ rsync -av  /usr/lib/systemd/system/docker.service  192.168.184.30:/usr/lib/systemd/system/docker.service
  • 192.168.184.28 ,192.168.184.29,192.168.184.30重啟Docker服務
$ systemctl daemon-reload &&  systemctl restart docker
  • 檢查 flanneld 服務
$ journalctl  -u flanneld |grep 'Lease acquired'
$ ifconfig flannel.1

Kubernetes (k8s) 叢集部署(四) 完整版

至此flannel網路配置完成,k8s的叢集也部署完成,下面我們來建立pod測試叢集之間網路的連通性.建立第一個K8S應用,測試叢集節點是否通訊

  • 建立一個測試用的nginx pod.
$ kubectl run test-nginx --image=nginx --replicas=3 --port=80
  • 檢視獲取IP情況
$ kubectl get pod -o wide

Kubernetes (k8s) 叢集部署(四) 完整版

  • 暴露服務,建立service.
$ kubectl expose deployment test-nginx --port=8080 --target-port=80 --external-ip=192.168.184.30
  • 檢視service明細。
$ kubectl get svc

Kubernetes (k8s) 叢集部署(四) 完整版

  • 測試svc訪問-就大功告成了
$ curl -I 192.168.184.30:8080

Kubernetes (k8s) 叢集部署(四) 完整版

kubectl常用命令使用

  • kubectl run 執行一個映象
$ kubectl run sonarqube --image=192.168.184.28:5000/sonarqube:5.6.5 --replicas=1 --port=9000

Kubernetes (k8s) 叢集部署(四) 完整版

  • 確認Deployment
$ kubectl get deployment

Kubernetes (k8s) 叢集部署(四) 完整版

  • 確認pod
$ kubectl get pods

Kubernetes (k8s) 叢集部署(四) 完整版

  • 刪除pods
$  kubectl delete pods sonarqube-9969f57d5-d94fc

Kubernetes (k8s) 叢集部署(四) 完整版

  • 刪除deployment
$ kubectl delete deployment sonarqube

Kubernetes (k8s) 叢集部署(四) 完整版

本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章