(十二)etcd集群

参考:

1. 集群搭建(启动脚本)

#!/bin/bash

TOKEN=token-01
CLUSTER_STATE=new

NAME_1=machine-1
NAME_2=machine-2
NAME_3=machine-3

HOST_1=127.0.0.1
PORT_1=2371
PEER_PORT_1=2381

HOST_2=127.0.0.1
PORT_2=2372
PEER_PORT_2=2382


HOST_3=127.0.0.1
PORT_3=2373
PEER_PORT_3=2383

CLUSTER=${NAME_1}=http://${HOST_1}:${PEER_PORT_1},${NAME_2}=http://${HOST_2}:${PEER_PORT_2},${NAME_3}=http://${HOST_3}:${PEER_PORT_3}


# For machine 1
THIS_NAME=${NAME_1}
THIS_IP=${HOST_1}
THIS_PORT=${PORT_1}
THIS_PEER_PORT=${PEER_PORT_1}

etcd --data-dir=data_1.etcd --name ${THIS_NAME} 
	--initial-advertise-peer-urls http://${THIS_IP}:${THIS_PEER_PORT} --listen-peer-urls http://${THIS_IP}:${THIS_PEER_PORT} 
	--advertise-client-urls http://${THIS_IP}:${THIS_PORT} --listen-client-urls http://${THIS_IP}:${THIS_PORT} 
	--initial-cluster ${CLUSTER} 
	--initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN} &


# For machine 2
THIS_NAME=${NAME_2}
THIS_IP=${HOST_2}
THIS_PORT=${PORT_2}
THIS_PEER_PORT=${PEER_PORT_2}

etcd --data-dir=data_2.etcd --name ${THIS_NAME} 
	--initial-advertise-peer-urls http://${THIS_IP}:${THIS_PEER_PORT} --listen-peer-urls http://${THIS_IP}:${THIS_PEER_PORT} 
	--advertise-client-urls http://${THIS_IP}:${THIS_PORT} --listen-client-urls http://${THIS_IP}:${THIS_PORT} 
	--initial-cluster ${CLUSTER} 
	--initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN} &
	
	
	
# For machine 3
THIS_NAME=${NAME_3}
THIS_IP=${HOST_3}
THIS_PORT=${PORT_3}
THIS_PEER_PORT=${PEER_PORT_3}

etcd --data-dir=data_3.etcd --name ${THIS_NAME} 
	--initial-advertise-peer-urls http://${THIS_IP}:${THIS_PEER_PORT} --listen-peer-urls http://${THIS_IP}:${THIS_PEER_PORT} 
	--advertise-client-urls http://${THIS_IP}:${THIS_PORT} --listen-client-urls http://${THIS_IP}:${THIS_PORT} 
	--initial-cluster ${CLUSTER} 
	--initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN} &

2. 基本操作

2.1 endpoints

[root@Thor ~]# etcdctl --endpoints=127.0.0.1:2371,127.0.0.1:2372,127.0.0.1:2373 member list
parse 127.0.0.1:2371: first path segment in URL cannot contain colon
[root@Thor ~]#

处理方式:

  1. 参考first path segment in URL cannot contain colon
[root@Thor ~]# 
[root@Thor ~]# etcdctl --endpoints=//127.0.0.1:2371,//127.0.0.1:2372,//127.0.0.1:2373 member list
8231876619f7abe6: name=machine-2 peerURLs=http://127.0.0.1:2382 clientURLs=http://127.0.0.1:2372 isLeader=true
ad94f96e205aed4b: name=machine-1 peerURLs=http://127.0.0.1:2381 clientURLs=http://127.0.0.1:2371 isLeader=false
f847edbddf60c945: name=machine-3 peerURLs=http://127.0.0.1:2383 clientURLs=http://127.0.0.1:2373 isLeader=false
[root@Thor ~]# 
  1. export ETCDCTL_API=3
[root@Thor ~]# etcdctl --endpoints=//127.0.0.1:2371,//127.0.0.1:2372,//127.0.0.1:2373 put name ld
No help topic for 'put'
[root@Thor ~]# export ETCDCTL_API=3
[root@Thor ~]# etcdctl --endpoints=//127.0.0.1:2371,//127.0.0.1:2372,//127.0.0.1:2373 put name ld
Error: dial tcp: lookup //127.0.0.1: no such host
[root@Thor ~]# 
[root@Thor ~]# etcdctl --endpoints=127.0.0.1:2371,127.0.0.1:2372,127.0.0.1:2373 put name ld
OK
[root@Thor ~]# etcdctl --endpoints=127.0.0.1:2371,127.0.0.1:2372,127.0.0.1:2373 get name
name
ld
[root@Thor ~]# 

