Home > Database > Mysql Tutorial > How to use statefulset to build a MySQL cluster in kubernetes

How to use statefulset to build a MySQL cluster in kubernetes

WBOY
Release: 2023-05-27 21:10:27
forward
896 people have browsed it

Implementation steps

  • Create a StatefulSet object in the Kubernetes cluster.

  • Create a headless service to provide service discovery and load balancing for StatefulSet.

  • Define three MySQL containers in the StatefulSet and configure them in master-slave replication mode.

  • Use Kubernetes ConfigMap and Secret to store the configuration information and authentication information of the container.

  • Check whether the cluster is working properly.

Specific code

First, create a namespace named mysql in the Kubernetes cluster:

kubectl create namespace mysql
Copy after login

Then, Create a ConfigMap to store the configuration information of the MySQL container, such as the MySQL configuration file my.cnf:

apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-config
  namespace: mysql
data:
  my.cnf: |
    [mysqld]
    server-id=1
    log-bin=mysql-bin
    binlog_format=row
    innodb_flush_log_at_trx_commit=1
Copy after login

Next, create a Secret to store the authentication information of the MySQL container, such as user name and password :

apiVersion: v1
kind: Secret
metadata:
  name: mysql-secret
  namespace: mysql
type: Opaque
data:
  username: dXNlcm5hbWU=
  password: cGFzc3dvcmQ=
Copy after login

Among them, username and password are the base64-encoded username and password respectively.

Create a StatefulSet object to define MySQL

Then, create a StatefulSet object to define the configuration and number of copies of the MySQL container:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
  namespace: mysql
spec:
  replicas: 3
  serviceName: mysql-headless
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.7
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secret
              key: password
        volumeMounts:
        - name: mysql-data
          mountPath: /var/lib/mysql
        - name: mysql-config
          mountPath: /etc/mysql/conf.d/my.cnf
          subPath: my.cnf
      volumes:
      - name: mysql-data
        persistentVolumeClaim:
          claimName: mysql-data
      - name: mysql-config
        configMap:
          name: mysql-config
  volumeClaimTemplates:
  - metadata:
      name: mysql-data
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 10Gi
Copy after login

In the above YAML file, we A StatefulSet object named mysql is defined, which contains three MySQL containers. Each container has a persistent volume for storing MySQL data and mounts the MySQL configuration file stored by ConfigMap.

Create a headless service

Next, we need to create a headless service. This service will not assign ClusterIP, but will create multiple Endpoints. Each Endpoint corresponds to the IP address of a Pod. , can be used to directly access the Pod:

apiVersion: v1
kind: Service
metadata:
  name: mysql-headless
  namespace: mysql
spec:
  ports:
  - name: mysql
    port: 3306
  clusterIP: None
  selector:
    app: mysql
Copy after login

Finally, we need to check whether MySQL master-slave replication is working properly. Use the kubectl command line tool to enter the container in Kubernetes and execute the corresponding commands. If you want to go into the pod and connect to the master node of the MySQL container to create the table, you can do this by running the following command:

kubectl exec -it mysql-0 --namespace=mysql -- /bin/bash
mysql -u root -p
create database testdb;
use testdb;
create table testtable (id int, name varchar(255));
insert into testtable values (1, 'test');
exit;
Copy after login

The above is the detailed content of How to use statefulset to build a MySQL cluster in kubernetes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template