Etcd 服务搭建

ops/2025/3/19 14:23:11/

💢欢迎来到张胤尘的开源技术站
💥开源如江河,汇聚众志成。代码似星辰,照亮行征程。开源精神长,传承永不忘。携手共前行,未来更辉煌💥

文章目录

  • Etcd 服务搭建
    • 预编译的二进制文件安装
      • 下载 `etcd` 的压缩包
      • 解压文件
      • 将可执行文件移动到系统路径
      • 验证版本
      • 配置 `etcd` 服务
        • 创建配置文件
        • 创建 `systemd` 服务文件
        • 启动 `etcd` 服务
        • 检查服务状态
    • 源代码编译安装
      • 克隆 `etcd` 仓库
      • 编译代码
      • 将编译后的文件移动到系统路径
      • 验证版本
      • 配置 `etcd` 服务
        • 创建配置文件
        • 创建 `systemd` 服务文件
        • 启动 `etcd` 服务
        • 检查服务状态
    • 包管理器安装
      • `Ubuntu`
      • `CentOS`
      • `Fedora`
    • `docker` 容器化安装
      • 拉取官方镜像
      • 默认配置文件
      • 启动 `etcd` 容器
      • 验证
    • 认证配置
      • 添加用户并设置密码
      • 创建角色
      • 为角色授予权限
      • 为用户分配角色
      • 开启认证功能
      • 服务重启
      • 验证

Etcd 服务搭建

在 Linux 上安装 etcd 服务可以通过几种方式进行:预编译的二进制文件安装、源代码编译安装、使用包管理器安装、docker 容器化安装。

预编译的二进制文件安装

etcd__11">下载 etcd 的压缩包

访问 etcd 的 GitHub Releases 页面:etcd

选择适合您系统的版本(例如 v3.5.19 或其他版本),并使用以下命令下载:

wget https://github.com/etcd-io/etcd/releases/download/v3.5.19/etcd-v3.5.19-linux-amd64.tar.gz

或者直接使用 curl 命令下载:

curl -L https://github.com/etcd-io/etcd/releases/download/v3.5.19/etcd-v3.5.19-linux-amd64.tar.gz -o etcd-v3.5.19-linux-amd64.tar.gz

解压文件

解压下载的压缩包:

tar -xvf etcd-v3.5.19-linux-amd64.tar.gz
cd etcd-v3.5.19-linux-amd64

将可执行文件移动到系统路径

etcdetcdctl 移动到 /usr/local/bin 目录,使其全局可用:

sudo mv etcd etcdctl /usr/local/bin/

验证版本

检查 etcd 的版本信息:

$ etcd --version
etcd Version: 3.5.19
Git SHA: 815eaba
Go Version: go1.23.7
Go OS/Arch: linux/amd64
$ etcdctl version
etcdctl version: 3.5.19
API version: 3.5

etcd__60">配置 etcd 服务

安装完成后,需要配置 etcd 服务以确保其正常运行:

创建配置文件

创建 /etc/etcd.conf 文件并添加配置:

cat <<EOF | sudo tee /etc/etcd.conf
ETCD_NAME=$(hostname -s)
ETCD_DATA_DIR=/var/lib/etcd/
EOF
创建 systemd 服务文件

创建 /etc/systemd/system/etcd.service 文件:

cat <<EOF | sudo tee /etc/systemd/system/etcd.service
[Unit]
Description=Etcd Server
Documentation=https://github.com/coreos/etcd
After=network.target[Service]
User=root
Type=notify
EnvironmentFile=-/etc/etcd.conf
ExecStart=/usr/local/bin/etcd
Restart=on-failure
RestartSec=10s
LimitNOFILE=40000[Install]
WantedBy=multi-user.target
EOF
etcd__98">启动 etcd 服务
sudo systemctl daemon-reload
sudo systemctl enable etcd
sudo systemctl start etcd
检查服务状态
sudo systemctl status etcd

源代码编译安装

如果需要最新功能或定制化版本,可以从源代码编译 etcd

etcd__116">克隆 etcd 仓库

git clone -b v3.5.19 https://github.com/etcd-io/etcd.git
cd etcd

编译代码

运行构建脚本:

./build.sh

将编译后的文件移动到系统路径

sudo mv bin/etcd /usr/local/bin/
sudo mv bin/etcdctl /usr/local/bin/

验证版本

检查 etcd 的版本信息:

$ etcd --version
etcd Version: 3.5.19
Git SHA: 815eaba
Go Version: go1.23.7
Go OS/Arch: linux/amd64
$ etcdctl version
etcdctl version: 3.5.19
API version: 3.5

