Personal Blog

devops

etcd bin部署

使用bin方式部署etcd服务集群。

一、准备工作

  1. 主机资源准备

    确保有三台机器可用,每台机器的配置应满足 etcd 的运行需求。例如:

    主机名IP 地址
    node1192.168.174.130
    node2192.168.174.131
    node3192.168.174.132
  2. 设置免密登录

    在三台机器上分别执行以下命令,生成 SSH 密钥并分发到其他机器:

     ssh-keygen
     ssh-copy-id root@192.168.174.130
     ssh-copy-id root@192.168.174.131
     ssh-copy-id root@192.168.174.132
    
  3. 安装必要的软件包
     yum install vim curl tar -y
    
  4. 关闭并且禁用防火墙自动启动
     systemctl stop firewalld
     systemctl disable firewalld
    
  5. 关闭selinux
     setenforce 0
     sed 's/SELINUX=enforcing/SELINUX=permissive/' -i /etc/selinux/config
    

二、部署 etcd

  1. 下载 etcd 二进制文件

    在任意一台机器上下载 etcd 的二进制文件,并分发到其他机器。例如:

     ETCD_VER=v3.5.11
     DOWNLOAD_URL=https://github.com/etcd-io/etcd/releases/download
    
     mkdir -p /data/service/etcd/bin
     curl -L ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -o /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
     tar xzvf /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz -C /data/service/etcd/bin --strip-components=1
    

    将二进制文件分发到其他机器:

     ssh 192.168.174.131 "mkdir -p /data/service/etcd/bin"
     ssh 192.168.174.132 "mkdir -p /data/service/etcd/bin"
     scp /data/service/etcd/bin/etcd* 192.168.174.131:/data/service/etcd/bin
     scp /data/service/etcd/bin/etcd* 192.168.174.132:/data/service/etcd/bin
    
  2. 创建配置文件

    在每台机器上创建 etcd 的配置文件 etcd.conf。以下是每台机器的配置示例:

    etcd1 (192.168.174.130)

     cat > /data/service/etcd/conf/etcd.conf <<EOF
     name: "etcd1"
     data-dir: "/data/service/etcd/data"
     listen-peer-urls: "http://192.168.174.130:2380"
     #listen-client-urls: "http://192.168.174.130:2379,http://127.0.0.1:2379"
     listen-client-urls: "http://0.0.0.0:2379"
    
     initial-advertise-peer-urls: "http://192.168.174.130:2380"
     advertise-client-urls: "http://192.168.174.130:2379"
     initial-cluster: "etcd1=http://192.168.174.130:2380,etcd2=http://192.168.174.131:2380,etcd3=http://192.168.174.132:2380"
     initial-cluster-token: "etcd-cluster"
     initial-cluster-state: "new"
     EOF
    

    etcd2 (192.168.174.131)

     cat > /data/service/etcd/conf/etcd.conf <<EOF
     name: "etcd2"
     data-dir: "/data/service/etcd/data"
     listen-peer-urls: "http://192.168.174.131:2380"
     listen-client-urls: "http://0.0.0.0:2379"
    
     initial-advertise-peer-urls: "http://192.168.174.131:2380"
     advertise-client-urls: "http://192.168.174.131:2379"
     initial-cluster: "etcd1=http://192.168.174.130:2380,etcd2=http://192.168.174.131:2380,etcd3=http://192.168.174.132:2380"
     initial-cluster-token: "etcd-cluster"
     initial-cluster-state: "new"
    
     EOF
    

    etcd3 (192.168.174.132)

     cat > /data/service/etcd/conf/etcd.conf <<EOF
     name: "etcd3"
     data-dir: "/data/service/etcd/data"
     listen-peer-urls: "http://192.168.174.132:2380"
     listen-client-urls: "http://0.0.0.0:2379"
    
     initial-advertise-peer-urls: "http://192.168.174.132:2380"
     advertise-client-urls: "http://192.168.174.132:2379"
     initial-cluster: "etcd1=http://192.168.174.130:2380,etcd2=http://192.168.174.131:2380,etcd3=http://192.168.174.132:2380"
     initial-cluster-token: "etcd-cluster"
     initial-cluster-state: "new"
     EOF
    

    注意:将上述配置文件中的 IP 地址和节点名称替换为对应机器的信息。

  3. 启动 etcd 服务

    1. 手动启动(临时测试):

      在每台机器上使用以下命令启动 etcd:

       /data/service/etcd/bin/etcd --config-file=/data/service/etcd/conf/etcd.conf
      
    2. systemctl启动(推荐方式)

      1. 在每台机器上配置以下systemd的配置来启动 etcd:

         cat > /etc/systemd/system/etcd.service <<EOF
         [Unit]
         Description=etcd key-value store
         Documentation=https://github.com/etcd-io/etcd
         After=network.target
        
         [Service]
         Type=notify
         ExecStart=/data/service/etcd/bin/etcd --config-file=/data/service/etcd/conf/etcd.conf
         Restart=always
         LimitNOFILE=65536
         RestartSec=5s
         TimeoutStartSec=30s
        
         [Install]
         WantedBy=multi-user.target
         EOF
        
      2. 重新加载并启动etcd

         systemctl daemon-reload
         systemctl start etcd
         systemctl enable etcd
        

三、验证集群状态

在任意一台机器上运行以下命令,检查集群状态:

export ETCDCTL_API=3

# 重要:查看健康状态
/data/service/etcd/bin/etcdctl --endpoints=http://192.168.174.130:2379,http://192.168.174.131:2379,http://192.168.174.132:2379 endpoint health  -w table

+-----------------------------+--------+------------+-------+
|          ENDPOINT           | HEALTH |    TOOK    | ERROR |
+-----------------------------+--------+------------+-------+
| http://192.168.174.130:2379 |   true | 3.211471ms |       |
| http://192.168.174.131:2379 |   true | 4.608397ms |       |
| http://192.168.174.132:2379 |   true | 3.810893ms |       |
+-----------------------------+--------+------------+-------+

如果集群正常运行,应返回每个节点的状态信息。

四、其他常见操作

在任意一台机器上运行以下命令:

export ETCDCTL_API=3

# 设置key value 对
[root@localhost ~]# /data/service/etcd/bin/etcdctl --endpoints=http://192.168.174.130:2379,http://192.168.174.131:2379,http://192.168.174.132:2379 put testkey testval
OK

# 查看
[root@localhost ~]# /data/service/etcd/bin/etcdctl --endpoints=http://192.168.174.130:2379,http://192.168.174.131:2379,http://192.168.174.132:2379 get testkey
testkey
testval

# 删除key-value对
[root@localhost member]# /data/service/etcd/bin/etcdctl --endpoints=http://192.168.174.130:2379,http://192.168.174.131:2379,http://192.168.174.132:2379 del testkey
1
[root@localhost member]# /data/service/etcd/bin/etcdctl --endpoints=http://192.168.174.130:2379,http://192.168.174.131:2379,http://192.168.174.132:2379 get testkey

DEVOPS · ETCD · DEPLOYMENT
devops etcd deployment