单节点的etcd实例扩容成3节点集群模式
1、环境信息:
节点1: etcd1 192.168.122.222
节点2: etcd2 192.168.122.247
节点3: etcd3 192.168.122.173
2、制作etcd自签名证书
根据以下方法制作etcd需要的证书:
创建自签名证书几种方式 | 你的菜根
3、启动etcd和扩容
扩容etcd顺序是: 1节点—>2节点—>3节点
3.1 启动单etcd节点
启动命令:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| etcd \ --name=etcd1 \ --cert-file=/etc/etcdca/etcd.pem \ --key-file=/etc/etcdca/etcd-key.pem \ --peer-cert-file=/etc/etcdca/etcd.pem \ --peer-key-file=/etc/etcdca/etcd-key.pem \ --trusted-ca-file=/etc/etcdca/ca.pem \ --peer-trusted-ca-file=/etc/etcdca/ca.pem \ --initial-advertise-peer-urls=https://192.168.122.222:2380 \ --listen-peer-urls=https://192.168.122.222:2380 \ --listen-client-urls=https://192.168.122.222:2379,http://127.0.0.1:2379 \ --advertise-client-urls=https://192.168.122.222:2379 \ --initial-cluster-token=etcd-cluster-0 \ --initial-cluster=etcd1=https://192.168.122.222:2380 \ --initial-cluster-state=new \ --data-dir=/data/etcdnew \ --wal-dir= \ --snapshot-count=50000 \ --auto-compaction-retention=1 \ --auto-compaction-mode=periodic \ --max-request-bytes=10485760 \ --log-level=info
|
检查启动是否完成: etcdctl member list
3.2 扩容etcd成2节点
因为原集群时一个节点的集群,在新的成员启动前集群无法继续工作,,因为它需要两个成员作为galosh才能在一致性上达成一致。这个行为仅仅发生在 etcdctl member add
影响集群和新成员成功建立连接到已有成员的时间内。
在etcd1上操作命令把etcd2加入到成员中:
etcdctl member add etcd2 --peer-urls="https://192.168.122.247:2380"
加入新节点后此时整个集群处于不可用状态,等到etcd2启动完选好lead后集群才能正常提供服务。
etcd2启动命令:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| etcd \ --name=etcd2 \ --cert-file=/etc/etcdca/etcd.pem \ --key-file=/etc/etcdca/etcd-key.pem \ --peer-cert-file=/etc/etcdca/etcd.pem \ --peer-key-file=/etc/etcdca/etcd-key.pem \ --trusted-ca-file=/etc/etcdca/ca.pem \ --peer-trusted-ca-file=/etc/etcdca/ca.pem \ --initial-advertise-peer-urls=https://192.168.122.247:2380 \ --listen-peer-urls=https://192.168.122.247:2380 \ --listen-client-urls=https://192.168.122.247:2379,http://127.0.0.1:2379 \ --advertise-client-urls=https://192.168.122.247:2379 \ --initial-cluster-token=etcd-cluster-0 \ --initial-cluster=etcd1=https://192.168.122.222:2380,etcd2=https://192.168.122.247:2380 \ --initial-cluster-state=existing \ --data-dir=/data/etcdnew \ --wal-dir= \ --snapshot-count=50000 \ --auto-compaction-retention=1 \ --auto-compaction-mode=periodic \ --max-request-bytes=10485760 \ --log-level=info
|
因为是已存在集群–initial-cluster-state=existing –initial-cluster中把节点1的地址也加入进去。
加入成功后查看集群成员:etcdctl member list
查看集群状态:
1
| etcdctl --endpoints=https://192.168.122.247:2379,https://192.168.122.222:2379 --cacert=/etc/etcdca/ca.pem --cert=/etc/etcdca/etcd.pem --key=/etc/etcdca/etcd-key.pem --write-out=table endpoint status
|
3.3 扩容etcd成3节点集群
把etcd3加入到集群命令: etcdctl member add etcd3 --peer-urls="https://192.168.122.173:2380"
etcd3启动命令:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| etcd \ --name=etcd3 \ --cert-file=/etc/etcdca/etcd.pem \ --key-file=/etc/etcdca/etcd-key.pem \ --peer-cert-file=/etc/etcdca/etcd.pem \ --peer-key-file=/etc/etcdca/etcd-key.pem \ --trusted-ca-file=/etc/etcdca/ca.pem \ --peer-trusted-ca-file=/etc/etcdca/ca.pem \ --initial-advertise-peer-urls=https://192.168.122.173:2380 \ --listen-peer-urls=https://192.168.122.173:2380 \ --listen-client-urls=https://192.168.122.173:2379,http://127.0.0.1:2379 \ --advertise-client-urls=https://192.168.122.173:2379 \ --initial-cluster-token=etcd-cluster-0 \ --initial-cluster=etcd1=https://192.168.122.222:2380,etcd2=https://192.168.122.247:2380,etcd3=https://192.168.122.173:2380 \ --initial-cluster-state=existing \ --data-dir=/data/etcdnew \ --wal-dir= \ --snapshot-count=50000 \ --auto-compaction-retention=1 \ --auto-compaction-mode=periodic \ --max-request-bytes=10485760 \ --log-level=info
|
启动完成后查看集群的状态:
1
| etcdctl --endpoints=https://192.168.122.247:2379,https://192.168.122.222:2379,https://192.168.122.173:2379 --cacert=/etc/etcdca/ca.pem --cert=/etc/etcdca/etcd.pem --key=/etc/etcdca/etcd-key.pem --write-out=table endpoint status
|
3.4 测试宕节点情况下集群的状态
1个节点宕机并且把数据清理
恢复步骤: 把节点从member成员里移除,再执行下扩容节点的步骤,扩容成功后数据会自动同步,无需手动restore db。