3

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.

1 Answer 1

7

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:

Sign up to request clarification or add additional context in comments.

5 Comments

But if I run ifconfig, it displays pods network not the host.
Kubernetes and Docker are abstracting some things away from you. Network is one of it, filesystem another one. Is there a special use-case why you need the network interface of the host, or are you just trying to figure out how Kubernetes works? :-)
I wouldn't recommend pods accessing the host whether file system or network unless you have no other choice. But in order for your ifconfig to work, you might need to set hostNetwork to true in the pod.
I need to run a bash script on the host using inside of a daemonset pod conditionally from a nodejs app.
@VahidAlimohamadi whatever you're trying to accomplish, there's probably a better way than that. Maybe a separate question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.