Kubernetes控制平面组件:etcd高可用集群搭建

云原生学习路线导航页(持续更新中)

  • kubernetes学习系列快捷链接
    • Kubernetes架构原则和对象设计(一)
    • Kubernetes架构原则和对象设计(二)
    • Kubernetes架构原则和对象设计(三)
    • Kubernetes控制平面组件:etcd(一)
    • Kubernetes控制平面组件:etcd(二)
    • Kubernetes控制平面组件:etcd常用配置参数
    • kubectl 和 kubeconfig 基本原理
    • kubeadm 升级 k8s集群 1.17到1.20
    • Kubernetes常见问题解答
    • 查看云机器的一些常用配置
  • 本文将给出 ETCD 高可用集群的搭建方法,并演示如何进行数据备份、数据恢复、集群停机和集群重启
  • 参考链接:https://github.com/cncamp/101/blob/master/module5/etcd-ha-demo/install-ha-etcd.MD

1.etcd 高可用集群的搭建

推荐先阅读:Kubernetes控制平面组件:etcd常用配置参数,搞清楚etcd的常用参数,再阅读本节将会更加清晰

1.1.Install cfssl

# Debian/Ubuntu
apt install golang-cfssl

# 或者使用go直接安装
go install github.com/cloudflare/cfssl/cmd/cfssl@latest
go install github.com/cloudflare/cfssl/cmd/cfssljson@latest

  • 作用:安装 cfssl 工具,用于生成 TLS 证书。
  • 原因:ETCD 集群需要 TLS 证书来加密节点之间的通信,确保数据安全性。

1.2.Generate tls certs and clone etcd code

mkdir /root/go/src/github.com/etcd-io
cd /root/go/src/github.com/etcd-io
git clone https://github.com/etcd-io/etcd.git
cd etcd/hack/tls-setup
  • 作用:
    • 创建 Go 工作目录。
    • 克隆 ETCD 官方仓库。
    • 进入 TLS 证书生成脚本目录。目的是先生成证书,才能去启动etcd
  • 原因:ETCD 官方提供了 TLS 证书生成的脚本和配置文件,方便用户快速生成证书。

1.3.Edit req-csr.json and keep 127.0.0.1 and localhost only for single cluster setup.

vi config/req-csr.json
  • 作用:编辑证书签名请求(CSR)配置文件,配置文件编辑好就可以生成证书了
  • 原因:
    • etcd 的证书签名请求文件,默认会生成一些ip,我们需要把ips改成自己的etcd集群ip
      在这里插入图片描述

    • 因为我这里虽然构建3节点etcd集群,但是都在本地一台机器上,所有只需要保留 127.0.0.1 和 localhost,避免生成不必要的证书。
      在这里插入图片描述

1.4.Generate certs

export infra0=127.0.0.1
export infra1=127.0.0.1
export infra2=127.0.0.1
make
mkdir /tmp/etcd-certs
mv certs /tmp/etcd-certs
  • 作用:
    • 先设置环境变量,指定集群节点的 IP 地址。因为我们准备将etcd的三个节点分别命名为 infra0、infra1、infra2
    • 使用 make 命令生成 TLS 证书。默认证书会生成到 当前目录/certs
    • 创建证书存储目录,并将生成的证书移动到该目录。
  • 原因:
    • 环境变量用于指定集群节点的 IP 地址。
    • make 命令调用 cfssl 生成证书。
    • 将证书集中存储,便于后续使用。后续使用etcdctl时需要执行cert目录

