etcd安装及集群配置

1、安装

解压安装包,拷贝etcd、etcdctl到/usr/bin下
#此时可以直接etcd启动,但是不建议这么做,因为没有利用配置文件制定参数,则default.etcd会出现在当前目录下,不好管理且不利于参数管理及集群管理。

#以下两个配置文件可以在别的虚拟机以yum install etcd安装之后在相同的位置得到。
将配置文件etcd.conf拷贝到/etc/etcd下
将配置文件etcd.service拷贝到/usr/lib/systemd/system下

启动:systemctl start etcd
状态:systemctl status etcd
停止:systemctl stop etcd

2、集群--这里只讨论static方式
static静态集群:在确切知道节点情况的时候可采取这种方式,又分为两种方式:
命令行方式,如下:
etcd --name infra0 --initial-advertise-peer-urls http://192.168.75.137:2380 \
  --listen-peer-urls http://192.168.75.137:2380 \
  --listen-client-urls http://192.168.75.137:2379,http://127.0.0.1:2379 \
  --advertise-client-urls http://192.168.75.137:2379 \
  --initial-cluster-token etcd-cluster-1 \
  --initial-cluster infra0=http://192.168.75.137:2380,infra1=http://192.168.75.138:2380,infra2=http://192.168.75.139:2380 \
  --initial-cluster-state new

================================

etcd --name infra1 --initial-advertise-peer-urls http://192.168.75.138:2380 \
  --listen-peer-urls http://192.168.75.138:2380 \
  --listen-client-urls http://192.168.75.138:2379,http://127.0.0.1:2379 \
  --advertise-client-urls http://192.168.75.138:2379 \
  --initial-cluster-token etcd-cluster-1 \
  --initial-cluster infra0=http://192.168.75.137:2380,infra1=http://192.168.75.138:2380,infra2=http://192.168.75.139:2380 \
  --initial-cluster-state new

================================

etcd --name infra2 --initial-advertise-peer-urls http://192.168.75.139:2380 \
  --listen-peer-urls http://192.168.75.139:2380 \
  --listen-client-urls http://192.168.75.139:2379,http://127.0.0.1:2379 \
  --advertise-client-urls http://192.168.75.139:2379 \
  --initial-cluster-token etcd-cluster-1 \
  --initial-cluster infra0=http://192.168.75.137:2380,infra1=http://192.168.75.138:2380,infra2=http://192.168.75.139:2380 \
  --initial-cluster-state new


配置文件方式,如下:
vim /opt/etcd/etcd-v3.1.6-linux-amd64/etcd.conf

ETCD_NAME=etcd00
ETCD_DATA_DIR="/opt/etcd/etcd-v3.1.6-linux-amd64/etcd00"
ETCD_LISTEN_PEER_URLS="http://192.168.75.137:2380"
ETCD_LISTEN_CLIENT_URLS="http://192.168.75.137:2379,http://127.0.0.1:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.75.137:2380"
ETCD_INITIAL_CLUSTER="etcd00=http://192.168.75.137:2380,etcd01=http://192.168.75.138:2380,etcd02=http://192.168.75.139:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster-00"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.75.137:2379"

vim /usr/lib/systemd/system/etcd.service
[Unit]
Description=EtcdServer
After=network.target
After=network-online.target
Wants=network-online.target
 
[Service]
Type=notify
WorkingDirectory=/var/lib/etcd/
EnvironmentFile=-/etc/etcd/etcd.conf
User=etcd
# setGOMAXPROCS to number of processors
ExecStart=/bin/bash-c "GOMAXPROCS=$(nproc) /usr/bin/etcd --name=\"${ETCD_NAME}\"\
  --data-dir=\"${ETCD_DATA_DIR}\" \
  --initial-advertise-peer-urls=\"${ETCD_INITIAL_ADVERTISE_PEER_URLS}\" \
  --listen-peer-urls=\"${ETCD_LISTEN_PEER_URLS}\" \
  --listen-client-urls=\"${ETCD_LISTEN_CLIENT_URLS}\"\
  --advertise-client-urls=\"${ETCD_ADVERTISE_CLIENT_URLS}\"\
  --initial-cluster-token=\"${ETCD_INITIAL_CLUSTER_TOKEN}\"\
  --initial-cluster=\"${ETCD_INITIAL_CLUSTER}\" \
  --initial-cluster-state=\"${ETCD_INITIAL_CLUSTER_STATE}\""

Restart=on-failure
LimitNOFILE=65536
 
[Install]
WantedBy=multi-user.target

================================================

