微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MySQL主从

day07 MysqL主从

一、StorageClass

StorageClass    存储类。
pv              存储对象。

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

Helm --> StorageClass

1、安装Helm

Github:https://github.com/helm/helm/releases
下载连接:wget 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
[root@k8s-master-01 ~]# helm repo add aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts 
[root@k8s-master-01 ~]# helm repo add stable http://mirror.azure.cn/kubernetes/charts/

2、下载nfs
[root@k8s-master-01 ~]# helm search repo nfs-client  
[root@k8s-master-01 ~]# helm pull stable/nfs-client-provisioner
[root@k8s-master-01 ~]# tar -xf nfs-client-provisioner-1.2.11.tgz 

3、修改配置文件
[root@k8s-master-01 ~]# cd nfs-client-provisioner/
[root@k8s-master-01 nfs-client-provisioner]# vim values.yaml 
nfs:
  server: 192.168.15.101
  path: /nfs/v3
  
4、部署kube-system名称空间
[root@k8s-master-01 nfs-client-provisioner]# helm install nfs ./ -n kube-system 

5、查看
[root@k8s-master-01 ~]# kubectl get pods -n kube-system
NAME                                          READY   STATUS    RESTARTS   AGE
nfs-nfs-client-provisioner-6765d46dcb-v2vln   1/1     Running   0          9m28s

# 卸载
[root@k8s-master-01 nfs-client-provisioner]# helm uninstall nfs -n kube-system

# 查看helm源添加情况
[root@k8s-master-01 ~]# helm repo list

2、测试SC

---
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  # 使用StorageClass
  accessModes:
    - "ReadWriteMany"
  resources:
    requests:
      storage: "10Gi"

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

# 报错没有权限
[root@k8s-master-01 nfs]# groupadd www -g 666
[root@k8s-master-01 nfs]# useradd www -u 666 -g 666 -M -r -s /sbin/nologin
[root@k8s-master-01 nfs]# chown -R www.www /nfs

二、MysqL一主多从

1、安装MysqL(主从都要执行)

官网:https://www.mysql.com/

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、添加配置文件
[root@MysqL-master ~]# vim /etc/my.cnf
[MysqLd]
basedir=/usr/local/MysqL
datadir=/MysqL_data
port=3306
socket=/usr/local/MysqL/MysqL.sock
character-set-server=utf8mb4
log-error=/var/log/MysqLd.log
pid-file=/tmp/MysqLd.pid
[MysqL]
socket=/usr/local/MysqL/MysqL.sock
[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 ~]# grep 'temporary password' /var/log/MysqLd.log
2022-01-12T11:04:12.289151Z 1 [Note] A temporary password is generated for root@localhost: )Xuqgqmbu0ha

# 链接数据库
[root@MysqL-master ~]# MysqL -uroot -p')Xuqgqmbu0ha'
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.1、创建复制用户(在主库创建)

# 创建用户
MysqL> grant replication slave on *.* to 'shanhe'@'%' 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、开启binlog日志(主从都要执行)

# 在集群中,server_id必须唯一。
[root@MysqL-master ~]# vim /etc/my.cnf 
server-id=1
log-bin=/MysqL_data/log-bin/binlog

[root@MysqL-master MysqL_data]# mkdir /MysqL_data/log-bin
[root@MysqL-master MysqL_data]# chown MysqL.MysqL /MysqL_data/log-bin
[root@MysqL-master ~]# systemctl restart MysqLd

2.3、实现主从复制(从 节点配置)

1、查看主节点binlog日志的状态
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、在从节点配置主从复制
MysqL> # 登录后执行下面
change master to
master_host='192.168.15.105',
master_port=3306,
master_user='shanhe',
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
*************************** 1. row ***************************
             Slave_IO_Running: Yes
            Slave_sql_Running: Yes
            
5、查看错误日志
[root@MysqL-slave ~]# tail -f /var/log/MysqLd.log
# 停止已经启动的绑定
stop slave;
# 重置绑定
reset master;

3、容器化MysqL主从

3.1 部署

1、在k8s主节点启动一个容器
[root@k8s-master-01 ~]# docker run -it -d MysqL:5.7 bash

2、把文件复制到这里
[root@k8s-master-01 ~]# mkdir MysqL
[root@k8s-master-01 MysqL]# docker cp e872e2680382:/usr/local/bin/docker-entrypoint.sh .

