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;

MySQL主从

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
上一篇:k8s故障检测与自愈(一)


下一篇:子页面数据爬取