etcd__156">配置 etcd 服务

安装完成后,需要配置 etcd 服务以确保其正常运行:

创建配置文件

创建 /etc/etcd.conf 文件并添加配置:

cat <<EOF | sudo tee /etc/etcd.conf
ETCD_NAME=$(hostname -s)
ETCD_DATA_DIR=/var/lib/etcd/
EOF
创建 systemd 服务文件

创建 /etc/systemd/system/etcd.service 文件:

cat <<EOF | sudo tee /etc/systemd/system/etcd.service
[Unit]
Description=Etcd Server
Documentation=https://github.com/coreos/etcd
After=network.target[Service]
User=root
Type=notify
EnvironmentFile=-/etc/etcd.conf
ExecStart=/usr/local/bin/etcd
Restart=on-failure
RestartSec=10s
LimitNOFILE=40000[Install]
WantedBy=multi-user.target
EOF
etcd__194">启动 etcd 服务
sudo systemctl daemon-reload
sudo systemctl enable etcd
sudo systemctl start etcd
检查服务状态
sudo systemctl status etcd

包管理器安装

虽然大多数 Linux 发行版的包管理器中包含 etcd,但是这些版本可能较旧。如果需要最新版本,建议使用预编译的二进制文件。

如果是实际生成环境不推荐使用该安装方法。

Ubuntu

sudo apt-get update
sudo apt-get install etcd

CentOS

sudo yum install etcd

Fedora

sudo dnf install etcd

docker 容器化安装

Docker hub

拉取官方镜像

从 Docker Hub 拉取 etcd 的官方镜像。例如,拉取最新版本的 etcd

docker pull bitnami/etcd:latest

或者指定特定版本:

docker pull bitnami/etcd:3.5.19

拉取完成后,查看镜像信息:

$ sudo docker images
REPOSITORY     TAG       IMAGE ID       CREATED       SIZE
bitnami/etcd   latest    c8fb74306c9b   2 days ago    192MB

默认配置文件

默认配置文件如下所示(使用时需要根据实际的情况进行修改):

# This is the configuration file for the etcd server.# Human-readable name for this member.
name: 'default'# Path to the data directory.
data-dir: /bitnami/etcd/data# Path to the dedicated wal directory.
wal-dir: # Number of committed transactions to trigger a snapshot to disk.
snapshot-count: 10000# Time (in milliseconds) of a heartbeat interval.
heartbeat-interval: 100# Time (in milliseconds) for an election to timeout.
election-timeout: 1000# Raise alarms when backend size exceeds the given quota. 0 means use the
# default quota.
quota-backend-bytes: 0# List of comma separated URLs to listen on for peer traffic.
listen-peer-urls: http://localhost:2380# List of comma separated URLs to listen on for client traffic.
listen-client-urls: http://localhost:2379# Maximum number of snapshot files to retain (0 is unlimited).
max-snapshots: 5# Maximum number of wal files to retain (0 is unlimited).
max-wals: 5# Comma-separated white list of origins for CORS (cross-origin resource sharing).
cors:# List of this member's peer URLs to advertise to the rest of the cluster.
# The URLs needed to be a comma-separated list.
initial-advertise-peer-urls: http://localhost:2380# List of this member's client URLs to advertise to the public.
# The URLs needed to be a comma-separated list.
advertise-client-urls: http://localhost:2379# Discovery URL used to bootstrap the cluster.
discovery:# Valid values include 'exit', 'proxy'
discovery-fallback: 'proxy'# HTTP proxy to use for traffic to discovery service.
discovery-proxy:# DNS domain used to bootstrap initial cluster.
discovery-srv:# Initial cluster configuration for bootstrapping.
initial-cluster:# Initial cluster token for the etcd cluster during bootstrap.
initial-cluster-token: 'etcd-cluster'# Initial cluster state ('new' or 'existing').
initial-cluster-state: 'new'# Reject reconfiguration requests that would cause quorum loss.
strict-reconfig-check: false# Accept etcd V2 client requests
enable-v2: true# Enable runtime profiling data via HTTP server
enable-pprof: true# Valid values include 'on', 'readonly', 'off'
proxy: 'off'# Time (in milliseconds) an endpoint will be held in a failed state.
proxy-failure-wait: 5000# Time (in milliseconds) of the endpoints refresh interval.
proxy-refresh-interval: 30000# Time (in milliseconds) for a dial to timeout.
proxy-dial-timeout: 1000# Time (in milliseconds) for a write to timeout.
proxy-write-timeout: 5000# Time (in milliseconds) for a read to timeout.
proxy-read-timeout: 0client-transport-security:# Path to the client server TLS cert file.cert-file:# Path to the client server TLS key file.key-file:# Enable client cert authentication.client-cert-auth: false# Path to the client server TLS trusted CA cert file.trusted-ca-file:# Client TLS using generated certificatesauto-tls: falsepeer-transport-security:# Path to the peer server TLS cert file.cert-file:# Path to the peer server TLS key file.key-file:# Enable peer client cert authentication.client-cert-auth: false# Path to the peer server TLS trusted CA cert file.trusted-ca-file:# Peer TLS using generated certificates.auto-tls: false# Allowed CN for inter peer authentication.allowed-cn:# Allowed TLS hostname for inter peer authentication.allowed-hostname:# The validity period of the self-signed certificate, the unit is year.
self-signed-cert-validity: 1# Enable debug-level logging for etcd.
log-level: debuglogger: zap# Specify 'stdout' or 'stderr' to skip journald logging even when running under systemd.
log-outputs: [stderr]# Force to create a new one member cluster.
force-new-cluster: falseauto-compaction-mode: periodic
auto-compaction-retention: "1"

