How mysql/mysql-operator works
Main Components
• Kubernetes Cluster: The deployment target where the operator and MySQL instances run.
• Custom Resource Definitions (CRDs): Definitions (e.g., InnoDBCluster) that extend the Kubernetes API to represent MySQL cluster configurations.
• MySQL Operator: The core logic (located in the “mysqloperator” directory) that reacts to changes in the CRDs and controls MySQL cluster lifecycles.
• Controllers/Submodules: Within the operator, the folder structure (e.g., “controller/innodbcluster”, “controller/backup”, “controller/cloud”, etc.) reveals modular controllers managing distinct responsibilities such as backup operations, cluster initialization, logging, and cloud integrations.
• Helm Charts and Manifests: In the “helm” directory, you have two distinct charts – one for “mysql-operator” and one for “mysql-innodbcluster” – which simplify both the operator’s and clusters’ deployment. Additionally, there are Kubernetes manifest files (in “deploy”) for applying CRDs and deployment YAMLs using kubectl.
• Docker and Build/Deployment Scripts: Scripts like “build.sh”, “gen_dockerfile.sh”, and Dockerfiles in “docker-build” and “docker-deps” indicate containerization and build processes.
• Testing Infrastructure: A robust “tests” directory with unit, integration (CI/CD) and end-to-end test scenarios ensures that operator logic is verified against various cluster scenarios.
Relationships and Interactions
• The operator is deployed inside a Kubernetes namespace (typically “mysql-operator”) and continuously monitors CRDs (created via manifest or Helm) for custom resources such as InnoDBCluster.
• When a CRD is applied (for instance, defining a new MySQL cluster), the operator’s controllers are triggered. For example, the ‘innodbcluster’ controllers will:
• Validate the cluster configuration.
• Spawn and manage the individual MySQL Server and MySQL Router instances (each of which may be implemented as StatefulSets or Deployments).
• Manage backup operations and cloud/authentication aspects via the “backup” and “cloud” controllers.
Architectural Patterns and Design Principles
• Controller Pattern: The operator follows the Kubernetes controller/reconciler paradigm to continuously reconcile desired versus actual state.
• Modularity: With controllers split into different modules (e.g., backup, cloud, innodbcluster), the design promotes separation of concerns. Each module manages different aspects of the cluster lifecycle.
• Integration with Kubernetes APIs: The dynamic interactions with CRDs, Services, StatefulSets/Deployments, and external custom configurations are crucial.