1.5.Start etcd cluster member1

  • 创建 start-all.sh 文件,将下面的命令复制进去

    • 声明了3个etcd实例,–initial-cluster-state为new,指明cert地址、节点名称、data-dir
    • 因为我要在同一台机器上启动3个实例,所以3个实例的端口是各异的
    #
    # each etcd instance name need to be unique
    # x380 is for peer communication
    # x379 is for client communication
    # dir-data cannot be shared
    #
    nohup etcd --name infra0 \
    --data-dir=/tmp/etcd/infra0 \
    --listen-peer-urls https://127.0.0.1:3380 \
    --initial-advertise-peer-urls https://127.0.0.1:3380 \
    --listen-client-urls https://127.0.0.1:3379 \
    --advertise-client-urls https://127.0.0.1:3379 \
    --initial-cluster-token etcd-cluster-1 \
    --initial-cluster infra0=https://127.0.0.1:3380,infra1=https://127.0.0.1:4380,infra2=https://127.0.0.1:5380 \
    --initial-cluster-state new \
    --client-cert-auth --trusted-ca-file=/tmp/etcd-certs/certs/ca.pem \
    --cert-file=/tmp/etcd-certs/certs/127.0.0.1.pem \
    --key-file=/tmp/etcd-certs/certs/127.0.0.1-key.pem \
    --peer-client-cert-auth --peer-trusted-ca-file=/tmp/etcd-certs/certs/ca.pem \
    --peer-cert-file=/tmp/etcd-certs/certs/127.0.0.1.pem \
    --peer-key-file=/tmp/etcd-certs/certs/127.0.0.1-key.pem 2>&1 > /var/log/infra0.log &
    
    nohup etcd --name infra1 \
    --data-dir=/tmp/etcd/infra1 \
    --listen-peer-urls https://127.0.0.1:4380 \
    --initial-advertise-peer-urls https://127.0.0.1:4380 \
    --listen-client-urls https://127.0.0.1:4379 \
    --advertise-client-urls https://127.0.0.1:4379 \
    --initial-cluster-token etcd-cluster-1 \
    --initial-cluster infra0=https://127.0.0.1:3380,infra1=https://127.0.0.1:4380,infra2=https://127.0.0.1:5380 \
    --initial-cluster-state new \
    --client-cert-auth --trusted-ca-file=/tmp/etcd-certs/certs/ca.pem \
    --cert-file=/tmp/etcd-certs/certs/127.0.0.1.pem \
    --key-file=/tmp/etcd-certs/certs/127.0.0.1-key.pem \
    --peer-client-cert-auth --peer-trusted-ca-file=/tmp/etcd-certs/certs/ca.pem \
    --peer-cert-file=/tmp/etcd-certs/certs/127.0.0.1.pem \
    --peer-key-file=/tmp/etcd-certs/certs/127.0.0.1-key.pem 2>&1 > /var/log/infra1.log &
    
    nohup etcd --name infra2 \
    --data-dir=/tmp/etcd/infra2 \
    --listen-peer-urls https://127.0.0.1:5380 \
    --initial-advertise-peer-urls https://127.0.0.1:5380 \
    --listen-client-urls https://127.0.0.1:5379 \
    --advertise-client-urls https://127.0.0.1:5379 \
    --initial-cluster-token etcd-cluster-1 \
    --initial-cluster infra0=https://127.0.0.1:3380,infra1=https://127.0.0.1:4380,infra2=https://127.0.0.1:5380 \
    --initial-cluster-state new \
    --client-cert-auth --trusted-ca-file=/tmp/etcd-certs/certs/ca.pem \
    --cert-file=/tmp/etcd-certs/certs/127.0.0.1.pem \
    --key-file=/tmp/etcd-certs/certs/127.0.0.1-key.pem \
    --peer-client-cert-auth --peer-trusted-ca-file=/tmp/etcd-certs/certs/ca.pem \
    --peer-cert-file=/tmp/etcd-certs/certs/127.0.0.1.pem \
    --peer-key-file=/tmp/etcd-certs/certs/127.0.0.1-key.pem 2>&1 > /var/log/infra2.log &
    
  • 执行创建集群

    chmod +0777 start-all.sh
    ./start-all.sh
    
  • 执行后集群就启动了,ps -ef | grep etcd 可以看出3个etcd节点已经有了
    在这里插入图片描述

  • 常见错误

    • 如果执行报错:nohup: nohup: failed to run command ‘etcd’nohup: failed to run command ‘etcd’failed to run command ‘etcd’: No such file or directory: No such file or directory,说明还没有etcd命令,需要安装一下
      # centos中
      yum install etcd
      # 设置使用的etcdctl api为v3
      export ETCDCTL_API=3
      

1.6.Member list 验证 etcd

