Skip to the content.

MicroK8s Mayastor Etcd 集群手动恢复步骤

仅在 MicroK8s Mayastor v1.0.0 环境验证,v2.0.0 版本尚未测试。请先在测试环境演练并做好数据备份。

背景

测试环境的 MicroK8s 集群因为未管理,节点长期处于内存过载状态,Mayastor 所依赖的所有 etcd Pod 均被驱逐并删除,etcd-operator-mayastor自动修复流程也随之失效。但在每个节点的主机磁盘上仍然保留着对应的 etcd 数据目录。通过手动重建一个满足 EtcdCluster 期望的 Pod,并让该 Pod 关联原有的 EtcdCluster UID,可以触发 operator 自动收敛,恢复整个 etcd 集群。

恢复流程概览

  1. 确认残留数据目录完好无损。
  2. 获取 Mayastor EtcdCluster 的当前 UID 与存储卷信息。
  3. 在目标节点上手动创建一个与 operator 约定一致、挂载残留数据的 etcd Pod。
  4. 为 Pod 打上 metadata.ownerReferences.uid 等 operator 识别所需的标签/注解。
  5. 等待 etcd-operator-mayastor 侦测到 Pod 恢复,并自动重新拉起其它副本。

以下步骤以默认命名空间与 StorageClass 为例,实际操作时请根据环境调整。

详细步骤

1. 检查节点上的 etcd 数据

在原 etcd Pod 所在节点上确认数据目录仍然存在,例如:

sudo ls -l /var/snap/microk8s/common/mayastor/etcd/<POD_UID>/data

确保该目录下包含 member/wal/ 等子目录且时间戳合理。如果多个节点都有备份,优先选择最新且完整的一份。

2. 获取 EtcdCluster 信息

kubectl -n mayastor get etcdcluster etcd -o yaml

记录以下字段:

这些信息稍后用于构造新的 Pod。

3. 构造手动恢复 Pod 的 YAML

参考原 EtcdCluster 的模板,创建一个临时文件 manual-etcd-restore.yaml

apiVersion: v1
kind: Pod
metadata:
  annotations:
    etcd.version: 3.5.4
  labels:
    app: etcd
    etcd_cluster: etcd
    etcd_node: etcd-5txbwfrkvf
  name: etcd-5txbwfrkvf
  namespace: mayastor
  ownerReferences:
  - apiVersion: etcd.database.canonical.com/v1beta3
    controller: true
    kind: EtcdCluster
    name: etcd
    uid: 7a3b3578-e66c-45b5-b9d8-1970bf2f4530
spec:
  affinity:
    podAntiAffinity:
      requiredDuringSchedulingIgnoredDuringExecution:
      - labelSelector:
          matchExpressions:
          - key: etcd_cluster
            operator: In
            values:
            - etcd
        topologyKey: kubernetes.io/hostname
  automountServiceAccountToken: false
  containers:
  - command:
    - /usr/local/bin/etcd
    - --data-dir=/var/etcd/data
    - --name=etcd-5txbwfrkvf
    - --initial-advertise-peer-urls=http://etcd-5txbwfrkvf.etcd.mayastor.svc:2380
    - --listen-peer-urls=http://0.0.0.0:2380
    - --listen-client-urls=http://0.0.0.0:2379
    - --advertise-client-urls=http://etcd-5txbwfrkvf.etcd.mayastor.svc:2379
    - --initial-cluster=etcd-w8g6hbfz64=http://etcd-w8g6hbfz64.etcd.mayastor.svc:2380,etcd-5txbwfrkvf=http://etcd-5txbwfrkvf.etcd.mayastor.svc:2380,etcd-qm5r6tt7pv=http://etcd-qm5r6tt7pv.etcd.mayastor.svc:2380
    - --initial-cluster-state=existing
    image: quay.io/coreos/etcd:v3.5.4
    livenessProbe:
      exec:
        command:
        - /bin/sh
        - -ec
        - ETCDCTL_API=3 etcdctl endpoint status
      failureThreshold: 3
      initialDelaySeconds: 10
      periodSeconds: 60
      successThreshold: 1
      timeoutSeconds: 10
    name: etcd
    ports:
    - containerPort: 2380
      name: server
      protocol: TCP
    - containerPort: 2379
      name: client
      protocol: TCP
    readinessProbe:
      exec:
        command:
        - /bin/sh
        - -ec
        - ETCDCTL_API=3 etcdctl endpoint status
      failureThreshold: 3
      initialDelaySeconds: 1
      periodSeconds: 5
      successThreshold: 1
      timeoutSeconds: 5
    volumeMounts:
    - mountPath: /var/etcd
      name: etcd-data
  hostname: etcd-5txbwfrkvf
  initContainers:
  - command:
    - /bin/sh
    - -c
    - "\n\t\t\t\t\tTIMEOUT_READY=0\n\t\t\t\t\twhile ( ! nslookup etcd-5txbwfrkvf.etcd.mayastor.svc
      )\n\t\t\t\t\tdo\n\t\t\t\t\t\t# If TIMEOUT_READY is 0 we should never time out
      and exit\n\t\t\t\t\t\tTIMEOUT_READY=$(( TIMEOUT_READY-1 ))\n                        if
      [ $TIMEOUT_READY -eq 0 ];\n\t\t\t\t        then\n\t\t\t\t            echo \"Timed
      out waiting for DNS entry\"\n\t\t\t\t            exit 1\n\t\t\t\t        fi\n\t\t\t\t\t\tsleep
      1\n\t\t\t\t\tdone"
    image: busybox:1.28.4
    name: check-dns
  nodeName: live2-txmall-1
  subdomain: etcd
  volumes:
  - hostPath:
      path: /var/snap/microk8s/common/mayastor/etcd/etcd-5txbwfrkvf
      type: DirectoryOrCreate
    name: etcd-data

关键点:

4. 创建 Pod 并观察日志

kubectl apply -f manual-etcd-restore.yaml
kubectl logs -n mayastor etcd-cluster-restore-0

确认 etcd 正常启动且没有出现 data corruption、member mismatch 等错误。如果日志正常,可以进入下一步;如报错需重新检查数据目录是否完整。

5. 等待 operator 自愈

当 Pod 正常运行并打上正确的标签后,etcd-operator-mayastor 会检测到已有一个成员在线,并开始调谐 EtcdCluster

在数分钟内应能看到 operator 创建新的 etcd 成员。全部副本 Ready 后,再次确认集群状态:

kubectl get etcdcluster -n mayastor etcd-cluster

额外建议