Etcd 集群配置

部署环境三台机子:
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

  1. 设置开机启动,新建文件/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填写对应的目录和启动命令。

  1. 启动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即可。

————————————————
常用指令:
etcd --version
etcdctl member list

检查集群状态

etcdctl endpoint health --
etcdctl put key1 value1
etcdctl get key1
etcdctl get foo --print-value-only

读取foo为前缀的所有键/值

etcdctl get --prefix foo
etcdctl del key

设置过期时间

设置一个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

你可能感兴趣的:(Etcd 集群配置)