mysql在k8s环境里安装及搭建主从架构

embedded/2024/12/23 7:56:13/

1、环境准备

k8s集群,版本1.27.0

2、搭建nfs服务器

本次用的k8smaster节点作为nfs服务器,因为需要在两个工作节点上连接nfs,所以工作节点上也要安装nfs

yum install -y nfs-utils

我们直接在nfs服务器(k8s-master)当中创建这三个目录并写入 /etc/exports 文件夹中(创建的目录可以修改)

mkdir -p /data/nfs/{mysql-master,mysql-slaver-01,mysql-slaver-02}

cat >> /etc/exports << EOF

/data/nfs/mysql-master *(rw,sync,no_root_squash)

/data/nfs/mysql-slaver-01 *(rw,sync,no_root_squash)

/data/nfs/mysql-slaver-02 *(rw,sync,no_root_squash)

EOF

执行命令启动并验证目录是否暴露成功

systemctl enable --now nfs-server

showmount -e 192.168.15.21

3、安装mysql集群

(1) 创建命名空间

kubectl create namespace mysql

apiVersion: v1

kind: Namespace

metadata:

        name: mysql

spec: {}

status: {}

(2) 创建MySQL密码的Secret

创建一个存储了MySQL密码的Secret,我们可以直接使用这行命令生成这个Secret的资源清单文件:

# 注意修改root的密码和命名空间,我的root密码设置为的是qwerty,生成后存入yaml文件执行

kubectl create secret generic hamysql-password --namespace=mysql --from-literal=mysql_root_password=qwerty --dry-run=client -o=yaml

(3) 安装MySQL主节点

创建pv和pvc

apiVersion: v1

kind: PersistentVolume

metadata:

  name: deploy-mysql-master-nfs-pv

  namespace: mysql

spec:

  capacity:

    storage: 1Gi

  accessModes:

    - ReadWriteMany

  nfs:

    # 注意修改IP地址和暴露的目录(如果不一样)

    server: 192.168.1.160

    path: /data/nfs/mysql-master

  storageClassName: "nfs"

---

apiVersion: v1

kind: PersistentVolumeClaim

metadata:

  name: deploy-mysql-master-nfs-pvc

  namespace: mysql

spec:

  accessModes:

    - ReadWriteMany

  storageClassName: "nfs"

  resources:

    requests:

      storage: 1Gi

  volumeName: deploy-mysql-master-nfs-pv

创建pv和pvc kubectl apply -f xxx

kubectl get pv,pvc -n mysql

写一个my.cnf文件

[mysqld]

skip-host-cache

skip-name-resolve

datadir          = /var/lib/mysql

socket           = /var/run/mysqld/mysqld.sock

secure-file-priv = /var/lib/mysql-files

pid-file         = /var/run/mysqld/mysqld.pid

user             = mysql

secure-file-priv = NULL

server-id        = 1

log-bin          = master-bin

log_bin_index    = master-bin.index

binlog_do_db     =wvg_user

binlog_ignore_db = information_schema

binlog_ignore_db = mysql

binlog_ignore_db = performance_schema

binlog_ignore_db = sys

binlog-format    = ROW

[client]

socket           = /var/run/mysqld/mysqld.sock

!includedir /etc/mysql/conf.d/

注意几个配置

# server id,要注意多个mysql节点唯一

server-id        = 1

# 生成的logbin的文件名

log-bin          = master-bin

log_bin_index    = master-bin.index

# 同步哪个数据库,这里我们为了测试之同步wvg_user这个数据库,写*/不写就是所有都同步

binlog_do_db     = wvg_user

# 排除哪个数据库,可以写多行,排除的数据库不会被主从同步,这里写上mysql自带的几个数据库

binlog_ignore_db = information_schema

... # 还有几行省略

# binlog的格式

binlog-format    = ROW

接下来将创建一个ConfigMap来存储这个配置文件。可以使用以下配置生成yaml资源清单文件内容:

