<pre><code>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: &quot;true&quot;
            - 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: [ &quot;ReadWriteOnce&quot; ]
        resources:
          requests:
            storage: 1Gi
        storageClassName: test-sc
</code></pre>
<hr />
<pre><code>apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: cp1
  annotations:
    service.alpha.kubernetes.io/tolerate-unready-endpoints: &quot;true&quot;
spec:
  ports:
    - port: 3306
      name: mysql
      targetPort: 3306
  clusterIP: None
  selector:
    app: mysql
</code></pre>
<p>Its my app deployment yaml file. which work perfectly,</p>
<pre><code>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
</code></pre>
<p>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.</p>
<p><strong>Error: Error from server (BadRequest): pod mysql-0 does not have a host assigned</strong></p>
<p><strong>UPDATE 1:</strong></p>
<p>Warning FailedScheduling 5h40m default-scheduler 0/1 nodes are available: 1 pod has unbound immediate PersistentVolumeClaims</p>
<p>pvc and pv are in pending status</p>
<p><strong>UPDATE 2:</strong></p>
<p>PVC in <code>pending</code> status because storage class cant create the needed PV</p>
<p>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 &quot;docker.io/hostpath&quot; or manually created by system administrator</p>
<p><strong>UPDATE 3:</strong></p>
<p>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 <code>volumeClaimTemplates</code></p>

<p>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 <code>Pending</code> PVC so they couldnt bound together. after changing the appropriate fields they bound together.</p>
<p>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.</p>
<p>check out <a href="https://kubernetes.io/blog/2016/12/statefulset-run-scale-stateful-applications-in-kubernetes/" rel="nofollow noreferrer">kubernetes article</a> on it when is was first popularised and set as in useful state, and theire current <a href="https://kubernetes.io/docs/concepts/workloads/controllers/statefulset/" rel="nofollow noreferrer">docs</a> on it</p>
