数据库
首页 > 数据库> > k8s之storageclass和MySQL主从

k8s之storageclass和MySQL主从

作者:互联网

k8s之storageclass和MySQL主从

1、StorageClass

StorageClass    存储类。
pv              存储对象。

StorageClass 是按照pvc的要求,动态的生成pv。

Helm --> StorageClass

1.1、安装Helm

Github:https://github.com/helm/helm/releases
下载连接:https://get.helm.sh/helm-v3.7.2-linux-amd64.tar.gz
[root@k8s-master-01 ~]# tar -xf helm-v3.7.2-linux-amd64.tar.gz 
[root@k8s-master-01 ~]# mv linux-amd64/helm /usr/local/bin/

yum   --->   yum源
helm  --->   helm源

1、添加helm源
[root@k8s-master-01 ~]# helm repo add moikot https://moikot.github.io/helm-charts
"moikot" has been added to your repositories
[root@k8s-master-01 ~]# helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts 
"aliyun" has been added to your repositories
[root@k8s-master-01 ~]# helm repo add stable http://mirror.azure.cn/kubernetes/charts/
"stable" has been added to your repositories

[root@k8s-master-01 ~]# helm search repo nfs-client
NAME                         	CHART VERSION	APP VERSION	DESCRIPTION                                       
moikot/nfs-client-provisioner	1.3.0        	3.1.0      	nfs-client is an automatic provisioner that use...
stable/nfs-client-provisioner	1.2.11       	3.1.0      	DEPRECATED - nfs-client is an automatic provisi...
#下载
[root@k8s-master-01 ~]# helm pull stable/nfs-client-provisioner

[root@k8s-master-01 ~]# tar -xf nfs-client-provisioner-1.2.11.tgz 

[root@k8s-master-01 ~]# cd nfs-client-provisioner/
[root@k8s-master-01 nfs-client-provisioner]# vim values.yaml 
nfs:
  server: 192.168.11.206
  path: /nfs/v3
[root@k8s-master-01 nfs-client-provisioner]# helm install nfs ./ -n kube-system 

#查看是否部署成功
kubectl get pods -n kube-system 
kubectl get sc

1.2、测试SC

##vim sc.yaml
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc001
  namespace: default
spec:
  storageClassName: nfs-client
  accessModes:
    - "ReadWriteMany"
  resources:
    requests:
      storage: "6Gi"


[root@k8s-master-01 k8s]# kubectl apply -f sc1.yaml 
deployment.apps/nfs created
persistentvolumeclaim/nfs created




#如果报错需要修改
	#unexpected error getting claim reference: selfLink was empty, can't  make reference
#主要原因是kubernetes 1.20版本 禁用了 selfLink导致。
	vim /etc/kubernetes/manifests/kube-apiserver.yaml #添加- --feature-gates=RemoveSelfLink=false,然后重新部署。
spec:
  containers:
  - command:
    - kube-apiserver
    - --feature-gates=RemoveSelfLink=false
    
    

##vim sc1.yaml
---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: nfs
spec:
  selector:
    matchLabels:
      app: nfs
  template:
    metadata:
      labels:
        app: nfs
    spec:
      containers:
        - name: nginx
          image: nginx
          volumeMounts:
            - mountPath: /usr/share/nginx/html
              name: html
      volumes:
        - name: html
          persistentVolumeClaim:
            claimName: nfs
---
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfs
  namespace: default
spec:
  storageClassName: nfs-client
  accessModes:
    - "ReadWriteMany"
  resources:
    requests:
      storage: "10Gi"
#启动测试
curl  启动的pods的ip

2、MySQL一主多从

简介

主库负责写   从库负责读 
	主库写入的数据复制到从库里面,通过binlog日志复制过去的
    1.分为一个master 一个server
    2.在master节点开启binlog日志,会在master节点产生一个dump线程,dump线程读取master的binlog日志看是否有修改的内容 
    3.如果有 去通知从节点的io线程
    4.从节点收到通知,查询本地缓存,看数据是否过来,过来就不需要同步了 ,结果会通过ack信号(回应信号)反馈给master
    5.更新从节点的master.info 里面存放的是从master节点拿过来的数据,
    6.数据拿过来先写在缓存里 中继日志
    7.sql线程在中继日志读数据 写入到数据库

2.1、安装MySQL

官网:https://www.mysql.com/
#常用5.7 、5.6 版本

#准备工作 删除Mariadb
yum -y remove mariadb-libs.x86_64

1、下载安装包
[root@mysql-master ~]# wget  https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz

2、安装
[root@mysql-master ~]# tar -xf mysql-5.7.36-linux-glibc2.12-x86_64.tar.gz  -C /usr/local/
[root@mysql-master local]# ln -s /usr/local/mysql-5.7.36-linux-glibc2.12-x86_64 /usr/local/mysql

