k8s运维开发

Kubernetes Operator开发与StatefulSet有状态应用管理

背景与挑战

在Kubernetes生态中,有状态应用(如数据库、消息队列、缓存系统)的管理一直是最具挑战性的领域之一。与传统无状态应用相比,有状态应用需要确保:

  • 数据持久性:确保数据不因Pod重启或迁移而丢失
  • 稳定标识:维护固定的网络标识和存储映射关系
  • 有序操作:部署、扩展、升级等操作需要严格的顺序控制
  • 状态一致性:保证集群状态的一致性和数据完整性

以MySQL数据库集群为例,传统Deployment无法满足以下需求:
- 主从节点的有序启动和配置
- 自动故障转移和数据同步
- 备份恢复的自动化管理
- 配置变更的滚动更新

实现方案

基于Operator模式我们开发了MySQL集群管理Operator,实现有状态应用的自动化管理

  • CRD(CustomResourceDefinition)​:定义MySQLCluster资源类型,封装集群规格、备份策略等配置
  • 控制器逻辑​​:持续调和实际状态与期望状态,处理节点故障转移、数据备份等复杂操作
  • 初始化与配置管理​​:自动处理MySQL系统初始化、主从复制配置和参数优化
  1. apiVersion: mysql.oracle.com/v2
  2. kind: InnoDBCluster
  3. metadata:
  4. name: mycluster
  5. spec:
  6. instances: 3
  7. secretName: my-secret
  8. tlsUseSelfSigned: true
  9. router:
  10. instances: 1
  11. podSpec:
  12. resources:
  13. requests:
  14. memory: "2Gi"
  15. cpu: "1000m"
  16. limits:
  17. memory: "4Gi"
  18. cpu: "2000m"
  19. # 关键:Pod反亲和性确保节点分布
  20. affinity:
  21. podAntiAffinity:
  22. requiredDuringSchedulingIgnoredDuringExecution:

Certbot

Install

Certbot is packaged in EPEL (Extra Packages for Enterprise Linux). To use Certbot, you must first enable the EPEL repository.

After doing this, you can install Certbot by running:

  1. sudo yum install certbot

Generate cert

Certbot supports a number of different “plugins” that can be used to obtain and/or install certificates.

Since your server architecture doesn’t yet support automatic installation you should probably use the certonly command to obtain your certificate.

  1. certbot certonly --webroot -w /var/www/example -d example.com -d www.example.com -w /var/www/thing -d thing.is -d m.thing.is

This command will obtain a single cert for example.com, www.example.com, thing.is, and m.thing.is; it will place files below /var/www/example to prove control of the first two domains, and under /var/www/thing for the second pair.

Currently it is possible to perform a DNS validation also with the certbot LetsEncrypt client