|
| 1 | +--- |
| 2 | +title: Customizing Pod Resource Configurations and Labels in a MySQL Cluster Managed by KubeBlocks |
| 3 | +description: Learn how to customize Pod resource configurations and labels in a MySQL cluster using KubeBlocks to meet specific workload demands. |
| 4 | +keywords: [KubeBlocks, MySQL, Pod Resource Configuration, Kubernetes, Cluster Management] |
| 5 | +sidebar_position: 2 |
| 6 | +sidebar_label: Custom Pod Resources |
| 7 | +--- |
| 8 | + |
| 9 | +# Customizing Pod Resource Configurations and Labels in a MySQL Cluster Managed by KubeBlocks |
| 10 | + |
| 11 | +In certain scenarios, different Pods within the same database cluster may require varying resource allocations. For example: |
| 12 | +- Some replicas dedicated to report generation might leverage additional resources to handle analytical queries efficiently. |
| 13 | + |
| 14 | +With KubeBlocks, you can customize Pod resource configurations and labels to tailor each instance to meet its unique requirements. |
| 15 | +This guide demonstrates how to deploy a MySQL cluster with a custom replica provisioned with higher CPU and memory resources. |
| 16 | + |
| 17 | +## Prerequisites |
| 18 | + |
| 19 | +Before proceeding, ensure the following: |
| 20 | +- Environment Setup: |
| 21 | + - A Kubernetes cluster is up and running. |
| 22 | + - The kubectl CLI tool is configured to communicate with your cluster. |
| 23 | + - KubeBlocks CLI and KubeBlocks Operator are installed. Follow the installation instructions here. |
| 24 | +- Namespace Preparation: To keep resources isolated, create a dedicated namespace for this tutorial: |
| 25 | + |
| 26 | +```bash |
| 27 | +$ kubectl create ns demo |
| 28 | +namespace/demo created |
| 29 | +``` |
| 30 | + |
| 31 | +## Deploy a MySQL Semi-Synchronous Cluster |
| 32 | + |
| 33 | +Deploy a 2-node semi-sync MySQL cluster (1 primary, 1 secondary): |
| 34 | + |
| 35 | +```yaml |
| 36 | +kubectl apply -f - <<EOF |
| 37 | +apiVersion: apps.kubeblocks.io/v1 |
| 38 | +kind: Cluster |
| 39 | +metadata: |
| 40 | + name: example-mysql-cluster |
| 41 | + namespace: demo |
| 42 | +spec: |
| 43 | + clusterDef: mysql |
| 44 | + topology: semisync |
| 45 | + terminationPolicy: Delete |
| 46 | + componentSpecs: |
| 47 | + - name: mysql |
| 48 | + serviceVersion: 8.0.35 |
| 49 | + replicas: 2 |
| 50 | + resources: |
| 51 | + limits: |
| 52 | + cpu: '0.5' |
| 53 | + memory: 0.5Gi |
| 54 | + requests: |
| 55 | + cpu: '0.5' |
| 56 | + memory: 0.5Gi |
| 57 | + volumeClaimTemplates: |
| 58 | + - name: data |
| 59 | + spec: |
| 60 | + storageClassName: "" |
| 61 | + accessModes: |
| 62 | + - ReadWriteOnce |
| 63 | + resources: |
| 64 | + requests: |
| 65 | + storage: 20Gi |
| 66 | +EOF |
| 67 | +``` |
| 68 | + |
| 69 | +## Verifying the Deployment |
| 70 | + |
| 71 | +Monitor the cluster status and wait until the STATUS changes to Running: |
| 72 | +```bash |
| 73 | +$ kubectl get cluster example-mysql-cluster -n demo -w |
| 74 | +``` |
| 75 | +Example Output: |
| 76 | +```bash |
| 77 | +NAME CLUSTER-DEFINITION TERMINATION-POLICY STATUS AGE |
| 78 | +example-mysql-cluster mysql Delete Creating 17s |
| 79 | +example-mysql-cluster mysql Delete Running 2m33s |
| 80 | +``` |
| 81 | + |
| 82 | + |
| 83 | +## Add a Custom Replica with Different Resource Configurations |
| 84 | +To add a new replica with higher CPU and memory allocations, update the cluster specification as follows: |
| 85 | +```yaml |
| 86 | +kubectl apply -f - <<EOF |
| 87 | +apiVersion: apps.kubeblocks.io/v1 |
| 88 | +kind: Cluster |
| 89 | +metadata: |
| 90 | + name: example-mysql-cluster |
| 91 | + namespace: demo |
| 92 | +spec: |
| 93 | + clusterDef: mysql |
| 94 | + topology: semisync |
| 95 | + terminationPolicy: Delete |
| 96 | + componentSpecs: |
| 97 | + - name: mysql |
| 98 | + serviceVersion: 8.0.35 |
| 99 | + replicas: 3 |
| 100 | + resources: |
| 101 | + limits: |
| 102 | + cpu: '0.5' |
| 103 | + memory: 0.5Gi |
| 104 | + requests: |
| 105 | + cpu: '0.5' |
| 106 | + memory: 0.5Gi |
| 107 | + volumeClaimTemplates: |
| 108 | + - name: data |
| 109 | + spec: |
| 110 | + storageClassName: "" |
| 111 | + accessModes: |
| 112 | + - ReadWriteOnce |
| 113 | + resources: |
| 114 | + requests: |
| 115 | + storage: 20Gi |
| 116 | + instances: |
| 117 | + - name: custom |
| 118 | + replicas: 1 |
| 119 | + labels: |
| 120 | + custom-resource-config: "true" |
| 121 | + resources: |
| 122 | + limits: |
| 123 | + cpu: 1 |
| 124 | + memory: 1Gi |
| 125 | + requests: |
| 126 | + cpu: 1 |
| 127 | + memory: 1Gi |
| 128 | +EOF |
| 129 | +``` |
| 130 | +**Explanation:** |
| 131 | +- Default Resource Configuration: The mysql component is configured with 0.5 CPU and 0.5Gi memory per Pod. |
| 132 | +- Custom Resource Configuration: A single instance (custom) is configured with 1 CPU and 1Gi memory. |
| 133 | + |
| 134 | + |
| 135 | +## Verifying the Deployment |
| 136 | +Monitor the cluster status until it returns to Running: |
| 137 | +```bash |
| 138 | +$ kubectl get cluster -n demo -w |
| 139 | +``` |
| 140 | +Example output: |
| 141 | +```bash |
| 142 | +NAME CLUSTER-DEFINITION TERMINATION-POLICY STATUS AGE |
| 143 | +example-mysql-cluster mysql Delete Updating 79s |
| 144 | +example-mysql-cluster mysql Delete Running 2m44s |
| 145 | +``` |
| 146 | +List all the Pods in the cluster: |
| 147 | +```bash |
| 148 | +$ kubectl get pods -n demo |
| 149 | +``` |
| 150 | +Example Output: |
| 151 | +```bash |
| 152 | +NAME READY STATUS RESTARTS AGE |
| 153 | +example-mysql-cluster-mysql-0 4/4 Running 0 2m49s |
| 154 | +example-mysql-cluster-mysql-1 4/4 Running 0 2m49s |
| 155 | +example-mysql-cluster-mysql-custom-0 4/4 Running 0 97s |
| 156 | +``` |
| 157 | +**Observation**: |
| 158 | +- The custom Pod name includes "-custom" in the middle (e.g., 'example-mysql-cluster-mysql-custom-0'), reflecting the instance template name "custom". |
| 159 | + |
| 160 | +Use the KubeBlocks CLI to check the cluster and verify the resource configurations: |
| 161 | +```bash |
| 162 | +$ kbcli cluster describe example-mysql-cluster -n demo |
| 163 | +``` |
| 164 | +Example Output: |
| 165 | +```bash |
| 166 | +Resources Allocation: |
| 167 | +COMPONENT INSTANCE-TEMPLATE CPU(REQUEST/LIMIT) MEMORY(REQUEST/LIMIT) STORAGE-SIZE STORAGE-CLASS |
| 168 | +mysql custom 1 / 1 1Gi / 1Gi data:20Gi <none> |
| 169 | +mysql 500m / 500m 512Mi / 512Mi data:20Gi <none> |
| 170 | +``` |
| 171 | +**Observation**: |
| 172 | +- The default replica has 0.5 CPU and 0.5Gi memory. |
| 173 | +- The custom replica has 1 CPU and 1Gi memory. |
| 174 | + |
| 175 | +## Expose the custom Pod as a Service |
| 176 | +To expose the custom Pod via a separate Service, use the following configuration: |
| 177 | +```yaml |
| 178 | +kubectl apply -f - <<EOF |
| 179 | +apiVersion: apps.kubeblocks.io/v1 |
| 180 | +kind: Cluster |
| 181 | +metadata: |
| 182 | + name: example-mysql-cluster |
| 183 | + namespace: demo |
| 184 | +spec: |
| 185 | + clusterDef: mysql |
| 186 | + topology: semisync |
| 187 | + terminationPolicy: Delete |
| 188 | + # expose a service |
| 189 | + services: |
| 190 | + - name: custom-pod |
| 191 | + componentSelector: mysql |
| 192 | + serviceName: custom-pod |
| 193 | + spec: |
| 194 | + selector: |
| 195 | + custom-resource-config: "true" |
| 196 | + ipFamilyPolicy: PreferDualStack |
| 197 | + ports: |
| 198 | + - name: tcp-mysql |
| 199 | + port: 3306 |
| 200 | + protocol: TCP |
| 201 | + targetPort: mysql |
| 202 | + type: ClusterIP |
| 203 | + componentSpecs: |
| 204 | + - name: mysql |
| 205 | + serviceVersion: 8.0.35 |
| 206 | + replicas: 3 |
| 207 | + resources: |
| 208 | + limits: |
| 209 | + cpu: '0.5' |
| 210 | + memory: 0.5Gi |
| 211 | + requests: |
| 212 | + cpu: '0.5' |
| 213 | + memory: 0.5Gi |
| 214 | + volumeClaimTemplates: |
| 215 | + - name: data |
| 216 | + spec: |
| 217 | + storageClassName: "" |
| 218 | + accessModes: |
| 219 | + - ReadWriteOnce |
| 220 | + resources: |
| 221 | + requests: |
| 222 | + storage: 20Gi |
| 223 | + instances: |
| 224 | + - name: custom |
| 225 | + replicas: 1 |
| 226 | + labels: |
| 227 | + custom-resource-config: "true" |
| 228 | + resources: |
| 229 | + limits: |
| 230 | + cpu: 1 |
| 231 | + memory: 1Gi |
| 232 | + requests: |
| 233 | + cpu: 1 |
| 234 | + memory: 1Gi |
| 235 | +EOF |
| 236 | +``` |
| 237 | +### Verify the Update |
| 238 | +Check the Cluster status: |
| 239 | +```bash |
| 240 | +$ kubectl get cluster -n demo -w |
| 241 | +``` |
| 242 | +Example output: |
| 243 | +```bash |
| 244 | +NAME CLUSTER-DEFINITION TERMINATION-POLICY STATUS AGE |
| 245 | +example-mysql-cluster mysql Delete Updating 16m |
| 246 | +example-mysql-cluster mysql Delete Running 17m |
| 247 | +``` |
| 248 | +Then list the Services: |
| 249 | +```bash |
| 250 | +$ kubectl get svc -n demo |
| 251 | +``` |
| 252 | +Example output: |
| 253 | +```bash |
| 254 | +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE |
| 255 | +example-mysql-cluster-custom-pod ClusterIP 172.20.202.249 <none> 3306/TCP 12m |
| 256 | +example-mysql-cluster-mysql ClusterIP 172.20.11.166 <none> 3306/TCP 12m |
| 257 | +example-mysql-cluster-mysql-headless ClusterIP None <none> 3306/TCP,3601/TCP,9104/TCP,3501/TCP,3502/TCP,9901/TCP 12m |
| 258 | +``` |
| 259 | + |
| 260 | +### Accessing Your Custom Pod via the Service |
| 261 | +Retrieve the root credentials: |
| 262 | +```bash |
| 263 | +$ kubectl get secrets -n demo example-semisync-mysql-mysql-account-root -o jsonpath='{.data.username}' | base64 -d |
| 264 | + |
| 265 | +$ kubectl get secrets -n demo example-mysql-cluster-mysql-account-root -o jsonpath='{.data.password}' | base64 -d |
| 266 | +``` |
| 267 | +Example Output: |
| 268 | +```bash |
| 269 | +root |
| 270 | + |
| 271 | +uk263gR24s |
| 272 | +``` |
| 273 | + |
| 274 | +Connect to the custom Pod from inside one of the MySQL containers: |
| 275 | +```bash |
| 276 | +$ kubectl exec -it example-mysql-cluster-mysql-0 -n demo -- mysql -hexample-mysql-cluster-custom-pod -uroot -puk263gR24s |
| 277 | +``` |
| 278 | +This custom Pod is provisioned with additional resources, making it ideal for running complex queries or analytical workloads. |
| 279 | + |
| 280 | +## Cleanup |
| 281 | +To remove all created resources, delete the MySQL cluster along with its namespace: |
| 282 | + |
| 283 | +```bash |
| 284 | +kubectl delete cluster example-mysql-cluster -n demo |
| 285 | +kubectl delete ns demo |
| 286 | +``` |
| 287 | + |
| 288 | + |
| 289 | +## Conclusion |
| 290 | +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. |
0 commit comments