ETCD 集群搭建

ETCD 集群搭建

环境

CentOS 7 192.168.1.221
CentOS 7 192.168.1.222
CentOS 7 192.168.1.223

下载

[root@test-1-221 ~]#wget -c https://github.com/etcd-io/etcd/releases/download/v3.3.12/etcd-v3.3.12-linux-amd64.tar.gz

安装

[root@test-1-221 ~]#tar -zxvf etcd-v3.3.12-linux-amd64.tar.gz
[root@test-1-221 ~]#cd etcd-v3.3.12-linux-amd64
[root@test-1-221 etcd-v3.3.12-linux-amd64]#cp etcd etcdctl /usr/bin

配置文件

[root@test-1-221 ~]#vim /usr/lib/systemd/system/etcd.service
[Unit]
Description=Etcd Server
After=network.target
[Service]
Type=simple
WorkingDirectory=/var/lib/etcd/ 
EnvironmentFile=-/etc/etcd/etcd.conf
ExecStart=/usr/bin/etcd
Type=notify
[Install]
WantedBy=multi-user.target文件对对对[root@test-1-221 ~]#vim /etc/etcd/etcd.conf
#[member]
ETCD_NAME=test-1-221
ETCD_DATA_DIR="/var/lib/etcd"
ETCD_LISTEN_CLIENT_URLS="http://192.168.1.221:2379,http://127.0.0.1:2379"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.1.221:2379,http://127.0.0.1:2379"
#[cluster]
ETCD_LISTEN_PEER_URLS="http://192.168.1.221:2380"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.1.221:2380"
ETCD_INITIAL_CLUSTER="test-1-221=http://192.168.1.221:2380,test-1-222=http://192.168.1.222:2380,test-1-223=http://192.168.1.223:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
#new为新建集群,existing为加入已有集群

重启etcd服务

[root@test-1-221 ~]# systemctl start etcd.service
[root@test-1-222 ~]# systemctl start etcd.service
[root@test-1-223 ~]# systemctl start etcd.service

查看集群状态

[root@test-1-221 ~]#etcdctl member list
deead697bf5d6912: name=test-1-222 peerURLs=http://192.168.1.222:2380 clientURLs=http://127.0.0.1:2379,http://192.168.1.222:2379 isLeader=false
f1a016d41a47d370: name=test-1-223 peerURLs=http://192.168.1.223:2380 clientURLs=http://127.0.0.1:2379,http://192.168.1.223:2379 isLeader=true
f93a3e37631fb8a3: name=test-1-221 peerURLs=http://192.168.1.221:2380 clientURLs=http://127.0.0.1:2379,http://192.168.1.221:2379 isLeader=false

查看节点角色:

[root@test-1-221 ~]#curl http://127.0.0.1:2379/v2/stats/leader
{"leader":"f1a016d41a47d370","followers":{"deead697bf5d6912":{"latency":{"current":0.002184,"average":0.005985857142857143,"standardDeviation":0.004100336272919625,"minimum":0.001448,"maximum":0.014162},"counts":{"fail":0,"success":7}},"f93a3e37631fb8a3":{"latency":{"current":0.001907,"average":0.011239333333333336,"standardDeviation":0.012893240110831549,"minimum":0.001907,"maximum":0.038351},"counts":{"fail":0,"success":6}}}}

添加一条数据:

[root@test-1-221 ~]#curl -vvv http://127.0.0.1:2379/v2/keys/testkey -XPUT -d value="jin ri tou tiao"
* About to connect() to 127.0.0.1 port 2379 (#0)
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 2379 (#0)
> PUT /v2/keys/testkey HTTP/1.1
> User-Agent: curl/7.29.0
> Host: 127.0.0.1:2379
> Accept: */*
> Content-Length: 21
> Content-Type: application/x-www-form-urlencoded
> 
* upload completely sent off: 21 out of 21 bytes
< HTTP/1.1 201 Created
< Content-Type: application/json
< X-Etcd-Cluster-Id: e0006c36e087eb18
< X-Etcd-Index: 11
< X-Raft-Index: 13
< X-Raft-Term: 21
< Date: Sun, 03 Mar 2019 01:20:37 GMT
< Content-Length: 106
< 
{"action":"set","node":{"key":"/testkey","value":"jin ri tou tiao","modifiedIndex":11,"createdIndex":11}}
* Connection #0 to host 127.0.0.1 left intact

获取数据

[root@test-1-221 ~]# etcdctl get testkey
jin ri tou tiao

或者

[root@test-1-221 ~]# curl -X GET http://192.168.1.223:2379/v2/keys/testkey
{"action":"get","node":{"key":"/testkey","value":"jin ri tou tiao","modifiedIndex":11,"createdIndex":11}}
内容来源:今日头条
角标
继续阅读(剩余50%
我要举报