K8S 中创建 etcd 集群

16 篇文章 3 订阅
13 篇文章 0 订阅

目标: 单节点 etcd 服务, k8s中启动etcd集群,创建 root 用户, 启用鉴权; 通过命令行客户端访问数据。

K8s 集群内部有内置的 etcd 集群,但这个集群由于是 k8s 内部使用的,出于安全的原因不对外开放,也不建议应用来访问这个集群。

通常使用 etcd 的话,自己创建集群。 开发环境可以使用单进程的服务。

如下第1,2 步是启动单进程的服务(单节点)并创建用户。 第4步是启动 etcd 集群。

# 1.启动服务

etcd

# 2.连接 etcd 服务,创建 root 用户,启用 auth

export ETCDCTL_API=3

ENDPOINTS=localhost:2379

etcdctl --endpoints=${ENDPOINTS} role add root

etcdctl --endpoints=${ENDPOINTS} role get root

etcdctl --endpoints=${ENDPOINTS} user add root

etcdctl --endpoints=${ENDPOINTS} user grant-role root root

etcdctl --endpoints=${ENDPOINTS} user get root

etcdctl --endpoints=${ENDPOINTS} auth enable

# now all client requests go through auth

# 3.Working with users

The user subcommand for etcdctl handles all things having to do with user accounts.

A listing of users can be found with:

$ etcdctl user list

Creating a user is as easy as

$ etcdctl user add myusername

Creating a new user will prompt for a new password. The password can be supplied from standard input when an option --interactive=false is given. --new-user-password can also be used for supplying the password.

Roles can be granted and revoked for a user with:

$ etcdctl user grant-role myusername foo

$ etcdctl user revoke-role myusername bar

The user’s settings can be inspected with:

$ etcdctl user get myusername

And the password for a user can be changed with

$ etcdctl user passwd myusername

$etcdctl --endpoints=${ENDPOINTS} --user=root --password=12345 put current-year 2022

$etcdctl --endpoints=${ENDPOINTS} --user=root --password=12345 get current-year

# 4. 如何创建多实例 etcd 集群

先创建 etcd 集群root账号的密码, create secret for etcd:

kubectl --namespace=my-cluster create secret generic etcd-server --from-literal=etcd-root-password=12345

创建的 Secret name: etcd-server, key: etcd-root-password

helm repo add bitnami https://charts.bitnami.com/bitnami

helm pull bitnami/etcd

mv etcd etch-server

helm install -n my-cluster -f ./etcd-server/values.yaml --set replicaCount=3 etcd-server  etcd-server

每一个实例启动的时候,都会指定  --initial-cluster 参数来配置静态的初始集群:

Clustering Guide | etcd

默认 values.yaml 里面的 auth.rbac.enabled 配置是true, 会设置 root 账号的密码。 前面创建的secret 和 key 是给到 etcd auth 用的。

通过 bitnami 的 etcd helm 启动etcd集群之后,会有提示在集群内可以访问的域名。

** Please be patient while the chart is being deployed **

etcd can be accessed via port 2379 on the following DNS name from within your cluster:

    etcd-server.my-cluster.svc.cluster.local

To create a pod that you can use as a etcd client run the following command:

    kubectl run etcd-server-client --restart='Never' --image docker.io/bitnami/etcd:3.5.1-debian-10-r56 --env ROOT_PASSWORD=$(kubectl get secret --namespace my-cluster etcd-server -o jsonpath="{.data.etcd-root-password}" | base64 --decode) --env ETCDCTL_ENDPOINTS="etcd-server.my-cluster.svc.cluster.local:2379" --namespace my-cluster --command -- sleep infinity

Then, you can set/get a key using the commands below:

    kubectl exec --namespace  my-cluster -it etcd-server-client -- bash

    etcdctl --user root:$ROOT_PASSWORD put /message Hello

    etcdctl --user root:$ROOT_PASSWORD get /message

To connect to your etcd server from outside the cluster execute the following commands:

    kubectl port-forward --namespace my-ecluster svc/etcd-server 2379:2379 &

    echo "etcd URL: http://127.0.0.1:2379"

可以查看 pod 的环境变量:

kubectl --namespace my-cluster describe pods etcd-server-0

ETCD_INITIAL_CLUSTER:              etcd-server-0=http://etcd-server-0.etcd-server-headless.my-cluster.svc.cluster.local:2380,etcd-server-1=http://etcd-server-1.etcd-server-headless.my-cluster.svc.cluster.local:2380,etcd-server-2=http://etcd-server-2.etcd-server-headless.my-cluster.svc.cluster.local:2380

# 5. request cluster ID mismatch 的问题解决

如果遇到 etcd: request cluster ID mismatch 的错误。 说明集群里面有初始的状态信息(脏数据)。

此时etcd节点都已经启动,但是无法连接,有request cluster ID mismatch报错。

找到每个节点的etcd数据存储目录,

# cat /etc/etcd/etcd.conf

删除各节点/var/lib/etcd/default.etcd,重启etcd即可解决。

由于删除的是数据存储目录,不是新建etcd集群,或者有重要数据的不可直接删除。

也可以uninstall etcd deployment, 然后清理 PVC, PV。 再重新指定 replicaCount 数量部署集群.

# 6.如何干净地清理 kubernetes 集群上的 etcd ?

如果要彻底 delete/uninstall etcd 集群,uninstall 之后还要清理pvc, pv。 否则账户角色信息还会有留存。

kubectl --namespace my-cluster get pvc

kubectl --namespace my-cluster get pv

kubectl --namespace my-cluster delete pvc xxxx zzzz

kubectl --namespace my-cluster delete pv xxxx zzzz

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Kubernetes (k8s) 集群Etcd 是作为核心组件之一的分布式键值存储系统,用于存储和管理集群配置数据和服务发现信息。以下是搭建 Etcd 集群的基本步骤: 1. **下载安装**: 首先需要从 Etcd 的官方 GitHub 仓库下载最新版本的二进制文件。你可以选择适合你的操作系统(如 Linux 或 macOS)的版本。 2. **设置环境变量**: 创建一个 `.bashrc` 或者 `.zshrc` 文件,并添加 Etcd 的路径到 `PATH` 变量,便于后续命令行操作。 3. **初始化集群**: 对每个节点运行 Etcd 后台服务,使用 `-name` 参数指定本节点名称,例如: ``` etcd -name node1 --initial-cluster="node1=http://localhost:2380,node2=http://localhost:2381,node3=http://localhost:2382" ``` 这里创建了一个包含三个节点的集群,假设它们都在本地主机上运行。实际部署,你需要使用对应的 IP 地址和端口。 4. **启动集群**: 分别在各个节点上执行上述命令,当所有节点都启动后,Etcd 集群会自动选举领导者并开始同步数据。 5. **验证集群状态**: 使用 `etcdctl` 工具连接到任意节点检查集群健康状况和数据一致性。 6. **配置 Kubernetes**: 在 k8s ,你需要将 Etcd 集群的地址添加到 `kubelet` 和 `kubectl` 的配置,通过修改 `/etc/kubernetes/manifests/kube-apiserver.yaml` 和 `/etc/kubernetes/manifests/kube-controller-manager.yaml` 等配置文件。 7. **重启服务**: 重启 k8s 控制平面(如 kube-apiserver、kube-controller-manager 等),让它们能够连接到新配置的 Etcd 集群
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值