k0smotron自动升级攻略:零停机实现Kubernetes版本无缝更新
k0smotron自动升级攻略:零停机实现Kubernetes版本无缝更新
【免费下载链接】k0smotronk0smotron项目地址: https://gitcode.com/gh_mirrors/k0/k0smotron
k0smotron作为轻量级Kubernetes管理工具,提供了强大的自动升级功能,能够帮助用户零停机实现Kubernetes版本的无缝更新。本文将详细介绍k0smotron的自动升级机制、触发条件、更新策略以及操作步骤,让你轻松掌握Kubernetes集群的平滑升级技巧。
升级触发条件:何时需要升级Kubernetes集群
k0smotron会持续监控K0sControlPlane资源,并在以下情况触发控制平面升级:
1. k0s版本变更
最常见的触发因素是修改K0sControlPlane资源中的spec.version字段。k0smotron会将该值与每个控制平面机器报告的版本进行比较,如果存在差异,就会启动升级流程。版本值在比较前会进行标准化处理,例如v1.31.2和v1.31.2+k0s.0会被视为等效版本。
2. 配置变更
k0smotron根据spec.k0sConfigSpec中修改的部分不同,处理方式也有所区别:
- 对
spec.k0sConfigSpec.k0s(k0s的ClusterConfig对象)的更改,会使用k0s动态配置直接在工作负载集群中修补ClusterConfig资源,无需替换任何机器。但需要注意,有些字段无法通过动态配置更改,会被忽略。 - 对
spec.k0sConfigSpec中其他字段(如args、files、preStartCommands等)的更改,k0smotron会通过比较每个机器注释中存储的引导配置哈希值与当前规范来检测。配置不匹配的机器将被标记为需要替换,无论spec.updateStrategy如何设置,都会使用Recreate工作流。
注意:k0smotron仅检测直接在
K0sControlPlane规范中进行的配置更改。spec.k0sConfigSpec.files字段支持通过contentFrom从外部Secret或ConfigMap对象加载文件内容,但如果仅更改这些对象的内容,k0smotron不会检测到,也不会触发升级。要传播更新的文件内容,需创建新的Secret或ConfigMap,并更新K0sControlPlane规范中的contentFrom以引用新对象。
3. 机器模板变更
当spec.machineTemplate.infrastructureRef指向新的或已更改的基础设施模板时,从旧模板修订版克隆的机器将被标记为需要替换。k0smotron通过检查每个基础设施机器上的cluster.x-k8s.io/cloned-from-name和cluster.x-k8s.io/cloned-from-groupkind注释来检测这一点。与配置更改一样,模板更改始终会触发机器重新创建。
更新策略:选择适合你的升级方式
k0smotron支持三种更新策略,通过spec.updateStrategy进行配置:
| 策略 | 行为 |
|---|---|
InPlace(默认) | 使用k0s autopilot在现有机器上更新k0s,无需替换它们 |
Recreate | 先创建新机器,然后移除旧机器 |
RecreateDeleteFirst | 先移除旧机器,然后创建新机器 |
警告:
Recreate策略不支持在--single模式下运行的集群。RecreateDeleteFirst策略至少需要3个控制平面节点。
零停机升级实战:一步步操作指南
使用k0s autopilot进行原地升级(InPlace)
当spec.updateStrategy为InPlace(或省略)时,k0smotron使用k0s autopilot在每个控制平面节点上更新k0s,无需替换机器。这种方式比重新创建机器更快,并且能保持节点上的本地数据完整。
- 检查已部署集群的配置。例如:
apiVersion: cluster.x-k8s.io/v1beta2 kind: Cluster metadata: name: docker-test namespace: default spec: clusterNetwork: pods: cidrBlocks: - 192.168.0.0/16 serviceDomain: cluster.local services: cidrBlocks: - 10.128.0.0/12 controlPlaneRef: apiGroup: controlplane.cluster.x-k8s.io kind: K0sControlPlane name: docker-test-cp infrastructureRef: apiGroup: infrastructure.cluster.x-k8s.io kind: DevCluster name: docker-test --- apiVersion: infrastructure.cluster.x-k8s.io/v1beta2 kind: DevCluster metadata: name: docker-test namespace: default spec: backend: docker: {} --- apiVersion: controlplane.cluster.x-k8s.io/v1beta2 kind: K0sControlPlane metadata: name: docker-test-cp spec: replicas: 3 version: v1.31.2+k0s.0 updateStrategy: InPlace k0sConfigSpec: args: - --enable-worker k0s: apiVersion: k0s.k0sproject.io/v1beta1 kind: ClusterConfig metadata: name: k0s spec: api: extraArgs: anonymous-auth: "true" # anonymous-auth=true is needed for k0s to allow unauthorized health-checks on /healthz telemetry: enabled: true machineTemplate: infrastructureRef: apiGroup: infrastructure.cluster.x-k8s.io kind: DevMachineTemplate name: docker-test-cp-template --- apiVersion: infrastructure.cluster.x-k8s.io/v1beta2 kind: DevMachineTemplate metadata: name: docker-test-cp-template namespace: default spec: template: spec: backend: docker: customImage: kindest/node:v1.31.0- 将
spec.version更新为目标k0s版本:
apiVersion: controlplane.cluster.x-k8s.io/v1beta2 kind: K0sControlPlane metadata: name: docker-test-cp spec: replicas: 3 version: v1.31.3+k0s.0 # updated version updateStrategy: InPlace k0sConfigSpec: args: - --enable-worker k0s: apiVersion: k0s.k0sproject.io/v1beta1 kind: ClusterConfig metadata: name: k0s spec: api: extraArgs: anonymous-auth: "true" telemetry: enabled: true machineTemplate: infrastructureRef: apiGroup: infrastructure.cluster.x-k8s.io kind: DevMachineTemplate name: docker-test-cp-template- 应用更改:
kubectl apply -f ./path-to-file.yamlk0smotron会在工作负载集群中创建一个autopilotPlan资源,协调所有控制平面节点的滚动更新。
使用Cluster API工作流进行重建升级(Recreate)
当spec.updateStrategy为Recreate时,k0smotron会一次替换一个控制平面机器:先创建所需版本的新机器,等待它们准备就绪,然后移除旧机器。
当spec.updateStrategy为RecreateDeleteFirst时,它会先移除旧机器,然后创建替换机器。这在资源受限的情况下很有用,但至少需要3个控制平面节点才能在滚动过程中维持法定人数。
警告:
Recreate策略不支持在--single模式下运行的集群。
- 检查已部署集群的配置。例如:
apiVersion: cluster.x-k8s.io/v1beta2 kind: Cluster metadata: name: docker-test namespace: default spec: clusterNetwork: pods: cidrBlocks: - 192.168.0.0/16 serviceDomain: cluster.local services: cidrBlocks: - 10.128.0.0/12 controlPlaneRef: apiGroup: controlplane.cluster.x-k8s.io kind: K0sControlPlane name: docker-test-cp infrastructureRef: apiGroup: infrastructure.cluster.x-k8s.io kind: DevCluster name: docker-test --- apiVersion: infrastructure.cluster.x-k8s.io/v1beta2 kind: DevCluster metadata: name: docker-test namespace: default spec: backend: docker: {} --- apiVersion: controlplane.cluster.x-k8s.io/v1beta2 kind: K0sControlPlane metadata: name: docker-test-cp spec: replicas: 3 version: v1.31.2+k0s.0 updateStrategy: Recreate k0sConfigSpec: args: - --enable-worker k0s: apiVersion: k0s.k0sproject.io/v1beta1 kind: ClusterConfig metadata: name: k0s spec: api: extraArgs: anonymous-auth: "true" # anonymous-auth=true is needed for k0s to allow unauthorized health-checks on /healthz telemetry: enabled: true machineTemplate: infrastructureRef: apiGroup: infrastructure.cluster.x-k8s.io kind: DevMachineTemplate name: docker-test-cp-template --- apiVersion: infrastructure.cluster.x-k8s.io/v1beta2 kind: DevMachineTemplate metadata: name: docker-test-cp-template namespace: default spec: template: spec: backend: docker: customImage: kindest/node:v1.31.0- 将
spec.version更新为目标k0s版本:
apiVersion: controlplane.cluster.x-k8s.io/v1beta2 kind: K0sControlPlane metadata: name: docker-test-cp spec: replicas: 3 version: v1.31.3+k0s.0 # updated version updateStrategy: Recreate k0sConfigSpec: args: - --enable-worker k0s: apiVersion: k0s.k0sproject.io/v1beta1 kind: ClusterConfig metadata: name: k0s spec: api: extraArgs: anonymous-auth: "true" # anonymous-auth=true is needed for k0s to allow unauthorized health-checks on /healthz telemetry: enabled: true machineTemplate: infrastructureRef: apiGroup: infrastructure.cluster.x-k8s.io kind: DevMachineTemplate name: docker-test-cp-template- 更新资源:
kubectl apply -f ./path-to-file.yaml监控升级状态:掌握升级进度
K0sControlPlane状态字段提供了对正在进行的升级的可见性:
kubectl get k0scontrolplane <name> -o yaml相关状态字段:
| 字段 | 描述 |
|---|---|
status.replicas | 非终止控制平面机器的总数 |
status.readyReplicas | 完全运行并准备就绪的机器 |
status.upToDateReplicas | 运行所需k0s版本的机器 |
status.availableReplicas | 当前可用于提供流量的机器 |
status.version | 所有机器的最低Kubernetes版本 |
对于InPlace升级,还可以检查工作负载集群内运行的autopilot计划:
kubectl --kubeconfig <workload-cluster-kubeconfig> get plan autopilot -o yaml常见问题与解决方案
由于旧版k0s autopilot中的一个错误,当控制平面节点也运行工作负载时(例如使用--enable-worker标志时),控制平面升级可能会卡在Cordoning阶段。此错误已在最新的k0s补丁版本中修复。
如果升级停滞,可使用以下步骤恢复:
- 识别卡住的节点:
kubectl --kubeconfig <workload-cluster-kubeconfig> get plan autopilot -o yaml手动排空节点。
修补相应
ControlNode对象上的k0sproject.io/autopilot-signal-data注释:将JSON值中的status字段从Cordoning更改为ApplyingUpdate。对任何其他卡住的节点重复上述步骤。
总结
k0smotron提供了灵活而强大的自动升级功能,通过合理配置更新策略和监控升级状态,用户可以轻松实现Kubernetes集群的零停机无缝更新。无论是使用InPlace策略进行快速原地升级,还是采用Recreate策略进行彻底的机器替换,k0smotron都能满足不同场景下的升级需求,确保集群的稳定运行和版本更新。
要了解更多关于k0smotron的信息,可以参考官方文档:docs/
【免费下载链接】k0smotronk0smotron项目地址: https://gitcode.com/gh_mirrors/k0/k0smotron
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考