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
#主要看这两个
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。