kubectl create configmap mysql-master-cm -n mysql --from-file=my.cnf --dry-run=client -o yaml

部署mysql主节点

apiVersion: v1

data:

  my.cnf: |

    [mysqld]

    skip-host-cache

    skip-name-resolve

    datadir          = /var/lib/mysql

    socket           = /var/run/mysqld/mysqld.sock

    secure-file-priv = /var/lib/mysql-files

    pid-file         = /var/run/mysqld/mysqld.pid

    user             = mysql

    secure-file-priv = NULL

    server-id        = 1

    log-bin          = master-bin

    log_bin_index    = master-bin.index

    binlog_do_db     = wvg_user

    binlog_ignore_db = information_schema

    binlog_ignore_db = mysql

    binlog_ignore_db = performance_schema

    binlog_ignore_db = sys

    binlog-format    = ROW

    [client]

    socket           = /var/run/mysqld/mysqld.sock

    !includedir /etc/mysql/conf.d/

kind: ConfigMap

metadata:

  name: mysql-master-cm

  namespace: mysql

---

apiVersion: v1

kind: Service

metadata:

  name: deploy-mysql-master-svc

  namespace: mysql

  labels:

    app: mysql-master

spec:

  ports:

  - port: 3306

    name: mysql

    targetPort: 3306

    nodePort: 30306

  selector:

    app: mysql-master

  type: NodePort

  sessionAffinity: ClientIP

---

apiVersion: apps/v1

kind: StatefulSet

metadata:

  name: deploy-mysql-master

  namespace: mysql

spec:

  selector:

    matchLabels:

      app: mysql-master

  serviceName: "deploy-mysql-master-svc"

  replicas: 1

  template:

    metadata:

      labels:

        app: mysql-master

    spec:

      terminationGracePeriodSeconds: 10

      containers:

      - args:

        - --character-set-server=utf8mb4

        - --collation-server=utf8mb4_unicode_ci

        - --lower_case_table_names=1

        - --default-time_zone=+8:00

        name: mysql

        image: docker.io/library/mysql:8.0.34

        ports:

        - containerPort: 3306

          name: mysql

        volumeMounts:

        - name: mysql-data

          mountPath: /var/lib/mysql

        - name: mysql-conf

          mountPath: /etc/my.cnf

          readOnly: true

          subPath: my.cnf

        env:

        - name: MYSQL_ROOT_PASSWORD

          valueFrom:

            secretKeyRef:

              key: mysql_root_password

              name: mysql-password

      volumes:

      - name: mysql-data

        persistentVolumeClaim:

          claimName: deploy-mysql-master-nfs-pvc

      - name: mysql-conf

        configMap:

          name: mysql-master-cm

          items:

          - key: my.cnf

            mode: 0644

            path: my.cnf

部署这个文件   kubectl apply -f mysql-master.yaml

(4) 安装mysql两个从节点

创建pv和pvc

apiVersion: v1

kind: PersistentVolume

metadata:

  name: deploy-mysql-slave-01-nfs-pv

  namespace: mysql

spec:

  capacity:

    storage: 1Gi

  accessModes:

    - ReadWriteMany

  nfs:

    server: 192.168.15.21

    path: /data/nfs/mysql-slaver-01

  storageClassName: "nfs"

---

apiVersion: v1

kind: PersistentVolumeClaim

metadata:

  name: deploy-mysql-slave-01-nfs-pvc

  namespace: mysql

spec:

  accessModes:

    - ReadWriteMany

  storageClassName: "nfs"

  resources:

    requests:

      storage: 1Gi

  volumeName: deploy-mysql-slave-01-nfs-pv

第一个从节点的配置文件

[mysqld]

skip-host-cache

skip-name-resolve

datadir          = /var/lib/mysql

socket           = /var/run/mysqld/mysqld.sock

secure-file-priv = /var/lib/mysql-files

