ETCD单机常用命令

从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配置文件

  1. cat <<EOF | sudo tee /opt/etcd/config/etcd.conf
  2. #节点名称
  3. ETCD_NAME=$(hostname -s)
  4. #数据存放位置
  5. ETCD_DATA_DIR=/var/lib/etcd
  6. EOF

创建systemd配置文件

  1. cat <<EOF | sudo tee /etc/systemd/system/etcd.service
  2. [Unit]
  3. Description=Etcd Server
  4. Documentation=https://github.com/coreos/etcd
  5. After=network.target
  6. [Service]
  7. User=root
  8. Type=notify
  9. EnvironmentFile=-/opt/etcd/config/etcd.conf
  10. ExecStart=/tmp/etcd/etcd
  11. Restart=on-failure
  12. RestartSec=10s
  13. LimitNOFILE=40000
  14. [Install]
  15. WantedBy=multi-user.target
  16. EOF

ExecStart是etcd的启动路径,EnvironmentFile是etcd的配置文件存放路径,根据你的情况修改一下

启动etcd

systemctl daemon-reload && systemctl enable etcd && systemctl start etcd

基本操作

  1. etcdctl -h
  2. NAME:
  3. etcdctl – A simple command line client for etcd3.
  4. USAGE:
  5. etcdctl [flags]
  6. VERSION:
  7. 3.4.10
  8. API VERSION:
  9. 3.4
  10. COMMANDS:
  11. alarm disarm Disarms all alarms
  12. alarm list Lists all alarms
  13. auth disable Disables authentication
  14. auth enable Enables authentication
  15. check datascale Check the memory usage of holding data for different workloads on a given server endpoint.
  16. check perf Check the performance of the etcd cluster
  17. compaction Compacts the event history in etcd
  18. defrag Defragments the storage of the etcd members with given endpoints
  19. del Removes the specified key or range of keys [key, range_end)
  20. elect Observes and participates in leader election
  21. endpoint hashkv Prints the KV history hash for each endpoint in –endpoints
  22. endpoint health Checks the healthiness of endpoints specified in `–endpoints` flag
  23. endpoint status Prints out the status of endpoints specified in `–endpoints` flag
  24. get Gets the key or a range of keys
  25. help Help about any command
  26. lease grant Creates leases
  27. lease keep-alive Keeps leases alive (renew)
  28. lease list List all active leases
  29. lease revoke Revokes leases
  30. lease timetolive Get lease information
  31. lock Acquires a named lock
  32. make-mirror Makes a mirror at the destination etcd cluster
  33. member add Adds a member into the cluster
  34. member list Lists all members in the cluster
  35. member promote Promotes a non-voting member in the cluster
  36. member remove Removes a member from the cluster
  37. member update Updates a member in the cluster
  38. migrate Migrates keys in a v2 store to a mvcc store
  39. move-leader Transfers leadership to another etcd cluster member.
  40. put Puts the given key into the store
  41. role add Adds a new role
  42. role delete Deletes a role
  43. role get Gets detailed information of a role
  44. role grant-permission Grants a key to a role
  45. role list Lists all roles
  46. role revoke-permission Revokes a key from a role
  47. snapshot restore Restores an etcd member snapshot to an etcd directory
  48. snapshot save Stores an etcd node backend snapshot to a given file
  49. snapshot status Gets backend snapshot status of a given file
  50. txn Txn processes all the requests in one transaction
  51. user add Adds a new user
  52. user delete Deletes a user
  53. user get Gets detailed information of a user
  54. user grantrole Grants a role to a user
  55. user list Lists all users
  56. user passwd Changes password of user
  57. user revokerole Revokes a role from a user
  58. version Prints the version of etcdctl
  59. watch Watches events stream on keys or prefixes
  60. OPTIONS:
  61. –cacert=\”\” verify certificates of TLS-enabled secure servers using this CA bundle
  62. –cert=\”\” identify secure client using this TLS certificate file
  63. –command-timeout=5s timeout for short running command (excluding dial timeout)
  64. –debug[=false] enable client-side debug logging
  65. –dial-timeout=2s dial timeout for client connections
  66. -d, –discovery-srv=\”\” domain name to query for SRV records describing cluster endpoints
  67. –discovery-srv-name=\”\” service name to query when using DNS discovery
  68. –endpoints=[127.0.0.1:2379] gRPC endpoints
  69. -h, –help[=false] help for etcdctl
  70. –hex[=false] print byte strings as hex encoded strings
  71. –insecure-discovery[=true] accept insecure SRV records describing cluster endpoints
  72. –insecure-skip-tls-verify[=false] skip server certificate verification (CAUTION: this option should be enabled only for testing purposes)
  73. –insecure-transport[=true] disable transport security for client connections
  74. –keepalive-time=2s keepalive time for client connections
  75. –keepalive-timeout=6s keepalive timeout for client connections
  76. –key=\”\” identify secure client using this TLS key file
  77. –password=\”\” password for authentication (if this option is used, –user option shouldn\’t include password)
  78. –user=\”\” username[:password] for authentication (prompt if password is not supplied)
  79. -w, –write-out=\”simple\” set the output format (fields, json, protobuf, simple, table)

输入、查看、更新、删除k-v:

  1. [root@localhost ~] etcdctl put /testkey \”Hello world\”
  2. OK
  3. [root@localhost ~] etcdctl get /testkey \”Hello world\”
  4. /testkey
  5. Hello world
  6. [root@localhost ~] etcdctl put /testkey \”Hello\”
  7. OK
  8. [root@localhost ~] etcdctl get /testkey \”Hello\”
  9. /testkey
  10. Hello
  11. [root@localhost ~] etcdctl del /testkey
  12. 1

获取json输出:

  1. [root@localhost ~] etcdctl get key1 -w json
  2. {\”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}
  3. [root@localhost ~] etcdctl get key1
  4. key1
  5. value1
  6. [root@localhost ~] echo dmFsdWUx|base64 -d
  7. value1[root@localhost ~]#

watch
在一个终端运行:

  1. [root@localhost etcd] etcdctl watch key1
  2. PUT
  3. key1
  4. valuex
  5. PUT
  6. key1
  7. valuez

在另一个终端:

  1. [root@localhost ~] etcdctl put key1 valuex
  2. OK
  3. [root@localhost ~] etcdctl put key1 valuez
  4. OK

租约
lease。etcd支持申请定时器,申请一个lease,会返回一个lease ID标识定时器。如果在put一个key的同时携带lease ID,就实现了一个自动过期的key。在etcd中,一个lease可以关联任意多的key,当lease过期后所有关联的key都将被自动删除。

  1. #生成
  2. [root@localhost etcd] etcdctl lease grant 300
  3. lease 694d73749a9d0515 granted with TTL(300s)
  4. #关联到key
  5. [root@localhost etcd] etcdctl put key3 300 –lease=694d73749a9d0515
  6. OK
  7. #维持租约
  8. [root@localhost etcd] etcdctl lease keep-alive 694d73749a9d0515
  9. lease 694d73749a9d0515 keepalived with TTL(300)
  10. #撤销租约
  11. [root@localhost ~] etcdctl lease revoke 694d73749a9d0515
  12. lease 694d73749a9d0515 revoked
购物车