etcd 3.3.11安裝和使用介紹

  • 環境準備

Linux版本

uname -a
Linux etcd1 3.10.0-1062.9.1.el7.x86_64 #1 SMP Fri Dec 6 15:49:49 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

三個節點

cat /etc/hosts
192.168.1.66 etcd1
192.168.1.67 etcd2
192.168.1.68 etcd3

關閉防火牆

systemctl stop firewalld
systemctl disable firewalld
  • 安裝

安裝etcd。

yum install -y etcd

配置etcd1:

#[Member]
#ETCD_CORS=""
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
#ETCD_WAL_DIR=""
ETCD_LISTEN_PEER_URLS="http://0.0.0.0:2380"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379,http://0.0.0.0:4001"
#ETCD_MAX_SNAPSHOTS="5"
#ETCD_MAX_WALS="5"
ETCD_NAME="etcd1"
#ETCD_SNAPSHOT_COUNT="100000"
#ETCD_HEARTBEAT_INTERVAL="100"
#ETCD_ELECTION_TIMEOUT="1000"
#ETCD_QUOTA_BACKEND_BYTES="0"
#ETCD_MAX_REQUEST_BYTES="1572864"
#ETCD_GRPC_KEEPALIVE_MIN_TIME="5s"
#ETCD_GRPC_KEEPALIVE_INTERVAL="2h0m0s"
#ETCD_GRPC_KEEPALIVE_TIMEOUT="20s"
#
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.1.66:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.1.66:2379,http://192.168.1.66:4001"
#ETCD_DISCOVERY=""
#ETCD_DISCOVERY_FALLBACK="proxy"
#ETCD_DISCOVERY_PROXY=""
#ETCD_DISCOVERY_SRV=""
ETCD_INITIAL_CLUSTER="etcd1=http://192.168.1.66:2380,etcd2=http://192.168.1.67:2380,etcd3=http://192.168.1.68:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-twingao-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
#ETCD_STRICT_RECONFIG_CHECK="true"
#ETCD_ENABLE_V2="true"

配置etcd2:

#[Member]
#ETCD_CORS=""
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
#ETCD_WAL_DIR=""
ETCD_LISTEN_PEER_URLS="http://0.0.0.0:2380"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379,http://0.0.0.0:4001"
#ETCD_MAX_SNAPSHOTS="5"
#ETCD_MAX_WALS="5"
ETCD_NAME="etcd2"
#ETCD_SNAPSHOT_COUNT="100000"
#ETCD_HEARTBEAT_INTERVAL="100"
#ETCD_ELECTION_TIMEOUT="1000"
#ETCD_QUOTA_BACKEND_BYTES="0"
#ETCD_MAX_REQUEST_BYTES="1572864"
#ETCD_GRPC_KEEPALIVE_MIN_TIME="5s"
#ETCD_GRPC_KEEPALIVE_INTERVAL="2h0m0s"
#ETCD_GRPC_KEEPALIVE_TIMEOUT="20s"
#
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.1.67:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.1.67:2379,http://192.168.1.67:4001"
#ETCD_DISCOVERY=""
#ETCD_DISCOVERY_FALLBACK="proxy"
#ETCD_DISCOVERY_PROXY=""
#ETCD_DISCOVERY_SRV=""
ETCD_INITIAL_CLUSTER="etcd1=http://192.168.1.66:2380,etcd2=http://192.168.1.67:2380,etcd3=http://192.168.1.68:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-twingao-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
#ETCD_STRICT_RECONFIG_CHECK="true"
#ETCD_ENABLE_V2="true"

配置etcd3:

#[Member]
#ETCD_CORS=""
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
#ETCD_WAL_DIR=""
ETCD_LISTEN_PEER_URLS="http://0.0.0.0:2380"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379,http://0.0.0.0:4001"
#ETCD_MAX_SNAPSHOTS="5"
#ETCD_MAX_WALS="5"
ETCD_NAME="etcd3"
#ETCD_SNAPSHOT_COUNT="100000"
#ETCD_HEARTBEAT_INTERVAL="100"
#ETCD_ELECTION_TIMEOUT="1000"
#ETCD_QUOTA_BACKEND_BYTES="0"
#ETCD_MAX_REQUEST_BYTES="1572864"
#ETCD_GRPC_KEEPALIVE_MIN_TIME="5s"
#ETCD_GRPC_KEEPALIVE_INTERVAL="2h0m0s"
#ETCD_GRPC_KEEPALIVE_TIMEOUT="20s"
#
#[Clustering]
ETCD_INITIAL_ADVERTISE_PEER_URLS="http://192.168.1.68:2380"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.1.68:2379,http://192.168.1.68:4001"
#ETCD_DISCOVERY=""
#ETCD_DISCOVERY_FALLBACK="proxy"
#ETCD_DISCOVERY_PROXY=""
#ETCD_DISCOVERY_SRV=""
ETCD_INITIAL_CLUSTER="etcd1=http://192.168.1.66:2380,etcd2=http://192.168.1.67:2380,etcd3=http://192.168.1.68:2380"
ETCD_INITIAL_CLUSTER_TOKEN="etcd-twingao-cluster"
ETCD_INITIAL_CLUSTER_STATE="new"
#ETCD_STRICT_RECONFIG_CHECK="true"
#ETCD_ENABLE_V2="true"

