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.pswp.cn/pingmian/73085.shtml
繁體地址,請注明出處:http://hk.pswp.cn/pingmian/73085.shtml
英文地址,請注明出處:http://en.pswp.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…