惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
人人都是产品经理
人人都是产品经理
G
GRAHAM CLULEY
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
L
LangChain Blog
AWS News Blog
AWS News Blog
V
Vulnerabilities – Threatpost
博客园 - 司徒正美
Last Week in AI
Last Week in AI
P
Privacy International News Feed
C
CERT Recently Published Vulnerability Notes
Simon Willison's Weblog
Simon Willison's Weblog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
Schneier on Security
Y
Y Combinator Blog
月光博客
月光博客
博客园 - Franky
T
Threatpost
Security Latest
Security Latest
C
Cybersecurity and Infrastructure Security Agency CISA
博客园 - 【当耐特】
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
大猫的无限游戏
大猫的无限游戏
A
Arctic Wolf
T
The Exploit Database - CXSecurity.com
A
About on SuperTechFans
I
Intezer
C
CXSECURITY Database RSS Feed - CXSecurity.com
C
Cisco Blogs
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
美团技术团队
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Microsoft Security Blog
Microsoft Security Blog
Know Your Adversary
Know Your Adversary
IT之家
IT之家
D
Docker
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
NISL@THU
NISL@THU
博客园 - 三生石上(FineUI控件)
L
Lohrmann on Cybersecurity
小众软件
小众软件
PCI Perspectives
PCI Perspectives
N
News and Events Feed by Topic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Google DeepMind News
Google DeepMind News
爱范儿
爱范儿
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Engineering at Meta
Engineering at Meta

Docker 从入门到实践

修订记录 如何贡献 Docker — 从入门到实践 高级网络配置 容器访问控制 自定义网桥 编辑网络配置文件 配置 docker0 网桥 工具和示例 映射容器端口到宿主主机的实现 示例:创建一个点到点连接 快速配置指南 附录 Dockerfile 最佳实践 Docker 命令查询 客户端命令(docker) 服务端命令(dockerd) 如何调试 Docker 常见问题总结 热门镜像介绍 Node.js 资源链接 归档项目 基本概念 Docker 容器 Docker 镜像 Docker Buildx 使用 BuildKit 构建镜像 使用 Buildx 构建镜像 使用 buildx 构建多种系统架构支持的 Docker 镜像 CI/CD GitHub Actions Drone Demo Drone 部署 Drone Alpine Busybox CentOS/Fedora Debian/Ubuntu 容器与云计算 亚马逊云 腾讯云 Docker Compose 项目 Compose 命令说明 Compose 模板文件 使用 Django 安装与卸载 Compose 简介 使用 compose 搭建 LNMP 环境 使用 Rails 使用 使用 WordPress 操作 Docker 容器 进入容器 后台运行 导出和导入容器 删除容器 启动容器 终止容器 Fedora CoreOS 安装 Fedora CoreOS Fedora CoreOS 介绍 Docker 数据管理 挂载主机目录 数据卷 etcd 集群 使用 etcdctl 什么是 etcd 在 IDE 中使用 Docker VS Code 中使用 Docker 使用 Dockerfile 定制镜像 使用 Docker 镜像 利用 commit 理解镜像构成 Dockerfile 指令详解 ADD 更高级的复制文件 ARG 构建参数 CMD 容器启动命令 COPY 复制文件 ENTRYPOINT 入口点 EXPOSE 声明端口 CentOS minio MongoDB MySQL Nginx PHP Redis Ubuntu WordPress Docker Registry 操作系统 本章小结 阿里云 简介 本章小结 etcd 安装 Docker — 从入门到实践
使用 etcdctl v2
2022-05-13 · via Docker 从入门到实践

# 使用 etcdctl v2

etcdctl 是一个命令行客户端,它能提供一些简洁的命令,供用户直接跟 etcd 服务打交道,而无需基于 HTTP API 方式。这在某些情况下将很方便,例如用户对服务进行测试或者手动修改数据库内容。我们也推荐在刚接触 etcd 时通过 etcdctl 命令来熟悉相关的操作,这些操作跟 HTTP API 实际上是对应的。

