How to configure automatic pod reboot in Kubernetes if another pod reboots

7/30/2021

I have two pods, after the first pod has rebooted, the other should automatically reboot after it. Pods are created using different deployments

-- alexis
kubernetes
kubernetes-pod

1 Answer

7/30/2021

You can write down the script in python or any language which will look for the event for POD or deployment as soon as one gets restarted your script will roll out the deployment this way for the second deployment PODs will be restarted.

You can run the script inside one POD or as deployment which will continuously monitor the Deployment-1 and restart the deployment-2 if any changes occurred in deployment-1.

Example python client

import logging
import time
from kubernetes import client, config, watch
import json
import requests
import time
logger = logging.getLogger('k8s_events')
logger.setLevel(logging.DEBUG)

# If running inside pod
config.load_incluster_config()

# If running locally
#config.load_kube_config()

v1 = client.CoreV1Api()
v1ext = client.ExtensionsV1beta1Api() 

w = watch.Watch()

mydict={}

webhook_url = '';

while True:
	pod_list= v1.list_namespaced_pod("default");

further, you can write the logic for checking the restart count.

https://kubernetes.io/docs/tasks/administer-cluster/access-cluster-api/

List of official libraries : https://kubernetes.io/docs/reference/using-api/client-libraries/#officially-supported-kubernetes-client-libraries

-- Harsh Manvar
Source: StackOverflow