k8s之mysql(pv,pvc)

  一、PersistentVolume (PV):简短一点就是管理员创建的一种存储资源

  二、PersistentVolumeClaim (PVC):就一种声明,说我要使用pv了。

  三、PV和PVC的配置

apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: local-storage
provisioner: kubernetes.io/no-provisioner
volumeBindingMode: WaitForFirstConsumer
apiVersion: v1
kind: PersistentVolume
metadata:
  name: xbd-mysql-pv
  labels:
    app: xbd-mysql-pv
spec:
  capacity:
    storage: 2Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  persistentVolumeReclaimPolicy: Delete
  storageClassName: local-storage
  local:
    path: /var/lib/mysql
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - node2
  # nfs 存储
  # storageClassName: nfs
  #   nfs: 
  #     path: /nfsdata/mysql 
  #     server: 192.168.5.200
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: xbd-mysql-pvc
spec:
  accessModes: ["ReadWriteOnce"]
  storageClassName: "local-storage"
  resources:
    requests:
      storage: 2Gi
  selector:
    matchLabels:
      app: xbd-mysql-pv

  四、实际中的使用,比如mysql

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: xbd-mysql
spec:
  serviceName: xbd-mysql
  replicas: 1
  selector:
    matchLabels:
      app: xbd-mysql
  template:
    metadata:
      labels:
        app: xbd-mysql
    spec:
      containers:
        - name: xbd-mysql
          image: registry.cn-chengdu.aliyuncs.com/xbd/xbd-mysql:1.0
          imagePullPolicy: IfNotPresent
          args:
            - --lower_case_table_names=1
            - --character-set-server=utf8
            - --collation-server=utf8_general_ci
          env:
            - name: MYSQL_ROOT_PASSWORD
              value: root
          volumeMounts:
            - mountPath: /var/lib/mysql
              name: xbd-mysql-storage
      imagePullSecrets:
        - name: aliyun-secret
      volumes:
        - name: xbd-mysql-storage
          persistentVolumeClaim:
            claimName: xbd-mysql-pvc
apiVersion: v1
kind: Service
metadata:
  name: xbd-mysql
spec:
  clusterIP: None
  ports:
    - port: 3306
      protocol: TCP
      targetPort: 3306
  selector:
    app: xbd-mysql
  type: ClusterIP

 

posted @ 2022-09-27 17:11  小不点丶  阅读(292)  评论(0编辑  收藏  举报