当前位置: 首页 > article >正文

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: 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.kler.cn/a/591390.html

相关文章:

  • Word 小黑第29套
  • 【k8s】containerd兼容docker镜像
  • 3DXML 与 SOLIDWORKS 格式转换:技术协同及迪威模型方案
  • 【JavaScript】三、数据类型
  • 【第13节】windows sdk编程:GDI编程
  • LeetCode 452. 用最少数量的箭引爆气球 java题解
  • springboot实现调用百度ocr实现身份识别
  • Joker靶机实战攻略
  • 《大语言模型》学习笔记(三)
  • 【R语言】lm线性回归及输出含义,置信区间,预测,R方,ggplot 拟合直线
  • 如何在前端处理文件上传,避免大文件造成的性能问题?
  • Linux并发程序设计(5):线程的相关操作
  • __str__特殊方法
  • 机器学习——数据清洗(缺失值处理、异常值处理、数据标准化)
  • 【QT:窗口】
  • 我在哪,要去哪
  • LogicFlow介绍
  • 漏洞知识点《一句话木马》
  • 堆(heap)
  • HTML CSS