zoukankan      html  css  js  c++  java
  • etcd学习之安装与命令

    ETCD学习

    下载etcd

    #下载
    wget https://github.com/etcd-io/etcd/releases/download/v3.3.18/etcd-v3.3.18-linux-amd64.tar.gz
    #解压
    tar zxvf etcd-v3.3.18-linux-amd64.tar.gz
    cd etcd-v3.3.18-linux-amd64.tar.gz
    #复制到用户目录
    cp etcd etcdctl /usr/local/bin
    #赋予执行权限
    chmod +x etcd
    chmod +x etcdctl
    

    启动etcd

    本人是作为练习,将etcd暴露在公网,可以直接通过ip访问,一般不建议这么做

    ./etcd --listen-client-urls 'http://0.0.0.0:2379' --advertise-client-urls 'http://0.0.0.0:2379'
    # 后台启动 
    #nohup etcd --listen-client-urls 'http://0.0.0.0:2379' --advertise-client-urls 'http://0.0.0.0:2379' &
    

    设置V3环境变量(默认V2)

    export ETCDCTL_API=3
    #或者永久设置 vim /etc/profile 在最后添加export ETCDCTL_API=3  然后source /etc/profile
    

    操作etcd (参考文档)

    Key-value 命令

    PUT [options] <key> <value>

    PUT设置kv键值对,如果k已经存在,则进行覆盖

    options可选项

    • lease -- lease ID(16进制的租约ID)关联到key上
    • prev-kv -- 返回修改前的前一个键值对
    • ignore-value -- 使用其当前值更新key
    • ignore-lease -- 使用其当前租约更新key

    输出

    OK

    示例

    # 设置
    etcdctl put foo bar
    # ok
    
    #读取
    etcdctl get foo
    # foo
    # bar
    
    # 设置新kv并读取前一次的kv
    etcdctl put foo bar1 --prev-kv
    # OK
    # foo
    # bar
    
    # 读取
    etcdctl get foo
    # foo
    # bar1
    
    # 给key添加租约(需要提前申请租约)
    etcdctl put foo bar --lease=1234abcd
    # 报错 etcdserver: requested lease not found。需要先申请一个租约
    
    # 申请一个500秒的租约
    etcdctl lease grant 500
    
    # 给key添加租约
    #lease 694d6ed6e8fed50f granted with TTL(500s)
    etcdctl put foo bar --lease=694d6ed6e8fed50f
    # OK
    
    # 使用其当前租约更新key(为了使用现有租约)
    etcdctl put foo bar4 --ignore-lease
    # ok
    etcdctl get foo
    # foo
    # bar4
    
    # 使用当前值更新kv(为了解除租约,保持值不变)
    etcdctl put foo --ignore-value
    # OK
    

    备注

    如果未将作为命令行参数给出,则此命令尝试从标准输入中读取值。
    如果value是值包含"-",会被解析为一个标志位,这个时候输入需要"--"解决

    etcdctl put foo -bar4
    # Error: unknown shorthand flag: 'b' in -bar4
    
    etcdctl put foo -- -bar4
    etcdctl put -- foo -bar5
    # OK
    

    可以具有多行或空格,但是必须使用双引号包起来,如下所示:

    etcdctl put foo "bar 1 2 3"
    # ok
    
    etcdctl get foo
    # foo
    # bar 1 2 3
    

    GET [options] <key> [range_end]

    如果指定了range_end,则GET会获取键或键范围[key,range_end)左闭右开

    • hex -- 以16进制编码输出kv

    • limit -- 限制结果集最大数量

    • prefix -- 获取相同前缀的key

    • order -- 以升序或者降序对结果进行排序

    • sort-by -- 根据创建时间、Key、修改时间、值、版本进行目标排序

    • rev -- 指定版本

    • print-value-only -- 与write-out = simple一起使用时仅打印值

    • consistency -- 线性一致性或序列一致性

    • from-key -- 从指定Key开始往后查找(根据字节比较)

    • keys-only -- 只读取key

    Output

    <key>

    <value>

    <next_key>

    <next_value>

    ...

    Examples

    首先设置一些Key

    etcdctl put foo bar
    # OK
    etcdctl put foo1 bar1
    # OK
    etcdctl put foo2 bar2
    # OK
    etcdctl put foo3 bar3
    # OK
    

    读取key为foo的值

    etcdctl get foo
    # foo
    # bar
    

    读取key为foo的值,并且进行16进制编码

    etcdctl get foo --hex
    # x66x6fx6f
    # x62x61x72
    

    读取key前缀为foo的值

    etcdctl get foo --prefix
    # foo
    # bar
    # foo1
    # bar1
    # foo2
    # foo2
    # foo3
    # bar3
    

    读取key前缀为foo的值并且倒序 (默认为ASCEND)

    etcdctl get foo --prefix --order="DESCEND"
    # foo3
    # bar3
    # foo2
    # bar2
    # foo1
    # bar1
    # foo
    # bar
    

    读取所有key的值

    etcdctl get --from-key ''
    # foo
    # bar
    # foo1
    # bar1
    # foo2
    # foo2
    # foo3
    # bar3
    

    根据创建时间读取所有key的值

    etcdctl get foo --from-key '' --sort-by=CREATE
    # foo
    # bar
    # foo1
    # bar1
    # foo2
    # foo2
    # foo3
    # bar3
    

    读取指定版本的kv

     etcdctl get foo --rev=0
    # foo
    # bar
    

    只读取指定版本的v

    etcdctl get foo --print-value-only
    # foo
    

    只读取指定版本的k

    etcdctl get foo --keys-only
    # foo
    # 
    

    读取key的字节排序值大于等于foo1的所有key

    etcdctl get --from-key foo1
    # foo1
    # bar1
    # foo2
    # bar2
    # foo3
    # bar3
    

    读取key的字节排序值大于等于foo1并且小于foo3的所有key

    etcdctl get foo1 foo3
    # foo1
    # bar1
    # foo2
    # bar2    
    

    读取key的字节排序值大于等于foo1的所有key的前2条

    etcdctl get --from-key foo1 --limit 2
    # foo1
    # bar1
    # foo2
    # bar2
    

    备注

    如果任何键或值包含不可打印的字符或控制字符,则由于换行而导致简单格式化的输出可能不明确。 为了解决这个问题,设置--hex十六进制编码所有字符串。

    DEL [options] <key> [range_end]

    如果指定了range_end,则删除指定的键或键范围[key,range_end)左闭右开

    Options

    • prefix -- 删除指定前缀的key

    • prev-kv -- 删除后返回kv值

    • from-key -- 从指定Key开始往后删除(根据字节比较)

    删除指定key

    etcdctl put foo bar
    # OK
    etcdctl del foo
    # 1
    etcdctl get foo
    

    删除指定key并返回kv的值

    etcdctl del foo1 --prev-kv
    # 1
    # foo1
    # bar1
    

    从指定key开始删除

    etcdctl del --from-key foo
    # 2
    

    删除以foo为前缀的key

    etcdctl del foo --prefix
    # 2
    

    TNX[options] (事务)

    TXN从标准输入读取多个etcd请求,并将它们作为单个原子事务应用。

    可选性

    • hex -- 输出16进制编码字符串

    • interactive -- 根据互动提示输入事务程序

    输入格式

    <Txn> ::= <CMP>* "
    " <THEN> "
    " <ELSE> "
    "
    <CMP> ::= (<CMPCREATE>|<CMPMOD>|<CMPVAL>|<CMPVER>|<CMPLEASE>) "
    "
    <CMPOP> ::= "<" | "=" | ">"
    <CMPCREATE> := ("c"|"create")"("<KEY>")" <CMPOP> <REVISION>
    <CMPMOD> ::= ("m"|"mod")"("<KEY>")" <CMPOP> <REVISION>
    <CMPVAL> ::= ("val"|"value")"("<KEY>")" <CMPOP> <VALUE>
    <CMPVER> ::= ("ver"|"version")"("<KEY>")" <CMPOP> <VERSION>
    <CMPLEASE> ::= "lease("<KEY>")" <CMPOP> <LEASE>
    <THEN> ::= <OP>*
    <ELSE> ::= <OP>*
    <OP> ::= ((see put, get, del etcdctl command syntax)) "
    "
    <KEY> ::= (%q formatted string)
    <VALUE> ::= (%q formatted string)
    <REVISION> ::= """[0-9]+"""
    <VERSION> ::= """[0-9]+"""
    <LEASE> ::= """[0-9]+""
    

    输出

    SUCCESS 如果etcd成功处理了事务,
    FAILURE 事务处理失败

    示例

    交互模式 如果确定了就要连续按两次enter,进行下一步

    etcdctl put key1 1
    etcdctl txn -i
    # compares:
    mod("key1") > "0"
    
    # success requests (get, put, delete):
    put key1 "overwrote-key1"
    
    # failure requests (get, put, delete):
    put key1 "created-key1"
    put key2 "some extra key"
    
    # SUCCESS
    
    # OK
    etcdctl get key --prefix
    
    key1
    overwrote-key1
    key2
    some extra key
    # OK
    

    非交互模式

    ./etcdctl txn <<<'mod("key1") > "0"
    
    put key1 "overwrote-key1"
    
    put key1 "created-key1"
    put key2 "some extra key"
    
    '
    
    # FAILURE
    
    # OK
    
    # OK
    

    备注

    在TXN命令中使用多行值时,换行符必须表示为 n。 文字换行符将导致解析失败。

    COMPACTION [options] <revision> (压缩)

    COMPACTION丢弃给定修订版之前的所有etcd事件历史记录。 由于etcd使用多版本并发控制模型,因此它将所有关键更新保留为事件历史记录。 当不再需要修订的事件历史记录时,可以将所有被替换的键压缩以回收etcd后端数据库中的存储空间。

    physical -为true以等待压缩以物理删除所有旧修订

    输出

    打印压缩的版本

    示例

    etcdctl compaction 1
    # compacted revision 1
    

    WATCH [options] [key or prefix] [range_end] [--] [exec-command arg1 arg2 ...] (监听)

    可选性

    • hex -- 十六进制打印

    • interactive -- 开启交互式session监听

    • prefix -- 监听包含前缀的key

    • prev-kv -- kv改变获取前一个kv

    • rev -- 监听版本

    输入格式

    仅交互式模式接受输入。

    watch [options] <key or prefix>
    
    

    输出

    <event>

    [

    <old_key>

    <old_value>

    ]

    <key>

    <value>

    ...

    示例

    非交互模式

    输入etcdctl watch foo命令开始监听,当监听的key有变化的时候,打印出 事件 key value

    # bash1
    etcdctl watch foo
    
    # bash2
    etcdctl put foo bar
    # ok
    
    # bash1
    # PUT
    # foo
    # bar
    

    LEASE <subcommand> (租约)

    LEASE提供了租约管理的命令集合

    LEASE GRANT <ttl>

    LEASE GRANT使用服务器选择的生存时间(以秒为单位)创建新的租约大于或等于请求的TTL值。

    输出

    Prints a message with the granted lease ID.

    Example

    etcdctl lease grant 60
    # lease 694d6ed6e8fed554 granted with TTL(60s)
    

    LEASE REVOKE <leaseID>

    租借撤销会销毁给定的租约,并删除所有附加的key。

    Output

    打印租约被撤销的消息

    示例

    etcdctl lease revoke 694d6ed6e8fed558
    # lease 694d6ed6e8fed558 revoked
    

    LEASE TIMETOLIVE <leaseID> [options]

    LEASE TIMETOLIVE 检索具有给定lease ID的租约信息。

    可选项

    • keys -- 获取在这个租约上的keys

    输出

    打印租约信息

    示例

    # 申请一个租约
    etcdctl lease grant 500
    # lease 694d6ed6e8fed55b granted with TTL(500s)
    
    # 将租约附加到kv上
    etcdctl put foo1 bar --lease=694d6ed6e8fed55b
    # OK
    
    # 将租约附加到kv上
    etcdctl put foo2 bar --lease=694d6ed6e8fed55b
    # OK
    
    # 查看租约的信息
    etcdctl lease timetolive 694d6ed6e8fed55b
    # lease 694d6ed6e8fed55b granted with TTL(500s), remaining(386s)
    
    # 查看租约的信息和签约的keys
    etcdctl lease timetolive 2d8257079fa1bc0c --keys
    # lease 694d6ed6e8fed55b granted with TTL(500s), remaining(352s), attached keys([foo1 foo2])
    
    # 查看租约的信息,以json输出
    etcdctl lease timetolive 2d8257079fa1bc0c --write-out=json
    # {"cluster_id":14841639068965178418,"member_id":10276657743932975437,"revision":31,"raft_term":2,"id":7587842816500225371,"ttl":321,"granted-ttl":500,"keys":null}
    
    # 查看租约的信息和签约的keys,以json输出
    etcdctl lease timetolive 2d8257079fa1bc0c --write-out=json --keys
    # {"cluster_id":14841639068965178418,"member_id":10276657743932975437,"revision":31,"raft_term":2,"id":7587842816500225371,"ttl":289,"granted-ttl":500,"keys":["Zm9vMQ==","Zm9vMg=="]}
    
    # 租约过期
    etcdctl lease timetolive 694d6ed6e8fed55b
    # lease 694d6ed6e8fed55b already expired
    

    LEASE LIST

    LEASE LIST 列出所有有效的租约

    输出

    有效租约清单

    示例

    etcdctl lease grant 500
    # lease 694d6ed6e8fed560 granted with TTL(500s)
    
    etcdctl lease list
    # found 1 leases
    # 694d6ed6e8fed560
    

    LEASE KEEP-ALIVE <leaseID>

    LEASE KEEP-ALIVE 定期刷新租约使其不过期

    Output

    为发送的每个保持活动状态打印一条消息,或打印一条消息,表明租约已到期。

    Example

    # 起一个10秒生存期的租约
    etcdctl lease grant 10
    # lease 694d6ed6e8fed562 keepalived with TTL(10)
    # lease 694d6ed6e8fed562 keepalived with TTL(10)
    # lease 694d6ed6e8fed562 keepalived with TTL(10)
    # lease 694d6ed6e8fed562 keepalived with TTL(10)
    # lease 694d6ed6e8fed562 keepalived with TTL(10)
    ...
    
  • 相关阅读:
    均匀采样单位圆
    3Sum
    查看SQL语句在SQL Server上的执行时间
    ASP.NET页面请求处理
    原型模式
    ASP.NET页面错误处理
    电子商务推荐位商品模型设计
    HttpModule与HttpHandler使用
    装饰者模式
    ASP.NET编程模型
  • 原文地址:https://www.cnblogs.com/zhouqi666/p/11996732.html
Copyright © 2011-2022 走看看