centos7,etcd集群部署,基于二进制包,集群通信采用http协议

etcd官网地址: https://etcd.io/
安装包路径:https://github.com/etcd-io/etcd/releases/
官网集群部署文档: https://etcd.io/docs/v3.5/op-guide/clustering/

安装前大致阅读一下,支持的平台和硬件建议:
https://etcd.io/docs/v3.5/op-guide/supported-platform/
https://etcd.io/docs/v3.5/op-guide/hardware/

理论知识参考一下: https://blog.csdn.net/MssGuo/article/details/134495748

本文主要是为了快速搭建基于http通信的etcd集群

主机信息

etcd-01192.168.44.186
etcd-02192.168.44.187
etcd-03192.168.44.188

环境配置:

  1. 设置主机名

     # 分别在每台执行
     hostnamectl set-hostname etcd-01
     hostnamectl set-hostname etcd-02
     hostnamectl set-hostname etcd-03
    
  2. 配置本地解析

    # 每台都执行
    cat >> /etc/hosts << EOF
    192.168.44.186  etcd-01
    192.168.44.187  etcd-02
    192.168.44.188  etcd-03
    EOF
    
  3. 关闭防火墙,selinux

     # 每台都执行
     systemctl disable firewalld --now
     setenforce 0
     sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
    
  4. 配置时间同步

     # 每台都执行
     yum install chrony -y && systemctl enable --now chronyd
    

集群部署

  1. 下载etcd二进制包

     # 每台都执行
     cd /opt
     wget https://github.com/etcd-io/etcd/releases/download/v3.5.13/etcd-v3.5.13-linux-amd64.tar.gz
     tar -zxvf etcd-v3.5.13-linux-amd64.tar.gz
     cp etcd-v3.5.13-linux-amd64/etcd* /usr/local/bin/
    
  2. 编写etcd.service配置文件,加入到systemd系统进程

    # etcd-01
    cat>/usr/lib/systemd/system/etcd.service<<'EOF'
    [Unit]
    Description=Etcd Server
    After=network.target
    After=network-online.target
    Wants=network-online.target
    
    [Service]
    Type=notify
    ExecStart=/usr/local/bin/etcd \
      --name=etcd-01 \
      --data-dir=/var/lib/etcd \
      --listen-client-urls=http://192.168.44.186:2379 \
      --listen-peer-urls=http://192.168.44.186:2380 \
      --advertise-client-urls=http://192.168.44.186:2379 \
      --initial-advertise-peer-urls=http://192.168.44.186:2380 \
      --initial-cluster=etcd-01=http://192.168.44.186:2380,etcd-02=http://192.168.44.187:2380,etcd-03=http://192.168.44.188:2380 \
      --initial-cluster-token=etcd-cluster \
      --initial-cluster-state=new \
      --logger=zap
    Restart=on-failure
    LimitNOFILE=65536
    
    [Install]
    WantedBy=multi-user.target
    EOF
    
    # etcd-02
    cat>/usr/lib/systemd/system/etcd.service<<'EOF'
    [Unit]
    Description=Etcd Server
    After=network.target
    After=network-online.target
    Wants=network-online.target
    
    [Service]
    Type=notify
    ExecStart=/usr/local/bin/etcd \
      --name=etcd-02 \
      --data-dir=/var/lib/etcd \
      --listen-client-urls=http://192.168.44.187:2379 \
      --listen-peer-urls=http://192.168.44.187:2380 \
      --advertise-client-urls=http://192.168.44.187:2379 \
      --initial-advertise-peer-urls=http://192.168.44.187:2380 \
      --initial-cluster=etcd-01=http://192.168.44.186:2380,etcd-02=http://192.168.44.187:2380,etcd-03=http://192.168.44.188:2380 \
      --initial-cluster-token=etcd-cluster \
      --initial-cluster-state=new \
      --logger=zap
    Restart=on-failure
    LimitNOFILE=65536
    
    [Install]
    WantedBy=multi-user.target
    EOF
    
    # etcd-03
    cat>/usr/lib/systemd/system/etcd.service<<'EOF'
    [Unit]
    Description=Etcd Server
    After=network.target
    After=network-online.target
    Wants=network-online.target
    
    [Service]
    Type=notify
    ExecStart=/usr/local/bin/etcd \
      --name=etcd-03 \
      --data-dir=/var/lib/etcd \
      --listen-client-urls=http://192.168.44.188:2379 \
      --listen-peer-urls=http://192.168.44.188:2380 \
      --advertise-client-urls=http://192.168.44.188:2379 \
      --initial-advertise-peer-urls=http://192.168.44.188:2380 \
      --initial-cluster=etcd-01=http://192.168.44.186:2380,etcd-02=http://192.168.44.187:2380,etcd-03=http://192.168.44.188:2380 \
      --initial-cluster-token=etcd-cluster \
      --initial-cluster-state=new \
      --logger=zap
    Restart=on-failure
    LimitNOFILE=65536
    
    [Install]
    WantedBy=multi-user.target
    EOF
    

    启动时的参数含义
    官网文档参考:https://etcd.io/docs/v3.5/op-guide/configuration/

    --name	      etcd的节点名字,自定义名字,etcd节点的名字不能相同
    --data-dir    etcd的数据存储目录
    --listen-client-urls	用于当前节点与客户端交互的URL地址,每个节点同样可以向客户端提供多个URL地址,多个地址使用道号分隔节课,端口一般保持默认2379即可
    --listen-peer-urls	   用于集群内各个节点之间通信的URL地址,每个节点可以监听多个URL地址,集群内部将通过这些URL地址进行数据交互,例如,Leader节点的选举、Message消息传输或是快照传输等,端口一般保持默认2380即可
    --advertise-client-urls	建议使用的客户端通信url,该值用于etcd 代理或etcd成员与etcd节点通信,与listen-client-urls参数值保持一致即可
    --initial-advertise-peer-urls	建议用于集群内部节点之间交互的URL地址,节点间将以该值进行通信,与listen-peer-urls参数值保持一致即可
    --initial-cluster	集群中所有的initial-advertise-peer-urls的合集,etcd启动的时候,会通过这个配置找到其他etcd节点的列表
    --initial-cluster-token	 节点的token值,该值可自定义,设置该值后集群将生成唯一id,并为每个节点也生成唯一id,当使用相同配置文件再启动一个集群时,只要该token值不一样,etcd 集群就不会相互影响
    --initial-cluster-state	初始化时集群的状态,可取值:new和existing,new代表新建的集群,existing 代表加入已经存在的集群
    

