本节主要学习Etcd的操作使用

c135f9accac5f430846ae6e115cf9e47.png

图1:Kubernetes之Log

1、查看帮助

[root@k8s-master ~]# etcdctl --help
e117c3966ca3828e1be0b1735d0391bb.png

图2:Etcd的帮助

2、set

设置某个键的值

格式:etcdctl set

--ttl '0': 设置该键值的超时时间(秒),不配置(默认0)则永不超时

例如:

[root@k8s-master ~]# etcdctl --ca-file=/usr/local/kubernetes/ssl/ca.pem --cert-file=/usr/local/kubernetes/ssl/etcd.pem --key-file=/usr/local/kubernetes/ssl/etcd-key.pem --endpoints=https://192.168.1.1:2379,https://192.168.1.2:2379,https://192.168.1.3:2379  set /data/key1 "hello world"

3、get

获取指定键的值

格式:etcdctl get

例如:

[root@k8s-master ~]# etcdctl --ca-file=/usr/local/kubernetes/ssl/ca.pem --cert-file=/usr/local/kubernetes/ssl/etcd.pem --key-file=/usr/local/kubernetes/ssl/etcd-key.pem --endpoints=https://192.168.1.1:2379,https://192.168.1.2:2379,https://192.168.1.3:2379  get /data/key1

由于是Etcd群集,在其他节点也能获取指定键的值

例如:

[root@k8s-node-1 ~]# etcdctl --ca-file=/usr/local/kubernetes/ssl/ca.pem --cert-file=/usr/local/kubernetes/ssl/etcd.pem --key-file=/usr/local/kubernetes/ssl/etcd-key.pem --endpoints=https://192.168.1.1:2379,https://192.168.1.2:2379,https://192.168.1.3:2379  get /data/key1

4、update

当键存在的时候,更新值(不存在的时候会报错)

格式:etcdctl update

[root@k8s-master ~]# etcdctl --ca-file=/usr/local/kubernetes/ssl/ca.pem --cert-file=/usr/local/kubernetes/ssl/etcd.pem --key-file=/usr/local/kubernetes/ssl/etcd-key.pem --endpoints=https://192.168.1.1:2379,https://192.168.1.2:2379,https://192.168.1.3:2379  update /data/key1 "python java"[root@k8s-master ~]# etcdctl --ca-file=/usr/local/kubernetes/ssl/ca.pem --cert-file=/usr/local/kubernetes/ssl/etcd.pem --key-file=/usr/local/kubernetes/ssl/etcd-key.pem --endpoints=https://192.168.1.1:2379,https://192.168.1.2:2379,https://192.168.1.3:2379  get /data/key1

5、rm

删除某个键值

格式:etcdctl rm

[root@k8s-master ~]# etcdctl --ca-file=/usr/local/kubernetes/ssl/ca.pem --cert-file=/usr/local/kubernetes/ssl/etcd.pem --key-file=/usr/local/kubernetes/ssl/etcd-key.pem --endpoints=https://192.168.1.1:2379,https://192.168.1.2:2379,https://192.168.1.3:2379  rm /data/key1

6、mk

创建一个新的键值

格式:etcdctl mk

[root@k8s-master ~]# etcdctl --ca-file=/usr/local/kubernetes/ssl/ca.pem --cert-file=/usr/local/kubernetes/ssl/etcd.pem --key-file=/usr/local/kubernetes/ssl/etcd-key.pem --endpoints=https://192.168.1.1:2379,https://192.168.1.2:2379,https://192.168.1.3:2379  mk /data/key1 "hello world"

7、操作目录

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

setdir:创建一个键目录

updatedir:更新一个已经存在的目录

rmdir:删除一个空目录

ls:列出目录(默认是跟目录)下的键或者子目录

例如:

[root@k8s-master ~]# etcdctl --ca-file=/usr/local/kubernetes/ssl/ca.pem --cert-file=/usr/local/kubernetes/ssl/etcd.pem --key-file=/usr/local/kubernetes/ssl/etcd-key.pem --endpoints=https://192.168.1.1:2379,https://192.168.1.2:2379,https://192.168.1.3:2379  mkdir /backup[root@k8s-master ~]# etcdctl --ca-file=/usr/local/kubernetes/ssl/ca.pem --cert-file=/usr/local/kubernetes/ssl/etcd.pem --key-file=/usr/local/kubernetes/ssl/etcd-key.pem --endpoints=https://192.168.1.1:2379,https://192.168.1.2:2379,https://192.168.1.3:2379  mkdir /bakcup/a[root@k8s-master ~]# etcdctl --ca-file=/usr/local/kubernetes/ssl/ca.pem --cert-file=/usr/local/kubernetes/ssl/etcd.pem --key-file=/usr/local/kubernetes/ssl/etcd-key.pem --endpoints=https://192.168.1.1:2379,https://192.168.1.2:2379,https://192.168.1.3:2379  mkdir /bakcup/b[root@k8s-master ~]# etcdctl --ca-file=/usr/local/kubernetes/ssl/ca.pem --cert-file=/usr/local/kubernetes/ssl/etcd.pem --key-file=/usr/local/kubernetes/ssl/etcd-key.pem --endpoints=https://192.168.1.1:2379,https://192.168.1.2:2379,https://192.168.1.3:2379  mkdir /bakcup/c [root@k8s-master ~]# etcdctl --ca-file=/usr/local/kubernetes/ssl/ca.pem --cert-file=/usr/local/kubernetes/ssl/etcd.pem --key-file=/usr/local/kubernetes/ssl/etcd-key.pem --endpoints=https://192.168.1.1:2379,https://192.168.1.2:2379,https://192.168.1.3:2379  ls /bakcup/
864fe0382a20800b68f918bbd9de8728.png

图3:操作目录

8、backup

(1)备份etcd数据

1、--data-dir: etcd的数据目录

2、--backup-dir: 备份到指定路径

[root@k8s-master ~]# etcdctl --ca-file=/usr/local/kubernetes/ssl/ca.pem --cert-file=/usr/local/kubernetes/ssl/etcd.pem --key-file=/usr/local/kubernetes/ssl/etcd-key.pem --endpoints=https://192.168.1.1:2379,https://192.168.1.2:2379,https://192.168.1.3:2379 backup --data-dir=/var/lib/etcd/default.etcd --backup-dir=/backup/default.bak

查看备份:

3c20ad1a4828871edbfe41ac55819860.png

图4:Etcd的备份

(2)使用备份数据启动etcd

[root@k8s-master ~]# etcd --data-dir=/backup/default.bak --force-new-cluster
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