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:

CDH5 Installation Path-C

Before You Begin

SSH Configuration

vi /etc/hosts
x.x.x.x linode01
x.x.x.x linode02
x.x.x.x linode03

hostnamectl set-hostname linode01

ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub linode01
ssh-copy-id -i ~/.ssh/id_rsa.pub linode02
ssh-copy-id -i ~/.ssh/id_rsa.pub linode03

Disable Firewall

systemctl stop firewalld.service
systemctl disable firewalld.service

Dependency

yum install psmisc -y
yum install libxslt-devel -y
yum install chkconfig bind-utils psmisc libxslt zlib sqlite cyrus-sasl-plain cyrus-sasl-gssapi fuse portmap fuse-libs redhat-lsb -y
yum install python-psycopg2 -y 
yum install snappy snappy-devel  -y

#NFS
yum install rpcbind -y
service rpcbind start

Install the Oracle JDK

cd /opt/
sudo wget --no-cookies --no-check-certificate --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn/java/jdk/7u80-b15/jdk-7u80-linux-x64

CDH5 Installation Path-B

Before You Begin

SSH Configuration

vi /etc/hosts
x.x.x.x linode01
x.x.x.x linode02
x.x.x.x linode03

hostnamectl set-hostname linode01

ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub linode01
ssh-copy-id -i ~/.ssh/id_rsa.pub linode02
ssh-copy-id -i ~/.ssh/id_rsa.pub linode03

Disable Firewall

systemctl stop firewalld.service
systemctl disable firewalld.service

Dependency

yum install psmisc -y
yum install libxslt-devel -y
yum install chkconfig bind-utils psmisc libxslt zlib sqlite cyrus-sasl-plain cyrus-sasl-gssapi fuse portmap fuse-libs redhat-lsb -y
yum install python-psycopg2 -y 
yum install snappy snappy-devel  -y

#NFS
yum install rpcbind -y
service rpcbind start

Install and Configure External Databases

sudo yum install postgresql-server postgresql -y
sudo su - postgres
initdb -D /var/lib/pgsql/data

#remote access
vi /var/lib/pgsql/data/postgresql.conf
listen_addresses ='*'

vi /var/lib/pgsql/data/pg_h