• etcdctl-管理操作etcd集群


    一、简介

    etcd就是个分布式非关系型数据库.
    3 个节点组成的集群,可以容忍 1 个节点故障。
    生成环境中,不推荐使用单个节点的 etcd 集群。

    1. etcd 支持存储多个版本的数据,允许查询指定 key 历史版本的数据。
    2. etcd 为了控制数据总空间,会周期性的清理数据的历史版本。
    3. etcd 不支持修改旧版本的数据。
    4. etcd 中,数据以二进制的方式存储在磁盘中。

    二、安装

    参考链接:Releases · etcd-io/etcd · GitHub

    2.1 使用脚本部署

    1. ETCD_VER=v3.4.20
    2. # choose either URL
    3. GOOGLE_URL=https://storage.googleapis.com/etcd
    4. GITHUB_URL=https://github.com/etcd-io/etcd/releases/download
    5. DOWNLOAD_URL=${GOOGLE_URL}
    6. rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
    7. rm -rf /tmp/etcd-download-test && mkdir -p /tmp/etcd-download-test
    8. curl -L ${DOWNLOAD_URL}/${ETCD_VER}/etcd-${ETCD_VER}-linux-amd64.tar.gz -o /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
    9. tar xzvf /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz -C /tmp/etcd-download-test --strip-components=1
    10. rm -f /tmp/etcd-${ETCD_VER}-linux-amd64.tar.gz
    11. cp /tmp/etcd-download-test/etcd /usr/bin/
    12. cp /tmp/etcd-download-test/etcdctl /usr/bin/
    13. etcd --version
    14. etcdctl version

    使用etcdctlv3的版本时,需设置环境变量ETCDCTL_API=3

    1. vim /etc/profile
    2. ...
    3. ETCDCTL_API=3
    4. ...
    5. ###
    6. source /etc/profile

    2.2 检查

    1. [root@k8s-master][16:09:03][FAIL] ~/etcdctl/etcd-v3.4.20-linux-amd64
    2. #etcd --version
    3. etcd Version: 3.4.20
    4. Git SHA: 1e26823
    5. Go Version: go1.16.15
    6. Go OS/Arch: linux/amd64
    7. [root@k8s-master][16:09:05][OK] ~/etcdctl/etcd-v3.4.20-linux-amd64
    8. #etcdctl version
    9. etcdctl version: 3.4.20
    10. API version: 3.4

    三、使用

    3.1 帮助信息

    1. #etcdctl --help
    2. NAME:
    3. etcdctl - A simple command line client for etcd3.
    4. USAGE:
    5. etcdctl [flags]
    6. VERSION:
    7. 3.4.20
    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 grant-role Grants a role to a user
    55. user list Lists all users
    56. user passwd Changes password of user
    57. user revoke-role 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)

    3.2 指定etcd集群

    1. HOST_1=10.240.0.17
    2. HOST_2=10.240.0.18
    3. HOST_3=10.240.0.19
    4. ENDPOINTS=$HOST_1:2379,$HOST_2:2379,$HOST_3:2379
    5. etcdctl --endpoints=$ENDPOINTS member list

    3.3. 增删改查

    3.3.1 增

     etcdctl --endpoints=$ENDPOINTS put foo "Hello World!"

    3.3.2 查 

    1. etcdctl --endpoints=$ENDPOINTS get foo
    2. etcdctl --endpoints=$ENDPOINTS --write-out="json" get foo 
    3. 基于相同前缀查找 
    4. etcdctl --endpoints=$ENDPOINTS put web1 value1
    5. etcdctl --endpoints=$ENDPOINTS put web2 value2
    6. etcdctl --endpoints=$ENDPOINTS put web3 value3
    7. etcdctl --endpoints=$ENDPOINTS get web --prefix

    3.3.3 删 

    1. etcdctl --endpoints=$ENDPOINTS put key myvalue
    2. etcdctl --endpoints=$ENDPOINTS del key
    3. etcdctl --endpoints=$ENDPOINTS put k1 value1
    4. etcdctl --endpoints=$ENDPOINTS put k2 value2
    5. etcdctl --endpoints=$ENDPOINTS del k --prefix

    3.3.4 集群状态

    集群状态主要是etcdctl endpoint status 和etcdctl endpoint health两条命令。

    1. etcdctl --write-out=table --endpoints=$ENDPOINTS endpoint status
    2. +------------------+------------------+---------+---------+-----------+-----------+------------+
    3. |     ENDPOINT     |        ID        | VERSION | DB SIZE | IS LEADER | RAFT TERM | RAFT INDEX |
    4. +------------------+------------------+---------+---------+-----------+-----------+------------+
    5. | 10.240.0.17:2379 | 4917a7ab173fabe7 | 3.0.0   | 45 kB   | true      |         4 |      16726 |
    6. | 10.240.0.18:2379 | 59796ba9cd1bcd72 | 3.0.0   | 45 kB   | false     |         4 |      16726 |
    7. | 10.240.0.19:2379 | 94df724b66343e6c | 3.0.0   | 45 kB   | false     |         4 |      16726 |
    8. +------------------+------------------+---------+---------+-----------+-----------+------------+
    9. etcdctl --endpoints=$ENDPOINTS endpoint health
    10. 10.240.0.17:2379 is healthy: successfully committed proposal: took = 3.345431ms
    11. 10.240.0.19:2379 is healthy: successfully committed proposal: took = 3.767967ms
    12. 10.240.0.18:2379 is healthy: successfully committed proposal: took = 4.025451ms

    3.3.5 集群成员

    跟集群成员相关的命令如下:

    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 

     例如 etcdctl member list列出集群成员的命令。

    1. etcdctl --endpoints=http://172.16.5.4:12379 member list -w table
    2. +-----------------+---------+-------+------------------------+-----------------------------------------------+
    3. |       ID        | STATUS  | NAME  |       PEER ADDRS       |                 CLIENT ADDRS                  |
    4. +-----------------+---------+-------+------------------------+-----------------------------------------------+
    5. | c856d92a82ba66a | started | etcd0 | http://172.16.5.4:2380 | http://172.16.5.4:2379,http://172.16.5.4:4001 |
    6. +-----------------+---------+-------+------------------------+-----------------------------------------------+

    3.4 指定授权文件

    在执行etcdctl命令时需要指定认证授权文件, 所以将认证授权步骤 别名至 etcdctl 简化操作

    1. # 指定ETCDCTL_API版本为3
    2. $ export ETCDCTL_API=3
    3. # 创建etcdctl别名,指定监听地址,和证书
    4. $ alias etcdctl='etcdctl --endpoints=https://127.0.0.1:2379 --cacert=/etc/kubernetes/pki/etcd/ca.crt --cert=/etc/kubernetes/pki/etcd/healthcheck-client.crt --key=/etc/kubernetes/pki/etcd/healthcheck-client.key'

    3.4.1 查看etcd集群的成员节点

    1. #etcdctl member list -w table
    2. +------------------+---------+------------+------------------------+------------------------+------------+
    3. | ID | STATUS | NAME | PEER ADDRS | CLIENT ADDRS | IS LEARNER |
    4. +------------------+---------+------------+------------------------+------------------------+------------+
    5. | 8dc8eb40f5ed7ad6 | started | k8s-master | https://10.0.0.16:2380 | https://10.0.0.16:2379 | false |
    6. +------------------+---------+------------+------------------------+------------------------+------------+

    3.4.2 查看etcd集群节点状态

    1. [root@k8s-master][16:19:15][OK] ~/etcdctl/etcd-v3.4.20-linux-amd64
    2. #etcdctl endpoint status -w table
    3. +------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
    4. | ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | IS LEARNER | RAFT TERM | RAFT INDEX | RAFT APPLIED INDEX | ERRORS |
    5. +------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
    6. | https://127.0.0.1:2379 | 8dc8eb40f5ed7ad6 | 3.5.3 | 46 MB | true | false | 10 | 380897 | 380897 | |
    7. +------------------------+------------------+---------+---------+-----------+------------+-----------+------------+--------------------+--------+
    8. [root@k8s-master][16:20:35][OK] ~/etcdctl/etcd-v3.4.20-linux-amd64
    9. #etcdctl endpoint health -w table
    10. +------------------------+--------+-------------+-------+
    11. | ENDPOINT | HEALTH | TOOK | ERROR |
    12. +------------------------+--------+-------------+-------+
    13. | https://127.0.0.1:2379 | true | 11.021122ms | |
    14. +------------------------+--------+-------------+-------+

    3.5 备份数据

    1. # 字符串拼接用于定时任务
    2. etcdctl snapshot save `hostname`-etcd_`date +%Y%m%d%H%M`.db

    3.6 恢复快照

    1. #停止etcd和apiserver
    2. ## 移走当前数据目录
    3. mv /var/lib/etcd/ /var/lib/etcd.bak
    4. #恢复快照
    5. etcdctl snapshot restore `hostname`-etcd_`date +%Y%m%d%H%M`.db --data-dir=/var/lib/etcd/

    二进制部署的ETCD恢复快照

    在这里插入图片描述

    四、故障排查

    journalctl -u etcd > a.log导出日志慢慢分析
  • 相关阅读:
    kafka3.X基本概念和使用
    Java基础之 JDK8 HashMap 源码分析(中间写出与JDK7的区别)
    艾美捷ProSci丨ProSci AADACL1 肽说明书
    ROS学习笔记08、机器人导航仿真(slam、map_server、amcl、move_base与导航消息介绍)
    python+opencv+机器学习车牌识别 计算机竞赛
    [附源码]java毕业设计网上招聘系统
    计算机组成原理(4)-----Cache的原理及相关知识点
    基于SSM的烟草管理系统的设计与实现该怎么写呀有会写的加我V Hik-5468其他都好谈
    详解如何在python中实现简单的app自动化框架
    JVM本地方法接口和执行引擎
  • 原文地址:https://blog.csdn.net/zfw_666666/article/details/126835459