[Kubernetes] 在K8S上部署MySQL并数据持久化

在K8S上安装MySQL

1、创建PV

apiVersion: v1
kind: PersistentVolume
metadata:
  name: model-db-pv
spec:
  accessModes:
  - ReadWriteOnce
  capacity:
    storage: 5Gi
  claimRef:
    apiVersion: v1
    kind: PersistentVolumeClaim
    name: model-db-pv-claim
    namespace: default
  hostPath:
    path: /home/work/share/model-db
  persistentVolumeReclaimPolicy: Retain
  volumeMode: Filesystem

其中:home/work/share/  为所有K8S都共有的目录,相当于所有主机mount了一个NFS目录

也可以

apiVersion: v1
kind: PersistentVolume
metadata:
  name: model-db-pv
spec:
  accessModes:
  - ReadWriteOnce
  capacity:
    storage: 5Gi
  claimRef:
    apiVersion: v1
    kind: PersistentVolumeClaim
    name: model-db-pv-claim
    namespace: default
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs
  nfs:
    path: /home/work/share/model-db
    server: 192.168.20.10
  volumeMode: Filesystem

 

2、创建PVC

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: model-db-pv-claim
  namespace: default
  labels:
    app: model-mysql
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi

3、确认pv及pvc的状态

# kubectl get pv     
# kubectl get pvc    

4. 创建mysql configMap

 

apiVersion: v1
kind: ConfigMap
metadata:
  name: model-db-config
  namespace: default
  labels:
    app: model-db
data:
  my.cnf: |-
    [client]
    default-character-set=utf8mb4
    [mysql]
    default-character-set=utf8mb4
    [mysqld] 
    character-set-server = utf8mb4  
    collation-server = utf8mb4_unicode_ci  
    init_connect='SET NAMES utf8mb4'  
    skip-character-set-client-handshake = true  
    max_connections=2000
    secure_file_priv=/var/lib/mysql
    bind-address=0.0.0.0
    symbolic-links=0
    sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'

 

5、部署(Deployment)文件

apiVersion: apps/v1
kind: Deployment
metadata:
  name: model-db
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: model-mysql
  template:
    metadata:
      labels:
        app: model-mysql
    spec:
      containers:
      - args:
        - --datadir
        - /var/lib/mysql/datadir
        env:
          - name: MYSQL_ROOT_PASSWORD
            value: root
          - name: MYSQL_USER
            value: user
          - name: MYSQL_PASSWORD
            value: user
        image: mysql:5.6
        name: model-db-container
        ports:
        - containerPort: 3306
          name: dbapi
        volumeMounts:
        - mountPath: /var/lib/mysql
          name: model-db-storage
        - name: config
          mountPath: /etc/mysql/conf.d/my.cnf
          subPath: my.cnf
      volumes:
      - name: model-db-storage
        persistentVolumeClaim:
          claimName: model-db-pv-claim
      - name: config      
        configMap:
          name: model-db-config
      - name: localtime
        hostPath:
          type: File
          path: /etc/localtime

 

6、创建svc(service)

apiVersion: v1
kind: Service
metadata:
  labels:
    app: model-mysql
  name: model-db-svc
  namespace: default
spec:
  type: NodePort
  ports:
  - name: http
    port: 3306
    nodePort: 30336
    protocol: TCP
    targetPort: 3306
  selector:
    app: model-mysql

 

积极向上的墨鱼仔 CSDN认证博客专家 AI学习者 努力奋斗者
AI 学习与生活
已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 精致技术 设计师:CSDN官方博客 返回首页