上一篇咱們介紹了kubernetes集羣架構以及系統參數配置,參考:二進制搭建kubernetes多master集羣【開篇、集羣環境和功能介紹】html
下面本文etcd集羣才用三臺centos7.5搭建完成。linux
etcd1:192.168.80.4git
etcd2:192.168.80.5github
etcd3:192.168.80.6docker
1、建立CA證書和密鑰json
kubernetes 系統各組件須要使用 TLS 證書對通訊進行加密,本文檔使用 CloudFlare 的 PKI 工具集 cfssl 來生成 Certificate Authority (CA) 證書和祕鑰文件,CA 是自簽名的證書,用來簽名後續建立的其它 TLS 證書。centos
下面步驟是在k8s-master1上操做的。證書只須要建立一次便可,之後在向集羣中添加新節點時只要將 /etc/kubernetes/cert 目錄下的證書拷貝到新節點上便可。api
一、安裝 CFSSL瀏覽器
curl -o /usr/local/bin/cfssl https://pkg.cfssl.org/R1.2/cfssl_linux-amd64 curl -o /usr/local/bin/cfssljson https://pkg.cfssl.org/R1.2/cfssljson_linux-amd64
chmod +x /usr/local/bin/cfssl*
二、建立CA配置文件安全
mkdir -p /etc/kubernetes/cert && cd /etc/kubernetes/cert
cat > ca-config.json <<EOF { "signing": { "default": { "expiry": "87600h" }, "profiles": { "kubernetes": { "usages": [ "signing", "key encipherment", "server auth", "client auth" ], "expiry": "87600h" } } } } EOF
ca-config.json:能夠定義多個 profiles,分別指定不一樣的過時時間、使用場景等參數;後續在簽名證書時使用某個 profile;
signing:表示該證書可用於簽名其它證書;生成的 ca.pem 證書中 CA=TRUE;
server auth:表示 client 能夠用該 CA 對 server 提供的證書進行驗證;
client auth:表示 server 能夠用該 CA 對 client 提供的證書進行驗證;
三、建立CA證書籤名請求
cat > ca-csr.json <<EOF { "CN": "kubernetes", "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "BeiJing", "L": "BeiJing", "O": "k8s", "OU": "4Paradigm" } ] } EOF
Common Name
,kube-apiserver 從證書中提取該字段做爲請求的用戶名 (User Name),瀏覽器使用該字段驗證網站是否合法;Organization
,kube-apiserver 從證書中提取該字段做爲請求用戶所屬的組 (Group);RBAC
受權的用戶標識;生成 CA 證書和私鑰:
cfssl gencert -initca ca-csr.json | cfssljson -bare ca
四、建立etcd證書籤名請求文件
cat > etcd-csr.json <<EOF { "CN": "etcd", "hosts": [ "127.0.0.1", "192.168.80.4", "192.168.80.5", "192.168.80.6" ], "key": { "algo": "rsa", "size": 2048 }, "names": [ { "C": "CN", "ST": "BeiJing", "L": "BeiJing", "O": "k8s", "OU": "4Paradigm" } ] } EOF
生成 CA 證書和私鑰:
cfssl gencert -ca=/etc/kubernetes/cert/ca.pem \ -ca-key=/etc/kubernetes/cert/ca-key.pem \ -config=/etc/kubernetes/cert/ca-config.json \ -profile=kubernetes etcd-csr.json | cfssljson -bare etcd
五、將*.pem證書分發到3臺etcd的/etc/kubernetes/cert目錄下
[root@k8s-master1 cert]# scp *.pem etcd1:/etc/kuberneters/cert [root@k8s-master1 cert]# scp *.pem etcd2:/etc/kuberneters/cert [root@k8s-master1 cert]# scp *.pem etcd3:/etc/kuberneters/cert
ETCD使用證書的組件以下:
2、部署etcd集羣
在三個節點都安裝etcd,下面的操做須要再三個節點都執行一遍
一、下載etcd安裝包
wget https://github.com/etcd-io/etcd/releases/download/v3.3.10/etcd-v3.3.10-linux-amd64.tar.gz
tar zxf etcd-v3.3.10-linux-amd64.tar.gz
cp etcd-v3.3.10-linux-amd64/etcd* /usr/local/bin
二、建立工做目錄
mkdir -p /var/lib/etcd
三、建立systemd unit 文件(紅色字填寫對應etcd主機的名稱和ip)
cat > /etc/systemd/system/etcd.service << EOF [Unit] Description=Etcd Server After=network.target After=network-online.target Wants=network-online.target Documentation=https://github.com/coreos [Service] Type=notify WorkingDirectory=/var/lib/etcd/ ExecStart=/usr/local/bin/etcd \ --name etcd1 \ --cert-file=/etc/kubernetes/cert/etcd.pem \ --key-file=/etc/kubernetes/cert/etcd-key.pem \ --peer-cert-file=/etc/kubernetes/cert/etcd.pem \ --peer-key-file=/etc/kubernetes/cert/etcd-key.pem \ --trusted-ca-file=/etc/kubernetes/cert/ca.pem \ --peer-trusted-ca-file=/etc/kubernetes/cert/ca.pem \ --initial-advertise-peer-urls https://192.168.80.4:2380 \ --listen-peer-urls https://192.168.80.4:2380 \ --listen-client-urls https://192.168.80.4:2379,http://127.0.0.1:2379 \ --advertise-client-urls https://192.168.80.4:2379 \ --initial-cluster-token etcd-cluster-0 \ --initial-cluster etcd1=https://192.168.80.4:2380,etcd2=https://192.168.80.5:2380,etcd3=https://192.168.80.6:2380 \ --initial-cluster-state new \ --data-dir=/var/lib/etcd Restart=on-failure RestartSec=5 LimitNOFILE=65536 [Install] WantedBy=multi-user.target EOF
爲了保證通訊安全,須要指定 etcd 的公私鑰(cert-file和key-file)、Peers 通訊的公私鑰和 CA 證書(peer-cert-file、peer-key-file、peer-trusted-ca-file)、客戶端的CA證書(trusted-ca-file);
建立etcd.pem 證書時使用的 etcd-csr.json 文件的 hosts 字段包含全部 etcd 節點的IP,不然證書校驗會出錯;
–initial-cluster-state 值爲 new 時,–name 的參數值必須位於 –initial-cluster 列表中.
四、啓動etcd服務而且設置開機自啓動
sudo systemctl daemon-reload
sudo systemctl enable etcd
sudo systemctl start etcd
五、查看etcd服務狀態信息
systemctl status etcd
最早啓動的 etcd 進程會卡住一段時間,等待其它節點上的 etcd 進程加入集羣,爲正常現象。
六、驗證etcd集羣狀態,以及查看leader,在任何一個etcd節點執行
[root@etcd1 cert]# etcdctl --ca-file=/etc/kubernetes/cert/ca.pem --cert-file=/etc/kubernetes/cert/etcd.pem --key-file=/etc/kubernetes/cert/etcd-key.pem cluster-health
member 1ee23928cd50adb0 is healthy: got healthy result from https://192.168.80.6:2379
member 535d7a5dfde52a30 is healthy: got healthy result from https://192.168.80.4:2379
member 8476403d43c0e204 is healthy: got healthy result from https://192.168.80.5:2379
[root@etcd1 cert]# etcdctl --ca-file=/etc/kubernetes/cert/ca.pem --cert-file=/etc/kubernetes/cert/etcd.pem --key-file=/etc/kubernetes/cert/etcd-key.pem member list
1ee23928cd50adb0: name=etcd3 peerURLs=https://192.168.80.6:2380 clientURLs=https://192.168.80.6:2379 isLeader=false
535d7a5dfde52a30: name=etcd1 peerURLs=https://192.168.80.4:2380 clientURLs=https://192.168.80.4:2379 isLeader=false
8476403d43c0e204: name=etcd2 peerURLs=https://192.168.80.5:2380 clientURLs=https://192.168.80.5:2379 isLeader=true
七、舉例:etcdctl調用集羣建立/kubernetes/network的key
[root@etcd3 cert]# etcdctl --ca-file=/etc/kubernetes/cert/ca.pem --cert-file=/etc/kubernetes/cert/etcd.pem --key-file=/etc/kubernetes/cert/etcd-key.pem mkdir /kubernetes/network
至此ETCD TLS證書集羣部署完成。
下一篇是關於docker使用flannel網絡的配置,請參考:二進制搭建kubernetes多master集羣【2、配置flannel網絡】