启动etcd集群
因为已经将etcd加入到了systemd系统进程,所以需要重新加载一下配置,在启动。
需要注意的:最先启动的etcd,会成为集群的leader。

# 3台主机同时执行
systemctl daemon-reload
systemctl start etcd
systemctl enable etcd

查看etcd集群状态

[root@etcd-01 opt]# etcdctl --endpoints="http://etcd-01:2379,http://etcd-02:2379,http://etcd-03:2379" endpoint status  --write-out=table
+---------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
|      ENDPOINT       |        ID        | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
+---------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
| http://etcd-01:2379 | 498b4ab41d98fd16 |  3.5.13 |   25 kB |      true |      false |         2 |          8 |                  8 |        |
| http://etcd-02:2379 | fa7b0b5a155b1015 |  3.5.13 |   25 kB |     false |      false |         2 |          8 |                  8 |        |
| http://etcd-03:2379 | ad44b09644b5536d |  3.5.13 |   25 kB |     false |      false |         2 |          8 |                  8 |        |
+---------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
# 可以看出 etcd-01节点是leader

查看etcd集群健康状态

[root@etcd-01 opt]# etcdctl --endpoints="http://etcd-01:2379,http://etcd-02:2379,http://etcd-03:2379" endpoint health  --write-out=table
+---------------------+--------+-------------+-------+
|      ENDPOINT       | HEALTH |    TOOK     | ERROR |
+---------------------+--------+-------------+-------+
| http://etcd-02:2379 |   true |   25.3956ms |       |
| http://etcd-01:2379 |   true | 25.460568ms |       |
| http://etcd-03:2379 |   true |  25.43311ms |       |
+---------------------+--------+-------------+-------+

列出etcd集群成员节点

[root@etcd-01 opt]# etcdctl --endpoints="http://etcd-01:2379,http://etcd-02:2379,http://etcd-03:2379" member list  --write-out=table
+------------------+---------+---------+----------------------------+----------------------------+------------+
|        ID        | STATUS  |  NAME   |         PEER ADDRS         |        CLIENT ADDRS        | IS LEARNER |
+------------------+---------+---------+----------------------------+----------------------------+------------+
| 498b4ab41d98fd16 | started | etcd-01 | http://192.168.44.186:2380 | http://192.168.44.186:2379 |      false |
| ad44b09644b5536d | started | etcd-03 | http://192.168.44.188:2380 | http://192.168.44.188:2379 |      false |
| fa7b0b5a155b1015 | started | etcd-02 | http://192.168.44.187:2380 | http://192.168.44.187:2379 |      false |
+------------------+---------+---------+----------------------------+----------------------------+------------+
  • 23
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是在 CentOS 中部署 etcd 集群的步骤: 1. 下载 etcd 安装 可以从 etcd 官网下载最新版的 etcd 安装,下载地址为:https://github.com/etcd-io/etcd/releases 2. 解压安装 将下载的 etcd 安装解压到指定目录,比如 /usr/local/etcd ``` tar -zxvf etcd-v3.4.13-linux-amd64.tar.gz -C /usr/local/etcd --strip-components=1 ``` 3. 创建 etcd 配置文件 在每个节点上创建 etcd 配置文件,比如 /etc/etcd.conf,配置内容如下: ``` name=node1 # 节点名称,每个节点名称不同 data-dir=/var/lib/etcd # 数据存储目录 initial-cluster=node1=http://192.168.1.1:2380,node2=http://192.168.1.2:2380,node3=http://192.168.1.3:2380 # 集群中所有节点的地址和端口号 initial-advertise-peer-urls=http://192.168.1.1:2380 # 当前节点的地址和端口号 listen-peer-urls=http://192.168.1.1:2380 # 监听地址和端口号 listen-client-urls=http://192.168.1.1:2379,http://127.0.0.1:2379 # 监听客户端连接的地址和端口号,可以指定多个 advertise-client-urls=http://192.168.1.1:2379 # 当前节点对外提供服务的地址和端口号 initial-cluster-state=new # 集群状态,new 表示新建集群 ``` 其中,initial-cluster 中的节点名称和地址需要根据实际情况修改。 4. 启动 etcd 集群 在每个节点上启动 etcd 服务: ``` /usr/local/etcd/etcd --config-file /etc/etcd.conf ``` 5. 验证 etcd 集群 使用 etcdctl 工具验证 etcd 集群是否正常运行: ``` export ETCDCTL_API=3 etcdctl --endpoints=http://192.168.1.1:2379,http://192.168.1.2:2379,http://192.168.1.3:2379 member list ``` 如果返回了所有节点的信息,则说明 etcd 集群已经成功启动。 以上是在 CentOS 中部署 etcd 集群的步骤,希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值