Etcd集群搭建步骤

Etcd是一个高可用的 Key/Value 存储系统,主要用于分享配置和服务发现。 
● 简单:支持 curl 方式的用户 RES API和客户端命令etcdctl ;
● 安全:可选 SSL 客户端证书认证 ;
● 快速:单实例可达每秒一千次写操作 ;
● 可靠:使用 Raft 实现分布式存储,高可用,强一致。

集群参数含义官网地址:https://coreos.com/etcd/docs/latest/op-guide/configuration.html
版本:etcd-v3.3.2-linux-amd64.tar.gz
下载链接:https://github.com/coreos/etcd/releases/download/v3.3.2/etcd-v3.3.2-linux-amd64.tar.gz


部署环境三台机子:
10.42.5.187
10.42.5.188
10.42.5.189

在10.42.5.187机子上部署:
1. 解压版本包
   tar -zxvf etcd-v3.3.2-linux-amd64.tar.gz

2. 将解压文件重命名并移到/etc/目录下
  mv etcd-v3.3.2-linux-amd64  /etc/etcd-v3.3.2

3. 将etcd和etcdctl加入系统变量PATH中(可以省略)
在 文件~/.bashrc中加入一行
export PATH=/etc/etcd-v3.3.2:$PATH
然后执行:source ~/.bashrc 立即生效

4.进入/etc/etcd-v3.3.2,并创建配置文件etcd.conf,内容:
name: etcd-1
data-dir: /etc/etcd-v3.3.2/data
listen-client-urls: http://0.0.0.0:2379
advertise-client-urls: http://10.42.5.187:2379
listen-peer-urls: http://0.0.0.0:2380
initial-advertise-peer-urls: http://10.42.5.187:2380
initial-cluster: etcd-1=http://10.42.5.187:2380,etcd-2=http://10.42.5.188:2380,etcd-3=http://10.42.5.189:2380
initial-cluster-token: etcd-cluster-my
initial-cluster-state: new


注释:listen-client-urls: http://10.42.5.187:2379 会报错,所以直接用0.0.0.0;
           listen-peer-urls:  http://10.42.5.187:2380  也会报错,所以直接用0.0.0.0;
name:本member的名称;
data-dir:存储的数据目录;
listen-client-urls:用于监听客户端etcdctl或者curl连接;0.0.0.0表示监听本机所有地址;
listen-peer-urls:用于监听集群中其它member的连接;0.0.0.0表示监听本机所有地址;
advertise-client-urls: 本机地址, 用于通知客户端,客户端通过此IPs与集群通信;
initial-advertise-peer-urls:本机地址,用于通知集群member,与member通信;
initial-cluster:描述集群中所有节点的信息,描述每个节点名称、ip、端口,集群静态启动使用,本member根据此信息去联系其他member;
initial-cluster-token:集群唯一标示;
initial-cluster-state:集群状态,新建集群时候设置为new,若是想加入某个已经存在的集群设置为existing

5.设置etcd版本号,默认为2,我们安装的版本为3.3.2,所以此处设置为3
默认示例:
[root@forimg system]# etcdctl
NAME:
   etcdctl - A simple command line client for etcd.

WARNING:
   Environment variable ETCDCTL_API is not set; defaults to etcdctl v2.
   Set environment variable ETCDCTL_API=3 to use v3 API or ETCDCTL_API=2 to use v2 API.

USAGE:
   etcdctl [global options] command [command options] [arguments...]
   
VERSION:
   3.3.2

在 文件~/.bashrc中加入一行改为3
export ETCDCTL_API=3

6. 设置开机启动,新建文件/usr/lib/systemd/system/etcd.service,(可以跳过,设置方便管理)配置内容:
[Unit]
Description=Etcd Server
After=network.target
After=network-online.target
Wants=network-online.target

[Service]
Type=notify
WorkingDirectory=/etc/etcd-v3.3.2
# User=etcd
ExecStart=/etc/etcd-v3.3.2/etcd --config-file /etc/etcd-v3.3.2/etcd.conf
Restart=on-failure
LimitNOFILE=65536

[Install]
WantedBy=multi-user.target


注释:
WorkingDirectory和ExecStart填写对应的目录和启动命令。

5. 启动etcd,启动第一个member的时候,此member等待其它member初始化完毕,处于watting状态,此时紧接着启动第二个就行了
systemctl daemon-reload
systemctl enable etcd
systemctl start etcd
systemctl restart etcd

6.10.42.5.188,10.42.5.189部署如同上面步骤,只是将步骤4里面ip改为自己ip即可。

