k8s有状态部署mysql主从(local pv持久化)

news/2024/4/17 16:04:16

1、修改自己对应的命名空间

2、local pv的方式必须先创建好目录在给权限

3、sts部署文件密码都要修改好在部署

yaml资源文件如下:

#配置mysql的root密码再部署,如果部署了在修改root密码就会失败,必须在初始化就把root密码修改好
#部署采用的local pv的模式持久化本地#  local-sc.yml 
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:name: local-path
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer  #延迟绑定参数,很重要---
apiVersion: v1
kind: PersistentVolume
metadata:name: pv-mysql-1
spec :capacity:storage: 5GivolumeMode: FilesystemaccessModes:- ReadWriteOncepersistentVolumeReclaimPolicy: RetainstorageClassName: local-pathlocal:path: /data/mysql-1nodeAffinity:required:nodeSelectorTerms:- matchExpressions:- key: kubernetes.io/hostnameoperator: Invalues :- cole-k8s-worker1---
apiVersion: v1
kind: PersistentVolume
metadata:name: pv-mysql-2
spec :capacity:storage: 5GivolumeMode: FilesystemaccessModes:- ReadWriteOncepersistentVolumeReclaimPolicy: RetainstorageClassName: local-pathlocal:path: /data/mysql-2nodeAffinity:required:nodeSelectorTerms:- matchExpressions:- key: kubernetes.io/hostnameoperator: Invalues :- cole-k8s-worker2---
apiVersion: v1
kind: PersistentVolume
metadata:name: pv-mysql-3
spec :capacity:storage: 5GivolumeMode: FilesystemaccessModes:- ReadWriteOncepersistentVolumeReclaimPolicy: RetainstorageClassName: local-pathlocal:path: /data/mysql-3nodeAffinity:required:nodeSelectorTerms:- matchExpressions:- key: kubernetes.io/hostnameoperator: Invalues :- cole-k8s-worker3---#application/mysql/mysql-configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:name: mysqlnamespace: public-middlabels:app: mysql
data:master.cnf: |# Apply this config only on the master.[mysqld]log-binslave.cnf: |# Apply this config only on slaves.[mysqld]super-read-only
---
# application/mysql/mysql-services.yaml
# Headless service for stable DNS entries of StatefulSet members.
apiVersion: v1
kind: Service
metadata:name: mysqlnamespace: public-middlabels:app: mysql
spec:ports:- name: mysqlport: 3306clusterIP: Noneselector:app: mysql
---
# Client service for connecting to any MySQL instance for reads.
# For writes, you must instead connect to the master: mysql-0.mysql.
apiVersion: v1
kind: Service
metadata:name: mysql-readnamespace: public-middlabels:app: mysql
spec:ports:- name: mysqlport: 3306selector:app: mysql
---
#application/mysql/mysql-statefulset.yamlapiVersion: apps/v1
kind: StatefulSet
metadata:name: mysqlnamespace: public-midd
spec:selector:matchLabels:app: mysqlserviceName: mysqlreplicas: 3template:metadata:labels:app: mysqlspec:# 设置初始化容器,进行一些准备工作initContainers:- name: init-mysqlimage: mysql:5.7# 为每个MySQL节点配置service-id# 如果节点序号是0,则使用master的配置, 其余节点使用slave的配置command:- bash- "-c"- |set -ex# Generate mysql server-id from pod ordinal index.[[ `uname -n` =~ -([0-9]+)$ ]] || exit 1ordinal=${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 ]]; thencp /mnt/config-map/master.cnf /mnt/conf.d/elsecp /mnt/config-map/slave.cnf /mnt/conf.d/fivolumeMounts:- name: time-localtimemountPath: /etc/localtimereadOnly: true- name: confmountPath: /mnt/conf.d- name: config-mapmountPath: /mnt/config-map- name: clone-mysqlimage: yizhiyong/xtrabackup:latest# 为除了节点序号为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).[[ `uname -n` =~ -([0-9]+)$ ]] || exit 1ordinal=${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/mysqlvolumeMounts:- name: time-localtimemountPath: /etc/localtimereadOnly: true- name: datamountPath: /var/lib/mysqlsubPath: mysql- name: confmountPath: /etc/mysql/conf.dcontainers:- name: mysqlimage: mysql:5.7# 设置支持免密登录env:# - name: MYSQL_ALLOW_EMPTY_PASSWORD# value: "1"- name: MYSQL_ROOT_PASSWORDvalue: "123456"ports:- name: mysqlcontainerPort: 3306volumeMounts:- name: time-localtimemountPath: /etc/localtimereadOnly: true- name: datamountPath: /var/lib/mysqlsubPath: mysql- name: confmountPath: /etc/mysql/conf.dresources:# 设置启动pod需要的资源,官方文档上需要500m cpu,1Gi memory。# 我本地测试的时候,会因为资源不足,报1 Insufficient cpu, 1 Insufficient memory错误,所以我改小了点requests:# m是千分之一的意思,100m表示需要0.1个cpucpu: 100m# Mi是兆的意思,需要100M 内存memory: 100MilivenessProbe:# 使用mysqladmin ping命令,对MySQL节点进行探活检测# 在节点部署完30秒后开始,每10秒检测一次,超时时间为5秒exec:command: [ "mysqladmin", "ping" ]initialDelaySeconds: 30periodSeconds: 10timeoutSeconds: 5readinessProbe:# 对节点服务可用性进行检测, 启动5秒后开始,每2秒检测一次,超时时间1秒exec:# Check we can execute queries over TCP (skip-networking is off).command: [ "mysql", "-h", "127.0.0.1", "-p123456", "-e", "SELECT 1" ]initialDelaySeconds: 5periodSeconds: 2timeoutSeconds: 1- name: xtrabackupimage: yizhiyong/xtrabackup:latestports:- name: xtrabackupcontainerPort: 3307# 开始进行备份文件校验、解析和开始同步command:- bash- "-c"- |set -excd /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_infoelif [[ -f xtrabackup_binlog_info ]]; then# We're cloning directly from master. Parse binlog position.[[ `cat xtrabackup_binlog_info` =~ ^(.*?)[[:space:]]+(.*?)$ ]] || exit 1rm -f xtrabackup_binlog_info xtrabackup_slave_infoecho "CHANGE MASTER TO MASTER_LOG_FILE='${BASH_REMATCH[1]}',\MASTER_LOG_POS=${BASH_REMATCH[2]}" > change_master_to.sql.infi# Check if we need to complete a clone by starting replication.if [[ -f change_master_to.sql.in ]]; thenecho "Waiting for mysqld to be ready (accepting connections)"until mysql -h 127.0.0.1 -p"123456" -e "SELECT 1"; do sleep 1; doneecho "Initializing replication from clone position"mysql -h 127.0.0.1 -p"123456"\-e "$(<change_master_to.sql.in), \MASTER_HOST='mysql-0.mysql', \MASTER_USER='root', \MASTER_PASSWORD='123456', \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.origfi# 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 --password=123456"volumeMounts:- name: time-localtimemountPath: /etc/localtimereadOnly: true- name: datamountPath: /var/lib/mysqlsubPath: mysql- name: confmountPath: /etc/mysql/conf.dresources:requests:cpu: 100mmemory: 100Mivolumes:- name: time-localtimehostPath:path: /etc/localtime- name: confemptyDir: { }- name: config-mapconfigMap:name: mysql# 设置PVCvolumeClaimTemplates:- metadata:name: data       spec:storageClassName: local-pathaccessModes: [ "ReadWriteOnce" ]resources:requests:storage: 5Gi