3、统一用户
[root@mysql-master ~]#  useradd -M -s /sbin/nologin -r mysql

4、依赖包
[root@mysql-master ~]# yum install -y ncurses-devel libaio-devel gcc gcc-c++ numactl libaio glibc cmake autoconf

5、创建一个数据存放目录
[root@mysql-master ~]# mkdir /mysql_data

6、统一授权
[root@mysql-master ~]# chown mysql.mysql /mysql_data
[root@mysql-master ~]# chown mysql.mysql -R /usr/local/mysql
[root@mysql-master ~]# chown mysql.mysql -R /usr/local/mysql-5.7.36-linux-glibc2.12-x86_64/

7、添加配置文件
vim /etc/my.cnf
[mysqld]
# 安装目录
basedir=/usr/local/mysql
# 数据目录
datadir=/mysql_data
# 端口
port=3306
# socket文件存放地
socket=/usr/local/mysql/mysql.sock
# 数据库默认的字符集编码
character-set-server=utf8
# 错误日志存放路径
log-error=/var/log/mysqld.log
# PID 文件存放路径
pid-file=/tmp/mysqld.pid

# mysql客户端配置
[mysql]
socket=/usr/local/mysql/mysql.sock

# mysql客户端配置
[client]
socket=/usr/local/mysql/mysql.sock

8、初始化
[root@mysql-master ~]# touch /var/log/mysqld.log
[root@mysql-master ~]# chown mysql.mysql /var/log/mysqld.log
[root@mysql-master ~]# /usr/local/mysql/bin/mysqld --initialize --user=mysql --basedir=/usr/local/mysql --datadir=/mysql_data

9、注册MySQL服务
[root@mysql-master ~]# vim /usr/lib/systemd/system/mysqld.service
[Unit]
Description=MySQL Server
Documentation=man:mysqld(8)
Documentation=https://dev.mysql.com/doc/refman/en/using-systemd.html
After=network.target
After=syslog.target
[Install]
WantedBy=multi-user.target
[Service]
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld --defaults-file=/etc/my.cnf
LimitNOFILE = 5000

10、测试启动
[root@mysql-master ~]# systemctl daemon-reload
[root@mysql-master ~]# systemctl start mysqld


11、测试连接
#加入环境变量
[root@mysql-master mysql]# vim /etc/profile
export MYSQL_HOME=/usr/local/mysql
export PATH=$PATH:$MYSQL_HOME/bin
[root@mysql-master mysql]# source /etc/profile

# 获取密码
[root@mysql-master mysql]# grep 'temporary password' /var/log/mysqld.log

[root@mysql-master mysql]# mysql -uroot -p'F?W6iLluYq9i&j'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.36

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

# 修改默认密码
mysql> alter user   root@localhost   identified  by  'Test123!';
Query OK, 0 rows affected (0.00 sec)

2.2、部署主从复制

2.2.1、创建复制用户(主库创建)
mysql> grant replication slave on *.* to 'zhang'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
2.2.2、开启binlog日志
# 在集群中,server_id必须唯一。相当于每个数据库的身份证号
#主节点和从节点都要写   但是注意id不要一样
[root@mysql-master ~]# vim /etc/my.cnf 
server-id=1 #这个注意不要一样
log-bin=/mysql_data/log-bin/binlog

#重启mysqld
systemctl restart mysqld 

[root@mysql-master mysql_data]# mkdir /mysql_data/log-bin
[root@mysql-master mysql_data] cd /mysql_data
[root@mysql-master mysql_data]# chown mysql.mysql log-bin
[root@mysql-master ~]# systemctl restart mysqld
2.2.3、实现主从复制(从节点)
1、查看主节点binlog日志的状态
[root@mysql-master mysql_data]# mysql -uroot -p'Test123!'

mysql> show master status ;
+---------------+----------+--------------+------------------+-------------------+
| File          | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+---------------+----------+--------------+------------------+-------------------+
| binlog.000001 |      154 |              |                  |                   |
+---------------+----------+--------------+------------------+-------------------+
1 row in set (0.00 sec)

2、在从节点配置主从复制

change master to
master_host='192.168.11.101',
master_port=3306,
master_user='zhang',
master_password='123456',
master_log_file='binlog.000001',
master_log_pos=154;

3、开启主从复制
mysql> start slave ;
Query OK, 0 rows affected (0.00 sec)

4、查看状态
mysql> show slave status \G

#主要看这两个

标签:MySQL,master,storageclass,mysql,helm,nfs,k8s,root,usr
来源: https://www.cnblogs.com/zhangshihu/p/15793003.html