Restart only sidecar container in Kubernetes

7/19/2019

I have two containers in my POD. First container is my main application and second is used as sidecar container having following images having following Dockerfile.

FROM scratch

EXPOSE 8080
ADD my-binary /

ENV GOROOT=/usr/lib/go
ENTRYPOINT ["/my-binary"]

Basically it's using scratch and my-binary is a go application which running as process. So I cannot exec on this side car container. I have a requirement to re-start the side container(my-binary), but there should be no change in the main container. Main container should not be altered in any way.

Is there any possibility, how I can achieve this?

Thank you so much for looking into this.

Someone has asked to provide the complete details of POD, then you can consider following pod structure

apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
  labels:
    app: my-deploy
spec:
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: main
        image: my-main-app-image
        ports:
        - containerPort: 80
      - name: my-go-binary
        image: my-go-binary-image

Please note-

kubectl exec POD_NAME -c CONTAINER_NAME reboot 

this is not going to work for 2nd container as it is scratch image.

-- nagendra547
containers
docker
kubernetes

1 Answer

7/20/2019

So, your reboot command wasn't working (of course it will newer work) cause of using scatch as the base image.

This image is most useful in the context of building base images (such as debian and busybox) or super minimal images (that contain only a single binary and whatever it requires, such as hello-world).

See, https://hub.docker.com/_/scratch

The base image scratch is Docker's reserved minimal image. It can be a starting point for building small-sized containers. Using the scratch “image” signals to the build process that you want the next command in the Dockerfile to be the first filesystem layer in your image.

Ref: https://docs.docker.com/develop/develop-images/baseimages/#create-a-simple-parent-image-using-scratch

From you provided dockerfile, there is the only filesystem is your go-binery. There is nothing other than this. That's why you can't (couldn't) run reboot command. If you change the base image like busybox or alpine or anything other you will be able to run the reboot command.

But keep in mind that your new base image must have a proper shell to run your expected command. For example, the busybox image has shell (bash) and so it's possible to run reboot command. And the alpine image has shell (sh) too.

-- Shudipta Sharma
Source: StackOverflow