https://www.xjx100.cn/news/3119010.html

相关文章

高级IO—poll,epoll,reactor

高级IO—poll,epoll,reactor 文章目录 高级IO—poll,epoll,reactorpoll函数poll函数接口poll服务器 epollepoll的系统调用epoll_createepoll_ctlepoll_wait epoll的工作原理epoll的工作方式水平触发边缘触发 epoll服务器 reactor poll函数 poll函数是一个用于多路复用的系统调…

【JavaEE初阶】死锁问题

目录 一、死锁的三种典型场景 1、一个线程&#xff0c;一把锁 2、两个线程&#xff0c;两把锁 3、N个线程&#xff0c;M把锁 死锁&#xff0c;是多线程代码中的一类经典问题。我们知道加锁是能解决线程安全问题的&#xff0c;但是如果加锁的方式不当&#xff0c;就可能产生死…

视图层、模板(补充)

视图层 响应对象 响应---》本质都是 HttpResponse HttpResponse---》字符串render----》放个模板---》模板渲染是在后端完成 js代码是在客户端浏览器里执行的模板语法是在后端执行的redirect----》重定向 字符串参数不是是空的状态码是 3开头JsonResponse---》json格式数据 …

ffmpeg在centos系统下的源文件下载脚本及编译脚本

下载脚本&#xff1a; #只做下载的动作&#xff0c;之后可以移动到其它环境下编译 #from https://trac.ffmpeg.org/wiki/CompilationGuide/Centos #yum install autoconf automake bzip2 bzip2-devel cmake freetype-devel gcc gcc-c git libtool make pkgconfig zlib-devel m…

加密挖矿、AI发展刺激算力需求激增!去中心化算力时代已来临!

2009年1月3日&#xff0c;中本聪在芬兰赫尔辛基的一个小型服务器上挖出了比特币的创世区块&#xff0c;并获得了50BTC的出块奖励。自加密货币诞生第一天起&#xff0c;算力一直在行业扮演非常重要的角色。行业对算力的真实需求&#xff0c;也极大推动了芯片厂商的发展&#xff…

点击元素以外的事件监听

在项目中&#xff0c;我们经常会遇到需要监听目标元素以外的区域被点击或鼠标移入移出等需求。 例如下面我们有一个表格里面嵌套表单的组件 我希望点击n行的时候&#xff0c;n行的元素变成表单元素进行输入或者选择&#xff0c; 当我点击其他其他区域n行又会恢复成数据展示…

nvm for windows使用与node/npm/yarn的配置

1 下载 nvm for windows download – github 下拉到Assets, 下载.exe文件 2 安装 安装到如下文件夹中 目录可以自己选, 可以换别的名字, 自己记住即可 新手建议全部看完再进行个人配置, 或者使用与博主一致的路径 D:\DevelopEnvironment\nvm3 配置nvm使用的镜像 node_mir…

Opencv手势控制音量!附源码!

效果演示&#xff1a; 废话不多说&#xff01;直接上源码&#xff01;下面写有所有代码注释&#xff01;&#xff01; import cv2 import mediapipe as mp #它包含了各种预训练的机器学习模型&#xff0c;可以用于姿势估计、手势识别等任务 from ctypes import cast, POINTE…