ETCD_NAME=etcd01
ETCD_DATA_DIR="/var/lib/etcd/etcd00"
ETCD_LISTEN_PEER_URLS="http://192.168.75.138:2380"
ETCD_LISTEN_CLIENT_URLS="http://192.168.75.138:2379,http://127.0.0.1:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.75.138:2380"
ETCD_INITIAL_CLUSTER="etcd00=http://192.168.75.137:2380,etcd01=http://192.168.75.138:2380,etcd02=http://192.168.75.139:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster-00"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.75.138:2379"

vim /usr/lib/systemd/system/etcd.service
[Unit]
Description=EtcdServer
After=network.target
After=network-online.target
Wants=network-online.target
 
[Service]
Type=notify
WorkingDirectory=/var/lib/etcd/
EnvironmentFile=-/etc/etcd/etcd.conf
User=etcd
# setGOMAXPROCS to number of processors
ExecStart=/bin/bash-c "GOMAXPROCS=$(nproc) /usr/bin/etcd --name=\"${ETCD_NAME}\"\
  --data-dir=\"${ETCD_DATA_DIR}\" \
  --initial-advertise-peer-urls=\"${ETCD_INITIAL_ADVERTISE_PEER_URLS}\" \
  --listen-peer-urls=\"${ETCD_LISTEN_PEER_URLS}\" \
  --listen-client-urls=\"${ETCD_LISTEN_CLIENT_URLS}\"\
  --advertise-client-urls=\"${ETCD_ADVERTISE_CLIENT_URLS}\"\
  --initial-cluster-token=\"${ETCD_INITIAL_CLUSTER_TOKEN}\"\
  --initial-cluster=\"${ETCD_INITIAL_CLUSTER}\" \
  --initial-cluster-state=\"${ETCD_INITIAL_CLUSTER_STATE}\""
Restart=on-failure
LimitNOFILE=65536
 
[Install]
WantedBy=multi-user.target

============================================

ETCD_NAME=etcd02
ETCD_DATA_DIR="/var/lib/etcd/etcd00"
ETCD_LISTEN_PEER_URLS="http://192.168.75.139:2380"
ETCD_LISTEN_CLIENT_URLS="http://192.168.75.139:2379,http://127.0.0.1:2379"
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.75.139:2380"
ETCD_INITIAL_CLUSTER="etcd00=http://192.168.75.137:2380,etcd01=http://192.168.75.138:2380,etcd02=http://192.168.75.139:2380"
ETCD_INITIAL_CLUSTER_STATE="new"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster-00"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.75.139:2379"

vim /usr/lib/systemd/system/etcd.service
[Unit]
Description=EtcdServer
After=network.target
After=network-online.target
Wants=network-online.target
 
[Service]
Type=notify
WorkingDirectory=/var/lib/etcd/
EnvironmentFile=-/etc/etcd/etcd.conf
User=etcd
# setGOMAXPROCS to number of processors
ExecStart=/bin/bash-c "GOMAXPROCS=$(nproc) /usr/bin/etcd --name=\"${ETCD_NAME}\"\
  --data-dir=\"${ETCD_DATA_DIR}\" \
  --initial-advertise-peer-urls=\"${ETCD_INITIAL_ADVERTISE_PEER_URLS}\" \
  --listen-peer-urls=\"${ETCD_LISTEN_PEER_URLS}\" \
  --listen-client-urls=\"${ETCD_LISTEN_CLIENT_URLS}\"\
  --advertise-client-urls=\"${ETCD_ADVERTISE_CLIENT_URLS}\"\
  --initial-cluster-token=\"${ETCD_INITIAL_CLUSTER_TOKEN}\"\
  --initial-cluster=\"${ETCD_INITIAL_CLUSTER}\" \
  --initial-cluster-state=\"${ETCD_INITIAL_CLUSTER_STATE}\""
Restart=on-failure
LimitNOFILE=65536
 
[Install]
WantedBy=multi-user.target

以上是常规安装方式,我采取的是解压之后自己指定路径的方式,而并没有将etcd和etcdctl拷贝到bin目录下面,也没有采取默认的相关路径。这样有利于配置文件和数据文件的维护。



















