Etcd 服务搭建

news2025/5/17 8:40:43

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

文章目录

  • 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: 0

client-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 certificates
  auto-tls: false

peer-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: debug

logger: 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: false

auto-compaction-mode: periodic
auto-compaction-retention: "1"

启动 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.coloradmin.cn/o/2317665.html

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

相关文章

【C++】stack和queue的使用及模拟实现(含deque的简单介绍)

文章目录 前言一、deque的简单介绍1.引入deque的初衷2.deque的结构3.为什么选择deque作为stack和queue的底层默认容器 二、stack1.stack的介绍2.stack的使用3.stack的模拟实现 三、queue1.queue的介绍2.queue的使用3.queue的模拟实现 前言 一、deque的简单介绍&#xff08;引入…

MySQL原理:逻辑架构

目的&#xff1a;了解 SQL执行流程 以及 MySQL 内部架构&#xff0c;每个零件具体负责做什么 理解整体架构分别有什么模块每个模块具体做什么 目录 1 服务器处理客户端请求 1.1 MySQL 服务器端逻辑架构说明 2 Connectors 3 第一层&#xff1a;连接层 3.1 数据库连接池(Conn…

ora-600 ktugct: corruption detected---惜分飞

接手一个oracle 21c的库恢复请求,通过Oracle数据库异常恢复检查脚本(Oracle Database Recovery Check)脚本检测之后,发现undo文件offline之后,做了resetlogs操作,导致该文件目前处于WRONG RESETLOGS状态 尝试恢复数据库ORA-16433错误 SQL> recover datafile 1; ORA-00283:…

Houdini :《哪吒2》神话与科技碰撞的创新之旅

《哪吒2》&#xff08;即《哪吒之魔童闹海》&#xff09;截止至今日&#xff0c;荣登全球票房榜第五。根据猫眼专业版数据&#xff0c;截至2025年3月15日&#xff0c;《哪吒2》全球累计票房&#xff08;含预售及海外&#xff09;超过150.19亿元&#xff0c;超越《星球大战&…

flink 写入es的依赖导入问题(踩坑记录)

flink 写入es的依赖导入问题(踩坑记录) ps&#xff1a;可能只是flink低版本才会有这个问题 1. 按照官网的导入方式&#xff1a; 2. 你会在运行sql-client的时候完美得到一个错误&#xff1a; Exception in thread "main" org.apache.flink.table.client.SqlClientEx…

PCL 高斯函数拟合(正太分布)

文章目录 一、简介二、实现代码三、实现效果一、简介 类似于之前最小二乘法的做法,我们需要先确定目标函数: 通过最小二乘法,找到使预测值与实际数据残差平方和最小的参数: 不过由于这是一个非线性最小二乘问题,因此这里无法使用矩阵的形式之间求解它的解析解了,因此这里…

深度革命:ResNet 如何用 “残差连接“ 颠覆深度学习

一文快速了解 ResNet创新点 在深度学习的历史长河中&#xff0c;2015年或许是最具突破性的一年。这一年&#xff0c;微软亚洲研究院的何恺明团队带着名为ResNet&#xff08;残差网络&#xff09;的模型横空出世&#xff0c;在ImageNet图像分类竞赛中以3.57%的错误率夺冠&#…

Java基础与集合

参考 Java基础知识详解&#xff1a;从面向对象到异常处理-CSDN博客 2024年 Java 面试八股文&#xff08;20w字&#xff09;_java面试八股文-CSDN博客 基础知识 java概述 什么是java&#xff1f; java是一种面向对象的编程语言 java特点 面向对象&#xff08;继承&#…

【Python 算法零基础 1.线性枚举】

我装作漠视一切&#xff0c;以为这样就可以不在乎 —— 25.3.17 一、线性枚举的基本概念 1.时间复杂度 线性枚举的时间复杂度为 O(nm)&#xff0c;其中 n是线性表的长度。m 是每次操作的量级&#xff0c;对于求最大值和求和来说&#xff0c;因为操作比较简单&#xff0c;所以 …

003 SpringCloud整合-LogStash安装及ELK日志收集

SpringCloud整合-LogStash安装及ELK日志收集 文章目录 SpringCloud整合-LogStash安装及ELK日志收集1.安装ElasticSearch和kibana2.Docker安装logstash1.拉取docker镜像2.创建外部挂载目录3.拷贝容器内部文件到宿主机4.修改外部挂载文件5.运行docker容器 3.整合kibana1.进入kiba…

