etcd叢集部署

难止汗發表於2024-08-16

etcd叢集部署

一、環境準備

  1、準備3臺伺服器。

作業系統  IP地址 主機名
CentOS7.9 192.168.110.12 etcd1
CentOS7.9 192.168.110.13 etcd2
CentOS7.9 192.168.110.15 etcd3

  2、配置3臺伺服器hosts。(3個節點相同操作)

  vim /etc/hosts

192.168.110.12 etcd1
192.168.110.13 etcd2
192.168.110.15 etcd3

  3、配置3臺伺服器時間同步。(3個節點相同操作)

yum -y install ntpd
vim /etc/ntp.conf
    #修改server的資訊為時間伺服器的資訊,這裡設定為阿里雲的時間伺服器
    server ntp1.aliyun.com iburst
    server ntp2.aliyun.com iburst
    server ntp3.aliyun.com iburst

  4、配置防火牆,放開時間ntpd服務埠。(3個節點相同操作)

firewall-cmd --permanent --add-port=123/udp
firewall-cmd --reload

  5、啟動ntpd服務,並檢查ntp同步狀態。(3個節點相同操作)

systemctl start ntpd
systemctl enable ntpd
ntpq -p

二、生成etcd自簽證書。(只在etcd1上操作)

  1、下載CFSSL工具。

wget https://github.com/cloudflare/cfssl/releases/download/v1.6.3/cfssl_1.6.3_linux_amd64
wget https://github.com/cloudflare/cfssl/releases/download/v1.6.3/cfssl-certinfo_1.6.3_linux_amd64
wget https://github.com/cloudflare/cfssl/releases/download/v1.6.3/cfssljson_1.6.3_linux_amd64

  2、將二進位制包移動至/usr/local/bin/下,並賦予許可權。

mv cfssl_1.6.3_linux_amd64 /usr/local/bin/cfssl
mv cfssl-certinfo_1.6.3_linux_amd64  /usr/local/bin/cfssl-certinfo
mv cfssljson_1.6.3_linux_amd64 /usr/local/bin/cfssljson
chmod u+x /usr/local/bin/cfssl
chmod u+x /usr/local/bin/cfssl-certinfo
chmod u+x /usr/local/bin/cfssljson

  3、建立證書目錄。(3個節點相同操作)

mkdir -p /data/etcd/{bin,ssl,data}

  4、進入證書目錄,建立CA配置檔案。

cd /data/etcd/ssl
cat > ca-config.json <<EOF
{
    "signing": {
        "default": {
            "expiry": "262800h"
        },
        "profiles": {
            "etcd": {
                "expiry": "262800h",
                "usages": [
                    "signing",
                    "key encipherment",
                    "server auth",
                    "client auth"
                ]
            }
        }
    }
}
EOF

  5、建立CA證書資訊檔案。(etcd1節點操作)

cat > ca-csr.json << EOF
{
    "CN": "Etcd CA",
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "ST": "Beijing",
            "L": "Beijing",
            "O": "Etcd CA",
            "OU": "Etcd CA"
        }
    ]
}
EOF

  6、生成CA秘鑰和證書

cfssl gencert -initca ca-csr.json | cfssljson -bare ca

  這會生成兩個檔案:ca.pem(CA 證書)和 ca-key.pem(CA 私鑰)。

  7、建立etcd證書資訊檔案

cat > server-csr.json << EOF
{
    "CN": "etcd-server",
    "hosts": [
        "localhost",
        "127.0.0.1",
        "192.168.110.12",
        "192.168.110.13",
        "192.168.110.15",
        "192.168.110.16",
        "192.168.110.17",
        "etcd1",
        "etcd2",
        "etcd3",
        "etcd4",
        "etcd5",   
        "etcd1.com",
        "*.etcd1.com"
    ],
    "key": {
        "algo": "rsa",
        "size": 2048
    },
    "names": [
        {
            "C": "CN",
            "ST": "Beijing",
            "L": "Beijing",
            "O": "Etcd Server",
            "OU": "Etcd Server"
        }
    ]
}
EOF      

  這裡在配置etcd證書資訊時,在hosts項中加入了預留地址,以便後期擴容,如果不確定預留地址資訊可以使用萬用字元域名的方式,後期使用dns解析的方式來解決證書不匹配的問題。

  

  8、生成etcd證書。

cfssl gencert -ca=ca.pem -ca-key=ca-key.pem -config=ca-config.json -profile=etcd server-csr.json | cfssljson -bare server

  這會生成兩個檔案:server.pem(etcd 證書)和 server-key.pem(etcd 證書私鑰)。

  9、傳輸證書和私鑰到etcd2和etcd3

scp *.pem etcd2:/data/etcd/ssl
scp *.pem etcd3:/data/etcd/ssl

部署etcd叢集(3個節點相同操作)

  1、下載etcd二進位制包。

wget https://github.com/etcd-io/etcd/releases/download/v3.5.15/etcd-v3.5.15-linux-amd64.tar.gz

  2、解壓,移動命令檔案,配置環境變數

tar zxf etcd-v3.5.15-linux-amd64.tar.gz
cp etcd-v3.5.15-linux-amd64/etcd* /data/etcd/bin/
echo "export PATH=/data/etcd/bin:\$PATH" > /etc/profile.d/etcd.sh
source /etc/profile.d/etcd.sh

  3、建立systemd管理檔案。

cat > /usr/lib/systemd/system/etcd.service << EOF

[Unit]
Description=Etcd Service
After=network.target

[Service]
Type=notify
ExecStart=/data/etcd/bin/etcd \
--name=etcd1 \
--cert-file=/data/etcd/ssl/server.pem \
--key-file=/data/etcd/ssl/server-key.pem \
--peer-cert-file=/data/etcd/ssl/server.pem \
--peer-key-file=/data/etcd/ssl/server-key.pem \
--trusted-ca-file=/data/etcd/ssl/ca.pem \
--peer-trusted-ca-file=/data/etcd/ssl/ca.pem \
--peer-client-cert-auth \
--client-cert-auth \
--initial-advertise-peer-urls=https://192.168.110.12:2380 \
--listen-peer-urls=https://192.168.110.12:2380 \
--listen-client-urls=https://192.168.110.12:2379,https://127.0.0.1:2379 \
--advertise-client-urls=https://192.168.110.12:2379 \
--initial-cluster-token=etcd-cluster-1 \
--initial-cluster=etcd1=https://192.168.110.12:2380,etcd2=https://192.168.110.13:2380,etcd3=https://192.168.110.15:2380 \
--initial-cluster-state=new \
--data-dir=/data/etcd/data

Restart=on-failure
RestartSec=5


[Install]
WantedBy=multi-user.target

EOF

  其中紅色部分資訊根據不同的節點進行配置,藍色部分資訊根據自己定義的name資訊進行配置。

  4、啟動etcd服務,設定開機啟動

systemctl start etcd
systemctl enable etcd

   5、檢視叢集狀態。

etcdctl --endpoints=https://192.168.110.12:2379,https://192.168.110.13:2379,https://192.168.110.15:2379 \
  --cacert=/data/etcd/ssl/ca.pem \
  --cert=/data/etcd/ssl/server.pem \
  --key=/data/etcd/ssl/server-key.pem \
  endpoint health

相關文章