<p>I have a requirement to delete all pods for <code>service1-deployment</code> when container restart happens in <code>service2-deployment</code>.</p>
<p>I found out that we can do it through <a href="https://kubernetes.io/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/" rel="nofollow noreferrer">lifeCycle event</a> handler <code>service2-deployment</code>.</p>
<p>But we can not specify <code>kubeclt delete pod</code> command here since it runs inside a pod. Is there any easy way to restart 2nd pod based on 1st Pod's lifeCycle events?</p>

Gajukorse
<p><strong>Disclaimer</strong> - as mentioned in the comments,  you should avoid this solution (<a href="https://softwareengineering.stackexchange.com/questions/411082/should-microservices-be-independent/411136#411136">microservices should be independent</a>) until you really have no other choice.</p>
<hr />
<p>You can <a href="https://kubernetes.io/docs/tasks/configure-pod-container/attach-handler-lifecycle-event/#define-poststart-and-prestop-handlers" rel="nofollow noreferrer">setup both <code>postStart</code> and <code>preStop</code> handlers</a> (for installing <code>kubectl</code> binary and for deleting the pods from deployment), but first you need to create a <a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/" rel="nofollow noreferrer">proper Service Account for the pod</a> and <a href="https://kubernetes.io/docs/reference/access-authn-authz/rbac/" rel="nofollow noreferrer">Role(Bindings)</a> with permissions to delete pods:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: role-for-hook
subjects:
- kind: ServiceAccount
  name: service-account-for-hook
  namespace: default
roleRef:
  kind: ClusterRole
  name: delete-pods-role
  apiGroup: rbac.authorization.k8s.io
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: service-account-for-hook
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: delete-pods-role
  labels:
    # Add these permissions to the &quot;view&quot; default role.
    rbac.authorization.k8s.io/aggregate-to-view: &quot;true&quot;
rules:
- apiGroups: [&quot;*&quot;]
  resources: [&quot;pods&quot;]
  verbs: [&quot;delete&quot;,&quot;list&quot;]
</code></pre>
<p>Then, you can use newly created Service Account + <code>postStart</code> and <code>preStop</code> handlers in pod / deployment definition - example for NGINX image. I assumed, that the label for the pods from the <code>Service1</code> is <code>app=service1</code></p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: service2-deployment
spec:
  selector:
    matchLabels:
      app: service2
  replicas: 2
  template:
    metadata:
      labels:
        app: service2
    spec:
      serviceAccountName: service-account-for-hook
      containers:
      - name: service2
        image: nginx:latest
        lifecycle:
          postStart:
            exec:
              command: [&quot;/bin/sh&quot;, &quot;-c&quot;, &quot;apt update &amp;&amp; apt install curl &amp;&amp; curl -L https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl -o /usr/local/bin/kubectl &amp;&amp; chmod +x /usr/local/bin/kubectl&quot;] 
          preStop:
            exec:
              command: [&quot;/bin/sh&quot;, &quot;-c&quot;, &quot;kubectl delete pods -l app=service1&quot;]
        ports:
          - containerPort: 80
</code></pre>
<p>Then, if the pod from <code>Service2</code> is restarted, the pods from <code>Service1</code> are also restarted.</p>
<p>Commands used in the <code>command:</code> could be different for you, it depends which base image you are using for your application.</p>
