k8s-v1.2.3部署mysql-8服务

19 篇文章 1 订阅
18 篇文章 3 订阅

一、环境准备

CentOS Linux release 7.7.1908 (Core) 3.10.0-1062.el7.x86_64

kubeadm-1.22.3-0.x86_64

kubelet-1.22.3-0.x86_64

kubectl-1.22.3-0.x86_64

kubernetes-cni-0.8.7-0.x86_64

主机名IPVIP
k8s-master01192.168.30.106192.168.30.115
k8s-master02192.168.30.107
k8s-master03192.168.30.108
k8s-node01192.168.30.109
k8s-node02192.168.30.110
k8s-nfs192.168.30.114

二、配置好PV和PVC

这里我就不再详细说明,可以参考我上篇(归海听雪:k8s-1.22.3版本中使用持久化卷之StorageClass+NFS

三、部署mysql8服务

1、把mysql8配置文件放到configmap

这里面的一些配置参数,可以根据自已的实际情况进行修改优化

vim mysql-configmap.yaml

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-config
  namespace: k8s-mysql
  labels:
    app: mysql
data:
  my.cnf: |-
    [client]
    default-character-set=utf8mb4
    [mysql]
    default-character-set=utf8mb4
    [mysqld]
    max_connections = 2000
    secure_file_priv=/var/lib/mysql
    sql_mode=STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION
    skip-name-resolve
    open_files_limit = 65535
    table_open_cache = 128
    log_error = /var/lib/mysql/mysql-error.log #错误日志路径
    slow_query_log = 1
    long_query_time = 1 #慢查询时间 超过1秒则为慢查询
    slow_query_log_file = /var/lib/mysql/mysql-slow.log
    default-storage-engine = InnoDB #默认存储引擎
    innodb_file_per_table = 1
    innodb_open_files = 500
    innodb_buffer_pool_size = 64M
    innodb_write_io_threads = 4
    innodb_read_io_threads = 4
    innodb_thread_concurrency = 0
    innodb_purge_threads = 1
    innodb_flush_log_at_trx_commit = 2
    innodb_log_buffer_size = 2M
    innodb_log_file_size = 32M
    innodb_log_files_in_group = 3
    innodb_max_dirty_pages_pct = 90
    innodb_lock_wait_timeout = 120
    bulk_insert_buffer_size = 8M
    myisam_sort_buffer_size = 8M
    myisam_max_sort_file_size = 10G
    myisam_repair_threads = 1
    interactive_timeout = 28800
    wait_timeout = 28800
    [mysqldump]
    quick
    max_allowed_packet = 16M #服务器发送和接受的最大包长度
    [myisamchk]
    key_buffer_size = 8M
    sort_buffer_size = 8M
    read_buffer = 4M
    write_buffer = 4M

kubectl apply -f mysql-configmap.yaml

2、生成mysql-statefulset文件

vim mysql-statefulset.yaml

apiVersion: v1
kind: Service
metadata:
  namespace: default
  name: mysql-headless
spec:
  clusterIP: None  #无头服务
  selector:
    app: mysql
  ports:
  - name: mysql
    port: 3306
    protocol: TCP
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
  namespace: default
  name: mysql
spec:
  podManagementPolicy: OrderedReady  #pod名-> 0-N,删除N->0
  replicas: 1
  revisionHistoryLimit: 10
  serviceName: mysql-headless
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:  #name没写,会默认生成的
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: registry-op.test.cn/mysql:8.0.19
        ports:
        - containerPort: 3306
          name: client
        env:
        - name: MYSQL_ROOT_PASSWORD    ## 配置Root用户默认密码
          value: "112233"
        resources:
          limits:
            cpu: 2000m
            memory: 512Mi
          requests:
            cpu: 2000m
            memory: 512Mi
# health check
        livenessProbe:
          initialDelaySeconds: 30
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 3
          exec:
            command: ["mysqladmin", "-uroot", "-p${MYSQL_ROOT_PASSWORD}", "ping"]
        readinessProbe:
          initialDelaySeconds: 10
          periodSeconds: 10
          timeoutSeconds: 5
          successThreshold: 1
          failureThreshold: 3
          exec:
            command: ["mysqladmin", "-uroot", "-p${MYSQL_ROOT_PASSWORD}", "ping"]

# health check
        volumeMounts:
        - name: conf
          mountPath: /etc/mysql/conf.d/my.cnf
          subPath: my.cnf
        - name: data
          mountPath: /var/lib/mysql
          readOnly: false
        - name: localtime
          readOnly: true
          mountPath: /etc/localtime
      volumes:
      - name: conf
        configMap:
          name: mysql-config
          defaultMode: 0755
      - name: localtime
        hostPath:
           type: File
           path: /etc/localtime
      imagePullSecrets:
      - name: registry-op.test.cn
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      accessModes: ["ReadWriteMany"]
      storageClassName: managed-nfs-storage
      volumeMode: Filesystem
      resources:
        requests:
          storage: 20Gi

kubectl apply -f mysql-statefulset.yaml

3、设置外部访问

vim mysql-service.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: default
  labels:
    app: mysql
spec:
  type: NodePort
  ports:
    - name: tcp
      port: 3306
      nodePort: 32306
  selector:
    app: mysql

kubectl apply -f mysql-service.yaml

4、查看状态

# kubectl get pods|grep mysql
mysql-0                                   1/1     Running   0               15h

# kubectl get svc|grep mysql
mysql            NodePort    10.96.196.95    <none>        3306:32306/TCP   15h
mysql-headless   ClusterIP   None            <none>        3306/TCP         15h

5、登录mysql验证

# kubectl exec -it mysql-0 /bin/sh -n default
# mysql -uroot -p112233
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 11172
Server version: 8.0.19 MySQL Community Server - GPL

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

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>

说明mysql8可以正常使用了

6、查看存储

# kubectl get pv|grep mysql
pvc-98be7119-fe14-4d50-9f68-a4e4031c68b0   20Gi       RWX            Retain           Bound    default/data-mysql-0             managed-nfs-storage                  15h

# kubectl get pvc|grep mysql
data-mysql-0   Bound    pvc-98be7119-fe14-4d50-9f68-a4e4031c68b0   20Gi       RWX            managed-nfs-storage   15h

#登录到192.168.10.114存储上看一下数据

# ll /data/volumes/default-data-mysql-0-pvc-98be7119-fe14-4d50-9f68-a4e4031c68b0/
total 178224
drwxr-x--- 2 polkitd input     4096 Dec 28 18:34 #innodb_temp
-rw-r----- 1 polkitd input       56 Dec 28 18:34 auto.cnf
-rw-r----- 1 polkitd input  3110839 Dec 28 18:34 binlog.000001
-rw-r----- 1 polkitd input      340 Dec 28 18:36 binlog.000002
-rw-r----- 1 polkitd input       32 Dec 28 18:34 binlog.index
-rw------- 1 polkitd input     1676 Dec 28 18:34 ca-key.pem
-rw-r--r-- 1 polkitd input     1112 Dec 28 18:34 ca.pem
-rw-r--r-- 1 polkitd input     1112 Dec 28 18:34 client-cert.pem
-rw------- 1 polkitd input     1680 Dec 28 18:34 client-key.pem
-rw-r----- 1 polkitd input     5400 Dec 28 18:34 ib_buffer_pool
-rw-r----- 1 polkitd input 33554432 Dec 28 18:36 ib_logfile0
-rw-r----- 1 polkitd input 33554432 Dec 28 18:34 ib_logfile1
-rw-r----- 1 polkitd input 33554432 Dec 28 18:34 ib_logfile2
-rw-r----- 1 polkitd input 12582912 Dec 28 18:36 ibdata1
-rw-r----- 1 polkitd input 12582912 Dec 28 18:35 ibtmp1
drwxr-x--- 2 polkitd input     4096 Dec 28 18:34 mysql
-rw-r----- 1 polkitd input     3647 Dec 28 18:34 mysql-error.log
-rw-r----- 1 polkitd input      537 Dec 28 18:34 mysql-slow.log
-rw-r----- 1 polkitd input 30408704 Dec 28 18:36 mysql.ibd
drwxr-x--- 2 polkitd input     4096 Dec 28 18:34 performance_schema
-rw------- 1 polkitd input     1676 Dec 28 18:34 private_key.pem
-rw-r--r-- 1 polkitd input      452 Dec 28 18:34 public_key.pem
-rw-r--r-- 1 polkitd input     1112 Dec 28 18:34 server-cert.pem
-rw------- 1 polkitd input     1676 Dec 28 18:34 server-key.pem
drwxr-x--- 2 polkitd input     4096 Dec 28 18:34 sys
drwxr-x--- 2 polkitd input     4096 Dec 28 18:36 test
-rw-r----- 1 polkitd input 12582912 Dec 28 18:34 undo_001
-rw-r----- 1 polkitd input 10485760 Dec 28 18:36 undo_002

四、外部访问验证

找一台外部机器,安装一下mysql8的client包

1、下载mysql8包

mkdir /opt/mysql8
cd /opt/mysql8
wget   https://cdn.mysql.com/archives/mysql-8.0/mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar

2、安装

tar -xf mysql-8.0.16-2.el7.x86_64.rpm-bundle.tar
rpm -ivh mysql-community-client-8.0.16-2.el7.x86_64.rpm mysql-community-libs-8.0.16-2.el7.x86_64.rpm mysql-community-common-8.0.16-2.el7.x86_64.rpm

3、验证登录mysql

前面已经配置了mysql8为nodeport访问,并且端口是32306,所以理论外部可以直接访问的

#mysql -h 192.168.30.109 -P 32306 -uroot -p112233
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 11315
Server version: 8.0.19 MySQL Community Server - GPL

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

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>

可以测试一下,把当前的pod删除,自动创建后,看一下数据库中的数据是否正常。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

归海听雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值