TKE(K8S)部署mysql使用CFS存储

2022-07-17 18:10:31 浏览数 (1)

**前置条件

1,创建CFS实例

2,权限组 来访地址: * 用户权限是:no_root_squash 读写权限:读写

/var/lib/mysql 目录下的文件属性 都是mysql

代码语言:txt复制
root@mysql-cfs-data-66c5dc7875-fnm22:/var/lib/mysql# pwd
/var/lib/mysql
root@mysql-cfs-data-66c5dc7875-fnm22:/var/lib/mysql# ls -lrt 
total 198064
-rw-r----- 1 mysql mysql 50331648 Jul 17 08:22  ib_logfile1
-rw-r----- 1 mysql mysql  8585216 Jul 17 08:22 '#ib_16384_1.dblwr'
drwxr-x--- 2 mysql mysql     4096 Jul 17 08:23  performance_schema
root@mysql-cfs-data-66c5dc7875-fnm22:/var/lib/mysql# id mysql
uid=999(mysql) gid=999(mysql) groups=999(mysql)
图片.png图片.png

需要将文件系统的权限组修改成 no_all_squash 访问用户会先与本机用户匹配,匹配失败后再映射为匿名用户或用户组;

图片.png图片.png

yaml文件:

代码语言:txt复制
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    k8s-app: mysql-cfs-data
    qcloud-app: mysql-cfs-data
  name: mysql-cfs-data
  namespace: storage
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: mysql-cfs-data
      qcloud-app: mysql-cfs-data
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        k8s-app: mysql-cfs-data
        qcloud-app: mysql-cfs-data
    spec:
      containers:
      - env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"
        image: mysql:5.7.16
        imagePullPolicy: IfNotPresent
        name: mysql
        resources:
          limits:
            cpu: 500m
            memory: 1Gi
          requests:
            cpu: 100m
            memory: 64Mi
        securityContext:
          privileged: false
        volumeMounts:
        - mountPath: /var/lib/mysql
          name: mysql-vol
      dnsPolicy: ClusterFirst
      imagePullSecrets:
      - name: qcloudregistrykey
      restartPolicy: Always
      volumes:
      - name: mysql-vol
        persistentVolumeClaim:
          claimName: cfs-pvc-root
---
##内网型CLB
apiVersion: v1
kind: Service
metadata:
  annotations:
    service.kubernetes.io/qcloud-loadbalancer-internal-subnetid: subnet-ge8hhr3e
  name: mysql-cfs-data
  namespace: storage
spec:
  externalTrafficPolicy: Cluster
  ports:
  - name: 3306-3306-tcp
    nodePort: 31761
    port: 3306
    protocol: TCP
    targetPort: 3306
  selector:
    k8s-app: mysql-cfs-data
    qcloud-app: mysql-cfs-data
  sessionAffinity: None
  type: LoadBalancer

查看部署结果

代码语言:txt复制
root@mysql-cfs-data-b58fc4767-7lzqp:/var/lib/mysql# df -h | grep /var         
192.168.1.6:/111   10G  241M  9.8G   3% /var/lib/mysql
root@mysql-cfs-data-b58fc4767-7lzqp:/var/lib/mysql# ls -lt
total 188452
drwxr-x--- 2 mysql mysql       19 Jul 17 09:28 chen
-rw-r----- 1 mysql mysql 12582912 Jul 17 09:26 ibtmp1
-rw-r----- 1 mysql mysql 50331648 Jul 17 09:25 ib_logfile0
-rw-r----- 1 mysql mysql 79691776 Jul 17 09:25 ibdata1
-rw-r----- 1 mysql mysql     1325 Jul 17 09:25 ib_buffer_pool
drwxr-x--- 2 mysql mysql     8192 Jul 17 09:25 sys
drwxr-x--- 2 mysql mysql     4096 Jul 17 09:25 mysql
drwxr-x--- 2 mysql mysql     8192 Jul 17 09:25 performance_schema
-rw-r----- 1 mysql mysql       56 Jul 17 09:25 auto.cnf
-rw-r----- 1 mysql mysql 50331648 Jul 17 09:25 ib_logfile1
图片.png图片.png

StatefulSet方式部署:

代码语言:txt复制
apiVersion: apps/v1
kind: StatefulSet
metadata:
  labels:
    k8s-app: mysql
    qcloud-app: mysql
  name: mysql
  namespace: cjweichen
spec:
  replicas: 1
  selector:
    matchLabels:
      k8s-app: mysql
      qcloud-app: mysql
  serviceName: ""
  template:
    metadata:
      labels:
        k8s-app: mysql
        qcloud-app: mysql
    spec:
      containers:
      - env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"
        image: mysql:5.7
        imagePullPolicy: IfNotPresent
        name: mysql
        resources: {}
        volumeMounts:
        - mountPath: /var/lib/mysql
          name: nfs
          subPath: mysql_docker/data
      dnsPolicy: ClusterFirst
      imagePullSecrets:
      - name: qcloudregistrykey
      volumes:
      - name: nfs
        nfs:
          path: /
          server: 172.16.3.7
图片.png图片.png
图片.png图片.png

0 人点赞