AI预测体彩排3新模型百十个定位预测+胆码预测+杀和尾+杀和值2025年3月18日第22弹

前面由于工作原因停更了很长时间&#xff0c;停更期间很多彩友一直私信我何时恢复发布每日预测&#xff0c;目前手头上的项目已经基本收尾&#xff0c;接下来恢复发布。当然&#xff0c;也有很多朋友一直咨询3D超级助手开发的进度&#xff0c;在这里统一回复下。 由于本人既精…

数据结构入门(1)——算法复杂度

目录 一、前言 二、数据结构 2.1数据结构的概念 2.2数据结构的组成 2.3算法 三、oj题引进 四、复杂度 4.1复杂度的概念 4.2大O渐进表示法 4.3时间复杂度 4.4时间复杂度计算示例 4.4.1示例1 4.4.2示例2 4.4.3示例3 4.4.4示例4 4.4.5示例5 4.4.6示例6 4.4.7示例7 4.4.8示例8 4.5空…

【最新版】智慧小区物业管理小程序源码+uniapp全开源

一.系统介绍 智慧小区物业管理小程序,包含小区物业缴费、房产管理、在线报修、业主活动报名、在线商城等功能。为物业量身打造的智慧小区运营管理系统,贴合物业工作场景,轻松提高物业费用收缴率,更有功能模块个性化组合,助力物业节约成本高效运营。 二.搭建环境 系统环…

DeepSeek搭建本地知识库

1. 注册硅基流动 首先&#xff0c;打开浏览器&#xff0c;访问硅基流动的官方网站。 https://account.siliconflow.cn/ 在注册页面准确输入你的手机号&#xff0c;完成账号注册。这是搭建本地知识库的第一步&#xff0c;为后续获取重要权限做准备。 成功注册后&#xff0c;进…

实验9 高级搜索技术1

实验9 高级搜索技术1 一、实验目的 &#xff08;1&#xff09;掌握高级搜索技术的相关理论&#xff0c;能根据实际情况选取合适的搜索方法&#xff1b; &#xff08;2&#xff09;进一步熟悉爬山法搜索技术&#xff0c;掌握其在搜索过程中的优缺点&#xff1b; &#xff08;3&…

【数据挖掘】Python基础环境安装配置

【数据挖掘】Python基础环境安装配置 一、摘要二、安装Python3.13.2三、安装Jupyter Notebook四、安装Numpy和Pandas以及matplotlib五、安装scikit-learn库和seaborn库 一、摘要 本文主要介绍如何在Windows上安装Python3.13.2&#xff0c;然后基于该Python版本安装Jupyter not…

【2025新版本】【谷粒商城版】Kubernetes

本文作者&#xff1a; slience_me 文章目录 【2025】Kubernetes1. docker安装2. kubernetes安装前3. kubeadm,kubelet,kubectl3.1 简介kubeadmkubeletkubectl常用指令 3.2 安装3.3 kubeadm初始化3.4 加入从节点(工作节点)3.5 安装Pod网络插件&#xff08;CNI&#xff09;3.6 Ku…

vulhub-Billu-b0x攻略

靶场下载链接 https://download.vulnhub.com/billu/Billu_b0x.zip 将kali和Billu,NAT连接 获取靶场ip arp-scan -l 使用diesearch进行目录扫描 dirsearch -u " " 查看目录中的信息 打开add.php,得到有上传文件功能的&#xff08;看到后面你会发现其实这里就可以完…

vue3+Ts+elementPlus二次封装Table分页表格,表格内展示图片、switch开关、支持

目录 一.项目文件结构 二.实现代码 1.子组件&#xff08;表格组件&#xff09; 2.父组件&#xff08;使用表格&#xff09; 一.项目文件结构 1.表格组件&#xff08;子组件&#xff09;位置 2.使用表格组件的页面文件&#xff08;父组件&#xff09;位置 3.演示图片位置 ele…

数字人本地部署之llama-本地推理模型

llama 本地服务命令 llama-server.exe -m "data/LLM/my.gguf" --port 8080 -m data/LLM/my.gguf -m 属于命令行选项&#xff0c;一般用来指定要加载的模型文件。 data/LLM/my.gguf 是模型文件的路径。gguf 格式的文件是一种用于存储语言模型权重的文件格式&…