3、修改配置文件
[root@k8s-master-01 MysqL]# ll
-rwxrwxr-x 1 root root 14632 Jan 12 20:17 docker-entrypoint.sh
-rw-r--r-- 1 root root    15 Jan 12 20:06 Dockerfile
[root@k8s-master-01 MysqL]# vim docker-entrypoint.sh 
sed -i "s/symbolic-links=0/symbolic-links=$RANDOM/g"  /etc/MysqL/MysqL.conf.d/MysqLd.cnf

4、编写Dockerfile
[root@k8s-master-01 MysqL]# cat Dockerfile 
FROM MysqL:5.7
ADD docker-entrypoint.sh /usr/local/bin/docker-entrypoint.sh 
EXPOSE 3306 33060

5、构建镜像并且启动
[root@k8s-master-01 MysqL]# docker build -t alvinos/MysqL:5.7 .

6、编写MysqLd配置文件
[root@k8s-master-01 MysqL]# vim  MysqLd.conf
[MysqLd]
datadir = /MysqL_data
port=3306
socket  = /var/run/MysqLd/MysqL.sock
character-set-server=utf8
log-error=/var/run/MysqLd/MysqLd.pid
server-id=1
log-bin=/MysqL_data/binlog

3.2 部署资源清单

---
kind: Deployment
apiVersion: apps/v1
Metadata:
  name: MysqL-master
spec:
  selector:
    matchLabels:
      app: MysqL
      devel: MysqL-master
  template:
    Metadata:
      labels:
        app: MysqL
        devel: MysqL-master
    spec:
      containers:
        - name: MysqL
          image: alvinos/MysqL:5.7.4-stable
          imagePullPolicy: IfNotPresent
          envFrom:
            - secretRef:
                name: MysqL
---
kind: Service
apiVersion: v1
Metadata:
  name: MysqL-master
spec:
  ports:
    - port: 3306
      targetPort: 3306
      name: MysqL-master
  selector:
    app: MysqL
    devel: MysqL-master
---
kind: Job
apiVersion: batch/v1
Metadata:
  name: create-user
spec:
  template:
    spec:
      restartPolicy: OnFailure
      containers:
        - name: MysqL
          image: alvinos/MysqL:5.7.4-stable
          command:
            - "/bin/sh"
            - "-c"
            - |
              while true;
              do
                /usr/bin/MysqL -uroot -p123456 -hMysqL-master.default.svc.cluster.local -e"status;" &>/dev/null

                if [ $? -eq 0 ];then

                  /usr/bin/MysqL -uroot -p123456 -hMysqL-master.default.svc.cluster.local -e "grant replication slave on *.* to 'shanhe'@'%' identified by '123456';"
                  /usr/bin/MysqL -uroot -p123456 -hMysqL-master.default.svc.cluster.local -e "flush privileges;"

                  break

                fi

              done

---
kind: Deployment
apiVersion: apps/v1
Metadata:
  name: MysqL-slave
spec:
  selector:
    matchLabels:
      app: MysqL
      devel: MysqL-slave
  template:
    Metadata:
      labels:
        app: MysqL
        devel: MysqL-slave
    spec:
      containers:
        - name: MysqL
          image: alvinos/MysqL:5.7.4-stable
          envFrom:
            - secretRef:
                name: MysqL
---
kind: Service
apiVersion: v1
Metadata:
  name: MysqL-slave
spec:
  selector:
    app: MysqL
    devel: MysqL-slave
  ports:
    - port: 3306
      targetPort: 3306
      name: slave
---
kind: Job
apiVersion: batch/v1
Metadata:
  name: add-cluster
spec:
  template:
    spec:
      restartPolicy: OnFailure
      containers:
        - name: MysqL
          image: alvinos/MysqL:5.7.4-stable
          command:
            - "/bin/sh"
            - "-c"
            - |

              while true;
              do

                  /usr/bin/MysqL -uroot -p123456 -hMysqL-master.default.svc.cluster.local -e"status;" &>/dev/null

                  if [ $? -eq 0 ];then

                      MysqL -uroot -p123456 -hMysqL-master.default.svc.cluster.local -e "show master status ;" | awk 'NR==2{print $0}' > log

                      FILE=`cat log | awk '{print $1}'`
                      Position=`cat log | awk '{print $2}'`

                      MysqL -uroot -p123456 -hMysqL-slave.default.svc.cluster.local -e "change master to master_host='MysqL-master.default.svc.cluster.local', master_port=3306, master_user='shanhe', master_password='123456', master_log_file='${FILE}', master_log_pos=${Position};"

                      MysqL -uroot -p123456 -hMysqL-slave.default.svc.cluster.local -e "start slave;"

                      break
                  fi

              done

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

相关推荐