<p>I want to deploy a front-end pod which is connected to backend pod( having mysql in it) and  store the data into persistent volume.</p>

Sujeet Jha
<p><strong>Connecting fronted pod with backend pod</strong></p>

<p>Create a service for your deployment and point your app to that service name
The key to connecting a frontend to a backend is the backend Service. A Service creates a persistent IP address and DNS name entry so that the backend microservice can always be reached. A Service uses <a href="https://kubernetes.io/docs/concepts/overview/working-with-objects/labels/" rel="nofollow noreferrer">selectors</a> to find the Pods that it routes traffic to.</p>

<p>First, configure the MySQL service as a  <code>ClusterIP</code>  service. It will be private, visible only for other services. This is could be done removing the line with the option  <code>type</code>.</p>

<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Service
metadata:
   name: app-api-mysql-svc
spec:
   selector:
     app: app-api-mysql
   ports:
     - protocol: TCP
       port: 80
       targetPort: [the port exposed by the mysql pod]
</code></pre>

<p>Now that you have your backend, you can create a frontend that connects to the backend. The frontend connects to the backend worker Pods by using the DNS name given to the backend Service. The DNS name is “<strong>app-api-mysql-svc</strong>”, which is the value of the <code>name</code> field in the preceding Service configuration file.</p>

<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Service
metadata:
  name: frontend
spec:
  selector:
    app: app-api-mysql
  ports:
  - protocol: "TCP"
    port: 80
    targetPort: 80
  type: LoadBalancer
</code></pre>

<p>Similar to the backend, the frontend has service too. The configuration for the Service has <code>type: LoadBalancer</code>, which means that the Service uses the default load balancer of your cloud provider.</p>

<p>You can also <strong>proxy</strong> all your backend calls through your front end server</p>

<p>If you are routing (or willing to route) all your microservices/backend call thru the server side of your front end and if are deploying both your front end and backend in the same k8s cluster in the same namespace, then you can use KubeDNS add-on (If it is not available in your k8s cluster yet, you can check with the k8s admin) to resolve the backend service name to it's IP. From your front end  <strong>server</strong>, Your backend service will always be resolvable by it's name.</p>

<p>Since you have kubeDNS in your k8s cluster, and both frontend and backend services resides in same k8s cluster and same namespace, we can make use of k8s' inbuilt service discovery mechanism. Backend service and frontend service will be discoverable each other by it's name. That means, you can simply use the DNS name "backend" to reach your backend service from your frontend  <strong>pods</strong>. So, just proxy all the backend request through your front end nginx to your upstream backend service. In the frontend nginx  <strong>pods</strong>, backend service's IP will resolvable for the domain name "backend". This will save you the CORS headache too. This setup is portable, meaning, it doesn't matter whether you are deploying in dev or stage or prod, name "backend" will always resolve to the corresponding backend.</p>

<p>More information you can find here: <a href="https://stackoverflow.com/questions/45165418/kubernetes-communication-between-frontend-and-backend">backend-frontend</a>, <a href="https://kubernetes.io/docs/tasks/access-application-cluster/connecting-frontend-backend/" rel="nofollow noreferrer">frontend-backend-pod-connection</a>.</p>

<p><strong>Connecting Persistent Volume to pod</strong></p>

<p>MySQL requires a PersistentVolume to store data. Their PersistentVolumeClaims will be created at the deployment step.</p>

<p>Many cluster environments have a default StorageClass installed. When a StorageClass is not specified in the PersistentVolumeClaim, the cluster’s default StorageClass is used instead.</p>

<p>When a <strong>PersistentVolumeClaim</strong> is created, a <strong>PersistentVolume</strong> is dynamically provisioned based on the <strong>StorageClass</strong> configuration.</p>

<p>Here you can find detailed guide how to configure MySQL pod with Persisten Volume: <a href="https://kubernetes.io/docs/tutorials/stateful-application/mysql-wordpress-persistent-volume/" rel="nofollow noreferrer">pv-mysql-wordpress</a>.</p>
