首页 > 数据库 >基于k8s的statefulset+pv安装mysql5.7主从集群

基于k8s的statefulset+pv安装mysql5.7主从集群

时间:2023-09-15 19:12:10浏览次数:54  
标签:statefulset pv name mysql5.7 datadir xtrabackup nfs mysql master

前提假设:

1.已安装k8s环境;

2.因为我使用nfs作为pv存储介质,所以需要预先配置好nfs服务

nfs安装可参考:https://blog.csdn.net/wudinaniya/article/details/81068518

 

步骤:

1.规划mysql持久化文件在nfs中的存储路径;

2.创建mysql pv;

3.创建mysql configmap;

4.创建mysql service;

5.创建mysql statefulset;

6.验证mysql是否安装成功。

 

步骤1:

在nfs路径下(我是192.168.152.12的/home/nfs/volumes),创建mysql-datadir-1  mysql-datadir-2  mysql-datadir-3三个文件夹

mkdir mysql-datadir-1  mysql-datadir-2  mysql-datadir-3

 

 

步骤2:创建pv

pv.yaml:

指向步骤1中的3个mysql-datadir文件夹。 文件中的server填入nfs-server服务所在ip

 

复制代码
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-datadir-1
spec:
  capacity:
    storage: 2Gi
  accessModes: ["ReadWriteMany","ReadWriteOnce"]
  nfs:
    path: /home/nfs/volumes/mysql-datadir-1
    server: 192.168.152.12

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-datadir-2
spec:
  capacity:
    storage: 2Gi
  accessModes: ["ReadWriteMany","ReadWriteOnce"]
  nfs:
    path: /home/nfs/volumes/mysql-datadir-2
    server: 192.168.152.12

---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-datadir-3
spec:
  capacity:
    storage: 2Gi
  accessModes: ["ReadWriteMany","ReadWriteOnce"]
  nfs:
    path: /home/nfs/volumes/mysql-datadir-3
    server: 192.168.152.12
复制代码

 

创建pv

kubectl apply -f pv.yaml

 

步骤3:创建mysql configmap

configmap.yaml:

复制代码
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql
  labels:
    app: mysql
data:
  master.cnf: |
    [mysqld]
    log-bin
    default-time-zone='+8:00'
    log_bin_trust_function_creators=1
    lower_case_table_names=1
  slave.cnf: |
    [mysqld]
    super-read-only
    default-time-zone='+8:00'
    log_bin_trust_function_creators=1
复制代码

 

创建configmap

kubectl apply -f configmap.yaml 

 

 

 

 

 

 

步骤4:创建mysql service

service.yaml: 其中服务mysql用于读写;服务mysql-read用于只读。

复制代码
apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  ports:
  - name: mysql
    port: 3306
  clusterIP: None
  selector:
    app: mysql

---
apiVersion: v1
kind: Service
metadata:
  name: mysql-read
  labels:
    app: mysql
spec:
  ports:
  - name: mysql
    port: 3306
  selector:
    app: mysql
复制代码

 

创建service:

kubectl apply -f service.yaml

 

 

 

步骤5:创建statefulset

statefulset.yaml:

注意事项:官网或其他博客用的xtrabackup镜像无法拉取,所以使用国内阿里云的镜像替代:registry.cn-hangzhou.aliyuncs.com/hxpdocker/xtrabackup:1.0

