使用discovery的方式来搭建etcd集群方式有两种:etcd discovery与DNS discovery。在基于已有集群动态发现方式部署etcd集群一文中介绍了etcd discovery的方式,本文我们就来讲讲DNS discovery这种方式的实现。 etcd在基于DNS做服务发现时,实际上是利用DNS的SRV记录不断轮训查询实现的。DNS SRV是DNS数据库中支持的一种资源记录的类型,它记录了哪台计算机提供了哪个服务这么一个简单信息。 本文采用dnsmasq作为dns服务器。
创建DNS记录 1.增加DNS SRV记录 运行:$ vim /etc/dnsmasq.conf #增加内容如下 srv-host=_etcd-server._tcp.ywnz.com,etcd1.ywnz.com,2380,0,100 srv-host=_etcd-server._tcp.ywnz.com,etcd2.ywnz.com,2380,0,100 srv-host=_etcd-server._tcp.ywnz.com,etcd3.ywnz.com,2380,0,100 2.增加对应的域名解析 运行:$ vim /etc/dnsmasq.hosts #增加内容如下 192.168.2.210 etcd1.ywnz.com 192.168.2.211 etcd2.ywnz.com 192.168.2.212 etcd3.ywnz.com 3.重启dnsmasq $ systemctl restart dnsmasq 4.测试是否成功 # 查询SRV记录 $ dig @192.168.2.210 +noall +answer SRV _etcd-server._tcp.ywnz.com _etcd-server._tcp.ywnz.com. 0 IN SRV 0 100 2380 etcd2.ywnz.com. _etcd-server._tcp.ywnz.com. 0 IN SRV 0 100 2380 etcd1.ywnz.com. _etcd-server._tcp.ywnz.com. 0 IN SRV 0 100 2380 etcd3.ywnz.com. # 查询域名解析结果 $ dig @192.168.2.210 +noall +answer etcd1.ywnz.com etcd2.ywnz.com etcd3.ywnz.com etcd1.ywnz.com. 0 IN A 192.168.2.210 etcd2.ywnz.com. 0 IN A 192.168.2.211 etcd3.ywnz.com. 0 IN A 192.168.2.212 5.修改DNS服务器 Linux系统默认从/etc/resolv.conf配置文件读取DNS服务器,为了让etcd能够从dnsmasq服务器获取自定义域名解析,要修改三台etcd服务器的/etc/resolv.conf文件。 # 编辑resolv.conf文件 # 文件内容如下,保证我们自定义的dnsmasq服务器在第一位 $ vim /etc/resolv.conf nameserver 192.168.2.210
配置etcd 1.修改etcd配置文件 开启DNS服务发现,主要是删除掉ETCD_INITIAL_CLUSTER字段(用于静态服务发现),并指定DNS SRV域名(ETCD_DISCOVERY_SRV) 这里以etcd1节点为例(etcd2、etcd3同理): 运行:$ vim /opt/etcd/config/etcd.conf ETCD_NAME=etcd1 ETCD_DATA_DIR="/var/lib/etcd/etcd1" ETCD_LISTEN_PEER_URLS="https://etcd1.ywnz.com:2380" ETCD_LISTEN_CLIENT_URLS="https://etcd1.ywnz.com:2379" ETCD_INITIAL_ADVERTISE_PEER_URLS="https://etcd1.ywnz.com:2380" ETCD_INITIAL_CLUSTER_STATE="new" ETCD_INITIAL_CLUSTER_TOKEN="ywnz-etcd-cluster" ETCD_ADVERTISE_CLIENT_URLS="https://etcd1.ywnz.com:2379,https://etcd1.ywnz.com:4001" ETCD_DISCOVERY_SRV="ywnz.com" 2.测试etcd集群 按上面配置好各集群节点后,分别在各节点启动etcd。 $ systemctl start etcd 启动完成后,执行以下命令查看集群成员: $ etcdctl --endpoints "https://etcd1.ywnz.com:2379" member list 1da4b0d74a9ecfff: name=etcd2 peerURLs=https://etcd2.ywnz.com:2380 clientURLs=https://etcd2.ywnz.com:2379,https://etcd2.ywnz.com:4001 isLeader=false 7dd139e539054326: name=etcd1 peerURLs=https://etcd1.ywnz.com:2380 clientURLs=https://etcd1.ywnz.com:2379,https://etcd1.ywnz.com:4001 isLeader=true a12d567c7c7f2e2b: name=etcd3 peerURLs=https://etcd3.ywnz.com:2380 clientURLs=https://etcd3.ywnz.com:2379,https://etcd3.ywnz.com:4001 isLeader=false
相关主题 |