loading...
Cover image for Run Kubernetes Pod with root privileges (not recommended)

Run Kubernetes Pod with root privileges (not recommended)

techworld_with_nana profile image Techworld with Nana Updated on ・1 min read

kubernetes article (7 Part Series)

1) How to setup a Keycloak Gatekeeper to secure the services in your Kubernetes cluster 2) Kubectl | Minikube | Kops - What's the difference? πŸ€” 3 ... 5 3) How to create Kubernetes secrets? 4) What is a Kubernetes Stateful Set? 5) Difference between EmptyDir and HostPath volume types in Kubernetes 6) Kubernetes ConfigMap and Secret explained 7) Run Kubernetes Pod with root privileges (not recommended)

Problem:

Most Docker containers and the processes inside run with non-root user, because of better security. If the container process is running with root (uid 0) it will be the same root as on the host. In this case user may get access to host from the container, thus gaining the root privilege on the host. This is of course a security concern.

However there can be a case when you need to run a container with root privilege because of permission issues of the volumes on the host.

Solution:

In order to run a container inside a pod with root, add following config:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: my-app
spec:
  template: 
    spec:
      containers:
      - image: my-image
        name: my-app
        ...
        securityContext:
          allowPrivilegeEscalation: false
          runAsUser: 0

Now when you enter the 'my-image' container with docker exec or kubectl exec, you will see that the user is root. πŸ™ŒπŸΌ


⭐️ FREE Docker & Kubernetes course ⭐️

I'm happy to connect with you on πŸ™‚

kubernetes article (7 Part Series)

1) How to setup a Keycloak Gatekeeper to secure the services in your Kubernetes cluster 2) Kubectl | Minikube | Kops - What's the difference? πŸ€” 3 ... 5 3) How to create Kubernetes secrets? 4) What is a Kubernetes Stateful Set? 5) Difference between EmptyDir and HostPath volume types in Kubernetes 6) Kubernetes ConfigMap and Secret explained 7) Run Kubernetes Pod with root privileges (not recommended)

Posted on by:

techworld_with_nana profile

Techworld with Nana

@techworld_with_nana

DevOps Consultant | YouTuber 🎬 | Software Engineer πŸ‘©πŸ»β€πŸ’» Based in Austria πŸ‡¦πŸ‡Ή

Discussion

markdown guide
 

I know everyone always says don't run as ROOT. But what if you are using a database system that has to run as root. It's easy to say "dont do this" but in a situation like this, how do you protect all othe containers/pods from this "crazy" pod that runs processes as root.

 

However there can be a case when you need to run a container with root privilege because of permission issues of the volumes on the host.

Fix the permissions issues, not resort to run as root.

Sorry to tell you but youre article is really BAD ADVICE, specially when coming from a "senior" devops "engineer".

 
 

I have started the article by explicitly stating that this has a security issue. and for more context, I have done this as a quick fix, specifically for Jenkins that was running in a docker container. And if Jenkins job builds and pushes a Docker image, it will need the docker commands. So mounting the docker process into the Jenkins container was a work around I found for this case.

You are more than welcome to suggest SPECIFIC alternative solutions, instead of generally stating it's a bad solution, which I myself already mentioned in the article. Thanks.

 

For other readers: running a container with root privileges is a DEFINITELY NO.

I kind of get you. The reason of why others are pointing this is a super bad practice/anti-pattern is because your post title is "Run Kubernetes Pod with root privileges" (tagged with #tutorial and with a very elaborated and motivational image), that title is more a How-To guide than an advice request. So yes, is important to point this is a bad practice before other more inexperienced devs/devops read it.

You could change your post's purpose asking for recommendations in how to fix your permissions issues, tag it with #help and you will see the difference in the replys.

 

Thanks for pointing this out! I actually will adjust it, because could really be misleading

Still contains the tag tutorial.

This kind of article is what promotes bad practices all over the internet.

This just remembers the chmod 777 all around StackOverflow as a way of solving issues.

I removed the tag..don't know why it was not saved.

Just archived it. I don't rely on that post anyway.

 

No way you should run a container with root privileges... You need to fix whatever permissions are preventing you from running as different user.

 
 

It sounds like more of a security door is wide open than a concern, you don't seem phased, why is this?
Genuine question btw, some security problems sound way worse then they are.