We have a k8s POD that contains two containers, c1 and c2. How do we trigger a restart of the second container (c2) based on some event of the first container (c1)?
For instance, container c1 is a data container that pulls data from s3 bucket. The data is shared and used by the second container (c2). We want to restart the second container (c2) when data is pulled from s3 so it can be loaded to memory by container c2.
I think you could achieve it using the shareProcessNamespace
option.
Take a look to this dummy example:
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: mult
iple name: multiple
spec:
selector:
matchLabels:
app: multiple
template:
metadata:
labels:
app: multiple
spec:
shareProcessNamespace: true
containers:
- image: nginx:alpine
name: nginx
- image: redis:alpine
name: redis
The result should be something like this:
# kubectl get pods
multiple-5f44d65855-xzgz2 2/2 Running 0 84s
Now, if you enter in the container nginx
of the pod, you can see the shared PID namespace:
# kubectl exec -it multiple-5f44d65855-xzgz2 -c nginx -- ps aux
PID USER TIME COMMAND
1 root 0:00 /pause
51 root 0:00 nginx: master process nginx -g daemon off;
56 nginx 0:00 nginx: worker process
57 nginx 0:00 nginx: worker process
58 nginx 0:00 nginx: worker process
59 nginx 0:00 nginx: worker process
74 999 0:00 redis-server
86 root 0:00 ps aux
At this point, you can perform your business logic that can be a kill -9 or, if you can trap the signal, intercept a sighup in order to restart the process.