etcdctl --endpoints https://127.0.0.1:3379 --cert /tmp/etcd-certs/certs/127.0.0.1.pem --key /tmp/etcd-certs/certs/127.0.0.1-key.pem --cacert /tmp/etcd-certs/certs/ca.pem member list
  • 作用:查看 ETCD 集群的成员列表。
  • 原因:验证集群是否正常运行,并确认所有节点已成功加入集群。
  • 如果报错:flag provided but not defined: -cert,说明没有设置 etcdctl 的版本
    export ETCDCTL_API=3
    

2.数据备份

2.1.Insert some data

  • 插入一些数据,模拟etcd的正常使用
    • key=a value=b
    • key=/a value=/b
    • key=/a/f value=ok
# 插入3条数据
[root@VM-226-235-tencentos ~/go/src/github.com/etcd-io/etcd/hack/tls-setup]# etcdctl --endpoints https://127.0.0.1:3379 --cert /tmp/etcd-certs/certs/127.0.0.1.pem --key /tmp/etcd-certs/certs/127.0.0.1-key.pem --cacert /tmp/etcd-certs/certs/ca.pem put a b
OK
[root@VM-226-235-tencentos ~/go/src/github.com/etcd-io/etcd/hack/tls-setup]# etcdctl --endpoints https://127.0.0.1:3379 --cert /tmp/etcd-certs/certs/127.0.0.1.pem --key /tmp/etcd-certs/certs/127.0.0.1-key.pem --cacert /tmp/etcd-certs/certs/ca.pem put /a /b
OK
[root@VM-226-235-tencentos ~/go/src/github.com/etcd-io/etcd/hack/tls-setup]# etcdctl --endpoints https://127.0.0.1:3379 --cert /tmp/etcd-certs/certs/127.0.0.1.pem --key /tmp/etcd-certs/certs/127.0.0.1-key.pem --cacert /tmp/etcd-certs/certs/ca.pem put /a/f ok
OK

# 查看所有的数据
[root@VM-226-235-tencentos ~/go/src/github.com/etcd-io/etcd/hack/tls-setup]# etcdctl --endpoints https://127.0.0.1:3379 --cert /tmp/etcd-certs/certs/127.0.0.1.pem --key /tmp/etcd-certs/certs/127.0.0.1-key.pem --cacert /tmp/etcd-certs/certs/ca.pem get --prefix ""
/a
/b
/a/f
ok
a
b

2.2.Backup

  • 执行备份命令,将当前etcd集群全量备份为快照snapshot,备份生成文件snapshot.db
    etcdctl --endpoints https://127.0.0.1:3379 \
    	--cert /tmp/etcd-certs/certs/127.0.0.1.pem \
    	--key /tmp/etcd-certs/certs/127.0.0.1-key.pem \
    	--cacert /tmp/etcd-certs/certs/ca.pem snapshot save snapshot.db
    
  • 执行后集群就备份了,ls 查看当前目录文件,会多出一个 snapshot.db
  • 在集群出现故障或数据丢失时,可以通过备份恢复数据。

3.销毁etcd集群,模拟故障

ps -ef | grep "/tmp/etcd/infra" | grep -v grep | awk '{print $2}'|xargs kill
  • 作用:终止所有 ETCD 节点的进程。
  • 原因:在恢复数据之前,需要停止所有 ETCD 实例。
rm -rf /tmp/etcd
  • 作用:删除 ETCD 数据目录。
  • 原因:模拟数据丢失场景,测试备份恢复功能。