etcd__415">启动 etcd 容器

sudo docker run -d --name etcd \--restart=always \--user 1000:0 \-p 2379:2379 \-p 2380:2380 \-e ETCD_ROOT_PASSWORD="123456" \-v /configPath/etcd.yaml:/opt/bitnami/etcd/conf/etcd.yaml \-v /dataPath/data:/bitnami/etcd \bitnami/etcd:latest
  • -d:后台运行容器。
  • --name etcd:为容器指定名称 etcd
  • --restart=always:设置容器的重启策略为 always。这意味着无论容器因何种原因退出(正常退出或异常退出),都会自动重启该容器。
  • --user 1000:0:指定容器运行的用户和用户组。如果是生产环境,则需要严格按照实际的环境信息来配置运行的权限
  • -p 2379:2379:将容器的 etcd 客户端端口(2379)映射到宿主机的 2379 端口。
  • -p 2380:2380:将容器的 etcd 节点间通信端口(2380)映射到宿主机的 2380 端口。
  • -e ETCD_ROOT_PASSWORD="123456":初始化 root 用户的密码。(需要根据实际环境配置)
  • -v /configPath/etcd.yaml:/opt/bitnami/etcd/conf/etcd.yaml:自定义的 etcd 配置文件传递给容器,容器将使用该配置文件启动 etcd 服务。( configPath 需要根据实际环境配置)
  • -v /dataPath/data:/bitnami/etcd:自定义数据目录,etcd 容器中使用该数据目录启动服务。( dataPath 需要根据实际环境配置)
  • bitnami/etcd:latest:使用的 etcd 镜像名称和版本信息。

验证

检查 etcd 容器是否成功启动:

$ docker ps
CONTAINER ID   IMAGE                 COMMAND                   CREATED         STATUS         PORTS                                                                                      NAMES
956e2cb3227e   bitnami/etcd:latest   "/opt/bitnami/script…"   3 minutes ago   Up 3 minutes   0.0.0.0:2379->2379/tcp, [::]:2379->2379/tcp, 0.0.0.0:2380->2380/tcp, [::]:2380->2380/tcp   etcd

使用 etcdctl 工具验证 etcd 是否正常运行:

$ docker exec -it etcd etcdctl --user root:123456 --endpoints=:2379 put testK testV
OK
$ docker exec -it etcd etcdctl --user root:123456 --endpoints=:2379 get testK
testK
testV

认证配置

添加用户并设置密码

使用 etcdctl 工具添加用户并设置密码。例如,添加一个名为 root 的用户:

etcdctl user add root

系统会提示输入密码,输入两次相同的密码完成设置。

创建角色

首先,需要创建一个角色(如果尚未创建)。例如,创建一个名为 root 的角色:

etcdctl role add root

为角色授予权限

接下来,为角色授予权限。例如,授予 root 角色对所有键的读写权限:

etcdctl role grant-permission root readwrite --prefix /
  • --prefix / 表示对所有键(包括子路径)授予权限。
  • 如果只想授予对特定键的权限,可以指定键名,例如:
etcdctl role grant-permission root readwrite /specific-key

为用户分配角色

最后,将角色分配给用户。例如,为用户 root 分配 root 角色:

