client certificate is used to authenticate client by server. (客戶端證書:用於被服務端認證)node
server certificate is used by server and verified by client for server identity.(服務端證書:用來驗證使用證書的服務器的身份)json
peer certificate is used by etcd cluster members as they communicate with each other in both ways.(對等證書:由etcd集羣成員使用,由於它們以兩種方式相互通訊;對等證書便是客戶端證書,也是服務端證書)bootstrap
注:服務端證書通常要指定hosts,即容許哪些IP或者域名使用此證書,客戶端證書通常不須要指定hosts,或者指定爲[](爲空)api
本例中:只採用一個ca根證書;也能夠採用多個不一樣ca分別進行驗證服務器
ca證書:ca.pem、ca-key.pem(本例中etcd、k8s組件全使用同一個ca證書)app
etcd證書:ide
peer.pem、peer-key.pem:etcd各節點相互通訊的對等證書及私鑰(hosts指定全部etcd節點IP)spa
server.pem、server-key.pem:etcd各節點本身的服務器證書及私鑰(hosts指定當前etcd節點的IP)命令行
client.pem、client-key.pem:命令行客戶端訪問etcd使用的證書私鑰(hosts能夠不寫或者爲空)代理
apiserver-etcd-client.pem、apiserver-etcd-client-key.pem:apiserver訪問etcd的證書及私鑰;
注:其中peer.pem和server.pem可使用一個,由於都是服務端證書(hosts指定全部etcd節點IP)
client.pem和apiserver-etcd-client.pem可使用一個,由於都是客戶端證書(hosts都爲空或不寫)
k8s證書:
kube-apiserver.pem:kube-apiserver節點使用的證書(每一個master生成一個,hosts爲當前master的IP)
kubelet.pem:kube-apiserver訪問kubelet時的客戶端證書(每一個master一個,hosts爲當前master的IP)
aggregator-proxy.pem:kube-apiserver使用聚合時,客戶端訪問代理的證書(hosts爲空)
admin.pem:kubectl客戶端的證書(hosts爲空或者不寫)
關於cfssl的用法參考我另外一篇博客:http://www.javashuo.com/article/p-tefktauk-bb.html
cat ca-config.json { "signing": { "default": { "expiry": "438000h" }, "profiles": { "kubernetes": { "usages": [ "signing", "key encipherment", "server auth", "client auth" ], "expiry": "438000h" } } } }
cat ca-csr.json { "CN": "kubernetes", "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "HangZhou", "L": "XS", "O": "k8s", "OU": "System" } ], "ca": { "expiry": "876000h" } }
cfssl gencert -initca ca-csr.json | cfssljson -bare ca
cat peer-csr.json { "CN": "etcd", "hosts": [ "192.168.10.110", "192.168.10.111", "192.168.10.112", "127.0.0.1" ], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "HangZhou", "L": "XS", "O": "k8s", "OU": "System" } ] } cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes peer-csr.json | cfssljson -bare peer 注:由於是對等證書,須要指定hosts(即只能在指定的IP上使用);hosts指定的IP均爲etcd各節點的IP
cat server-csr.json { "CN": "etcd", "hosts": [ "192.168.10.110", "127.0.0.1" ], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "HangZhou", "L": "XS", "O": "k8s", "OU": "System" } ] } cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes server-csr.json | cfssljson -bare server 注:由於是server證書,須要指定hosts(即只能在指定的IP上使用),hosts當前etcd節點的IP地址; 注:各節點均要生成此證書,若是羨麻煩,能夠直接使用peer的證書
{ "CN": "etcd", "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "HangZhou", "L": "XS", "O": "k8s", "OU": "System" } ] } cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes client-csr.json | cfssljson -bare client 注:由於是client端證書,不須要指定hosts,由於須要在任意節點使用此證書 注:由於kube-apiserver也是做爲etcd的客戶端訪問etcd的,因此kube-apiserver也可使用此證書; 注:kube-apiserver使用上面的證書時,hosts爲空,也能夠指定hosts單獨爲apiserver生成證書 使用示例:查看集羣是否健康 etcdctl --key-file=./client-key.pem \ --cert-file=./client.pem \ --ca-file=./ca.pem --endpoints="https://192.168.10.110:2379" \ cluster-health
cat admin-csr.json { "CN": "admin", "hosts": [], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "HangZhou", "L": "XS", "O": "system:masters", "OU": "System" } ] } cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes admin-csr.json | cfssljson -bare admin 注:CN至關於k8s中的User,names中的O至關於k8s的Group,在認證時會基於此進行鑑權 注:本例中O對應的值爲system:masters,在k8s中system:masters組綁定到了cluster-admin上了 注:cluster-admin的clusterrolebinding是對整個集羣具備管理權限 注:此證書也可用於kube-apiserver訪問kubelet使用的認證文件 --kubelet-https=true \ --kubelet-client-certificate=/etc/kubernetes/ssl/admin.pem \ --kubelet-client-key=/etc/kubernetes/ssl/admin-key.pem
[root@master01 ~]# kubectl describe clusterrolebinding cluster-admin Name: cluster-admin Labels: kubernetes.io/bootstrapping=rbac-defaults Annotations: rbac.authorization.kubernetes.io/autoupdate: true Role: Kind: ClusterRole Name: cluster-admin Subjects: Kind Name Namespace ---- ---- --------- Group system:masters 注:能夠看到system:masters的類型爲Group,綁定在名字爲cluster-admin的clusterrolebinding上 注:由此admin.pem經過k8s的RBAC認證後,才能管理整個集羣;
#生成admin使用的kubeconfig文件,以完成對集羣的訪問 #設置集羣參數 kubectl config set-cluster myk8s \ --certificate-authority=ca.pem \ --embed-certs=true \ --server=https://192.168.10.103:6443 #設置客戶端認證參數 kubectl config set-credentials cluster-admin \ --client-certificate=admin.pem \ --client-key=admin-key.pem \ --embed-certs=true #設置上下文參數 kubectl config set-context admin --cluster=myk8s --user=cluster-admin #選擇使用的上下文 kubectl config use-context admin
cat kube-apiserver-csr.json { "CN": "kubernetes", "hosts": [ "127.0.0.1", "192.168.10.103", "192.168.10.250", "10.68.0.1", "www.51yunv.com", "kubernetes", "kubernetes.default", "kubernetes.default.svc", "kubernetes.default.svc.cluster", "kubernetes.default.svc.cluster.local" ], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "HangZhou", "L": "XS", "O": "k8s", "OU": "System" } ] } cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-apiserver-csr.json | cfssljson -bare kube-apiserver 注:kube-apiserver做爲全部訪問的入口,必定要把hosts的內容指全面,不然會形成沒法訪問的狀況; 注:在本例中的192.168.10.103,表示當前master節點的IP地址(每一個master的替換IP後,都要生成此證書) 注:在本例中的192.168.10.250,表示VIP,即若是前面用keepalived+haproxy作HA時的VIP地址 注:在本例中的10.68.0.1,表示集羣中第一個service的地址,pod會經過此地址訪問kube-apiserver; 注:在本例中的www.51yunv.com,表示當kube-apiserver對外部提供訪問時使用的域名; 注:在本例中的kubernetes.default.svc.cluster.local,表示此集羣中的Pod經過dns訪問kube-apiserver使用的地址
cat aggregator-proxy-csr.json { "CN": "aggregator", "hosts": [], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "HangZhou", "L": "XS", "O": "k8s", "OU": "System" } ] } cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes aggregator-proxy-csr.json | cfssljson -bare aggregator-proxy 注:aggregator證書用開啓kube-apiserver聚合時,客戶端訪問kube-aggregator使用的證書
kubelet訪問apiserver使用的是kubeconfig文件;須要先生成證書,而後經過證書生成kubeconfig文件
cat kubelet-csr.json { "CN": "system:node:10.10.5.218", "hosts": [ "127.0.0.1", "10.10.5.218" ], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "HangZhou", "L": "XS", "O": "system:nodes", "OU": "System" } ] } cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kubelet-csr.json | cfssljson -bare kubelet 注:hosts中的10.10.5.218爲當前node節點的IP地址,不一樣的node要更換後生成當前node節點可使用的證書; 而後在每一個節點上都要生成當前節點可用的kubeconfig文件 #生成kubeconfig文件 kubectl config set-cluster k8s \ --certificate-authority=ca.pem \ --embed-certs=true \ --server=https://192.168.10.103:6443 \ --kubeconfig=kubelet.kubeconfig kubectl config set-credentials system:node:192.168.10.105 \ --client-certificate=kubelet.pem \ --embed-certs=true \ --client-key=kubelet-key.pem \ --kubeconfig=kubelet.kubeconfig kubectl config set-context default --cluster=k8s --user=system:node:192.168.10.105 --kubeconfig=kubelet.kubeconfig kubectl config use-context default --kubeconfig=kubelet.kubeconfig
kube-proxy訪問apiserver使用的是kubeconfig文件;須要先生成證書,而後經過證書生成kubeconfig文件
cat kube-proxy-csr.json { "CN": "system:kube-proxy", "hosts": [], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "HangZhou", "L": "XS", "O": "k8s", "OU": "System" } ] } cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=kubernetes kube-proxy-csr.json | cfssljson -bare kube-proxy 注:CN至關於k8s中的User,names中的O至關於k8s的Group,在RBAC籤權時的須要認證 注:本例中CN對應的值爲system:kube-proxy,在k8s中system:kube-proxy綁定到了system:node-proxier 注:system:node-proxier是專用於kube-proxy訪問kube-apiserver的 生成kubeconfig文件 kubectl config set-cluster k8s \ --certificate-authority=ca.pem \ --embed-certs=true \ --server=https://192.168.10.103:6443 \ --kubeconfig=kube-proxy.kubeconfig kubectl config set-credentials kube-proxy \ --client-certificate=kube-proxy.pem \ --embed-certs=true \ --client-key=kube-proxy-key.pem \ --kubeconfig=kube-proxy.kubeconfig kubectl config set-context default --cluster=k8s --user=kube-proxy --kubeconfig=kube-proxy.kubeconfig kubectl config use-context default --kubeconfig=kube-proxy.kubeconfig
[root@master01 etc]# kubectl describe clusterrolebinding system:node-proxier Name: system:node-proxier Labels: kubernetes.io/bootstrapping=rbac-defaults Annotations: rbac.authorization.kubernetes.io/autoupdate: true Role: Kind: ClusterRole Name: system:node-proxier Subjects: Kind Name Namespace ---- ---- --------- User system:kube-proxy 注:能夠看到system:kube-proxy用戶綁定在system:node-proxier的clusterrolebinding上 這樣當kube-proxy訪問kube-apiserver就能夠經過RBAC的認證;