2.2 watch

[root@Thor ~]# etcdctl --endpoints=127.0.0.1:2371,127.0.0.1:2372,127.0.0.1:2373 watch name
PUT
name
ld
PUT
name
pipi
[root@Thor etcd]# etcdctl --endpoints=127.0.0.1:2371,127.0.0.1:2372,127.0.0.1:2373 put name ld
proto: no coders for int
proto: no encoder for ValueSize int [GetProperties]
OK
[root@Thor etcd]# etcdctl --endpoints=127.0.0.1:2371,127.0.0.1:2372,127.0.0.1:2373 put name pipi
OK

2.3 distribute lock

[root@Thor etcd]# etcdctl --endpoints=127.0.0.1:2371,127.0.0.1:2372,127.0.0.1:2373 lock mutex1
proto: no coders for int
proto: no encoder for ValueSize int [GetProperties]
mutex1/49456844cb8df005
[root@Thor ~]# etcdctl --endpoints=127.0.0.1:2371,127.0.0.1:2372,127.0.0.1:2373 lock mutex1

2.4 snapshot

[root@Thor etcd]# ls
data_1.etcd  data_2.etcd  data_3.etcd  start.sh
[root@Thor etcd]# etcdctl --endpoints=127.0.0.1:2371,127.0.0.1:2372,127.0.0.1:2373 snapshot save my.db
Snapshot saved at my.db
[root@Thor etcd]# ls
data_1.etcd  data_2.etcd  data_3.etcd  my.db  start.sh

3. 集群操作

3.1 status && health

[root@Thor ~]# etcdctl --endpoints=127.0.0.1:2371,127.0.0.1:2372,127.0.0.1:2373 endpoint status
127.0.0.1:2371, ad94f96e205aed4b, 3.3.10, 20 kB, false, 2, 21
127.0.0.1:2372, 8231876619f7abe6, 3.3.10, 20 kB, true, 2, 21
127.0.0.1:2373, f847edbddf60c945, 3.3.10, 20 kB, false, 2, 21
[root@Thor ~]# 
[root@Thor ~]# etcdctl --endpoints=127.0.0.1:2371,127.0.0.1:2372,127.0.0.1:2373  --write-out=table endpoint status
+----------------+------------------+---------+---------+-----------+-----------+------------+
|    ENDPOINT    |        ID        | VERSION | DB SIZE | IS LEADER | RAFT TERM | RAFT INDEX |
+----------------+------------------+---------+---------+-----------+-----------+------------+
| 127.0.0.1:2371 | ad94f96e205aed4b |  3.3.10 |   20 kB |     false |         2 |         21 |
| 127.0.0.1:2372 | 8231876619f7abe6 |  3.3.10 |   20 kB |      true |         2 |         21 |
| 127.0.0.1:2373 | f847edbddf60c945 |  3.3.10 |   20 kB |     false |         2 |         21 |
+----------------+------------------+---------+---------+-----------+-----------+------------+
[root@Thor ~]# etcdctl --endpoints=127.0.0.1:2371,127.0.0.1:2372,127.0.0.1:2373  --write-out=table endpoint health
127.0.0.1:2372 is healthy: successfully committed proposal: took = 1.394643ms
127.0.0.1:2371 is healthy: successfully committed proposal: took = 1.69421ms
127.0.0.1:2373 is healthy: successfully committed proposal: took = 1.656679ms
[root@Thor ~]# etcdctl --endpoints=127.0.0.1:2371,127.0.0.1:2372,127.0.0.1:2373 endpoint health
127.0.0.1:2371 is healthy: successfully committed proposal: took = 1.538547ms
127.0.0.1:2373 is healthy: successfully committed proposal: took = 1.514083ms
127.0.0.1:2372 is healthy: successfully committed proposal: took = 1.102497ms
[root@Thor ~]# 

3.2 member list

[root@Thor etcd]# etcdctl --endpoints=127.0.0.1:2371,127.0.0.1:2372,127.0.0.1:2373 member list
8231876619f7abe6, started, machine-2, http://127.0.0.1:2382, http://127.0.0.1:2372
ad94f96e205aed4b, started, machine-1, http://127.0.0.1:2381, http://127.0.0.1:2371
f847edbddf60c945, started, machine-3, http://127.0.0.1:2383, http://127.0.0.1:2373
[root@Thor etcd]# 
原文地址:https://www.cnblogs.com/walkinginthesun/p/10261035.html