4.使用快照恢复etcd集群数据

  • 创建 restore.sh 文件,将下面的命令复制进去
    • 使用 snapshot 恢复3个etcd实例,指定将数据恢复到哪里–data-dir
    export ETCDCTL_API=3
    etcdctl snapshot restore snapshot.db \
      --name infra0 \
      --data-dir=/tmp/etcd/infra0 \
      --initial-cluster infra0=https://127.0.0.1:3380,infra1=https://127.0.0.1:4380,infra2=https://127.0.0.1:5380 \
      --initial-cluster-token etcd-cluster-1 \
      --initial-advertise-peer-urls https://127.0.0.1:3380
    
    etcdctl snapshot restore snapshot.db \
        --name infra1 \
        --data-dir=/tmp/etcd/infra1 \
        --initial-cluster infra0=https://127.0.0.1:3380,infra1=https://127.0.0.1:4380,infra2=https://127.0.0.1:5380 \
        --initial-cluster-token etcd-cluster-1 \
        --initial-advertise-peer-urls https://127.0.0.1:4380
    
    etcdctl snapshot restore snapshot.db \
      --name infra2 \
      --data-dir=/tmp/etcd/infra2 \
      --initial-cluster infra0=https://127.0.0.1:3380,infra1=https://127.0.0.1:4380,infra2=https://127.0.0.1:5380 \
      --initial-cluster-token etcd-cluster-1 \
      --initial-advertise-peer-urls https://127.0.0.1:5380
    
  • 执行恢复集群数据,完成后 ls /tmp/etcd 查看数据是否恢复回来了
    chmod +0777 restore.sh
    ./restore.sh
    ls /tmp/etcd
    

5.重启etcd集群

  • 创建 restart-all.sh 文件,将下面的命令复制进去
    • 使用 重新启动 3个etcd实例,–data-dir指定数据目录
    nohup etcd --name infra0 \
    --data-dir=/tmp/etcd/infra0 \
    --listen-peer-urls https://127.0.0.1:3380 \
    --listen-client-urls https://127.0.0.1:3379 \
    --advertise-client-urls https://127.0.0.1:3379 \
    --client-cert-auth --trusted-ca-file=/tmp/etcd-certs/certs/ca.pem \
    --cert-file=/tmp/etcd-certs/certs/127.0.0.1.pem \
    --key-file=/tmp/etcd-certs/certs/127.0.0.1-key.pem \
    --peer-client-cert-auth --peer-trusted-ca-file=/tmp/etcd-certs/certs/ca.pem \
    --peer-cert-file=/tmp/etcd-certs/certs/127.0.0.1.pem \
    --peer-key-file=/tmp/etcd-certs/certs/127.0.0.1-key.pem 2>&1 > /var/log/infra0.log &
    
    nohup etcd --name infra1 \
    --data-dir=/tmp/etcd/infra1 \
    --listen-peer-urls https://127.0.0.1:4380 \
    --listen-client-urls https://127.0.0.1:4379 \
    --advertise-client-urls https://127.0.0.1:4379 \
    --client-cert-auth --trusted-ca-file=/tmp/etcd-certs/certs/ca.pem \
    --cert-file=/tmp/etcd-certs/certs/127.0.0.1.pem \
    --key-file=/tmp/etcd-certs/certs/127.0.0.1-key.pem \
    --peer-client-cert-auth --peer-trusted-ca-file=/tmp/etcd-certs/certs/ca.pem \
    --peer-cert-file=/tmp/etcd-certs/certs/127.0.0.1.pem \
    --peer-key-file=/tmp/etcd-certs/certs/127.0.0.1-key.pem 2>&1 > /var/log/infra1.log &
    
    nohup etcd --name infra2 \
    --data-dir=/tmp/etcd/infra2 \
    --listen-peer-urls https://127.0.0.1:5380 \
    --listen-client-urls https://127.0.0.1:5379 \
    --advertise-client-urls https://127.0.0.1:5379 \
    --client-cert-auth --trusted-ca-file=/tmp/etcd-certs/certs/ca.pem \
    --cert-file=/tmp/etcd-certs/certs/127.0.0.1.pem \
    --key-file=/tmp/etcd-certs/certs/127.0.0.1-key.pem \
    --peer-client-cert-auth --peer-trusted-ca-file=/tmp/etcd-certs/certs/ca.pem \
    --peer-cert-file=/tmp/etcd-certs/certs/127.0.0.1.pem \
    --peer-key-file=/tmp/etcd-certs/certs/127.0.0.1-key.pem 2>&1 > /var/log/infra2.log &
    
  • 执行重启集群,完成后 ps -ef | grep etcd 查看3个etcd节点是否都重新启动了
    ps -ef | grep etcd
    

