Topologies
Operations
Backup And Restores
Custom Secret
Monitoring
In certain scenarios, different Pods within the same database cluster may require varying resource allocations. For example:
With KubeBlocks, you can customize Pod resource configurations and labels to tailor each instance to meet its unique requirements. This guide demonstrates how to deploy a MySQL cluster with a custom replica provisioned with higher CPU and memory resources.
Before proceeding, ensure the following:
kubectl create ns demo
namespace/demo created
Deploy a 2-node semi-sync MySQL cluster (1 primary, 1 secondary):
kubectl apply -f - <<EOF
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
name: example-mysql-cluster
namespace: demo
spec:
clusterDef: mysql
topology: semisync
terminationPolicy: Delete
componentSpecs:
- name: mysql
serviceVersion: 8.0.35
replicas: 2
resources:
limits:
cpu: '0.5'
memory: 0.5Gi
requests:
cpu: '0.5'
memory: 0.5Gi
volumeClaimTemplates:
- name: data
spec:
storageClassName: ""
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
EOF
Monitor the cluster status and wait until the STATUS changes to Running:
kubectl get cluster example-mysql-cluster -n demo -w
Expected Output:
NAME CLUSTER-DEFINITION TERMINATION-POLICY STATUS AGE
example-mysql-cluster mysql Delete Creating 17s
example-mysql-cluster mysql Delete Running 2m33s
To add a new replica with higher CPU and memory allocations, update the cluster specification as follows:
kubectl apply -f - <<EOF
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
name: example-mysql-cluster
namespace: demo
spec:
clusterDef: mysql
topology: semisync
terminationPolicy: Delete
componentSpecs:
- name: mysql
serviceVersion: 8.0.35
replicas: 3
resources:
limits:
cpu: '0.5'
memory: 0.5Gi
requests:
cpu: '0.5'
memory: 0.5Gi
volumeClaimTemplates:
- name: data
spec:
storageClassName: ""
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
instances:
- name: custom
replicas: 1
labels:
custom-resource-config: "true"
resources:
limits:
cpu: 1
memory: 1Gi
requests:
cpu: 1
memory: 1Gi
EOF
Explanation:
Monitor the cluster status until it returns to Running:
kubectl get cluster -n demo -w
Example Output:
NAME CLUSTER-DEFINITION TERMINATION-POLICY STATUS AGE
example-mysql-cluster mysql Delete Updating 79s
example-mysql-cluster mysql Delete Running 2m44s
List all the Pods in the cluster:
kubectl get pods -n demo
Expected Output:
NAME READY STATUS RESTARTS AGE
example-mysql-cluster-mysql-0 4/4 Running 0 2m49s
example-mysql-cluster-mysql-1 4/4 Running 0 2m49s
example-mysql-cluster-mysql-custom-0 4/4 Running 0 97s
Observation:
Use the KubeBlocks CLI to check the cluster and verify the resource configurations:
kbcli cluster describe example-mysql-cluster -n demo
Expected Output:
Resources Allocation:
COMPONENT INSTANCE-TEMPLATE CPU(REQUEST/LIMIT) MEMORY(REQUEST/LIMIT) STORAGE-SIZE STORAGE-CLASS
mysql custom 1 / 1 1Gi / 1Gi data:20Gi <none>
mysql 500m / 500m 512Mi / 512Mi data:20Gi <none>
Observation:
To expose the custom Pod via a separate Service, use the following configuration:
kubectl apply -f - <<EOF
apiVersion: apps.kubeblocks.io/v1
kind: Cluster
metadata:
name: example-mysql-cluster
namespace: demo
spec:
clusterDef: mysql
topology: semisync
terminationPolicy: Delete
# expose a service
services:
- name: custom-pod
componentSelector: mysql
serviceName: custom-pod
spec:
selector:
custom-resource-config: "true"
ipFamilyPolicy: PreferDualStack
ports:
- name: tcp-mysql
port: 3306
protocol: TCP
targetPort: mysql
type: ClusterIP
componentSpecs:
- name: mysql
serviceVersion: 8.0.35
replicas: 3
resources:
limits:
cpu: '0.5'
memory: 0.5Gi
requests:
cpu: '0.5'
memory: 0.5Gi
volumeClaimTemplates:
- name: data
spec:
storageClassName: ""
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
instances:
- name: custom
replicas: 1
labels:
custom-resource-config: "true"
resources:
limits:
cpu: 1
memory: 1Gi
requests:
cpu: 1
memory: 1Gi
EOF
Check the Cluster status:
kubectl get cluster -n demo -w
Example Output:
NAME CLUSTER-DEFINITION TERMINATION-POLICY STATUS AGE
example-mysql-cluster mysql Delete Updating 16m
example-mysql-cluster mysql Delete Running 17m
Then list the Services:
kubectl get svc -n demo
Example Output:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
example-mysql-cluster-custom-pod ClusterIP 172.20.202.249 <none> 3306/TCP 12m
example-mysql-cluster-mysql ClusterIP 172.20.11.166 <none> 3306/TCP 12m
example-mysql-cluster-mysql-headless ClusterIP None <none> 3306/TCP,3601/TCP,9104/TCP,3501/TCP,3502/TCP,9901/TCP 12m
Retrieve the root credentials:
kubectl get secrets -n demo example-semisync-mysql-mysql-account-root -o jsonpath='{.data.username}' | base64 -d
kubectl get secrets -n demo example-mysql-cluster-mysql-account-root -o jsonpath='{.data.password}' | base64 -d
Expected Output:
root
uk263gR24s
Connect to the custom Pod from inside one of the MySQL containers:
kubectl exec -it example-mysql-cluster-mysql-0 -n demo -- mysql -hexample-mysql-cluster-custom-pod -uroot -puk263gR24s
This custom Pod is provisioned with additional resources, making it ideal for running complex queries or analytical workloads.
To remove all created resources, delete the MySQL cluster along with its namespace:
kubectl delete cluster example-mysql-cluster -n demo
kubectl delete ns demo
By customizing Pod resource configurations and labels through KubeBlocks, you can build a flexible and resource-efficient MySQL environment. Whether you need a powerful primary instance or specialized report-generation replicas, KubeBlocks Operator enables you to fine-tune each Pod’s CPU, memory, and storage according to workload demands.