欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

CentOS 7 ETCD集群部署

程序员文章站 2022-07-13 22:39:17
...

一、前言
Etcd 是 CoreOS 基于 Raft 开发的分布式 key-value 存储,可用于服务发现、共享配置以及一致性保障(如数据库选主、分布式锁等)

Etcd 集群配置分为三种:
静态发现
Etcd 动态发现
DNS 动态发现 通过DNS的SRV解析动态发现集群

二、环境准备

主机名 ip 系统版本
etcd0 192.168.1.85 Centos 7
etcd1 192.168.1.86 Centos 7
etcd2 192.168.1.87 Centos 7

1.1、在三台机器上均执行

[[email protected] ~]# yum install etcd -y
[[email protected] ~]# rpm -qa etcd
etcd-3.3.11-2.el7.centos.x86_64

1.2、创建Etcd所需目录,在三台机器上均执行

mkdir /data/k8s/etcd/{data,wal} -p
chown -R etcd.etcd /data/k8s/etcd

三、配置集群
3.1、etcd0 配置文件

ETCD_DATA_DIR="/data/k8s/etcd/data"
ETCD_WAL_DIR="/data/k8s/etcd/wal"
ETCD_LISTEN_PEER_URLS="http://192.168.1.85:2380"
ETCD_LISTEN_CLIENT_URLS="http://192.168.1.85:2379"
ETCD_MAX_SNAPSHOTS="5"
ETCD_MAX_WALS="5"
ETCD_NAME="etcd1"
ETCD_SNAPSHOT_COUNT="100000"
ETCD_HEARTBEAT_INTERVAL="100"
ETCD_ELECTION_TIMEOUT="1000"

ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.1.85:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.1.85:2379"

ETCD_INITIAL_CLUSTER="etcd1=http://192.168.1.85:2380,etcd2=http://192.168.1.86:2380,etcd3=http://192.168.1.87:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"

3.2、etcd1 配置文件

ETCD_DATA_DIR="/data/k8s/etcd/data"
ETCD_WAL_DIR="/data/k8s/etcd/wal"
ETCD_LISTEN_PEER_URLS="http://192.168.1.86:2380"
ETCD_LISTEN_CLIENT_URLS="http://192.168.1.86:2379"
ETCD_MAX_SNAPSHOTS="5"
ETCD_MAX_WALS="5"
ETCD_NAME="etcd2"
ETCD_SNAPSHOT_COUNT="100000"
ETCD_HEARTBEAT_INTERVAL="100"
ETCD_ELECTION_TIMEOUT="1000"

ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.1.86:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.1.86:2379"

ETCD_INITIAL_CLUSTER="etcd1=http://192.168.1.85:2380,etcd2=http://192.168.1.86:2380,etcd3=http://192.168.1.87:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"

3.3、etcd2 配置文件

ETCD_DATA_DIR="/data/k8s/etcd/data"
ETCD_WAL_DIR="/data/k8s/etcd/wal"
ETCD_LISTEN_PEER_URLS="http://192.168.1.87:2380"
ETCD_LISTEN_CLIENT_URLS="http://192.168.1.87:2379"
ETCD_MAX_SNAPSHOTS="5"
ETCD_MAX_WALS="5"
ETCD_NAME="etcd3"
ETCD_SNAPSHOT_COUNT="100000"
ETCD_HEARTBEAT_INTERVAL="100"
ETCD_ELECTION_TIMEOUT="1000"

ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.1.87:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.1.87:2379"

ETCD_INITIAL_CLUSTER="etcd1=http://192.168.1.85:2380,etcd2=http://192.168.1.86:2380,etcd3=http://192.168.1.87:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"

四、启动测试(三个节点都需要操作)

[[email protected] etcd]# systemctl start etcd
[[email protected] etcd]# systemctl status etcd
● etcd.service - Etcd Server
   Loaded: loaded (/usr/lib/systemd/system/etcd.service; disabled; vendor preset: disabled)
   Active: active (running) since Thu 2019-11-07 09:28:54 CST; 5s ago
 Main PID: 1546 (etcd)
    Tasks: 8
   Memory: 41.3M
   CGroup: /system.slice/etcd.service
           └─1546 /usr/bin/etcd --name=etcd1 --data-dir=/data/k8s/etcd/data --listen-client-urls=http://192.168.1.85:2379

Nov 07 09:28:54 etcd0.k8s.com etcd[1546]: 3b8b38de05e2c497 [term: 1] received a MsgVote message with higher term from 9c64fba479c5e94 [term: 2]
Nov 07 09:28:54 etcd0.k8s.com etcd[1546]: 3b8b38de05e2c497 became follower at term 2
Nov 07 09:28:54 etcd0.k8s.com etcd[1546]: 3b8b38de05e2c497 [logterm: 1, index: 3, vote: 0] cast MsgVote for 9c64fba479c5e94 [logterm: 1, index: 3] at term 2
Nov 07 09:28:54 etcd0.k8s.com etcd[1546]: raft.node: 3b8b38de05e2c497 elected leader 9c64fba479c5e94 at term 2
Nov 07 09:28:54 etcd0.k8s.com etcd[1546]: published {Name:etcd1 ClientURLs:[http://192.168.1.85:2379]} to cluster 19456f0bfd57284e
Nov 07 09:28:54 etcd0.k8s.com etcd[1546]: ready to serve client requests
Nov 07 09:28:54 etcd0.k8s.com etcd[1546]: serving insecure client requests on 192.168.1.85:2379, this is strongly discouraged!
Nov 07 09:28:54 etcd0.k8s.com systemd[1]: Started Etcd Server.
Nov 07 09:28:54 etcd0.k8s.com etcd[1546]: set the initial cluster version to 3.3
Nov 07 09:28:54 etcd0.k8s.com etcd[1546]: enabled capabilities for version 3.3

4.1、查看 /var/log/message 日志中,会有日下体现:

Nov  7 09:28:53 etcd1 etcd: added member 9c64fba479c5e94 [http://192.168.1.86:2380] to cluster 19456f0bfd57284e
Nov  7 09:28:53 etcd1 etcd: added member 3b8b38de05e2c497 [http://192.168.1.85:2380] to cluster 19456f0bfd57284e
Nov  7 09:28:53 etcd1 etcd: added member 76ea8679db7365b3 [http://192.168.1.87:2380] to cluster 19456f0bfd57284e

五、查看集群状态

[[email protected] etcd]# ETCDCTL_API=3 etcdctl --endpoints=http://192.168.1.85:2379,http://192.168.1.86:2379,http://192.168.1.87:2379 endpoint health
http://192.168.1.86:2379 is healthy: successfully committed proposal: took = 1.103545ms
http://192.168.1.87:2379 is healthy: successfully committed proposal: took = 2.122478ms
http://192.168.1.85:2379 is healthy: successfully committed proposal: took = 2.690215ms
[[email protected] etcd]# etcdctl --endpoints=http://192.168.1.85:2379,http://192.168.1.86:2379,http://192.168.1.87:2379 cluster-health
member 9c64fba479c5e94 is healthy: got healthy result from http://192.168.1.85:2379
member 3b8b38de05e2c497 is healthy: got healthy result from http://192.168.1.86:2379
member 76ea8679db7365b3 is healthy: got healthy result from http://192.168.1.87:2379
cluster is healthy
相关标签: k8s_docker