如何在Kubernetes中对MySQL数据使用PersistentVolume

时间:2018-09-25 03:28:30

标签: mysql kubernetes

我正在Minikube上开发数据库环境。 我想通过Kubernetes的PersistentVolume函数持久化MySQL数据。 但是,如果hostPath指定了/ var / lib / mysql(MySQL数据目录),则在启动MySQL服务器时会发生错误,并且不会启动。

kubernetes-config.yaml

  apiVersion: v1
  kind: PersistentVolume
  metadata:
    name: nfs001-pv
    labels:
      app: nfs001-pv
  spec:
    capacity:
      storage: 1Gi
    accessModes:
      - ReadWriteMany
    persistentVolumeReclaimPolicy: Retain
    mountOptions:
      - hard
    nfs:
      path: /share/mydata
      server: 192.168.99.1
  ---
  apiVersion: v1
  kind: PersistentVolumeClaim
  metadata:
    name: nfs-claim
  spec:
    accessModes:
      - ReadWriteMany
    resources:
      requests:
        storage: 1Gi
    storageClassName: ""
    selector:
      matchLabels:
        app: nfs001-pv
  ---
  apiVersion: apps/v1beta2
  kind: Deployment
  metadata:
    name: sk-app
    labels:
      app: sk-app
  spec:
    replicas: 1
    selector:
      matchLabels:
        app: sk-app
    template:
      metadata:
        labels:
          app: sk-app
      spec:
        containers:
        - name: sk-app
          image: mysql:5.7
          imagePullPolicy: IfNotPresent
          env:
          - name: MYSQL_ROOT_PASSWORD
            value: password
          ports:
          - containerPort: 3306
          volumeMounts:
          - mountPath: /var/lib/mysql
            name: mydata
        volumes:
          - name: mydata
            persistentVolumeClaim:
              claimName: nfs-claim
  ---
  apiVersion: v1
  kind: Service
  metadata:
    name: sk-app
    labels:
      app: sk-app
  spec:
    type: NodePort
    ports:
    - port: 3306
      nodePort: 30001
    selector:
      app: sk-app

我如何启动它?

-后记-

当我尝试“ kubectl日志”时,出现以下错误消息。

chown: changing ownership of '/var/lib/mysql/': Operation not permitted

当我尝试“ kubectl describe xxx”时,得到了以下结果。

kubectl描述pv:

Name:            nfs001-pv
Labels:          app=nfs001-pv
Annotations:     pv.kubernetes.io/bound-by-controller=yes
StorageClass:    
Status:          Bound
Claim:           default/nfs-claim
Reclaim Policy:  Retain
Access Modes:    RWX
Capacity:        1Gi
Message:         
Source:
  Type:      NFS (an NFS mount that lasts the lifetime of a pod)
  Server:    192.168.99.1
  Path:      /share/mydata
  ReadOnly:  false
Events:        <none>

kubectl描述pvc:

Name:          nfs-claim
Namespace:     default
StorageClass:  
Status:        Bound
Volume:        nfs001-pv
Labels:        <none>
Annotations:   pv.kubernetes.io/bind-completed=yes
               pv.kubernetes.io/bound-by-controller=yes
Capacity:      1Gi
Access Modes:  RWX
Events:        <none>

kubectl描述部署:

Name:                   sk-app
Namespace:              default
CreationTimestamp:      Tue, 25 Sep 2018 14:22:34 +0900
Labels:                 app=sk-app
Annotations:            deployment.kubernetes.io/revision=1
Selector:               app=sk-app
Replicas:               1 desired | 1 updated | 1 total | 0 available | 1 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=sk-app
  Containers:
   sk-app:
    Image:  mysql:5.7
    Port:   3306/TCP
    Environment:
      MYSQL_ROOT_PASSWORD:  password
    Mounts:
      /var/lib/mysql from mydata (rw)
  Volumes:
   mydata:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  nfs-claim
    ReadOnly:   false
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      False   MinimumReplicasUnavailable
  Progressing    True    ReplicaSetUpdated
OldReplicaSets:  <none>
NewReplicaSet:   sk-app-d58dddfb (1/1 replicas created)
Events:
  Type    Reason             Age   From                   Message
  ----    ------             ----  ----                   -------
  Normal  ScalingReplicaSet  23s   deployment-controller  Scaled up replica set sk-app-d58dddfb to 1

1 个答案:

答案 0 :(得分:1)

卷看起来不错,所以看起来您的nfs卷的根目录上只有一个权限问题,该卷已作为dfin <- structure(list(ID = c(1L, 1L, 1L, 2L, 2L, 2L), TIME = c(0L, 1L, 2L, 0L, 10L, 15L), CONC = c(5L, 4L, 3L, 2L, 2L, 1L), STATUS = c(0L, 1L, 0L, 0L, 0L, 0L)), class = "data.frame", row.names = c(NA, -6L)) 挂载在您的容器上。

您可以:

1)使用nfs挂载命令挂载该nfs卷并运行:

/var/lib/mysql

2)在您的部署中运行一个initContainer,类似于:

chmod 777 .  # This gives rwx to anybody so need to be mindful.