參數說明:

ETCD_DATA_DIR:數據存儲目錄
ETCD_LISTEN_PEER_URLS:與其他節點通信時的監聽地址列表,通信協議可以是http、https
ETCD_LISTEN_CLIENT_URLS:與客戶端通信時的監聽地址列表
ETCD_NAME:節點名稱
ETCD_INITIAL_ADVERTISE_PEER_URLS:節點在整個集羣中的通信地址列表,可以理解爲能與外部通信的ip端口
ETCD_ADVERTISE_CLIENT_URLS:告知集羣中其他成員自己名下的客戶端的地址列表
ETCD_INITIAL_CLUSTER:集羣內所有成員的地址,這就是爲什麼稱之爲靜態發現,因爲所有成員的地址都必須配置
ETCD_INITIAL_CLUSTER_TOKEN:初始化集羣口令,用於標識不同集羣
ETCD_INITIAL_CLUSTER_STATE:初始化集羣狀態,new表示新建

啓動etcd服務,並配置自啓動。

systemctl start etcd
systemctl enable etcd

如果不能啓動etcd服務,可以刪除所有節點/var/lib/etcd目錄下內容,然後重新啓動。

查看etcd版本。

etcd --version
etcd Version: 3.3.11
Git SHA: 2cf9e51
Go Version: go1.10.3
Go OS/Arch: linux/amd64

查看etcdctl版本。

etcdctl --version
etcdctl version: 3.3.11
API version: 2

etcd api分爲2和3版本,其對應的命令行客戶端etcdctl也有兩種版本模式,兩個版本在使用上有些區別,從上面的命令看安裝後etcd api缺省是2版本,採用如下方法可以改爲3版本。

vi /etc/profile
export ETCDCTL_API=3
source /etc/profile
  • 使用

查看集羣節點:

etcdctl member list
3bfc158cf02dfab9, started, etcd3, http://192.168.1.68:2380, http://192.168.1.68:2379,http://192.168.1.68:4001
8835dd4fc48bb08a, started, etcd2, http://192.168.1.67:2380, http://192.168.1.67:2379,http://192.168.1.67:4001
fbe83a7120c1fef4, started, etcd1, http://192.168.1.66:2380, http://192.168.1.66:2379,http://192.168.1.66:4001

在etcd1節點停止etcd服務,再查看集羣,發現無法連接,因爲etcdctl缺省訪問本地的etcd服務。

systemctl stop etcd
etcdctl member list
Error: dial tcp 127.0.0.1:2379: connect: connection refused

在命令行指定endpoint,etcdctl可以向其他endpoint請求數據,防止一個節點出故障導致集羣無法訪問。

etcdctl --endpoints=192.168.1.66:2379,192.168.1.67:2379,192.168.1.68:2379 member list
3bfc158cf02dfab9, started, etcd3, http://192.168.1.68:2380, http://192.168.1.68:2379,http://192.168.1.68:4001
8835dd4fc48bb08a, started, etcd2, http://192.168.1.67:2380, http://192.168.1.67:2379,http://192.168.1.67:4001
fbe83a7120c1fef4, started, etcd1, http://192.168.1.66:2380, http://192.168.1.66:2379,http://192.168.1.66:4001

查看endpoint的狀態。

etcdctl --endpoints=192.168.1.66:2379,192.168.1.67:2379,192.168.1.68:2379 endpoint status
Failed to get the status of endpoint 192.168.1.66:2379 (context deadline exceeded)
192.168.1.67:2379, 8835dd4fc48bb08a, 3.3.11, 20 kB, false, 16, 20
192.168.1.68:2379, 3bfc158cf02dfab9, 3.3.11, 20 kB, true, 16, 20

endpoint健康檢查。