etcd 项目二进制发行包中已经包含了 etcdctl 工具,没有的话,可以从 github.com/etcd-io/etcd/releases (opens new window) 下载。

etcdctl 支持如下的命令,大体上分为数据库操作和非数据库操作两类,后面将分别进行解释。

$ etcdctl -h
NAME:
   etcdctl - A simple command line client for etcd.

USAGE:
   etcdctl [global options] command [command options] [arguments...]

VERSION:
   2.0.0-rc.1

COMMANDS:
   backup	backup an etcd directory
   mk		make a new key with a given value
   mkdir	make a new directory
   rm		remove a key
   rmdir	removes the key if it is an empty directory or a key-value pair
   get		retrieve the value of a key
   ls		retrieve a directory
   set		set the value of a key
   setdir	create a new or existing directory
   update	update an existing key with a given value
   updatedir	update an existing directory
   watch	watch a key for changes
   exec-watch	watch a key for changes and exec an executable
   member	member add, remove and list subcommands
   help, h	Shows a list of commands or help for one command

GLOBAL OPTIONS:
   --debug			output cURL commands which can be used to reproduce the request
   --no-sync			don't synchronize cluster information before sending request
   --output, -o 'simple'	output response in the given format (`simple` or `json`)
   --peers, -C 			a comma-delimited list of machine addresses in the cluster (default: "127.0.0.1:4001")
   --cert-file 			identify HTTPS client using this SSL certificate file
   --key-file 			identify HTTPS client using this SSL key file
   --ca-file 			verify certificates of HTTPS-enabled servers using this CA bundle
   --help, -h			show help
   --version, -v		print the version

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

# 数据库操作

数据库操作围绕对键值和目录的 CRUD (符合 REST 风格的一套操作:Create)完整生命周期的管理。

etcd 在键的组织上采用了层次化的空间结构(类似于文件系统中目录的概念),用户指定的键可以为单独的名字,如 testkey,此时实际上放在根目录 / 下面,也可以为指定目录结构,如 cluster1/node2/testkey,则将创建相应的目录结构。

注:CRUD 即 Create, Read, Update, Delete,是符合 REST 风格的一套 API 操作。

# set

指定某个键的值。例如

$ etcdctl set /testdir/testkey "Hello world"
Hello world

1
2

支持的选项包括:

--ttl '0'			该键值的超时时间(单位为秒),不配置(默认为 0)则永不超时
--swap-with-value value 若该键现在的值是 value,则进行设置操作
--swap-with-index '0'	若该键现在的索引值是指定索引,则进行设置操作

1
2
3

# get

获取指定键的值。例如

$ etcdctl set testkey hello
hello
$ etcdctl update testkey world
world

1
2
3
4

当键不存在时,则会报错。例如

$ etcdctl get testkey2
Error:  100: Key not found (/testkey2) [1]

1
2

支持的选项为

--sort	对结果进行排序
--consistent 将请求发给主节点,保证获取内容的一致性

1
2

# update

当键存在时,更新值内容。例如

$ etcdctl set testkey hello
hello
$ etcdctl update testkey world
world

1
2
3
4

当键不存在时,则会报错。例如

$ etcdctl update testkey2 world
Error:  100: Key not found (/testkey2) [1]

1
2

支持的选项为

--ttl '0'	超时时间(单位为秒),不配置(默认为 0)则永不超时

1

# rm

删除某个键值。例如

当键不存在时,则会报错。例如

$ etcdctl rm testkey2
Error:  100: Key not found (/testkey2) [8]

1
2

支持的选项为

--dir		如果键是个空目录或者键值对则删除
--recursive		删除目录和所有子键
--with-value 	检查现有的值是否匹配
--with-index '0'	检查现有的 index 是否匹配

1
2
3
4

# mk

如果给定的键不存在,则创建一个新的键值。例如

$ etcdctl mk /testdir/testkey "Hello world"
Hello world

1
2

当键存在的时候,执行该命令会报错,例如

