一 部署 kubectl 1.1 安裝kubectl 1 [root@k8smaster01 ~]# cd /opt/k8s/work 2 [root@k8smaster01 work]# wget https://dl.k8s.io/v1.14.2/kubernetes-client-linux- ...
一 部署 kubectl
1.1 安裝kubectl
1 [root@k8smaster01 ~]# cd /opt/k8s/work 2 [root@k8smaster01 work]# wget https://dl.k8s.io/v1.14.2/kubernetes-client-linux-amd64.tar.gz 3 [root@k8smaster01 work]# tar -zxvf kubernetes-client-linux-amd64.tar.gz
1.2 分發kubectl
1 [root@k8smaster01 ~]# cd /opt/k8s/work 2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh 3 [root@k8smaster01 work]# for master_ip in ${MASTER_IPS[@]} 4 do 5 echo ">>> ${master_ip}" 6 scp kubernetes/client/bin/kubectl root@${master_ip}:/opt/k8s/bin/ 7 ssh root@${master_ip} "chmod +x /opt/k8s/bin/*" 8 done
1.3 創建admin證書和密鑰
1 [root@k8smaster01 ~]# cd /opt/k8s/work 2 [root@k8smaster01 work]# cat > admin-csr.json <<EOF 3 { 4 "CN": "admin", 5 "hosts": [], 6 "key": { 7 "algo": "rsa", 8 "size": 2048 9 }, 10 "names": [ 11 { 12 "C": "CN", 13 "ST": "Shanghai", 14 "L": "Shanghai", 15 "O": "system:masters", 16 "OU": "System" 17 } 18 ] 19 } 20 EOF 21 #創建admin的CA證書請求文件解釋: O 為 system:masters:kube-apiserver 收到該證書後將請求的 Group 設置為 system:masters; 預定義的 ClusterRoleBinding cluster-admin 將 Group system:masters 與 Role cluster-admin 綁定,即該 Role 授予所有 API的許可權; 該證書只會被 kubectl 當做 client 證書使用,所以 hosts 欄位為空。
1 [root@k8smaster01 ~]# cd /opt/k8s/work 2 [root@k8smaster01 work]# cfssl gencert -ca=/opt/k8s/work/ca.pem \ 3 -ca-key=/opt/k8s/work/ca-key.pem -config=/opt/k8s/work/ca-config.json \ 4 -profile=kubernetes admin-csr.json | cfssljson -bare admin #生成CA密鑰(ca-key.pem)和證書(ca.pem)
1.4 創建kubeconfig文件
kubectl 預設從 ~/.kube/config 文件讀取 kube-apiserver 地址和認證信息。只需在master節點部署一次,其生成的 kubeconfig 文件是通用的,可以拷貝到需要執行 kubectl 命令的機器,重命名為/.kube/config即可。 註意:如果沒有特殊指明,本文檔的所有操作均在 k8smaster 節點上執行,然後遠程分發文件和執行命令。1 [root@k8smaster01 ~]# cd /opt/k8s/work 2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh 3 [root@k8smaster01 work]# kubectl config set-cluster kubernetes \ 4 --certificate-authority=/opt/k8s/work/ca.pem \ 5 --embed-certs=true \ 6 --server=${KUBE_APISERVER} \ 7 --kubeconfig=kubectl.kubeconfig # 設置集群參數 8 [root@k8smaster01 work]# kubectl config set-credentials admin \ 9 --client-certificate=/opt/k8s/work/admin.pem \ 10 --client-key=/opt/k8s/work/admin-key.pem \ 11 --embed-certs=true \ 12 --kubeconfig=kubectl.kubeconfig # 設置客戶端認證參數 13 [root@k8smaster01 work]# kubectl config set-context kubernetes \ 14 --cluster=kubernetes \ 15 --user=admin \ 16 --kubeconfig=kubectl.kubeconfig # 設置上下文參數 17 [root@k8smaster01 work]# kubectl config use-context kubernetes --kubeconfig=kubectl.kubeconfig # 設置預設上下文解釋: --certificate-authority:驗證 kube-apiserver 證書的根證書; --client-certificate、--client-key:剛生成的 admin 證書和私鑰,連接 kube-apiserver 時使用; --embed-certs=true:將 ca.pem 和 admin.pem 證書內容嵌入到生成的 kubectl.kubeconfig 文件中(預設寫入的是證書文件路徑,後續需要拷貝 kubeconfig 和該證書文件至到其它機器。)。
1.5 分發kubeconfig
1 [root@k8smaster01 ~]# cd /opt/k8s/work 2 [root@k8smaster01 work]# source /opt/k8s/bin/environment.sh 3 [root@k8smaster01 work]# for master_ip in ${MASTER_IPS[@]} 4 do 5 echo ">>> ${master_ip}" 6 ssh root@${master_ip} "mkdir -p ~/.kube" 7 scp kubectl.kubeconfig root@${master_ip}:~/.kube/config 8 done