pid-file         = /var/run/mysqld/mysqld.pid

user             = mysql

secure-file-priv = NULL

server-id        = 2

log-bin          = slave-bin

relay-log        = slave-relay-bin

relay-log-index  = slave-relay-bin.index

[client]

socket           = /var/run/mysqld/mysqld.sock

!includedir /etc/mysql/conf.d/

创建configmap

kubectl create configmap mysql-slave-01-cm -n mysql --from-file=my.cnf --dry-run=client -o yaml

创建从节点yaml文件

apiVersion: v1

data:

  my.cnf: |

    [mysqld]

    skip-host-cache

    skip-name-resolve

    datadir          = /var/lib/mysql

    socket           = /var/run/mysqld/mysqld.sock

    secure-file-priv = /var/lib/mysql-files

    pid-file         = /var/run/mysqld/mysqld.pid

    user             = mysql

    secure-file-priv = NULL

    server-id        = 2

    log-bin          = slave-bin

    relay-log        = slave-relay-bin

    relay-log-index  = slave-relay-bin.index

    [client]

    socket           = /var/run/mysqld/mysqld.sock

    !includedir /etc/mysql/conf.d/

kind: ConfigMap

metadata:

  name: mysql-slave-01-cm

  namespace: mysql

---

apiVersion: v1

kind: Service

metadata:

  name: deploy-mysql-slave-svc

  namespace: mysql

  labels:

    app: mysql-slave

spec:

  ports:

  - port: 3306

    name: mysql

    targetPort: 3306

    nodePort: 30308

  selector:

    app: mysql-slave

  type: NodePort

  sessionAffinity: ClientIP

---

apiVersion: apps/v1

kind: StatefulSet

metadata:

  name: deploy-mysql-slave-01

  namespace: mysql

spec:

  selector:

    matchLabels:

      app: mysql-slave

  serviceName: "deploy-mysql-slave-svc"

  replicas: 1

  template:

    metadata:

      labels:

        app: mysql-slave

    spec:

      terminationGracePeriodSeconds: 10

      containers:

      - args:

        - --character-set-server=utf8mb4

        - --collation-server=utf8mb4_unicode_ci

        - --lower_case_table_names=1

        - --default-time_zone=+8:00

        name: mysql

        image: docker.io/library/mysql:8.0.34

        ports:

        - containerPort: 3306

          name: mysql

        volumeMounts:

        - name: mysql-data

          mountPath: /var/lib/mysql

        - name: mysql-conf

          mountPath: /etc/my.cnf

          readOnly: true

          subPath: my.cnf

        env:

        - name: MYSQL_ROOT_PASSWORD

          valueFrom:

            secretKeyRef:

              key: mysql_root_password

              name: mysql-password

      volumes:

      - name: mysql-data

        persistentVolumeClaim:

          claimName: deploy-mysql-slave-01-nfs-pvc

      - name: mysql-conf

        configMap:

          name: mysql-slave-01-cm

          items:

          - key: my.cnf

            mode: 0644

            path: my.cnf

第二个pv和pvc

apiVersion: v1

kind: PersistentVolume

metadata:

  name: deploy-mysql-slave-02-nfs-pv

  namespace: mysql

spec:

  capacity:

    storage: 1Gi

  accessModes:

    - ReadWriteMany

  nfs:

    server: 192.168.15.21

    path: /data/nfs/mysql-slaver-02

  storageClassName: "nfs"

---

apiVersion: v1

kind: PersistentVolumeClaim

metadata:

  name: deploy-mysql-slave-02-nfs-pvc

  namespace: mysql

spec:

  accessModes:

    - ReadWriteMany

  storageClassName: "nfs"

  resources:

    requests:

      storage: 1Gi

  volumeName: deploy-mysql-slave-02-nfs-pv

配置文件

[mysqld]

skip-host-cache

skip-name-resolve

datadir          = /var/lib/mysql

