etcd单机部署单、多实例

一、单实例etcd

1、启动etcd服务

首先下载对应的etcd,然后进行解压:

[root@localhost software]# tar -xzvf etcd-v3.3.10-linux-amd64.tar.gz -C /project/opt/

查看etcd server版本:

[root@localhost etcd-v3.3.10-linux-amd64]# ./etcd --version
etcd Version: 3.3.10
Git SHA: 27fc7e2
Go Version: go1.10.4
Go OS/Arch: linux/amd64

启动etcd服务:

[root@localhost etcd-v3.3.10-linux-amd64]# ./etcd
2021-02-11 20:02:00.850358 I | etcdmain: etcd Version: 3.3.10
2021-02-11 20:02:00.850419 I | etcdmain: Git SHA: 27fc7e2
...
2021-02-11 20:02:02.176710 N | embed: serving insecure client requests on 127.0.0.1:2379, this is strongly discouraged!

可以看到etcd默认启动在2379端口上。

2、客户端测试

重新开一个终端,进行客户端测试。

  •  写入键值对
# 通过put添加{foo:bar}键值对
[root@localhost etcd-v3.3.10-linux-amd64]# ETCDCTL_API=3 ./etcdctl --endpoints=localhost:2379 put foo bar
OK
  • 读取键值
# 通过get读取键值
[root@localhost etcd-v3.3.10-linux-amd64]# ETCDCTL_API=3 ./etcdctl --endpoints=localhost:2379 get foo 
foo
bar

二、多实例etcd

1、配置与启动

如何单机启动多实例etcd,etcd server默认使用2380监听集群中其它server的请求,但是如果启动多个实例就需要监听不同的端口。如果单价创建三个实例,其目录如下:

# etcd_cluster目录如下结构
[root@localhost etcd_cluster]# tree
.
├── etcd1
│   ├── conf.yml
│   └── data
├── etcd2
│   ├── conf.yml
│   └── data
└── etcd3
    ├── conf.yml
    └── data

6 directories, 3 files

然后为三个实例各自创建三个配置文件:

  • etcd1配置文件conf.yml
name: etcd1
data-dir: /project/opt/etcd_cluster/etcd1/data
listen-client-urls: http://127.0.0.1:12379
advertise-client-urls: http://127.0.0.1:12379
listen-peer-urls: http://127.0.0.1:12380
initial-advertise-peer-urls: http://127.0.0.1:12380
initial-cluster: etcd1=http://127.0.0.1:12380,etcd2=http://127.0.0.1:22380,etcd3=http://127.0.0.1:32380
initial-cluster-token: etcd-cluster-1
initial-cluster-state: new
  • etcd2配置文件conf.yml
name: etcd2
data-dir: /project/opt/etcd_cluster/etcd2/data
listen-client-urls: http://127.0.0.1:22379
advertise-client-urls: http://127.0.0.1:22379
listen-peer-urls: http://127.0.0.1:22380
initial-advertise-peer-urls: http://127.0.0.1:22380
initial-cluster: etcd1=http://127.0.0.1:12380,etcd2=http://127.0.0.1:22380,etcd3=http://127.0.0.1:32380
initial-cluster-token: etcd-cluster-1
initial-cluster-state: new
  • etcd3配置文件conf.yml
name: etcd3
data-dir: /project/opt/etcd_cluster/etcd3/data
listen-client-urls: http://192.168.159.130:32379
advertise-client-urls: http://192.168.159.130:32379
listen-peer-urls: http://192.168.159.130:32380
initial-advertise-peer-urls: http://192.168.159.130:32380
initial-cluster: etcd1=http://192.168.159.130:12380,etcd2=http://192.168.159.130:22380,etcd3=http://192.168.159.130:32380
initial-cluster-token: etcd-cluster-1
initial-cluster-state: new

注意:配置文件中键值之间是有空格的。

此时可以通过etcd命令进行启动:

# 启动第一个实例
[root@localhost etcd-v3.3.10-linux-amd64]# ./etcd --config-file=/project/opt/etcd_cluster/etcd1/conf.yml

# 启动第二个实例
[root@localhost etcd-v3.3.10-linux-amd64]# ./etcd --config-file=/project/opt/etcd_cluster/etcd2/conf.yml

# 启动第三个实例
[root@localhost etcd-v3.3.10-linux-amd64]# ./etcd --config-file=/project/opt/etcd_cluster/etcd3/conf.yml

查看集群状态:

[root@localhost etcd-v3.3.10-linux-amd64]# ./etcdctl --endpoints http://127.0.0.1:12379,http://127.0.0.1:22379,http://127.0.0.1:32379 member list
883d459774f37cba: name=etcd2 peerURLs=http://192.168.159.130:22380 clientURLs= isLeader=false
aa9a2cb7a887f51b: name=etcd1 peerURLs=http://192.168.159.130:12380 clientURLs=http://127.0.0.1:12379 isLeader=true
aab0ded7be6d8b05: name=etcd3 peerURLs=http://192.168.159.130:32380 clientURLs=http://192.168.159.130:32379 isLeader=false

2、客户端测试

  • 写入数据

选择任意节点向集群写入数据。比如etcd1实例:

[root@localhost etcd-v3.3.10-linux-amd64]# ETCDCTL_API=3 ./etcdctl --endpoints 127.0.0.1:12379 put foo bar
OK
  • 容灾能力

如果关闭etcd2实例,不影响数据的正常读写:

[root@localhost etcd-v3.3.10-linux-amd64]# ETCDCTL_API=3 ./etcdctl --endpoints 127.0.0.1:12379 put fo ba
OK
作者:iveBoy
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/shenjianping/p/14398093.html