etcdctl user grant-role root root
  • 第一个 root 是用户名。
  • 第二个 root 是角色名。

开启认证功能

启用 etcd 认证功能:

$ etcdctl auth enable
Authentication Enabled

服务重启

配置文件修改后,需要重启 etcd 服务以使配置生效:

sudo systemctl restart etcd

验证

  • 验证:不输入用户名密码报错
$ etcdctl put testK testV
{**** "error":"rpc error: code = InvalidArgument desc = etcdserver: user name is empty"}
  • 验证:输入用户名密码,但是密码错误报错
$ etcdctl --user root:1234567 put testK testV
{**** "error":"rpc error: code = InvalidArgument desc = etcdserver: authentication failed, invalid user ID or password"}
  • 验证:输入正确的用户名密码,可以正常访问
$ etcdctl --user root:123456 put testK testV
OK
$ etcdctl --user root:123456 get testK
testK
testV

🌺🌺🌺撒花!

如果本文对你有帮助,就点关注或者留个👍
如果您有任何技术问题或者需要更多其他的内容,请随时向我提问。
在这里插入图片描述


http://www.ppmy.cn/ops/167042.html

相关文章

2025 年3月26日中国人民大学与加拿大女王大学金融硕士项目面试通知

【面试通知】中国人民大学与加拿大女王大学金融硕士 人大与加拿大女王大学金融硕士面试时间暂定于&#xff1a;2025 年3月26日 &#xff08;面试人将依次单独面试&#xff0c;请提前到达&#xff0c;面试将准时开始&#xff09; 人大与加拿大女王大学金融硕士面试地点&#x…

python二级每日十题(1)

\ 第一题,在Python中&#xff0c;变量名的命名规则&#xff1a;以字母或下划线开头&#xff0c;后面跟字母、下划线和数字&#xff1b;不能以数字开头.故选c项,博主正确 缩进&#xff1a;在逻辑行首的空白&#xff08;空格和制表符&#xff09;用来决定逻辑行的缩进层次&…

Python 生成数据(绘制简单的折线图)

数据可视化 指的是通过可视化表示来探索数据&#xff0c;它与数据挖掘 紧密相关&#xff0c;而数据挖掘指的是使用代码来探索数据集的规律和关联。数据集可以是用一行代码就能表 示的小型数字列表&#xff0c;也可以是数以吉字节的数据。 绘制简单的折线图 下面来使用matplotl…

Python基于Django和协同过滤算法实现电影推荐系统功能丰富版

说明&#xff1a;这是一个系统实战项目&#xff0c;如需项目代码可以直接到文章最后关注获取。 项目背景 随着互联网的普及和数字娱乐内容的爆发式增长&#xff0c;用户在海量的电影资源中找到自己感兴趣的影片变得愈发困难。为了提升用户体验并增加用户粘性&#xff0c;我们…

31、map deque list的实现原理【中高频】

文章目录 list&#xff1a;双向链表&#xff0c;适用于频繁在中间 插入和删除操作。在链表中&#xff0c;每个元素都有 指向前后元素的指针&#xff0c;所以 在任何位置进行插入和删除都非常高效。deque&#xff1a;双端队列&#xff0c;可以在头部和尾部 快速插入和删除&#…

平安养老险深圳分公司积极开展2025年“3·15”金融消费者权益保护教育宣传活动

为深刻把握金融工作的政治性、人民性&#xff0c;帮助社会公众增强维护自身合法权益的意识和能力&#xff0c;平安养老险深圳分公司在2025年3月7日至3月15日期间&#xff0c;以“保障金融权益&#xff0c;助力美好生活”为口号&#xff0c;聚焦“维护权益”主题&#xff0c;全面…

Vivo自带功能可以远程协助其他手机吗?vivo如何远程控制别的安卓手机

现在不少品牌的手机已经内置远程协助或远程控制功能&#xff0c;能够让一台手机远程操作另一台手机&#xff0c;对于上班族远距离快捷帮助家里长辈解决手机问题来说&#xff0c;非常便捷。 Vivo手机现在也有这项远程协助的内置功能。使用时&#xff0c;只要在控制端的vivo手机点…

Unity 获取Game窗口分辨率

方式1&#xff1a;调用api 直接调用&#xff1a;Handles.GetMainGameViewSize 方法2&#xff1a;反射 下载Unity源代码Github 查找GameView脚本:Editro/Mono/GameView/GameView.cs GameView脚本是内部类&#xff0c;继承PlayModeView,命名空间是UnityEditor PlayModeView继承…