socket           = /var/run/mysqld/mysqld.sock

secure-file-priv = /var/lib/mysql-files

pid-file         = /var/run/mysqld/mysqld.pid

user             = mysql

secure-file-priv = NULL

server-id        = 3

log-bin          = slave-bin

relay-log        = slave-relay-bin

relay-log-index  = slave-relay-bin.index

[client]

socket           = /var/run/mysqld/mysqld.sock

!includedir /etc/mysql/conf.d/

configmap

kubectl create configmap mysql-slave-02-cm -n deploy-test --from-file=my.cnf --dry-run=client -o yaml

安装mysql2

apiVersion: v1

data:

  my.cnf: |

    [mysqld]

    skip-host-cache

    skip-name-resolve

    datadir          = /var/lib/mysql

    socket           = /var/run/mysqld/mysqld.sock

    secure-file-priv = /var/lib/mysql-files

    pid-file         = /var/run/mysqld/mysqld.pid

    user             = mysql

    secure-file-priv = NULL

    server-id        = 3

    log-bin          = slave-bin

    relay-log        = slave-relay-bin

    relay-log-index  = slave-relay-bin.index

    [client]

    socket           = /var/run/mysqld/mysqld.sock

    !includedir /etc/mysql/conf.d/

kind: ConfigMap

metadata:

  name: mysql-slave-02-cm

  namespace: mysql

---

apiVersion: apps/v1

kind: StatefulSet

metadata:

  name: deploy-mysql-slave-02

  namespace: mysql

spec:

  selector:

    matchLabels:

      app: mysql-slave-02

  serviceName: "deploy-mysql-slave-svc"

  replicas: 1

  template:

    metadata:

      labels:

        app: mysql-slave-02

    spec:

      terminationGracePeriodSeconds: 10

      containers:

      - args:

        - --character-set-server=utf8mb4

        - --collation-server=utf8mb4_unicode_ci

        - --lower_case_table_names=1

        - --default-time_zone=+8:00

        name: mysql

        image: docker.io/library/mysql:8.0.34

        ports:

        - containerPort: 3306

          name: mysql

        volumeMounts:

        - name: mysql-data

          mountPath: /var/lib/mysql

        - name: mysql-conf

          mountPath: /etc/my.cnf

          readOnly: true

          subPath: my.cnf

        env:

        - name: MYSQL_ROOT_PASSWORD

          valueFrom:

            secretKeyRef:

              key: mysql_root_password

              name: mysql-password

      volumes:

      - name: mysql-data

        persistentVolumeClaim:

          claimName: deploy-mysql-slave-02-nfs-pvc

      - name: mysql-conf

        configMap:

          name: mysql-slave-02-cm

          items:

          - key: my.cnf

            mode: 0644

            path: my.cnf

4、搭建主从集群并验证

kubectl exec -itn mysql pod/deploy-mysql-master-0 -- mysql -uroot -p qwerty

show master status;

来到子节点执行以下命令

change master to master_host='deploy-mysql-master-0.deploy-mysql-master-svc.deploy-test.svc.cluster.local', master_port=3306, master_user='root', master_password='root', master_log_file='master-bin.000003', master_log_pos=157, master_connect_retry=30, get_master_public_key=1;

master_host: 这个参数是master的地址,kubernetes提供的解析规则是 pod名称.service名称.命名空间.svc.cluster.local ,所以我们master的mysql地址是 deploy-mysql-master-0.deploy-mysql-master-svc.deploy-test.svc.cluster.local

master_port: 主节点的mysql端口,我们没改默认是3306

master_user: 登录到主节点的mysql用户

master_password: 登录到主节点要用到的密码

master_log_file: 我们之前查看mysql主节点状态时候的 File 字段

master_log_pos: 我们之前查看mysql主节点状态时候的 Position 字段

master_connect_retry: 主节点重连时间

get_master_public_key: 连接主mysql的公钥获取方式

