搭建etcd集群
最后更新于:2022-04-02 02:56:49
[TOC]
## 安装epel源
```
wget http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
rpm -ivh epel-release-latest-7.noarch.rpm
```
## 安装etcd
```
yum install etcd
```
## 配置hosts
```
vim /etc/hosts
etcd-25-23 10.39.25.23
etcd-25-24 10.39.25.24
etcd-25-25 10.39.25.25
```
## 修改配置
10.39.25.23的配置
```
vim /etc/etcd/etcd.conf
ETCD_NAME=etcd-25-23
ETCD_DATA_DIR="/var/lib/etcd/etcd-cluster"
ETCD_LISTEN_PEER_URLS="http://10.39.25.23:2380"
ETCD_LISTEN_CLIENT_URLS="http://10.39.25.23:2379,http://127.0.0.1:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://10.39.25.23:2380"
ETCD_INITIAL_CLUSTER="etcd-25-23=http://10.39.25.23:2380,etcd-25-24=http://10.39.25.24:2380,etcd-25-25=http://10.39.25.25:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://10.39.25.23:2379"
systemctl start etcd
systemctl satus etcd
```
10.39.25.25的配置
```
vim /etc/etcd/etcd.conf
ETCD_NAME=etcd-25-25
ETCD_DATA_DIR="/var/lib/etcd/etcd-cluster"
ETCD_LISTEN_PEER_URLS="http://10.39.25.25:2380"
ETCD_LISTEN_CLIENT_URLS="http://10.39.25.25:2379,http://127.0.0.1:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://10.39.25.25:2380"
ETCD_INITIAL_CLUSTER="etcd-25-23=http://10.39.25.23:2380,etcd-25-24=http://10.39.25.24:2380,etcd-25-25=http://10.39.25.25:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://10.39.25.25:2379"
systemctl start etcd
systemctl satus etcd
```
```
10.39.25.24的配置
vim /etc/etcd/etcd.conf
ETCD_NAME=etcd-25-24
ETCD_DATA_DIR="/var/lib/etcd/etcd-cluster"
ETCD_LISTEN_PEER_URLS="http://10.39.25.24:2380"
ETCD_LISTEN_CLIENT_URLS="http://10.39.25.24:2379,http://127.0.0.1:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://10.39.25.24:2380"
ETCD_INITIAL_CLUSTER="etcd-25-23=http://10.39.25.23:2380,etcd-25-24=http://10.39.25.24:2380,etcd-25-25=http://10.39.25.25:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://10.39.25.24:2379"
systemctl start etcd
systemctl satus etcd
```
## 查看etcd集群节点
```
etcdctl member list
7e5e9b85b854c4f6: name=etcd-25-23 peerURLs=http://10.39.25.23:2380 clientURLs=http://10.39.25.23:2379 isLeader=true
c25ca32dd7fbc99f: name=etcd-25-24 peerURLs=http://10.39.25.24:2380 clientURLs=http://10.39.25.24:2379 isLeader=false
de67b2618c148f0d: name=etcd-25-25 peerURLs=http://10.39.25.25:2380 clientURLs=http://10.39.25.25:2379 isLeader=false
```
## 查看集群状态
```
etcdctl cluster-health
member 7e5e9b85b854c4f6 is healthy: got healthy result from http://10.39.25.23:2379
member c20ca32dd7fbc99f is healthy: got healthy result from http://10.39.25.24:2379
member de67b2618c148f0d is healthy: got healthy result from http://10.39.25.25:2379
cluster is healthy
```
## 测试:
在23上创建一个TianRandai的目录
```
[root@etcd-25-23 ~]# etcdctl mkdir TianRandai
```
在24与25上查看是是否存在
```
[root@etcd-25-24 ~]# etcdctl ls
/TianRandai
[root@etcd-25-25 ~]# etcdctl ls
/TianRandai
```
';