ETCD 使用

访问ETCD要使用证书

k8s现在使用的是etcd v3,必须提供ca、key、cert,否则会出现Error: context deadline exceeded

不加--endpoint参数时,默认访问的127.0.0.1:2379,而使用--endpoint参数时,必须提供ca,key,cert。

[root@k8s-test2 ~]# etcdctl endpoint health 
127.0.0.1:2379 is healthy: successfully committed proposal: took = 939.097µs

[root@k8s-test2 ~]# etcdctl --endpoints=https://10.0.26.152:2379 endpoint health 
https://10.0.26.152:2379 is unhealthy: failed to connect: context deadline exceeded

[root@k8s-test2 ~]# etcdctl --endpoints=https://10.0.26.152:2379 --cacert=/etc/k8s/ssl/etcd-root-ca.pem --key=/etc/k8s/ssl/etcd-key.pem  --cert=/etc/k8s/ssl/etcd.pem  endpoint health 
https://10.0.26.152:2379 is healthy: successfully committed proposal: took = 1.001505ms

注意:使用etcd v3的版本时,需要设置环境变量ETCDCTL_API=3(写入/etc/profile或者.bash_profile文件中)

否则,默认使用的是ETCDCTL_API=3。或者,使用命令式显示声明ETCDCTL_API=3。

Ex:

ETCDCTL_API=3 etcdctl get /registry/namespaces --prefix -w=json|python -m json.tool

目前发现,获取etcd中flannel的配置,只能使用ETCDCTL_API=2,而获取etcd中kubernetes的元数据,只能使用ETCDCTL_API=3

获取etcd中flannel的配置

[root@k8s-test2 ~]# ETCDCTL_API=2 etcdctl --endpoints=https://10.0.26.152:2379 --cert-file=/etc/k8s/ssl/etcd.pem --key-file=/etc/k8s/ssl/etcd-key.pem --ca-file=/etc/k8s/ssl/etcd-root-ca.pem ls /kube-centos/network/subnets
/kube-centos/network/subnets/172.18.78.0-24
/kube-centos/network/subnets/172.18.18.0-24

获取etcd中kubernetes的元数据

使用脚本


#!/bin/bash
# Get kubernetes keys from etcd
export ETCDCTL_API=3
keys=`etcdctl get /registry --prefix -w json|python -m json.tool|grep key|cut -d ":" -f2|tr -d '"'|tr -d ","`
for x in $keys;do
  echo $x|base64 -d|sort
done

注: key的值是经过base64编码,需要解码后才能看到实际值

注: 使用--prefix可以看到所有的子目录

注: ETCDCTL_API=2和ETCDCTL_API=3两个API版本中证书的参数不一样。

猜你喜欢

转载自blog.csdn.net/feifei3851/article/details/83410456