从Github上下载并解压安装包
wget https://github.com/coreos/etcd/releases/download/v3.4.14/etcd-v3.4.14-linux-amd64.tar.gz tar xzvf etcd-v3.4.14-linux-amd64.tar.gz mv etcd-v3.4.14-linux-amd64 /tmp/etcd ## 解压后是一些文档和两个二进制文件etcd和etcdctl。etcd是server端,etcdctl是客户端。
启动一个单节点的etcd服务,只需要运行etcd命令就行。
./etcd
配置信息
为了可以使用系统命令运行etcd,将文件夹下的二进制文件复制到bin下
cp /tmp/etcd/etcd* /usr/local/bin/
设定etcd配置文件
mkdir -p /var/lib/etcd/ mkdir -p /opt/etcd/config/ chmod 700 /var/lib/etcd #注意修改权限,否则无法启动
创建etcd配置文件
-
cat <<EOF | sudo tee /opt/etcd/config/etcd.conf
-
#节点名称
-
ETCD_NAME=$(hostname -s)
-
#数据存放位置
-
ETCD_DATA_DIR=/var/lib/etcd
-
EOF
创建systemd配置文件
-
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=-/opt/etcd/config/etcd.conf
-
ExecStart=/tmp/etcd/etcd
-
Restart=on-failure
-
RestartSec=10s
-
LimitNOFILE=40000
-
-
[Install]
-
WantedBy=multi-user.target
-
EOF
ExecStart是etcd的启动路径,EnvironmentFile是etcd的配置文件存放路径,根据你的情况修改一下
启动etcd
systemctl daemon-reload && systemctl enable etcd && systemctl start etcd
基本操作
-
etcdctl -h
-
NAME:
-
etcdctl – A simple command line client for etcd3.
-
-
USAGE:
-
etcdctl [flags]
-
-
VERSION:
-
3.4.10
-
-
API VERSION:
-
3.4
-
-
-
COMMANDS:
-
alarm disarm Disarms all alarms
-
alarm list Lists all alarms
-
auth disable Disables authentication
-
auth enable Enables authentication
-
check datascale Check the memory usage of holding data for different workloads on a given server endpoint.
-
check perf Check the performance of the etcd cluster
-
compaction Compacts the event history in etcd
-
defrag Defragments the storage of the etcd members with given endpoints
-
del Removes the specified key or range of keys [key, range_end)
-
elect Observes and participates in leader election
-
endpoint hashkv Prints the KV history hash for each endpoint in –endpoints
-
endpoint health Checks the healthiness of endpoints specified in `–endpoints` flag
-
endpoint status Prints out the status of endpoints specified in `–endpoints` flag
-
get Gets the key or a range of keys
-
help Help about any command
-
lease grant Creates leases
-
lease keep-alive Keeps leases alive (renew)
-
lease list List all active leases
-
lease revoke Revokes leases
-
lease timetolive Get lease information
-
lock Acquires a named lock
-
make-mirror Makes a mirror at the destination etcd cluster
-
member add Adds a member into the cluster
-
member list Lists all members in the cluster
-
member promote Promotes a non-voting member in the cluster
-
member remove Removes a member from the cluster
-
member update Updates a member in the cluster
-
migrate Migrates keys in a v2 store to a mvcc store
-
move-leader Transfers leadership to another etcd cluster member.
-
put Puts the given key into the store
-
role add Adds a new role
-
role delete Deletes a role
-
role get Gets detailed information of a role
-
role grant-permission Grants a key to a role
-
role list Lists all roles
-
role revoke-permission Revokes a key from a role
-
snapshot restore Restores an etcd member snapshot to an etcd directory
-
snapshot save Stores an etcd node backend snapshot to a given file
-
snapshot status Gets backend snapshot status of a given file
-
txn Txn processes all the requests in one transaction
-
user add Adds a new user
-
user delete Deletes a user
-
user get Gets detailed information of a user
-
user grant–role Grants a role to a user
-
user list Lists all users
-
user passwd Changes password of user
-
user revoke–role Revokes a role from a user
-
version Prints the version of etcdctl
-
watch Watches events stream on keys or prefixes
-
-
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
-
–discovery-srv-name=\”\” service name to query when using DNS discovery
-
–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 (CAUTION: this option should be enabled only for testing purposes)
-
–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
-
–password=\”\” password for authentication (if this option is used, –user option shouldn\’t include password)
-
–user=\”\” username[:password] for authentication (prompt if password is not supplied)
-
-w, –write-out=\”simple\” set the output format (fields, json, protobuf, simple, table)
-
输入、查看、更新、删除k-v:
-
[root@localhost ~] etcdctl put /testkey \”Hello world\”
-
OK
-
[root@localhost ~] etcdctl get /testkey \”Hello world\”
-
/testkey
-
Hello world
-
[root@localhost ~] etcdctl put /testkey \”Hello\”
-
OK
-
[root@localhost ~] etcdctl get /testkey \”Hello\”
-
/testkey
-
Hello
-
[root@localhost ~] etcdctl del /testkey
-
1
获取json输出:
-
[root@localhost ~] etcdctl get key1 -w json
-
{\”header\”:{\”cluster_id\”:14841639068965178418,\”member_id\”:10276657743932975437,\”revision\”:6,\”raft_term\”:2},\”kvs\”:[{\”key\”:\”a2V5MQ==\”,\”create_revision\”:5,\”mod_revision\”:5,\”version\”:1,\”value\”:\”dmFsdWUx\”}],\”count\”:1}
-
[root@localhost ~] etcdctl get key1
-
key1
-
value1
-
[root@localhost ~] echo dmFsdWUx|base64 -d
-
value1[root@localhost ~]#
watch
在一个终端运行:
-
[root@localhost etcd] etcdctl watch key1
-
PUT
-
key1
-
valuex
-
PUT
-
key1
-
valuez
在另一个终端:
-
[root@localhost ~] etcdctl put key1 valuex
-
OK
-
[root@localhost ~] etcdctl put key1 valuez
-
OK
租约
lease。etcd支持申请定时器,申请一个lease,会返回一个lease ID标识定时器。如果在put一个key的同时携带lease ID,就实现了一个自动过期的key。在etcd中,一个lease可以关联任意多的key,当lease过期后所有关联的key都将被自动删除。
-
#生成
-
[root@localhost etcd] etcdctl lease grant 300
-
lease 694d73749a9d0515 granted with TTL(300s)
-
#关联到key
-
[root@localhost etcd] etcdctl put key3 300 –lease=694d73749a9d0515
-
OK
-
#维持租约
-
[root@localhost etcd] etcdctl lease keep-alive 694d73749a9d0515
-
lease 694d73749a9d0515 keepalived with TTL(300)
-
#撤销租约
-
[root@localhost ~] etcdctl lease revoke 694d73749a9d0515
-
lease 694d73749a9d0515 revoked