💢歡迎來到張胤塵的開源技術站
💥開源如江河,匯聚眾志成。代碼似星辰,照亮行征程。開源精神長,傳承永不忘。攜手共前行,未來更輝煌💥
文章目錄
- 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
將可執行文件移動到系統路徑
將 etcd
和 etcdctl
移動到 /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
🌺🌺🌺撒花!
如果本文對你有幫助,就點關注或者留個👍
如果您有任何技術問題或者需要更多其他的內容,請隨時向我提問。