mysql service pending in kubernetes

4/7/2021

I created .yaml file to create mysql service on kubernetes for my internal application, but it's unreachable. I can reach application and also phpmyadmin to reach database but it's not working properly. I'm stuck with pending status on mysql pod.

.yaml file:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
  namespace: cust
  labels:
    app: db
spec:
  replicas: 1
  selector:
    matchLabels:
      app: db
  template:
    metadata:
      labels:
        app: db
    spec:
      containers:
      - name: mysql
        image: mysql
        imagePullPolicy: Never
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: flaskapi-cred
              key: db_root_password
        ports:
        - containerPort: 3306
          name: db-container
        volumeMounts:
          - name: mysql-persistent-storage
            mountPath: /var/lib/mysql
      volumes:
        - name: mysql-persistent-storage
          persistentVolumeClaim:
            claimName: mysql-pv-claim


---
apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: cust
  labels:
    app: db
spec:
  ports:
  - port: 3306
    protocol: TCP
    name: mysql
  selector:
    app: db
  type: LoadBalancer

kubectl get all output is:

NAME                                         READY   STATUS    RESTARTS   AGE
pod/flaskapi-deployment-59bcb745ff-gl8xn     1/1     Running   0          117s
pod/mysql-99fb77bf4-sbhlj                    0/1     Pending   0          118s
pod/phpmyadmin-deployment-5fc964bf9d-dk59t   1/1     Running   0          118s

NAME                                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/flaskapi-deployment     1/1     1            1           117s
deployment.apps/mysql                   0/1     1            0           118s
deployment.apps/phpmyadmin-deployment   1/1     1            1           118s

I already did docker pull mysql.

Edit

Name:           mysql-99fb77bf4-sbhlj
Namespace:      z2
Priority:       0
Node:           <none>
Labels:         app=db
                pod-template-hash=99fb77bf4
Annotations:    <none>
Status:         Pending
IP:
IPs:            <none>
Controlled By:  ReplicaSet/mysql-99fb77bf4
Containers:
  mysql:
    Image:      mysql
    Port:       3306/TCP
    Host Port:  0/TCP
    Environment:
      MYSQL_ROOT_PASSWORD:  <set to the key 'db_root_password' in secret 'flaskapi-secrets'>  Optional: false
    Mounts:
      /var/lib/mysql from mysql-persistent-storage (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-gmbnd (ro)
Conditions:
  Type           Status
  PodScheduled   False
Volumes:
  mysql-persistent-storage:
    Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:  mysql-pv-claim
    ReadOnly:   false
  default-token-gmbnd:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-gmbnd
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                 node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type     Reason            Age    From               Message
  ----     ------            ----   ----               -------
  Warning  FailedScheduling  3m44s  default-scheduler  persistentvolumeclaim "mysql-pv-claim" not found
  Warning  FailedScheduling  3m44s  default-scheduler  persistentvolumeclaim "mysql-pv-claim" not found
-- user15576361
kubernetes
mysql

2 Answers

4/7/2021

you are missing the volume to attach with the pod or deployment. PVC is required as your deployment configuration is using it.

you can see clearly : persistentvolumeclaim "mysql-pv-claim" not found

you can apply below YAML and try.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 20Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/mnt/data"
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
-- Harsh Manvar
Source: StackOverflow

4/7/2021

As the error in the message clearly shows persistentvolumeclaim "mysql-pv-claim" not found. So you need to provision a persistentvolumeclaim (PVC).

There is a static & dynamic provisioning but I'll explain static provisioning here as it will be relatively easy for you to understand & setup. You need to create a PersistentVolume (PV) which the PVC will use. There are various types of Volumes, about which you read here.

Which type of Volume you would wanna create would be your choice depending on your environment and needs. A simple example would be of volume type hostPath.

Create a PV:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: mysql-pv-volume
  namespace: cust
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    # The configuration here specifies that the volume is at /tmp/data on the cluster's Node
    path: "/tmp/data"

And then create a PVC:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pv-claim
  namespace: cust
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
  volumeName: mysql-pv-volume

Once the PVC is successfully created, your deployment shall go through.

-- rock&#39;n rolla
Source: StackOverflow