2
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
  namespace: cp1
spec:
  selector:
    matchLabels:
      app: mysql
  serviceName: mysql
  replicas: 1
  template:
    metadata:
      labels:
        app: mysql
    spec:
      terminationGracePeriodSeconds: 10
      containers:
        - name: mysql
          image: mysql:latest
          env:
            - name: MYSQL_ROOT_HOST
              value: '%'
            - name: MYSQL_LOG_CONSOLE
              value: "true"
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                configMapKeyRef:
                  key: MYSQL_PASSWORD
                  name: env-map-service
          ports:
            - containerPort: 3306
              name: mysql
          resources:
            requests:
              cpu: 500m
              memory: 1Gi
          volumeMounts:
            - name: mysql-vol
              mountPath: /var/lib/mysql
  volumeClaimTemplates:
    - metadata:
        name: mysql-vol
      spec:
        accessModes: [ "ReadWriteOnce" ]
        resources:
          requests:
            storage: 1Gi
        storageClassName: test-sc

apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: cp1
  annotations:
    service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:
  ports:
    - port: 3306
      name: mysql
      targetPort: 3306
  clusterIP: None
  selector:
    app: mysql

Its my app deployment yaml file. which work perfectly,

apiVersion: apps/v1
kind: Deployment
metadata:
  name: kube-child-app
  labels:
    app: kube-child-app
  namespace: cp1
spec:
  replicas: 1
  template:
    metadata:
      name: kube-child-app
      labels:
        app: kube-child-app
    spec:
      containers:
        - name: kube-child-app
          image: jahadulrakib/kube-child-app:latest
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 8081
      restartPolicy: Always
  selector:
    matchLabels:
      app: kube-child-app

I want to deploy a database application in my local kubernetes cluster. But its gives following error and do not run pod. here i attach my yaml file for pod up. i create for this StroageClass, PV.

Error: Error from server (BadRequest): pod mysql-0 does not have a host assigned

UPDATE 1:

Warning FailedScheduling 5h40m default-scheduler 0/1 nodes are available: 1 pod has unbound immediate PersistentVolumeClaims

pvc and pv are in pending status

UPDATE 2:

PVC in pending status because storage class cant create the needed PV

15m Warning FailedScheduling pod/mysql-0 0/1 nodes are available: 1 pod has unbound immediate PersistentVolumeClaims. 4m12s Normal ExternalProvisioning persistentvolumeclaim/mysql-vol-mysql-0 waiting for a volume to be created, either by external provisioner "docker.io/hostpath" or manually created by system administrator

UPDATE 3:

the issue seems to stem from difference between the available PV labels and configuration we want to use and the ones set in the statefulSet volumeClaimTemplates

Noam Yizraeli
  • 4,446
  • 18
  • 35
Jahadul Rakib
  • 476
  • 1
  • 5
  • 19
  • 1
    do you have some other application or pod running in `cp1` namespace? if not, that might be a node-selector configuration issue – Noam Yizraeli Sep 01 '21 at 08:27
  • no, other deployment and pod running well using cp1 namespace – Jahadul Rakib Sep 01 '21 at 08:31
  • 1
    is this the only event or log available? do you have some more node-selector configurations in the ones that are working? can you add a working example like a deployment? What's your cluster architecture like (worker,master, infra nodes)? – Noam Yizraeli Sep 01 '21 at 08:42
  • apiVersion: apps/v1 kind: Deployment metadata: name: kube-child-app labels: app: kube-child-app namespace: cp1 spec: replicas: 1 template: metadata: name: kube-child-app labels: app: kube-child-app spec: containers: - name: kube-child-app image: jahadulrakib/kube-child-app:latest imagePullPolicy: IfNotPresent ports: - containerPort: 8081 restartPolicy: Always selector: matchLabels: app: kube-child-app – Jahadul Rakib Sep 01 '21 at 08:55
  • please add that in the question as an EDIT with formatted code text not in the comments – Noam Yizraeli Sep 01 '21 at 09:02
  • 1
    added my workable deployment file in question. – Jahadul Rakib Sep 01 '21 at 09:35
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/236640/discussion-between-noam-yizraeli-and-rakib-diu). – Noam Yizraeli Sep 01 '21 at 09:42

1 Answers1

2

Looks like the PVC was not satisfied with the needed PV because the storageClass didnt create a new PV and the existing PV didnt match the Pending PVC so they couldnt bound together. after changing the appropriate fields they bound together.

Although it only uses a single replica a statefulSet is still the best option over using a deployment with a pvc instead mainly because of the application intention being a DB scaling a birds eye view of the project explain the application meaning better when using a statefulSet since it is stateful and it can in the future turn into a set of pods instead of just one.

check out kubernetes article on it when is was first popularised and set as in useful state, and theire current docs on it

Noam Yizraeli
  • 4,446
  • 18
  • 35