7. 检测集群状态
(1) 版本号
[root@pipeline-nj-1 etc]# etcd --version
etcd Version: 3.3.2
Git SHA: c9d46ab37
Go Version: go1.9.4
Go OS/Arch: linux/amd64
 (2) 列出成员
[root@forimg system]# etcdctl member list
570e7c4d5d42bc27: name=etcd-2 peerURLs=http://10.42.5.188:2380 clientURLs=http://10.42.5.188:2379 isLeader=false
cfac4bbae80da926: name=etcd-1 peerURLs=http://10.42.5.187:2380 clientURLs=http://10.42.5.187:2379 isLeader=true
e71e71be5f299670: name=etcd-3 peerURLs=http://10.42.5.189:2380 clientURLs=http://10.42.5.189:2379 isLeader=false
 
(3)检查集群状健康状态(cluster-health是V2版本,V3已经没有)
[root@pipeline-nj-2 etcd-v3.3.2]# etcdctl endpoint health --endpoints=10.42.5.187:2379,10.42.5.188:2379,10.42.5.189:2379
10.42.5.187:2379 is healthy: successfully committed proposal: took = 927.62µs
10.42.5.189:2379 is healthy: successfully committed proposal: took = 1.493606ms
10.42.5.188:2379 is healthy: successfully committed proposal: took = 1.804497ms

8.使用,参照官方网址:https://coreos.com/etcd/docs/latest/demo.html
(1)写
[root@pipeline-nj-1 etc]# etcdctl put key1 value1
OK

(2)读
[root@pipeline-nj-1 etc]# etcdctl get key1
key1
value1

(3)前缀读(将所有前缀是key的键值对都读出来)
[root@pipeline-nj-1 etc]# etcdctl get key --prefix
key1
value1
key2
value2
key3
value3
(4)删除
[root@pipeline-nj-1 etc]# etcdctl del key
0
[root@pipeline-nj-1 etc]# etcdctl del key1
1
(5)按前缀全部删除
[root@pipeline-nj-1 etc]# etcdctl del key --prefix
2
(6)监听某个键,每次键值有变化,这边都会收到
[root@pipeline-nj-1 etc]# etcdctl watch falg --prefix
PUT
falg
watchtest
PUT
falg
watchtest2
(7)设置过期时间TTL
#设置一个lease(id=3c27623e1704580d)过期时间为30秒
[root@pipeline-nj-2 etcd-v3.3.2]# etcdctl lease grant 30
lease 3c27623e1704580d granted with TTL(30s)

#将id=3c27623e1704580d的lease应用key5键值,即设置key5键值对生命周期为30秒,到期后自动消亡
[root@pipeline-nj-2 etcd-v3.3.2]# etcdctl put key5 value5 --lease=3c27623e1704580d
OK
[root@pipeline-nj-2 etcd-v3.3.2]# etcdctl get key5
key5
value5

#使id=3c27623e1704580d的lease继续存活30秒,到时间后自动消亡
[root@pipeline-nj-2 etcd-v3.3.2]# etcdctl lease keep-alive 3c27623e1704580d
lease 3c27623e1704580d keepalived with TTL(30)

#撤销id=3c27623e1704580d的release,立即终止生命周期
[root@pipeline-nj-2 etcd-v3.3.2]# etcdctl lease revoke 3c27623e1704580d
lease 3c27623e1704580d revoked

(8)检查集群状态
[root@pipeline-nj-2 etcd-v3.3.2]# etcdctl endpoint status --endpoints=10.42.5.187:2379,10.42.5.188:2379,10.42.5.189:2379
10.42.5.187:2379, cfac4bbae80da926, 3.3.2, 25 kB, true, 268, 57
10.42.5.188:2379, 570e7c4d5d42bc27, 3.3.2, 25 kB, false, 268, 57
10.42.5.189:2379, e71e71be5f299670, 3.3.2, 25 kB, false, 268, 57

(9)检查集群健康状态
[root@pipeline-nj-2 etcd-v3.3.2]# etcdctl endpoint health --endpoints=10.42.5.187:2379,10.42.5.188:2379,10.42.5.189:2379
10.42.5.187:2379 is healthy: successfully committed proposal: took = 927.62µs
10.42.5.189:2379 is healthy: successfully committed proposal: took = 1.493606ms
10.42.5.188:2379 is healthy: successfully committed proposal: took = 1.804497ms

你可能感兴趣的:(etcd,软件安装,配置,部署)