[TOC]node
到這裏,ETCD集羣、kube-nginx + keepalived、kube-apiserver都已經安裝完成。nginx
此時能夠測試一下前面安裝的是否正常json
kubectl與apiserver https通訊,apiserver對提供的證書進行認證和受權。kubectl做爲集羣的管理工具,須要被授予最高權限,這裏建立具備最高權限的admin證書api
建立證書籤名請求ssh
cd /opt/k8s/work cat > admin-csr.json <<EOF { "CN": "admin", "hosts": [], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "BeiJing", "L": "BeiJing", "O": "system:masters", "OU": "4Paradigm" } ] } EOF ################### ● O 爲system:masters,kube-apiserver收到該證書後將請求的Group設置爲system:masters ● 預約的ClusterRoleBinding cluster-admin將Group system:masters與Role cluster-admin綁定,該Role授予API的權限 ● 該證書只有被kubectl當作client證書使用,因此hosts字段爲空
生成證書和私鑰tcp
cd /opt/k8s/work cfssl gencert -ca=/opt/k8s/work/ca.pem \ -ca-key=/opt/k8s/work/ca-key.pem \ -config=/opt/k8s/work/ca-config.json \ -profile=kubernetes admin-csr.json | cfssljson -bare admin ls admin*
建立kubeconfig文件工具
kubeconfig爲kubectl的配置文件,包含訪問apiserver的全部信息,如apiserver地址、CA證書和自身使用的證書測試
cd /opt/k8s/work source /opt/k8s/bin/environment.sh # 設置集羣參數 kubectl config set-cluster kubernetes \ --certificate-authority=/opt/k8s/work/ca.pem \ --embed-certs=true \ --server=${KUBE_APISERVER} \ --kubeconfig=kubectl.kubeconfig #設置客戶端認證參數 kubectl config set-credentials admin \ --client-certificate=/opt/k8s/work/admin.pem \ --client-key=/opt/k8s/work/admin-key.pem \ --embed-certs=true \ --kubeconfig=kubectl.kubeconfig # 設置上下文參數 kubectl config set-context kubernetes \ --cluster=kubernetes \ --user=admin \ --kubeconfig=kubectl.kubeconfig # 設置默認上下文 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到其它機器時,還須要單獨拷貝證書)
分發到全部使用kubectl命令的節點spa
cd /opt/k8s/work source /opt/k8s/bin/environment.sh for node_ip in ${NODE_IPS[@]} do echo ">>> ${node_ip}" ssh root@${node_ip} "mkdir -p ~/.kube" scp kubectl.kubeconfig root@${node_ip}:~/.kube/config done #保存文件名爲~/.kube/config
[root@node01 work]# kubectl cluster-info Kubernetes master is running at https://vip.k8s.com:8443 To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'. [root@node01 work]# kubectl get all --all-namespaces NAMESPACE NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE default service/kubernetes ClusterIP 10.254.0.1 <none> 443/TCP 98 [root@node01 work]# kubectl get cs NAME STATUS MESSAGE ERROR scheduler Unhealthy Get http://127.0.0.1:10251/healthz: dial tcp 127.0.0.1:10251: connect: connection refused controller-manager Unhealthy Get http://127.0.0.1:10252/healthz: dial tcp 127.0.0.1:10252: connect: connection refused etcd-0 Healthy {"health":"true"} etcd-2 Healthy {"health":"true"} etcd-1 Healthy {"health":"true"}
若是提示有報錯,請檢查~/.kube/config以及配置證書是否有問題debug
在執行kubectl命令時,apiserver會將請求轉發到kubelet的https端口。這裏定義的RBAC規則,受權apiserver使用的證書(kubernetes.pem)用戶名(CN:kubernetes)訪問kubelet API的權限
kubectl create clusterrolebinding kube-apiserver:kubelet-apis --clusterrole=system:kubelet-api-admin --user kubernetes
安裝到這裏,上面的操做顯示結果相同的話,表示以前的操做都正常,能夠繼續往下
若是不同,仔細對照每一步,若是還不行,能夠聯繫博主。最下方有個人聯繫方式。