6.验证数据是否恢复

  • 获取etcd的member,查看节点是否正常
    [root@VM-226-235-tencentos ~/go/src/github.com/etcd-io/etcd/hack/tls-setup]# etcdctl --endpoints https://127.0.0.1:3379 --cert /tmp/etcd-certs/certs/127.0.0.1.pem --key /tmp/etcd-certs/certs/127.0.0.1-key.pem --cacert /tmp/etcd-certs/certs/ca.pem member list
    1701f7e3861531d4, started, infra0, https://127.0.0.1:3380, https://127.0.0.1:3379
    6a58b5afdcebd95d, started, infra1, https://127.0.0.1:4380, https://127.0.0.1:4379
    84a1a2f39cda4029, started, infra2, https://127.0.0.1:5380, https://127.0.0.1:5379
    
  • 获取etcd的所有数据,验证数据是否恢复
    [root@VM-226-235-tencentos ~/go/src/github.com/etcd-io/etcd/hack/tls-setup]# etcdctl --endpoints https://127.0.0.1:3379 --cert /tmp/etcd-certs/certs/127.0.0.1.pem --key /tmp/etcd-certs/certs/127.0.0.1-key.pem --cacert /tmp/etcd-certs/certs/ca.pem get --prefix ""
    /a
    /b
    /a/f
    ok
    a
    b
    

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/971144.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

HCIA项目实践--静态路由的拓展配置

7.7 静态路由的拓展配置 网络中的两个重要思想: (1) 实的不行来虚的; (2) 范围太大,划分范围。(分治) 7.7.1 负载均衡 (1)定义 负载均衡是一种网…

node.js + html调用ChatGPTApi实现Ai网站demo(带源码)

文章目录 前言一、demo演示二、node.js 使用步骤1.引入库2.引入包 前端HTML调用接口和UI所有文件总结 前言 关注博主,学习每天一个小demo 今天是Ai对话网站 又到了每天一个小demo的时候咯,前面我写了多人实时对话demo、和视频转换demo,今天…

类和对象(5)——抽象类和接口

目录 1. 抽象类 1.1 抽象类的概念 1.2 抽象类语法:abstract关键字 1.3 抽象类的特性 1.4 抽象类的作用 2. 接口 2.1 接口的概念 2.2 接口语法:interface关键字 2.3 接口的实现:implements关键字 2.4 接口的特性 2.5 实现多个接口 …

kubectl exec 实现的原理

kubectl exec 是 Kubernetes 提供的一个命令,它允许你在指定的 Pod 中执行命令,类似于在容器中打开一个终端会话。这个功能对于调试、监控和管理容器化应用非常有用。kubectl exec 的实现涉及到多个 Kubernetes 组件和机制,包括 API Server、…

【ubuntu24.04】 强制重启导致大模型的磁盘挂载出错

挂载NTFS文件系统出错 各种模型放在了这个机械硬盘上,虽然速度慢,但是好在容量大。大模型在工作,但是程序看起来有问题,导致系统卡死了,然后我重启了,然后报错:wrong fs type bad option &…

【数据结构】 栈和队列

在计算机科学的世界里,数据结构是构建高效算法的基础。栈(Stack)和队列(Queue)作为两种基本且重要的数据结构,在软件开发、算法设计等众多领域都有着广泛的应用。今天,我们就来深入探讨一下栈和…

移动端测试的挑战与解决方案:兼容性、网络问题及实战策略

引言 移动应用已成为用户触达服务的核心入口,但移动端测试面临设备多样性、网络波动、用户场景复杂等多重挑战。据Statista统计,2023年全球活跃移动设备超180亿台,操作系统(Android/iOS)版本碎片化率超30%,这对测试工程师提出了极高要求。本文深度解析移动端测试的核心痛…

【设计模式】03-理解常见设计模式-行为型模式(专栏完结)

前言 前面我们介绍完创建型模式和创建型模式,这篇介绍最后的行为型模式,也是【设计模式】专栏的最后一篇。 一、概述 行为型模式主要用于处理对象之间的交互和职责分配,以实现更灵活的行为和更好的协作。 二、常见的行为型模式 1、观察者模…

matlab欠驱动船舶模型预测控制