etcdctl --endpoints=192.168.1.66:2379,192.168.1.67:2379,192.168.1.68:2379 endpoint health
192.168.1.68:2379 is healthy: successfully committed proposal: took = 6.832804ms
192.168.1.67:2379 is healthy: successfully committed proposal: took = 10.387798ms
192.168.1.66:2379 is unhealthy: failed to connect: dial tcp 192.168.1.66:2379: connect: connection refused
Error: unhealthy cluster

etcdctl支持的3版本的命令。

etcdctl
NAME:
        etcdctl - A simple command line client for etcd3.

USAGE:
        etcdctl

VERSION:
        3.3.11

API VERSION:
        3.3


COMMANDS:
        get                     Gets the key or a range of keys
        put                     Puts the given key into the store
        del                     Removes the specified key or range of keys [key, range_end)
        txn                     Txn processes all the requests in one transaction
        compaction              Compacts the event history in etcd
        alarm disarm            Disarms all alarms
        alarm list              Lists all alarms
        defrag                  Defragments the storage of the etcd members with given endpoints
        endpoint health         Checks the healthiness of endpoints specified in `--endpoints` flag
        endpoint status         Prints out the status of endpoints specified in `--endpoints` flag
        endpoint hashkv         Prints the KV history hash for each endpoint in --endpoints
        move-leader             Transfers leadership to another etcd cluster member.
        watch                   Watches events stream on keys or prefixes
        version                 Prints the version of etcdctl
        lease grant             Creates leases
        lease revoke            Revokes leases
        lease timetolive        Get lease information
        lease list              List all active leases
        lease keep-alive        Keeps leases alive (renew)
        member add              Adds a member into the cluster
        member remove           Removes a member from the cluster
        member update           Updates a member in the cluster
        member list             Lists all members in the cluster
        snapshot save           Stores an etcd node backend snapshot to a given file
        snapshot restore        Restores an etcd member snapshot to an etcd directory
        snapshot status         Gets backend snapshot status of a given file
        make-mirror             Makes a mirror at the destination etcd cluster
        migrate                 Migrates keys in a v2 store to a mvcc store
        lock                    Acquires a named lock
        elect                   Observes and participates in leader election
        auth enable             Enables authentication
        auth disable            Disables authentication
        user add                Adds a new user
        user delete             Deletes a user
        user get                Gets detailed information of a user
        user list               Lists all users
        user passwd             Changes password of user
        user grant-role         Grants a role to a user
        user revoke-role        Revokes a role from a user
        role add                Adds a new role
        role delete             Deletes a role
        role get                Gets detailed information of a role
        role list               Lists all roles
        role grant-permission   Grants a key to a role
        role revoke-permission  Revokes a key from a role
        check perf              Check the performance of the etcd cluster
        help                    Help about any command

OPTIONS:
      --cacert=""                               verify certificates of TLS-enabled secure servers using this CA bundle
      --cert=""                                 identify secure client using this TLS certificate file
      --command-timeout=5s                      timeout for short running command (excluding dial timeout)
      --debug[=false]                           enable client-side debug logging
      --dial-timeout=2s                         dial timeout for client connections
  -d, --discovery-srv=""                        domain name to query for SRV records describing cluster endpoints
      --endpoints=[127.0.0.1:2379]              gRPC endpoints
  -h, --help[=false]                            help for etcdctl
      --hex[=false]                             print byte strings as hex encoded strings
      --insecure-discovery[=true]               accept insecure SRV records describing cluster endpoints
      --insecure-skip-tls-verify[=false]        skip server certificate verification
      --insecure-transport[=true]               disable transport security for client connections
      --keepalive-time=2s                       keepalive time for client connections
      --keepalive-timeout=6s                    keepalive timeout for client connections
      --key=""                                  identify secure client using this TLS key file
      --user=""                                 username[:password] for authentication (prompt if password is not supplied)
  -w, --write-out="simple"                      set the output format (fields, json, protobuf, simple, table)

創建key

etcdctl put hello world
OK

查詢給定key的值。第一行爲key,第二行爲value。

etcdctl get hello
hello
world

在etcd1節點監聽key hello值的變化,該命令會一直等待。

etcdctl watch hello

在etcd2節點修改該key的值。

etcdctl put hello world!
OK

在etcd1節點監聽命令立馬就有反饋。

etcdctl watch hello
PUT
hello
world!

先增加幾個key。

etcdctl put hello1 nihao
OK
etcdctl put hello2 ninhao
OK

基於key前綴查找。

etcdctl get hello --prefix
hello
world!
hello1
nihao
hello2
ninhao

查找所有的key值。

etcdctl put key1 value1
OK
etcdctl get "" --prefix
hello
world!
hello1
nihao
hello2
ninhao
key1
value1
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章