$ etcdctl set testkey "Hello world"
Hello world
$ ./etcdctl mk testkey "Hello world"
Error:  105: Key already exists (/testkey) [2]

1
2
3
4

支持的选项为

--ttl '0'	超时时间(单位为秒),不配置(默认为 0)则永不超时

1

# mkdir

如果给定的键目录不存在,则创建一个新的键目录。例如

$ etcdctl mkdir testdir

1

当键目录存在的时候,执行该命令会报错,例如

$ etcdctl mkdir testdir
$ etcdctl mkdir testdir
Error:  105: Key already exists (/testdir) [7]

1
2
3

支持的选项为

--ttl '0'	超时时间(单位为秒),不配置(默认为 0)则永不超时

1

# setdir

创建一个键目录,无论存在与否。

支持的选项为

--ttl '0'	超时时间(单位为秒),不配置(默认为 0)则永不超时

1

# updatedir

更新一个已经存在的目录。 支持的选项为

--ttl '0'	超时时间(单位为秒),不配置(默认为 0)则永不超时

1

# rmdir

删除一个空目录,或者键值对。

若目录不空,会报错

$ etcdctl set /dir/testkey hi
hi
$ etcdctl rmdir /dir
Error:  108: Directory not empty (/dir) [13]

1
2
3
4

# ls

列出目录(默认为根目录)下的键或者子目录,默认不显示子目录中内容。

例如

$ ./etcdctl set testkey 'hi'
hi
$ ./etcdctl set dir/test 'hello'
hello
$ ./etcdctl ls
/testkey
/dir
$ ./etcdctl ls dir
/dir/test

1
2
3
4
5
6
7
8
9

支持的选项包括

--sort	将输出结果排序
--recursive	如果目录下有子目录,则递归输出其中的内容
-p		对于输出为目录,在最后添加 `/` 进行区分

1
2
3

# 非数据库操作

# backup

备份 etcd 的数据。

支持的选项包括

--data-dir 		etcd 的数据目录
--backup-dir 	备份到指定路径

1
2

# watch

监测一个键值的变化,一旦键值发生更新,就会输出最新的值并退出。

例如,用户更新 testkey 键值为 Hello world。

$ etcdctl watch testkey
Hello world

1
2

支持的选项包括

--forever		一直监测,直到用户按 `CTRL+C` 退出
--after-index '0'	在指定 index 之前一直监测
--recursive		返回所有的键值和子键值

1
2
3

# exec-watch

监测一个键值的变化,一旦键值发生更新,就执行给定命令。

例如,用户更新 testkey 键值。

$ etcdctl exec-watch testkey -- sh -c 'ls'
default.etcd
Documentation
etcd
etcdctl
etcd-migrate
README-etcdctl.md
README.md

1
2
3
4
5
6
7
8

支持的选项包括

--after-index '0'	在指定 index 之前一直监测
--recursive		返回所有的键值和子键值

1
2

# member

通过 list、add、remove 命令列出、添加、删除 etcd 实例到 etcd 集群中。

例如本地启动一个 etcd 服务实例后,可以用如下命令进行查看。

$ etcdctl member list
ce2a822cea30bfca: name=default peerURLs=http://localhost:2380,http://localhost:7001 clientURLs=http://localhost:2379,http://localhost:4001

1
2

# 命令选项

  • --debug 输出 cURL 命令,显示执行命令的时候发起的请求
  • --no-sync 发出请求之前不同步集群信息
  • --output, -o 'simple' 输出内容的格式 (simple 为原始信息,json 为进行json格式解码,易读性好一些)
  • --peers, -C 指定集群中的同伴信息,用逗号隔开 (默认为: "127.0.0.1:4001")
  • --cert-file HTTPS 下客户端使用的 SSL 证书文件
  • --key-file HTTPS 下客户端使用的 SSL 密钥文件
  • --ca-file 服务端使用 HTTPS 时,使用 CA 文件进行验证
  • --help, -h 显示帮助命令信息
  • --version, -v 打印版本信息