复制代码
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  serviceName: mysql
  replicas: 3
  template:
    metadata:
      labels:
        app: mysql
    spec:
      initContainers:            
      - name: init-mysql
        image: mysql:5.7
        command:
        - bash
        - "-c"
        - |
          set -ex
          # Generate mysql server-id from pod ordinal index.
          [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
          ordinal=${BASH_REMATCH[1]}
          echo [mysqld] > /mnt/conf.d/server-id.cnf
          # Add an offset to avoid reserved server-id=0 value.
          echo server-id=$((100 + $ordinal)) >> /mnt/conf.d/server-id.cnf
          # Copy appropriate conf.d files from config-map to emptyDir.
          if [[ $ordinal -eq 0 ]]; then
            cp /mnt/config-map/master.cnf /mnt/conf.d/
          else
            cp /mnt/config-map/slave.cnf /mnt/conf.d/
          fi
        volumeMounts:
        - name: conf
          mountPath: /mnt/conf.d
        - name: config-map
          mountPath: /mnt/config-map
      - name: clone-mysql
        image: registry.cn-hangzhou.aliyuncs.com/hxpdocker/xtrabackup:1.0
        command:
        - bash
        - "-c"
        - |
          set -ex
          # Skip the clone if data already exists.
          [[ -d /var/lib/mysql/mysql ]] && exit 0
          # Skip the clone on master (ordinal index 0).
          [[ `hostname` =~ -([0-9]+)$ ]] || exit 1
          ordinal=${BASH_REMATCH[1]}
          [[ $ordinal -eq 0 ]] && exit 0
          # Clone data from previous peer.
          ncat --recv-only mysql-$(($ordinal-1)).mysql 3307 | xbstream -x -C /var/lib/mysql
          # Prepare the backup.
          xtrabackup --prepare --target-dir=/var/lib/mysql
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
          subPath: mysql
        - name: conf
          mountPath: /etc/mysql/conf.d
      containers:
      - name: mysql                 
        image: mysql:5.7
        env:
        - name: MYSQL_ALLOW_EMPTY_PASSWORD
          value: "1"
        ports:
        - name: mysql
          containerPort: 3306
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
          subPath: mysql
        - name: conf
          mountPath: /etc/mysql/conf.d
        resources:
          requests:
            cpu: 500m
            memory: 1Gi
        livenessProbe:
          exec:
            command: ["mysqladmin", "ping"]
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
        readinessProbe:
          exec:
            # Check we can execute queries over TCP (skip-networking is off).
            command: ["mysql", "-h", "127.0.0.1", "-e", "SELECT 1"]
          initialDelaySeconds: 5
          periodSeconds: 2
          timeoutSeconds: 1
      - name: xtrabackup         
        image: registry.cn-hangzhou.aliyuncs.com/hxpdocker/xtrabackup:1.0
        ports:
        - name: xtrabackup
          containerPort: 3307
        command:
        - bash
        - "-c"
        - |
          set -ex
          cd /var/lib/mysql

          # Determine binlog position of cloned data, if any.
          if [[ -f xtrabackup_slave_info && "x$(<xtrabackup_slave_info)" != "x" ]]; then
            # XtraBackup already generated a partial "CHANGE MASTER TO" query
            # because we're cloning from an existing slave. (Need to remove the tailing semicolon!)
            cat xtrabackup_slave_info | sed -E 's/;$//g' > change_master_to.sql.in
            # Ignore xtrabackup_binlog_info in this case (it's useless).
            rm -f xtrabackup_slave_info xtrabackup_binlog_info
          elif [[ -f xtrabackup_binlog_info ]]; then
            # We're cloning directly from master. Parse binlog position.
            [[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1
            rm -f xtrabackup_binlog_info xtrabackup_slave_info
            echo "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\
                  MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.in
          fi

          # Check if we need to complete a clone by starting replication.
          if [[ -f change_master_to.sql.in ]]; then
            echo "Waiting for mysqld to be ready (accepting connections)"
            until mysql -h 127.0.0.1 -e "SELECT 1"; do sleep 1; done

            echo "Initializing replication from clone position"
            mysql -h 127.0.0.1 \
                  -e "$(<change_master_to.sql.in), \
                          MASTER_HOST='mysql-0.mysql', \
                          MASTER_USER='root', \
                          MASTER_PASSWORD='', \
                          MASTER_CONNECT_RETRY=10; \
                        START SLAVE;" || exit 1
            # In case of container restart, attempt this at-most-once.
            mv change_master_to.sql.in change_master_to.sql.orig
          fi

          # Start a server to send backups when requested by peers.
          exec ncat --listen --keep-open --send-only --max-conns=1 3307 -c \
            "xtrabackup --backup --slave-info --stream=xbstream --host=127.0.0.1 --user=root"
        volumeMounts:
        - name: data
          mountPath: /var/lib/mysql
          subPath: mysql
        - name: conf
          mountPath: /etc/mysql/conf.d
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
      volumes:
      - name: conf
        emptyDir: {}
      - name: config-map
        configMap:
          name: mysql
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: ["ReadWriteOnce"]
      resources:
        requests:
          storage: 2Gi
复制代码

 

创建statefulset:

kubectl apply -f statefulset.yaml

 

可以看到,mysql pod正在初始化。因为需要拉取mysql镜像和xtrabackup镜像,所以需要等待

 

 

 

步骤6:验证是否安装成功

6.1查看mysql master状态并创建数据库testdb:

复制代码
[root@k8s-master mysql]# kubectl exec -it mysql-0 sh

# mysql

mysql> show databases; 

mysql> show master status;

mysql> create database testdb;

mysql> show databases;
复制代码

 

 

 

6.2 查看其中一个mysql slave的状态,并验证新创建的数据库testdb是否成功同步:

[root@k8s-master mysql-datadir-3]# kubectl exec -it mysql-1 sh

# mysql

mysql> show databases;

 

 

 

查看slave状态:

mysql> show slave status \G;

说明:Slave_IO_State: Waiting for master to send event 证明slave正在监听master

 

 

备注:

按照以上步骤,成功完成了基于k8s的mysql5.7主从集群安装。

过程并非顺利,可能会出现slave无法同步数据的问题。但重复安装2次后,问题消失。根因有待继续跟踪

 

参考链接:

https://blog.51cto.com/u_15127511/2657609

https://blog.csdn.net/weixin_43936969/article/details/106289127

https://www.cnblogs.com/cooper-73/p/12988614.html

标签:statefulset,pv,name,mysql5.7,datadir,xtrabackup,nfs,mysql,master
From: https://www.cnblogs.com/gaoyanbing/p/17705748.html

相关文章

  • MySQL5.7安装超详细步骤(图文超详细教程)
    一、首先下载MySQLMysql官网下载地址: MySQL::DownloadMySQLInstaller(ArchivedVersions)华为MySQL镜像站: https://mirrors.huaweicloud.com/mysql/Downloads/网易云开源镜像站: http://mirrors.163.com/在下载列表中选择需要安装的版本:  二、其次安装MySQL1......
  • 数据通信网络之IPv6以太网二层交换
    文章及资源归档至【AIShareLab】,回复通信系统与网络可获取。一、实验目的掌握VLAN、Trunk的基础配置。掌握以太网链路聚合的基础配置。掌握路由器子接口的配置,以及通过子接口实现VLAN之间互通的方法(单臂路由)二、实验环境及网络拓扑本实验模拟一个典型的简单园区网络,如图1所......
  • K8S系列(八)持久化存储(Pod Volumes,PV和PVC)
    一、概述:官方文档:https://kubernetes.io/zh-cn/docs/concepts/storage/volumes/在kubernetes集群中,使用subPath,有时,在单个Pod中共享卷以供多方使用是很有用的。volumeMounts.subPath属性可用于指定所引用的卷内的子路径,而不是其根路径。1、subPath是在挂载的卷......
  • 数据通信网络之IPv6以太网单臂路由
    文章及资源归档至公众号【AIShareLab】,回复通信系统与网络可获取。一、目的掌握VLAN的基础配置。掌握Trunk的基础配置。二、环境及网络拓扑本实验模拟一个简单的以太网二层交换网络,如图1所示,二层交换机AS1与AS2均接入了VLAN10及VLAN20的终端,并且这两台交换机之间......
  • 无涯教程-JavaScript - NPV函数
    描述NPV函数通过使用折现率以及一系列未来付款(负值)和收入(正值)来计算投资的净现值。语法NPV(rate,value1,[value2],...)争论Argument描述Required/OptionalRateTherateofdiscountoverthelengthofoneperiod.RequiredValue11to254argumentsrepresen......
  • linux7.X二进制方式安装MySQL5.7.X
    概述系统环境服务器型号VMwareVirtualPlatform操作系统版本RHEL7.6x86_64主机名linuxpg51PubIP192.168.115.51数据库安装版本mysqlVer14.14Distrib5.7.30,forlinux-glibc2.12(x86_64)usingEditLinewrapper安装路径规划[mysql@linuxpg51/]$tree-L1/mysql/mysq......
  • 数据通信网络之IPv6静态路由
    文章及资源归档至公众号【AIShareLab】,回复通信系统与网络可获取。一、目的掌握路由器的IPv6基础配置。掌握静态IPv6路由的基础配置。理解IPv6数据报文的路由过程。二、拓扑如图1所示,三台路由器R1、R2和R3分别通过相应物理接口进行连接,其中,R1及R3各自下联一个网段......
  • Python给你一个字符串,你怎么判断是不是ipv4地址?手写这段代码,并写出测试用例【杭州多测
    ipv4地址的格式:(1~255).(0 ~255).(0 ~255).(0 ~255)1.正则表达式importredefcheck_ip(one_str):compile_ip=re.compile('^(([1-9]|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])\.){3}(\d|[1-9]\d|1\d{2}|2[0-4]\d|25[0-5])$')ifcompile_ip.match(one_str......
  • PV及PVC实战
    安装及配置NFS(参考本人博客)创建NFS共享目录mkdir-p/data/volumes/v{1,2,3,4,5,6,7,8,9,10}新增共享目录[root@k8smaster1~]#cat/etc/exports/data/v1*(rw,no_root_squash)/data/volumes/v1192.168.1.0/24(rw,no_root_squash)/data/volumes/v2192.168.1.0/24(rw,no_root......
  • live555最新版本修改对ipv6的支持
    在live555官方代码中只支持IPv4,现实环境中绝大多数项目也是IPv4,但是用支持IPv6的EasyRTSPClient去连接海康的相机,人家早就支持IPv6了。并且现工程投标中也有部分明确需要IPv6,所以在原IPv4基础上,动手改造支持IPv6。那么如何支持呢?方法如下:定义IPV4和IPV6live_ipv4_ipv6.htype......