1、内容简介 matlab135-欠驱动船舶模型预测控制 可以交流、咨询、答疑 2、内容说明 略 针对在风 、 浪 、 流时变干扰下欠驱动水面船舶的轨迹跟踪控制问题 , 设计了一种基于模型 预测控制的轨迹跟踪控制器 . 考虑到欠驱动船舶在没有横向驱动力情况下…

计算机性能与网络体系结构探讨 —— 基于《计算机网络》谢希仁第八版

(꒪ꇴ꒪ ),Hello我是祐言QAQ我的博客主页:C/C语言,数据结构,Linux基础,ARM开发板,网络编程等领域UP🌍快上🚘,一起学习,让我们成为一个强大的攻城狮&#xff0…

Linux上安装jdk1.8和配置环境变量

步骤一::创建jdk安装目录(该/usr/local/ ,最好把我们自己下载的放到这,容易区分) 可以省略 步骤二:查看安装程序 [rootVM_0_4_centos src]# rpm -qa | grep -i jdk 若之前安装过jdk,下次安装一定把之前的删除干净 下载地址链接…

【Spring+MyBatis】留言墙的实现

目录 1. 添加依赖 2. 配置数据库 2.1 创建数据库与数据表 2.2 创建与数据库对应的实体类 3. 后端代码 3.1 目录结构 3.2 MessageController类 3.3 MessageService类 3.4 MessageMapper接口 4. 前端代码 5. 单元测试 5.1 后端接口测试 5.2 使用前端页面测试 在Spri…

Windows环境安装部署minimind步骤

Windows环境安装部署minimind步骤 必要的软件环境 git git,可下载安装版,本机中下载绿色版,解压到本地目录下(如:c:\soft\git.win64),可将此路径添加到PATH环境变量中,供其他程序…

RocketMQ与kafka如何解决消息丢失问题?

0 前言 消息丢失基本是分布式MQ中需要解决问题,消息丢失时保证数据可靠性的范畴。如何保证消息不丢失程序员面试中几乎不可避免的问题。本文主要说明RocketMQ和Kafka在解决消息丢失问题时,在生产者、Broker和消费者之间如何解决消息丢失问题。 1.Rocket…

基于AIOHTTP、Websocket和Vue3一步步实现web部署平台,无延迟控制台输出,接近原生SSH连接

背景:笔者是一名Javaer,但是最近因为某些原因迷上了Python和它的Asyncio,至于什么原因?请往下看。在着迷”犯浑“的过程中,也接触到了一些高并发高性能的组件,通过简单的学习和了解,aiohttp这个…

Golang的代码结构规划

Golang的代码结构规划 是一种具有高效性能的开发语言,其代码结构规划对于项目的可维护性和可扩展性至关重要。在Golang中,合理的代码结构可以使代码更加清晰易懂,方便团队协作和项目维护。本文将介绍Golang代码结构规划的最佳实践&#xff0c…

【算法与数据结构】并查集详解+题目

目录 一,什么是并查集 二,并查集的结构 三,并查集的代码实现 1,并查集的大致结构和初始化 2,find操作 3,Union操作 4,优化 小结: 四,并查集的应用场景 省份…

服务器部署DeepSeek,通过Ollama+open-webui部署

1. 安装ollama 1.1. linux 安装 Ollama是目前常用的AI模式部署的第三方工具,能一键部署deepSeek Ollama官方网址https://ollama.com/ 选择Download下载对应的服务版本 服务器选择Linux,下面是下载代码 curl -fsSL https://ollama.com/install.…

(三)Axure制作转动的唱片

效果图 属性: 图标库:iconfont-阿里巴巴矢量图标库 方形图片转为圆角图片,裁剪,然后加圆角, 唱片和底图是两个图片,点击播放,唱片在旋转。 主要是播放按钮和停止按钮,两个动态面板…

5G时代的运维变革与美信监控易的深度剖析

一、5G普及后的网络运维新变化:数据驱动的挑战与机遇 (一)数据流量的爆炸式增长 在2025年,5G技术已经如同汹涌的浪潮席卷全球。据相关科技数据显示,5G网络的普及使得数据流量呈现出令人咋舌的增长态势。 这种海量的数…