<p>Is there a way to get a kubernetes-service restarted in case the service is deleted or if it goes down ?</p>
<p>We have two services defined in a file service-zookeeper-group.yaml</p>
<p>On making a random check we found the service &quot;zk-group&quot; missing. (maybe someone accidentally deleted it)</p>
<pre><code>zk-service  LoadBalancer  10.88.12.113   10.128.0.3   2181:32581/TCP
zk-group    ClusterIP     None           &lt;none&gt;       &lt;none&gt;
</code></pre>
<p>Is there a way to perform &quot;liveness&quot; check for the service &quot;zk-group&quot; and get this service started if it goes down ?</p>
<p>service-zookeeper-group.yaml</p>
<pre><code>kind: Service
metadata:
  name: zk-group
  namespace: {{ .Release.Namespace }}
  labels:
    app: {{ .Release.Name }}-zk-app
spec:
  clusterIP: None
  selector:
    app: {{ .Release.Name }}-zk-app


apiVersion: v1
kind: Service
metadata:
  name: zk-service
  annotations:
    cloud.google.com/load-balancer-type: &quot;Internal&quot;
  labels:
    app: {{ .Release.Name }}-zk-app
spec:
  ports:
  - protocol: TCP
    name: tcp-{{ .Values.zk.port }}
    port: {{ .Values.zk.port }}
    targetPort: {{ .Values.zk.port }}
  type: LoadBalancer
  loadBalancerIP: {{ .Values.networking.loadBalancerIP }}
  loadBalancerSourceRanges: {{ .Values.networking.loadBalancerSourceRanges | toJson }}
  selector:
    app: {{ .Release.Name }}-zk-app

</code></pre>

<p>There is no Kubernetes &quot;native&quot; utility to check if a service exists and - if not - create one. If you really need this functionality you would probably need to extend your Cluster with a <a href="https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/#custom-controllers" rel="nofollow noreferrer">Kubernetes Controller</a>.</p>
<p>You can recreate the desired state with the GitOps pattern utilizing a GitOps controller such as <a href="https://github.com/fluxcd/flux" rel="nofollow noreferrer">Flux</a> or <a href="https://argoproj.github.io/argo-cd/" rel="nofollow noreferrer">Argo</a>. If someone really &quot;accidentally&quot; removes a resource those controllers would ensure to reapply the desired state within a given period of time.</p>
<blockquote>
<p>&quot;maybe someone accidentally deleted it&quot;</p>
</blockquote>
<p>Your specific case sounds more like a lack of monitoring and governance:</p>
<ol>
<li>If a service is deleted accidentally - why aren't any alerts popping up? A simple probe test (e.g. via <a href="https://github.com/prometheus/blackbox_exporter" rel="nofollow noreferrer">Prometheus BlackBox exporter</a> or <a href="https://www.elastic.co/de/beats/heartbeat" rel="nofollow noreferrer">Elastic Heartbeat</a> (if you are using the elastic stack)) can check for the availability of services within the cluster.</li>
<li>How is it possible that something important like a service gets deleted accidentally? Who is working with the cluster and who really needs the power to delete/adjust service resources? I guess it makes sense to evaluate the roles/permissions on your cluster.</li>
<li>Consider activating the Kubernetes <a href="https://kubernetes.io/docs/tasks/debug-application-cluster/audit/" rel="nofollow noreferrer">audit trail</a>: If I would be the cluster administrator I'd like to know who deleted the service.</li>
</ol>