然后 执行

start slave;

show slave status\G   查看状态

测试主从集群

CREATE DATABASE `wvg_user`;

USE `wvg_user`;

CREATE TABLE `user` (

  `user_id` BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT COMMENT '用户id',

  `username` VARCHAR(50) NOT NULL COMMENT '用户名',

  `age` TINYINT UNSIGNED DEFAULT 18 COMMENT '年龄',

  `gender` TINYINT UNSIGNED DEFAULT 2 COMMENT '性别;0=男,1=女,2=未知'

) COMMENT '用户表';

INSERT INTO `user` (`username`, `age`, `gender`) VALUES ('wvg', '23', '0');


http://www.ppmy.cn/embedded/100450.html

相关文章

【Git】修复Git RPC Failed HTTP 500 curl 22错误

现象 Git推送分支时&#xff0c;被中断&#xff0c;输出以下错误 error: RPC failed; HTTP 500 curl 22 The requested URL returned error: 500原因分析 Git设置的通信缓存较小&#xff0c;对于数据量较大的分支&#xff0c;推送时缓存空间不够&#xff0c;导致传输中断 解…

云端Ruby:将应用无缝融入云服务的艺术

标题&#xff1a;云端Ruby&#xff1a;将应用无缝融入云服务的艺术 随着云计算技术的飞速发展&#xff0c;越来越多的开发者选择将应用部署在云端。Ruby&#xff0c;作为一种灵活且功能强大的编程语言&#xff0c;可以轻松地与云服务集成&#xff0c;以实现弹性扩展、持续集成…

算法题目杂记

差分 来源 https://www.acwing.com/problem/content/799/ 题目 输入一个长度为 n的整数序列。 接下来输入 m个操作&#xff0c;每个操作包含三个整数 l,r,c&#xff0c;表示将序列中 [l,r] 之间的每个数加 c。 请你输出进行完所有操作后的序列。 输入格式 第一行包含两个整数…

F - Dist Max 2

// 特别是二分的验证。。 // 两个维度 第一个维度用滑窗来控制|x1-x2|>k // 第二个维度 只需要迭代mx 和mi 。。最远距离一定出现在这两个端点之一。。 // 这是一个我感觉挺特别的二维。。维护的数据。。。问题。。。 #include <bits/stdc.h> using namespace std; …

C++语言学习(八)—— 继承、派生与多态(二)

目录 一、多继承 1.1 多继承中的二义性问题 1.2 虚基类 二、多态 2.1 静态绑定与静态多态 2.2 动态绑定与动态多态 三、运算符重载 3.1 重载、- -运算符 3.2 重载赋值运算符 3.3 重载输出流<<、输入流>>运算符 3.3.1 重载输出流&#xff08;<<&am…

K8S 无状态应用有状态应用

在Kubernetes&#xff08;简称K8S&#xff09;中&#xff0c;无状态应用&#xff08;Stateless Applications&#xff09;和有状态应用&#xff08;Stateful Applications&#xff09;是描述应用行为和架构的两个重要概念&#xff0c;它们对于如何设计、部署和管理应用程序在Ku…

【学习笔记】Day 20

一、进度概述 1、机器学习常识12-18&#xff0c;以及相关代码复现 二、详情 12、SVM&#xff08;support vector machines&#xff0c;支持向量机&#xff09; 实际上&#xff0c;支持向量机是一种二分类模型&#xff0c;它将实例的特征向量映射为空间中的一些点&#xff0c;…

JS基础进阶3-DOM事件

DOM事件流 一、定义 DOM事件流指的是从页面接收事件的顺序。这个路径包括了事件的捕获阶段、目标阶段和冒泡阶段。 图片来源黑马pink老师PPT 二、事件流阶段 DOM事件流涉及三个主要阶段&#xff1a; 捕获阶段&#xff08;Capturing Phase&#xff09;&#xff1a; 事件从…