1. Operator etcd
Operator 是对 Kubernetes API 的一种扩展,支持用户定义一些 API 接口,及 CRD (customresourcedefinitions)。
etcd-Operator 能够解决一些部署和运维上的问题,例如可以自动创建高可用集群,自动化的故障恢复等。
etcd-operator 是基于 etcd 的 dns discovery 部署的集群。
1.1. 执行rbac
获取git 项目代码 https://github.com/coreos/etcd-operator/
创建 etcd-operator 及 etcd 集群运行依赖的权限信息
$ example/rbac/create_role.sh
1.2. 安装 Operator
在 default 的 namespace 中创建一个 etc-operator 的 deployment。
$ kubectl create -f example/deployment.yaml
该 deployment 会自动创建 CRD 相关的资源
$ kubectl get customresourcedefinitions
NAME KIND
etcdclusters.etcd.database.coreos.com CustomResourceDefinition.v1beta1.apiextensions.k8s.io
1.3. 创建 etcd 集群实例
有了 CRD 资源后,就可以创建出用户要使用的 etcd 集群。
$ kubectl create -f example/example-etcd-cluster.yaml
1.4. 查看集群状态
查看
$ kubectl get pod
NAME READY STATUS RESTARTS AGE
etcd-operator-85f7494fcf-zlsl4 1/1 Running 0 8m44s
example-etcd-cluster-ghszjmjhtk 1/1 Running 0 2m26s
example-etcd-cluster-mpfxvcwqml 1/1 Running 0 3m14s
example-etcd-cluster-qtt28lhhsg 1/1 Running 0 4m2s
查看 service
$ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
example-etcd-cluster ClusterIP None <none> 2379/TCP,2380/TCP 3d4h
example-etcd-cluster-client ClusterIP 10.178.75.231 <none> 2379/TCP 3d4h
查看集群成员列表
$ kubectl exec -it example-etcd-cluster-ghszjmjhtk sh
/ # ETCDCTL_API=3 etcdctl --endpoints http://127.0.0.1:2379 member list -w table
+------------------+---------+---------------------------------+------------------------------------------------------------------------------+------------------------------------------------------------------------------+
| ID | STATUS | NAME | PEER ADDRS | CLIENT ADDRS |
+------------------+---------+---------------------------------+------------------------------------------------------------------------------+------------------------------------------------------------------------------+
| 4fc39a0963e21b3a | started | example-etcd-cluster-mpfxvcwqml | http://example-etcd-cluster-mpfxvcwqml.example-etcd-cluster.default.svc:2380 | http://example-etcd-cluster-mpfxvcwqml.example-etcd-cluster.default.svc:2379 |
| 6c8c137284cff6a5 | started | example-etcd-cluster-ghszjmjhtk | http://example-etcd-cluster-ghszjmjhtk.example-etcd-cluster.default.svc:2380 | http://example-etcd-cluster-ghszjmjhtk.example-etcd-cluster.default.svc:2379 |
| 8aba046df47407c2 | started | example-etcd-cluster-qtt28lhhsg | http://example-etcd-cluster-qtt28lhhsg.example-etcd-cluster.default.svc:2380 | http://example-etcd-cluster-qtt28lhhsg.example-etcd-cluster.default.svc:2379 |
+------------------+---------+---------------------------------+------------------------------------------------------------------------------+------------------------------------------------------------------------------+
查看集群状态
$ kubectl exec -it example-etcd-cluster-ghszjmjhtk sh
/ # ETCDCTL_API=3 etcdctl --endpoints http://example-etcd-cluster-mpfxvcwqml.example-etcd-cluster.default.svc:2379,http://example-etcd-cluster-ghszjmjhtk.example-et
cd-cluster.default.svc:2379,http://example-etcd-cluster-qtt28lhhsg.example-etcd-cluster.default.svc:2379 endpoint status -w table
+------------------------------------------------------------------------------+------------------+---------+---------+-----------+-----------+------------+
| ENDPOINT | ID | VERSION | DB SIZE | IS LEADER | RAFT TERM | RAFT INDEX |
+------------------------------------------------------------------------------+------------------+---------+---------+-----------+-----------+------------+
| http://example-etcd-cluster-mpfxvcwqml.example-etcd-cluster.default.svc:2379 | 4fc39a0963e21b3a | 3.2.13 | 25 kB | false | 29 | 9 |
| http://example-etcd-cluster-ghszjmjhtk.example-etcd-cluster.default.svc:2379 | 6c8c137284cff6a5 | 3.2.13 | 25 kB | false | 29 | 9 |
| http://example-etcd-cluster-qtt28lhhsg.example-etcd-cluster.default.svc:2379 | 8aba046df47407c2 | 3.2.13 | 25 kB | true | 29 | 9 |
+------------------------------------------------------------------------------+------------------+---------+---------+-----------+-----------+------------+
注: 由于 etcd 3.2.13 版本的 endpoint status 命令没有 --cluster 参数,因此需要填写全部的 endpoints
写入数据
$ kubectl exec -it example-etcd-cluster-ghszjmjhtk sh
/ # ETCDCTL_API=3 etcdctl --endpoints http://example-etcd-cluster-mpfxvcwqml.example-etcd-cluster.default.svc:2379,http://example-etcd-cluster-ghszjmjhtk.example-et
cd-cluster.default.svc:2379,http://example-etcd-cluster-qtt28lhhsg.example-etcd-cluster.default.svc:2379 put operator-key operator-value
OK
读取数据
$ kubectl exec -it example-etcd-cluster-ghszjmjhtk sh
/ # ETCDCTL_API=3 etcdctl --endpoints http://example-etcd-cluster-mpfxvcwqml.example-etcd-cluster.default.svc:2379,http://example-etcd-cluster-ghszjmjhtk.example-et
cd-cluster.default.svc:2379,http://example-etcd-cluster-qtt28lhhsg.example-etcd-cluster.default.svc:2379 get operator-key
operator-key
operator-value