爲何要首先配置etcd集羣呢,由於etcd集羣是Kubernetes的基礎,全部數據都存儲在這裏git
其實這裏建議使用主機名,避免ip變化而致使的數據庫不可用,可是我習慣用ip,爲避免證書看起來不那麼亂,因此我將不一樣的證書放置到不一樣的目錄裏面github
cat > /etc/ssl/etcd/etcd-csr.json <<EOF
{
"CN": "etcd",
"hosts": [
"127.0.0.1",
"192.168.1.40",
"192.168.1.41",
"192.168.1.42"
],
"key": {
"algo": "rsa",
"size": 2048
},
"names": [
{
"C": "CN",
"ST": "ChengDu",
"L": "ChengDu",
"O": "k8s",
"OU": "dessler"
}
]
}
EOF
複製代碼
cfssl gencert -ca=/etc/ssl/ca.pem \
-ca-key=/etc/ssl/ca-key.pem \
-config=/etc/ssl/ca-config.json \
-profile=kubernetes etcd-csr.json | cfssljson -bare etcd複製代碼
ls
etcd.csr etcd-csr.json etcd-key.pem etcd.pem
複製代碼
過程我就省略了,都是些基本的文件傳輸操做數據庫
(本身注意數據保存目錄,根據本身的狀況決定)json
chmod 644 /etc/ssl/etcd/etcd-key.pem
useradd -s /sbin/nologin -M etcd
mkdir -p /var/lib/etcd/
chown -R etcd:etcd /var/lib/etcd/
複製代碼
這裏須要特別注意就是etcd的名字,集羣的3個節點是不能重複的,還有和這個名字和集羣的配置(好比我這裏的etcd01,etcd02,etcd03)bash
cat > /usr/lib/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]
User=etcd
Type=notify
WorkingDirectory=/var/lib/etcd/
ExecStart=/usr/bin/etcd \\
--data-dir=/var/lib/etcd \\
--name=etcd01 \\
--cert-file=/etc/ssl/etcd/etcd.pem \\
--key-file=/etc/ssl/etcd/etcd-key.pem \\
--trusted-ca-file=/etc/ssl/ca.pem \\
--peer-cert-file=/etc/ssl/etcd/etcd.pem \\
--peer-key-file=/etc/ssl/etcd/etcd-key.pem \\
--peer-trusted-ca-file=/etc/ssl/ca.pem \\
--peer-client-cert-auth \\
--client-cert-auth \\
--listen-peer-urls=https://192.168.1.40:2380 \\
--initial-advertise-peer-urls=https://192.168.1.40:2380 \\
--listen-client-urls=https://192.168.1.40:2379,http://127.0.0.1:2379 \\
--advertise-client-urls=https://192.168.1.40:2379 \\
--initial-cluster-token=etcd-cluster-0 \\
--initial-cluster=etcd01=https://192.168.1.40:2380,etcd02=https://192.168.1.41:2380,etcd03=https://192.168.1.42:2380 \\
--initial-cluster-state=new
Restart=on-failure
RestartSec=5
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF複製代碼
cat > /usr/lib/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]
User=etcd
Type=notify
WorkingDirectory=/var/lib/etcd/
ExecStart=/usr/bin/etcd \\
--data-dir=/var/lib/etcd \\
--name=etcd02 \\
--cert-file=/etc/ssl/etcd/etcd.pem \\
--key-file=/etc/ssl/etcd/etcd-key.pem \\
--trusted-ca-file=/etc/ssl/ca.pem \\
--peer-cert-file=/etc/ssl/etcd/etcd.pem \\
--peer-key-file=/etc/ssl/etcd/etcd-key.pem \\
--peer-trusted-ca-file=/etc/ssl/ca.pem \\
--peer-client-cert-auth \\
--client-cert-auth \\
--listen-peer-urls=https://192.168.1.41:2380 \\
--initial-advertise-peer-urls=https://192.168.1.41:2380 \\
--listen-client-urls=https://192.168.1.41:2379,http://127.0.0.1:2379 \\
--advertise-client-urls=https://192.168.1.41:2379 \\
--initial-cluster-token=etcd-cluster-0 \\
--initial-cluster=etcd01=https://192.168.1.40:2380,etcd02=https://192.168.1.41:2380,etcd03=https://192.168.1.42:2380 \\
--initial-cluster-state=new
Restart=on-failure
RestartSec=5
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF複製代碼
cat > /usr/lib/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]
User=etcd
Type=notify
WorkingDirectory=/var/lib/etcd/
ExecStart=/usr/bin/etcd \\
--data-dir=/var/lib/etcd \\
--name=etcd03 \\
--cert-file=/etc/ssl/etcd/etcd.pem \\
--key-file=/etc/ssl/etcd/etcd-key.pem \\
--trusted-ca-file=/etc/ssl/ca.pem \\
--peer-cert-file=/etc/ssl/etcd/etcd.pem \\
--peer-key-file=/etc/ssl/etcd/etcd-key.pem \\
--peer-trusted-ca-file=/etc/ssl/ca.pem \\
--peer-client-cert-auth \\
--client-cert-auth \\
--listen-peer-urls=https://192.168.1.42:2380 \\
--initial-advertise-peer-urls=https://192.168.1.42:2380 \\
--listen-client-urls=https://192.168.1.42:2379,http://127.0.0.1:2379 \\
--advertise-client-urls=https://192.168.1.42:2379 \\
--initial-cluster-token=etcd-cluster-0 \\
--initial-cluster=etcd01=https://192.168.1.40:2380,etcd02=https://192.168.1.41:2380,etcd03=https://192.168.1.42:2380 \\
--initial-cluster-state=new
Restart=on-failure
RestartSec=5
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
EOF
複製代碼
etcdctl --endpoints=https://192.168.1.40:2379,https://192.168.1.41:2379,https://192.168.1.42:2379 \
> --cert-file=/etc/ssl/etcd/etcd.pem \
> --ca-file=/etc/ssl/ca.pem \
> --key-file=/etc/ssl/etcd/etcd-key.pem \
> cluster-health
member e42b05792291d17 is healthy: got healthy result from https://192.168.1.41:2379
member 68326dea8aa5233d is healthy: got healthy result from https://192.168.1.40:2379
member e10fc861b3b13bc0 is healthy: got healthy result from https://192.168.1.42:2379複製代碼
注意的幾個地方:name 須要一一對應,ip地址也容易出錯服務器
集羣狀態正常,etd集羣建立成果ui