<p>I have a kubernetes cluster and 2 replicated databases.I want to be able to connect that databases inside cluster. Databases will be statefulset and have a headless server.
Lets say that pod names are D-1 and D-2. D-1 will be master and D-2 will be replica. I want to redirect all my traffic to master (D-1) but if master pod fails it should redirect
traffic to replica(D-2). I can not do it on client side so i must find a way in kubernetes. How can i build this structure ?
So if i curl to </p>

<blockquote>
  <p>database-service.default.svc.cluster.local</p>
</blockquote>

<p>it must always go to D-1 pod and if D-1 pod is not available it must redirect all traffic to D-2.</p>

<p>The answer depends on DB type you are using. </p>

<p>Generally <a href="https://kubernetes.io/docs/concepts/services-networking/service/" rel="nofollow noreferrer">Service</a> is just an abstract way to expose an application running on a set of <a href="https://kubernetes.io/docs/concepts/workloads/pods/pod-overview/" rel="nofollow noreferrer">Pods</a> as a network service. </p>

<pre><code>$  kubectl get svc -o wide
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)        AGE    SELECTOR
kubernetes           ClusterIP   10.0.0.1     &lt;none&gt;        443/TCP        6d3h   &lt;none&gt;
db-access            ClusterIP   10.0.5.233   &lt;none&gt;        3306/TCP       3m     app=my_db
</code></pre>

<p>Service in that example routes traffic to the <a href="https://kubernetes.io/docs/concepts/services-networking/endpoint-slices/" rel="nofollow noreferrer">Enpoint Slice</a> according to the <code>app=my_db</code> selector.</p>

<p>As you know, K8s evicts failed pods automatically that is why traffic isn't routed to the failed pods. However, </p>

<ul>
<li>K8s loadbalances traffic (through the service) when both DBs are online;</li>
<li>there is a delay between the Pod crash and moment it's evicted (so traffic isn't routed to it).</li>
</ul>

<p>That is why in order to achieve the fail over you have described:</p>

<blockquote>
  <p>I want to redirect all my traffic to master (D-1) but if master pod fails it should redirect traffic to replica(D-2)</p>
</blockquote>

<p>it looks like the "operator" is exactly what is needed here. Operator is not something that is included to K8s, but often provided by the DBVendor. </p>

<p>A good example here is the <a href="https://blogs.oracle.com/developers/introducing-the-oracle-mysql-operator-for-kubernetes" rel="nofollow noreferrer">Oracle MySQL Operator for Kubernetes</a>. </p>

<p>It is a K8s controller that can be installed into any existing K8s cluster. Once installed, it will enable users to create and manage production-ready MySQL clusters using a simple declarative configuration format.</p>

<p>It's feature rich, allows you to manage cluster, perform backups/restores, provides metrics, etc.</p>

<p>Additionally, you can consider using Cloud native distributed databases as <a href="https://stackoverflow.com/users/213269/jonas">@Jonas</a> suggested earlier.</p>

<p>Hope that helps.</p>
