How to run commands from a pod to the host in kubernetes

7/30/2018

Is it possible to run commands on host from within a pod in kubernetes.

So for example, I have a pod running a python image which calculates the size of the os. But the command it uses runs inside the pod, not in the host. Is it possible to run the command on the host from pod.

-- S Andrew
kubernetes

1 Answer

7/30/2018

Actually a command run inside a pod is run on the host. It's a container (Docker), not a virtual machine. That means when you execute something in the pod like retrieving the size of your RAM it will usually return the RAM of the whole machine. If you want to get the "size of the os" and you mean the hard drive with it, you need to mount the hard drive to count it.

If your actual problem is that you want to do something, which a normal container isn't allowed to, you can run a pod in privileged mode or configure whatever you exactly need. You need to add a security context to your pod like this (taken from the docs):

apiVersion: v1
kind: Pod
metadata:
  name: security-context-demo
spec:
  securityContext:
    runAsUser: 1000
    fsGroup: 2000
  volumes:
  - name: sec-ctx-vol
    emptyDir: {}
  containers:
  - name: sec-ctx-demo
    image: gcr.io/google-samples/node-hello:1.0
    volumeMounts:
    - name: sec-ctx-vol
      mountPath: /data/demo
    securityContext:
      privileged: true

Sources:

-- Pampy
Source: StackOverflow