etcd集群的搭建

Posted Devops、小铁匠

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了etcd集群的搭建相关的知识,希望对你有一定的参考价值。

由于最近在学习kubernetes,etcd作为kubernetes集群的主数据库,必须先启动。

etcds实例名称 IP地址 Hostname
etcd 1 192.168.142.161 kubernetes-node1.example.com
etcd 2 192.168.142.162 kubernetes-node2.example.com
etcd 3 192.168.142.163 kubernetes-node3.example.com
从GitHub官网上下载etcd发布的二进制文件,etcd和etcdctl文件复制到/usr/bin目录。
  • 设置systemd服务文件。
vim /usr/lib/systemd/system/etcd.service
内容如下:
[Unit]
Description=Etcd Server
After=network.target


[Service]
Type=simple
WorkingDirectory=/var/lib/etcd/   
EnvironmentFile=-/etc/etcd/etcd.conf #配置文件路径
ExecStart=/usr/bin/etcd
Type=notify
[Install]
WantedBy=multi-user.target
  • 编写/etc/etcd/etcd.conf文件
vim /etc/etcd/etcd.conf
# [member]
ETCD_NAME=etcd1   #etcd实例名称
ETCD_DATA_DIR="/var/lib/etcd"  #etcd数据保存目录
ETCD_LISTEN_CLIENT_URLS="http://192.168.142.161:2379,http://127.0.0.1:2379"
#供外部客户端使用的url
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.142.161:2379,http://127.0.0.1:2379"
#广播给外部客户端使用的url

#[cluster]
ETCD_LISTEN_PEER_URLS="http://192.168.142.161:2380"   #集群内部通信使用的URL
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.142.161:2380"
#广播给集群内其他成员访问的URL
ETCD_INITIAL_CLUSTER="etcd1=http://192.168.142.161:2380,etcd2=http://192.168.142.162:2380,etcd3=http://192.168.142.163:2380"
#初始集群成员列表
ETCD_INITIAL_CLUSTER_STATE="new"
#初始集群状态,new为新建集群
ETCD_INITIAL_CLUSTER_TOKEN="k8s-etcd-cluster"  #集群的名称
  • 启动etcd1服务器上的etcd服务。

    systemctl restart etcd
  • etcd2和etcd3为加入k8s-etcd-cluster集群的实例,需要将其ETCD_INITIAL_CLUSTER_STATE设置为"exist"。etcd2的完整配置如下:

# [member]
ETCD_NAME=etcd2
ETCD_DATA_DIR="/var/lib/etcd"
ETCD_LISTEN_CLIENT_URLS="http://192.168.142.162:2379,http://127.0.0.1:2379"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.142.162:2379,http://127.0.0.1:2379"

#[cluster]
ETCD_LISTEN_PEER_URLS="http://192.168.142.162:2380"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.142.162:2380"
ETCD_INITIAL_CLUSTER="etcd1=http://192.168.142.161:2380,etcd2=http://192.168.142.162:2380,etcd3=http://192.168.142.163:2380"
ETCD_INITIAL_CLUSTER_STATE="exist"
ETCD_INITIAL_CLUSTER_TOKEN="k8s-etcd-cluster"
  • 启动etcd2和etcd3服务器上的etcd服务。

    systemctl restart etcd

    启动完成后,在任意节点执行etcdctl cluster-healthy命令来查看运行状态。

    使用etcdctl member list 来查看集群中的成员列表。

另外一种安装方法
etcd集群搭建
下载二进制软件包
https://github.com/coreos/etcd/releases   

解压

将两个执行文件复制到/usr/bin/
etcd和etcdctl文件复制到/usr/bin目录

配置etcd-1

vi /etc/etcd/etcd.yml


name: etcd-1
data-dir: /var/lib/etcd
listen-client-urls: http://163.44.167.92:2379,http://127.0.0.1:2379
advertise-client-urls: http://163.44.167.92:2379,http://127.0.0.1:2379
listen-peer-urls: http://163.44.167.92:2380
initial-advertise-peer-urls: http://163.44.167.92:2380
initial-cluster: etcd-1=http://163.44.167.92:2380,etcd-2=http://150.95.148.52:2380,etcd-3=http://133.130.122.48:2380
initial-cluster-token: etcd-cluster-token
initial-cluster-state: new

配置etcd-2

vi /etc/etcd/etcd.yml
name: etcd-2
data-dir: /var/lib/etcd
listen-client-urls: http://150.95.148.52:2379,http://127.0.0.1:2379
advertise-client-urls: http://150.95.148.52:2379,http://127.0.0.1:2379
listen-peer-urls: http://150.95.148.52:2380
initial-advertise-peer-urls: http://150.95.148.52:2380
initial-cluster: etcd-1=http://163.44.167.92:2380,etcd-2=http://150.95.148.52:2380,etcd-3=http://133.130.122.48:2380
initial-cluster-token: etcd-cluster-token
initial-cluster-state: new


配置etcd-3
name: etcd-3
data-dir: /var/lib/etcd
listen-client-urls: http://133.130.122.48:2379,http://127.0.0.1:2379
advertise-client-urls: http://133.130.122.48:2379,http://127.0.0.1:2379
listen-peer-urls: http://133.130.122.48:2380
initial-advertise-peer-urls: http://133.130.122.48:2380
initial-cluster: etcd-1=http://163.44.167.92:2380,etcd-2=http://150.95.148.52:2380,etcd-3=http://133.130.122.48:2380
initial-cluster-token: etcd-cluster-token
initial-cluster-state: new



最后启动
etcd --config-file=/etc/etcd/etcd.yml &

查看成员
etcdctl member list
每个节点上执行查看健康状态
etcdctl cluster-health

以上是关于etcd集群的搭建的主要内容,如果未能解决你的问题,请参考以下文章

etcd集群搭建(高可用)

搭建etcd集群,python调etcd接口

etcd集群搭建

架构师面试题库1—etcd高可用集群搭建

etcd集群的搭建

Etcd 集群搭建