etcd——源码编译、部署、集群部署、测试
借一张图
1、下载
https://github.com/etcd-io/etcd
git clone https://github.com/etcd-io/etcd.git
2、编译
进入etcd目录,
mac/linux下, make clean && make build
在etcd/bin目录下,生成 etcd etcdctl etcdutl 三个可执行文件
在win10下,不用make的前提下,
go build -o bin\etcd.exe server\main.go
go build -o bin\etcdctl.exe etcdctl\main.go
出现\etcd\client\v3 目录下诸多文件报错,这些文件都用的目录引用,
把真实目录 \etcd\tests\integration\clientv3\examples下对应的文件直接替换 D:\git\go\etcd\etcd\client\v3 目录下的,
重新编译即可。
3、单节点启动
命令行下:
etcd -data-dir ~/data.etcd -advertise-client-urls http://0.0.0.0:2379 -listen-client-urls http://0.0.0.0:2379
在 goland ide 下,加启动参数
--data-dir bin\in-ide.etcd --advertise-client-urls http://0.0.0.0:2379 --listen-client-urls http://0.0.0.0:2379
4、测试
etcdctl member list etcdctl --endpoints=10.60.80.11:2379 member list etcdctl --endpoints=10.60.80.11:2379 put foo hihi etcdctl --endpoints=10.60.80.11:2379 get foo
5、win10下集群部署与测试
1、win 10 环境 下载 https://github.com/etcd-io/etcd/releases 解压 etcd-v3.4.20-windows-amd64.zip 2、启动命令见 https://blog.51cto.com/u_10125763/3700481 3、依次启动 start1.bat start2.bat start3.bat 4、查看集群状态 etcdctl --endpoints=[192.168.1.32:2379,192.168.1.32:3379,192.168.1.32:4379] member list etcdctl --endpoints=[192.168.1.32:2379,192.168.1.32:3379,192.168.1.32:4379] endpoint health --cluster=true etcdctl --endpoints=10.60.80.11:2379 member list 5、查看服务发现 etcdctl --endpoints=[192.168.1.32:2379,192.168.1.32:3379,192.168.1.32:4379] get /game/sanguo-test/server_state/gateway/1660642593870881200 /game/sanguo-test/server_state/gateway/1660642593870881200
start1.bat
.\etcd.exe --name etcd01 ^ --data-dir .\data\etcd01 ^ --advertise-client-urls http://192.168.1.32:2379 ^ --listen-client-urls http://192.168.1.32:2379 ^ --listen-peer-urls http://192.168.1.32:2380 ^ --initial-advertise-peer-urls http://192.168.1.32:2380 ^ --initial-cluster-token etcd-cluster-1 ^ --initial-cluster etcd01=http://192.168.1.32:2380,etcd02=http://192.168.1.32:2381,etcd03=http://192.168.1.32:2382 ^ --initial-cluster-state new pause
start2.bat
.\etcd.exe --name etcd02 ^ --data-dir .\data\etcd02 ^ --advertise-client-urls http://192.168.1.32:3379 ^ --listen-client-urls http://192.168.1.32:3379 ^ --listen-peer-urls http://192.168.1.32:2381 ^ --initial-advertise-peer-urls http://192.168.1.32:2381 ^ --initial-cluster-token etcd-cluster-1 ^ --initial-cluster etcd01=http://192.168.1.32:2380,etcd02=http://192.168.1.32:2381,etcd03=http://192.168.1.32:2382 ^ --initial-cluster-state new pause
start3.bat
.\etcd.exe --name etcd03 ^ --data-dir .\data\etcd03 ^ --advertise-client-urls http://192.168.1.32:4379 ^ --listen-client-urls http://192.168.1.32:4379 ^ --listen-peer-urls http://192.168.1.32:2382 ^ --initial-advertise-peer-urls http://192.168.1.32:2382 ^ --initial-cluster-token etcd-cluster-1 ^ --initial-cluster etcd01=http://192.168.1.32:2380,etcd02=http://192.168.1.32:2381,etcd03=http://192.168.1.32:2382 ^ --initial-cluster-state new pause
6、mac/linux下集群部署
创建执行文件
touch etcd-static.sh
在每个etcd节点上,指定集群成员:
# 指定集群token # 如果你所在的网络环境配置了多个etcd集群,为了避免意外发生, # 最好使用-initial-cluster-token参数为每个集群单独配置一个token认证。 # 这样就可以确保每个集群和集群的成员都拥有独特的ID。 TOKEN=token-01 CLUSTER_STATE=new NAME_1=etcd-node-1 NAME_2=etcd-node-2 NAME_3=etcd-node-3 HOST_1=10.60.100.21 HOST_2=10.60.100.21 HOST_3=10.60.100.21 PORT_1_CLIENT=2379 PORT_2_CLIENT=2479 PORT_3_CLIENT=2579 PORT_1_MEMBER=2380 PORT_2_MEMBER=2381 PORT_3_MEMBER=2382 CLUSTER=${NAME_1}=http://${HOST_1}:${PORT_1_MEMBER},${NAME_2}=http://${HOST_2}:${PORT_2_MEMBER},${NAME_3}=http://${HOST_3}:${PORT_3_MEMBER} ETCD_SERVER=/Users/admin/Documents/git/go/etcd/etcd/bin/etcd
每个节点添加一下各节点对应启动命令行:
etcd-node-1.sh
${ETCD_SERVER} --data-dir=data.etcd/${NAME_1} --name ${NAME_1} \ --initial-advertise-peer-urls http://${HOST_1}:${PORT_1_MEMBER} --listen-peer-urls http://${HOST_1}:${PORT_1_MEMBER} \ --advertise-client-urls http://${HOST_1}:${PORT_1_CLIENT} --listen-client-urls http://${HOST_1}:${PORT_1_CLIENT} \ --initial-cluster ${CLUSTER} \ --initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}
etcd-node-2.sh
${ETCD_SERVER} --data-dir=data.etcd/${NAME_2} --name ${NAME_2} \ --initial-advertise-peer-urls http://${HOST_2}:${PORT_2_MEMBER} --listen-peer-urls http://${HOST_2}:${PORT_2_MEMBER} \ --advertise-client-urls http://${HOST_2}:${PORT_2_CLIENT} --listen-client-urls http://${HOST_2}:${PORT_2_CLIENT} \ --initial-cluster ${CLUSTER} \ --initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}
etcd-node-3.sh
${ETCD_SERVER} --data-dir=data.etcd/${NAME_3} --name ${NAME_3} \ --initial-advertise-peer-urls http://${HOST_3}:${PORT_3_MEMBER} --listen-peer-urls http://${HOST_3}:${PORT_3_MEMBER} \ --advertise-client-urls http://${HOST_3}:${PORT_3_CLIENT} --listen-client-urls http://${HOST_3}:${PORT_3_CLIENT} \ --initial-cluster ${CLUSTER} \ --initial-cluster-state ${CLUSTER_STATE} --initial-cluster-token ${TOKEN}
查看集群状态
etcdctl --endpoints=[10.60.100.21:2379,10.60.100.21:2479,10.60.100.21:2579] member list etcdctl --endpoints=[10.60.100.21:2379,10.60.100.21:2479,10.60.100.21:2579] endpoint health --cluster=true
7、etcdctl命令备份
./etcdctl --endpoints=my-etcd-0.com:2379 get --prefix / ./etcdctl --endpoints=my-etcd-0.com:2379 get --prefix "" ./etcdctl --endpoints=my-etcd-0.com:2379 endpoint health ./etcdctl --endpoints=my-etcd-0.com:2379 get member list ./etcdctl --endpoints=[my-etcd-0.com:2379,my-etcd-1.com:2379,my-etcd-2.com:2379] get --prefix ""
cd /Users/admin/Documents/git/go/etcd/etcd/bin pwd echo "./etcdctl get / --prefix --endpoints=100.6.9.9:2379 " echo "" echo "=================" echo "" ./etcdctl get / --prefix --endpoints=100.6.9.9:2379 echo "./etcdctl get / --prefix --endpoints=100.6.9.9:2379 | grep game"