使用 DNS 启动 Etcd 集群搭建(备忘)

巫马望
2023-12-01

Etcd 集群

Etcd 集群搭建有很多种方式,官方文档: https://etcd.io/docs/v3.4/op-guide/clustering/

本文介绍使用 DNS 启动 Etcd 集群搭建: https://etcd.io/docs/v3.4/op-guide/clustering/#dns-discovery

Etcd 启动脚本

使用以下脚本:

#!/bin/bash

if [ $# -lt 8 ]; then
    echo "Usage: $0 <name> <dns> <etcd_domain> <hostnameOrIP> <cluster_state> <token> <client_port> <peer_port>"
    exit 0
fi

name=$1
dns=$2
etcd_domain=$3
host=$4
cluster_state=$5
token=$6
client_port=$7
peer_port=$8

docker stop ${name} && docker rm ${name}

set -ex

docker run --name=${name} -d --restart=unless-stopped \
    -p ${client_port}:${client_port} \
    -p ${peer_port}:${peer_port} \
    -v ${name}:/etcd-data \
    --dns=${dns} \
    quay.io/coreos/etcd:latest etcd \
    --data-dir=/etcd-data \
    --name ${name} \
    --discovery-srv ${etcd_domain} \
    --initial-advertise-peer-urls http://${host}:${peer_port} \
    --initial-cluster-token ${token} \
    --initial-cluster-state ${cluster_state} \
    --advertise-client-urls http://${host}:${client_port} \
    --listen-client-urls http://0.0.0.0:${client_port} \
    --listen-peer-urls http://0.0.0.0:${peer_port}

部署过程

假设要部署 3 个 Etcd 实例的集群:

Etcd 实例名域名主机端口
test_etcd1etcd1.test.com172.26.150.12512379 12380
test_etcd2etcd2.test.com172.26.150.12522379 22380
test_etcd3etcd3.test.com172.26.150.12532379 32380
1. 设置域名解析

这里使用 dnsmasq 来做 DNS 服务器。配置过程如下:

  • dnsmasq 所在主机 /etc/hosts 内添加:

    172.26.150.125    etcd1.test.com
    172.26.150.125    etcd2.test.com
    172.26.150.125    etcd3.test.com
    
  • dnsmasq 所在主机 /etc/dnsmasq.d/test_etcd.conf 内添加:

    srv-host=_etcd-server._tcp.test.com,etcd1.test.com,12380,100
    srv-host=_etcd-server._tcp.test.com,etcd2.test.com,22380,100
    srv-host=_etcd-server._tcp.test.com,etcd3.test.com,32380,100
    
  • 重启 dnsmasq :

    systemctl restart dnsmasq.service
    
  • 验证域名解析

    [fananchong@vm-centos7 backend]$ dig @localhost SRV _etcd-server._tcp.test.com
    
    ; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.4 <<>> @localhost SRV _etcd-server._tcp.test.com
    ; (2 servers found)
    ;; global options: +cmd
    ;; Got answer:
    ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 53757
    ;; flags: qr aa rd ra ad; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 4
    
    ;; OPT PSEUDOSECTION:
    ; EDNS: version: 0, flags:; udp: 4096
    ;; QUESTION SECTION:
    ;_etcd-server._tcp.test.com.    IN      SRV
    
    ;; ANSWER SECTION:
    _etcd-server._tcp.test.com. 0   IN      SRV     100 0 12380 etcd1.test.com.
    _etcd-server._tcp.test.com. 0   IN      SRV     100 0 32380 etcd3.test.com.
    _etcd-server._tcp.test.com. 0   IN      SRV     100 0 22380 etcd2.test.com.
    
    ;; ADDITIONAL SECTION:
    etcd3.test.com.         0       IN      A       172.26.150.125
    etcd2.test.com.         0       IN      A       172.26.150.125
    etcd1.test.com.         0       IN      A       172.26.150.125
    
    ;; Query time: 0 msec
    ;; SERVER: ::1#53(::1)
    ;; WHEN: Sun Apr 11 13:53:36 CST 2021
    ;; MSG SIZE  rcvd: 205
    
2. 启动 Etcd 实例

假设 DNS 服务器地址为 172.26.150.124

DNS=172.26.150.124
TOKEN=test_etcd_cluster
./etcd.sh test_etcd1 ${DNS} test.com etcd1.test.com new ${TOKEN} 12379 12380
./etcd.sh test_etcd2 ${DNS} test.com etcd2.test.com new ${TOKEN} 22379 22380
./etcd.sh test_etcd3 ${DNS} test.com etcd3.test.com new ${TOKEN} 32379 32380
3. 查看集群情况
[fananchong@vm-centos7 backend]$ etcdctl --endpoints="http://172.26.150.125:12379" member list
c7126a6672d8f591: name=test_etcd3 peerURLs=http://etcd3.test.com:32380 clientURLs=http://etcd3.test.com:32379 isLeader=false
d616124d605ecded: name=test_etcd1 peerURLs=http://etcd1.test.com:12380 clientURLs=http://etcd1.test.com:12379 isLeader=true
fc90e87086184606: name=test_etcd2 peerURLs=http://etcd2.test.com:22380 clientURLs=http://etcd2.test.com:22379 isLeader=false
[fananchong@vm-centos7 backend]$ etcdctl --endpoints="http://172.26.150.125:22379" member list
c7126a6672d8f591: name=test_etcd3 peerURLs=http://etcd3.test.com:32380 clientURLs=http://etcd3.test.com:32379 isLeader=false
d616124d605ecded: name=test_etcd1 peerURLs=http://etcd1.test.com:12380 clientURLs=http://etcd1.test.com:12379 isLeader=true
fc90e87086184606: name=test_etcd2 peerURLs=http://etcd2.test.com:22380 clientURLs=http://etcd2.test.com:22379 isLeader=false
[fananchong@vm-centos7 backend]$ etcdctl --endpoints="http://172.26.150.125:32379" member list
c7126a6672d8f591: name=test_etcd3 peerURLs=http://etcd3.test.com:32380 clientURLs=http://etcd3.test.com:32379 isLeader=false
d616124d605ecded: name=test_etcd1 peerURLs=http://etcd1.test.com:12380 clientURLs=http://etcd1.test.com:12379 isLeader=true
fc90e87086184606: name=test_etcd2 peerURLs=http://etcd2.test.com:22380 clientURLs=http://etcd2.test.com:22379 isLeader=false

以上

 类似资料: