Etcd 服务搭建

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

文章目录

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

Etcd 服务搭建

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

预编译的二进制文件安装

下载 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 服务

安装完成后,需要配置 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 服务
sudo systemctl daemon-reload
sudo systemctl enable etcd
sudo systemctl start etcd
检查服务状态
sudo systemctl status etcd

源代码编译安装

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

克隆 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 服务

安装完成后,需要配置 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 服务
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 容器

sudo docker run -d --name etcd \--restart=always \--user $(id -u):$(id -g) \-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 $(id -u):$(id -g):指定容器运行的用户和用户组。如果是生产环境,则需要严格按照实际的环境信息来配置运行的权限
  • -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.mzph.cn/pingmian/73085.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

玩转C#函数:参数、返回值与游戏中的攻击逻辑封装

Langchain系列文章目录 01-玩转LangChain&#xff1a;从模型调用到Prompt模板与输出解析的完整指南 02-玩转 LangChain Memory 模块&#xff1a;四种记忆类型详解及应用场景全覆盖 03-全面掌握 LangChain&#xff1a;从核心链条构建到动态任务分配的实战指南 04-玩转 LangChai…

WebRTC建立Description的通信的实际的原理

一、正确流程的核心逻辑 // 发送端正确代码示例 const senderPC new RTCPeerConnection();// 生成Offer时立即开始收集候选 ✅ senderPC.createOffer().then(offer > {await senderPC.setLocalDescription(offer); // 触发icecandidate事件sendToReceiver(offer); });// …

EmbodiedSAM:在线实时3D实例分割,利用视觉基础模型实现高效场景理解

2025-02-12&#xff0c;由清华大学和南洋理工大学的研究团队开发 一种名为 EmbodiedSAM&#xff08;ESAM&#xff09;的在线3D实例分割框架。该框架利用2D视觉基础模型辅助实时3D场景理解&#xff0c;解决了高质量3D数据稀缺的难题&#xff0c;为机器人导航、操作等任务提供了高…

信创-人大金仓数据库创建

一. 官文 资源下载地址 https://download.kingbase.com.cn/xzzx/index.htm 下载安装文件 下载授权文件 产品文档地址&#xff1a;https://help.kingbase.com.cn/v8/index.html 二. 概念 2.1 体系结构 ‌ 实例结构 ‌&#xff1a;由数据库文件和 KingbaseES 实例组成。数据…

C++第三种异质集合 std::any方式实现

#include <type_traits> #include <any> #include <functional> #include <iomanip> #include <iostream> #include <typeindex> #include <typeinfo> #include <unordered_map> #include <vector> //any是编译期的异质…

Springboot实现使用断点续传优化同步导入Excel

springboot实现使用断点续传优化同步导入Excel 需求前言断点续传前端实现后端实现完结撒花&#xff0c;如有需要收藏的看官&#xff0c;顺便也用发财的小手点点赞哈&#xff0c;如有错漏&#xff0c;也欢迎各位在评论区评论&#xff01; 需求前言 在跨境电商系统中&#xff0c…

mysql 对json的处理?

MySQL从5.7版本开始支持JSON数据类型&#xff0c;并提供了多种函数来查询和处理JSON数据。以下是一些基本的操作和函数&#xff1a; 创建包含JSON列的表&#xff1a; 可以直接在表定义中指定某列为JSON类型。 CREATE TABLE my_table (id INT NOT NULL AUTO_INCREMENT,data JSON…

Nexus L2 L3基本配置

接口基本配置 N7K上所有端口默认处于shutdown状态; N5K上所有端口默认处于no shutdown状态(所有端口都是switchport) 默认所有接口都是三层route模式, 只有当线卡不支持三层的时候, 接口才会处于二层switchport模式 show run all | in “system default” 创建SVI口需要提前打…

HCIA-AI人工智能笔记3:数据预处理

统讲解数据预处理的核心技术体系&#xff0c;通过Python/Pandas与华为MindSpore双视角代码演示&#xff0c;结合特征工程优化实验&#xff0c;深入解析数据清洗、标准化、增强等关键环节。 一、数据预处理技术全景图 graph TD A[原始数据] --> B{数据清洗} B --> B1[缺…

G-Star 校园开发者计划·黑科大|开源第一课之 Git 入门

万事开源先修 Git。Git 是当下主流的分布式版本控制工具&#xff0c;在软件开发、文档管理等方面用处极大。它能自动记录文件改动&#xff0c;简化合并流程&#xff0c;还特别适合多人协作开发。学会 Git&#xff0c;就相当于掌握了一把通往开源世界的钥匙&#xff0c;以后参与…

MySQL错误 “duplicate entry ‘1‘ for key ‘PRIMARY‘“ 解决方案

文章目录 1. 错误原因分析2. 快速解决方法场景1:手动插入重复值场景2:自增主键冲突场景3:批量插入冲突3. 长期预防策略4. 高级排查技巧该错误通常由主键冲突引起,表示尝试插入或更新的主键值已存在于表中。以下是分步排查和解决方法: 1. 错误原因分析 主键唯一性约束:表…

WEB攻防-PHP反序列化-字符串逃逸

目录 前置知识 字符串逃逸-减少 字符串逃逸-增多 前置知识 1.PHP 在反序列化时&#xff0c;语法是以 ; 作为字段的分隔&#xff0c;以 } 作为结尾&#xff0c;在结束符}之后的任何内容不会影响反序列化的后的结果 class people{ public $namelili; public $age20; } var_du…

把生产队的大模型Grok 3 beta用来实现字帖打磨

第一个版本&#xff0c;就是简单的田字格&#xff0c;Grok 3 beta 思考了15s就得到了html前端代码&#xff0c;javascript; 然而还不完美&#xff1b; 第二个版本&#xff0c;进一步&#xff0c;通过pinyin项目给汉字加上注音&#xff0c;米字格和四线格&#xff1b;&#xff…

windows+ragflow+deepseek实战之一excel表查询

ragflows平台部署参考文章 Win10系统Docker+DeepSeek+ragflow搭建本地知识库 ragflow通过python实现参考这篇文章 ragflow通过python实现 文章目录 背景效果1、准备数据2、创建知识库3、上传数据并解析4、新建聊天助理5、测试会话背景 前面已经基于Win10系统Docker+DeepSeek+…

OpenCV图像处理基础2

接着上一篇OpenCV图像处理基础1继续说。 图像阈值处理 1、简单阈值处理 ret, thresholded_image = cv2.threshold(image, thresh, maxval, cv2.THRESH_BINARY)thresh 是阈值,maxval 是最大值。 2、自适应阈值处理 thresholded_image = cv2.adaptiveThreshold(image, maxv…

go安装lazydocker

安装 先安装go环境 https://blog.csdn.net/Yqha1/article/details/146430281?fromshareblogdetail&sharetypeblogdetail&sharerId146430281&sharereferPC&sharesourceYqha1&sharefromfrom_link 安装lazydocker go install github.com/jesseduffield/laz…

【架构】单体架构 vs 微服务架构:如何选择最适合你的技术方案?

文章目录 ⭐前言⭐一、架构设计的本质差异&#x1f31f;1、代码与数据结构的对比&#x1f31f;2、技术栈的灵活性 ⭐二、开发与维护的成本博弈&#x1f31f;1、开发效率的阶段性差异&#x1f31f;2、维护成本的隐形陷阱 ⭐三、部署与扩展的实战策略&#x1f31f;1、部署模式的本…

C#实现分段三次Hermite插值

目录 一、Hermite插值介绍 1、功能说明 2、数学方法 二、代码实现 1、CubicHermiteInterpolator类封装 2、应用示例 三、导数值的获取方式 1、数学方法介绍 2、代码应用示例 四、其它封装的分段三次Hermite插值类 1、方式一 &#xff08;1&#xff09;封装代码 &…

重要重要!!fisher矩阵元素有什么含义和原理; Fisher 信息矩阵的形式; 得到fisher矩阵之后怎么使用

fisher矩阵元素有什么含义和原理 目录 fisher矩阵元素有什么含义和原理一、对角线元素( F i , i F_{i,i} Fi,i​)的含义与原理二、非对角线元素( F i , j F_{i,j} Fi,j​)的含义与原理Fisher 信息矩阵的形式矩阵的宽度有位置权重数量决定1. **模型参数结构决定矩阵维度**2.…

【STM32】uwTick在程序中的作用及用法,并与Delay函数的区别

一、uwTick 的作用 1.系统时间基准 uwTick 是一个全局变量&#xff08;volatile uint32_t&#xff09;&#xff0c;记录系统启动后的毫秒级时间累计值。默认情况下&#xff0c;它由 SysTick 定时器每 ​1ms 自动递增一次&#xff08;通过 HAL_IncTick() 函数。例如&#xff0…