etcd 是一个高可用的分布式 key-value 存储系统,可以用于分布式锁、服务发现、共享配置等场景。在使用 etcd 时,为了保证其高可用性,通常需要使用多个节点组成 etcd 集群。以下是配置 etcd 3 节点集群的步骤: 1. 安装 etcd 在每个节点上安装 etcd。具体步骤可以参考 etcd 的官方文档。 2. 修改配置文件 在每个节点上修改 etcd配置文件,一般位于 /etc/etcd/etcd.conf: ``` # Node 1 name: "node1" data-dir: "/var/lib/etcd" listen-peer-urls: "https://192.168.0.1:2380" listen-client-urls: "https://192.168.0.1:2379" initial-advertise-peer-urls: "https://192.168.0.1:2380" advertise-client-urls: "https://192.168.0.1:2379" initial-cluster: "node1=https://192.168.0.1:2380,node2=https://192.168.0.2:2380,node3=https://192.168.0.3:2380" initial-cluster-token: "my-etcd-cluster" initial-cluster-state: "new" client-transport-security: cert-file: "/etc/ssl/etcd/etcd.pem" key-file: "/etc/ssl/etcd/etcd-key.pem" trusted-ca-file: "/etc/ssl/etcd/ca.pem" peer-transport-security: cert-file: "/etc/ssl/etcd/etcd.pem" key-file: "/etc/ssl/etcd/etcd-key.pem" trusted-ca-file: "/etc/ssl/etcd/ca.pem" # Node 2 name: "node2" data-dir: "/var/lib/etcd" listen-peer-urls: "https://192.168.0.2:2380" listen-client-urls: "https://192.168.0.2:2379" initial-advertise-peer-urls: "https://192.168.0.2:2380" advertise-client-urls: "https://192.168.0.2:2379" initial-cluster: "node1=https://192.168.0.1:2380,node2=https://192.168.0.2:2380,node3=https://192.168.0.3:2380" initial-cluster-token: "my-etcd-cluster" initial-cluster-state: "new" client-transport-security: cert-file: "/etc/ssl/etcd/etcd.pem" key-file: "/etc/ssl/etcd/etcd-key.pem" trusted-ca-file: "/etc/ssl/etcd/ca.pem" peer-transport-security: cert-file: "/etc/ssl/etcd/etcd.pem" key-file: "/etc/ssl/etcd/etcd-key.pem" trusted-ca-file: "/etc/ssl/etcd/ca.pem" # Node 3 name: "node3" data-dir: "/var/lib/etcd" listen-peer-urls: "https://192.168.0.3:2380" listen-client-urls: "https://192.168.0.3:2379" initial-advertise-peer-urls: "https://192.168.0.3:2380" advertise-client-urls: "https://192.168.0.3:2379" initial-cluster: "node1=https://192.168.0.1:2380,node2=https://192.168.0.2:2380,node3=https://192.168.0.3:2380" initial-cluster-token: "my-etcd-cluster" initial-cluster-state: "new" client-transport-security: cert-file: "/etc/ssl/etcd/etcd.pem" key-file: "/etc/ssl/etcd/etcd-key.pem" trusted-ca-file: "/etc/ssl/etcd/ca.pem" peer-transport-security: cert-file: "/etc/ssl/etcd/etcd.pem" key-file: "/etc/ssl/etcd/etcd-key.pem" trusted-ca-file: "/etc/ssl/etcd/ca.pem" ``` 每个节点的配置文件需要修改以下参数: - name:节点名称,需要在集群中唯一。 - data-dir:数据存储目录。 - listen-peer-urls:节点间通信的地址。需要指定 https 协议和端口号。 - listen-client-urls:客户端访问 etcd 的地址。需要指定 https 协议和端口号。 - initial-advertise-peer-urls:节点向其他节点宣告自己的地址。需要指定 https 协议和端口号。 - advertise-client-urls:节点向客户端宣告自己的地址。需要指定 https 协议和端口号。 - initial-cluster:集群中所有节点的信息。每个节点信息格式为 name=https://ip:port。需要注意,节点名称一定要与配置文件中的 name 参数一致。 - initial-cluster-token:集群令牌,需要在所有节点中保持一致。 - initial-cluster-state:集群状态。第一次启动时为 new,之后为 existing。 - client-transport-security:客户端访问 etcd 的安全配置- peer-transport-security:节点间通信的安全配置。 3. 启动 etcd 在每个节点上启动 etcd 服务: ``` $ systemctl start etcd ``` 4. 验证集群状态 在任意一个节点上执行以下命令,可以查看集群状态: ``` $ etcdctl --endpoints=https://192.168.0.1:2379,https://192.168.0.2:2379,https://192.168.0.3:2379 --ca-file=/etc/ssl/etcd/ca.pem --cert-file=/etc/ssl/etcd/etcd.pem --key-file=/etc/ssl/etcd/etcd-key.pem cluster-health ``` 输出结果为: ``` member 9c61b7b4b666f72 is healthy: got healthy result from https://192.168.0.1:2379 member d21cfc0987b4d8f is healthy: got healthy result from https://192.168.0.2:2379 member f4d7a7f4d2a5cee is healthy: got healthy result from https://192.168.0.3:2379 cluster is healthy ``` 表示集群状态正常。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值