How to login as root user to Azure Kubernetes pod

Viewed 2407

I deployed Postgresql on Azure Kubernetes Service (AKS). It works fine. But when I login to the pod with kubectl exec -it pod_name bash, it's automatically login with "postgres" user and I can't switch to the "root" user.

If I could able to login Kubernetes' nodes with ssh, I could use docker exec -it -u root image_id and login with "root" user, but as I know it's not possible on Azure.

How can I login to the pods as "root" user on AKS?

Thanks!

2 Answers

You can add pod securityContext. where you can set the UID 0 which is for root user. By default then, The Pod will run as root user. Ref

apiVersion: v1
kind: Pod
metadata:
  name: demo-pod
spec:
  securityContext:
    runAsUser: 0

Or, If you want to run just the postgres container of your pod as root then you need to use container's security context.

apiVersion: v1
kind: Pod
metadata:
  name: security-context-demo-2
spec:
  containers:
  - name: postgres
    image: postgres:13.2
    securityContext:
      runAsUser: 0
      Privileged: true

Try this, it has helped me:

Kubectl -n [NAMESPACE](optional) exec --stdin --tty [POD_NAME] -- ./bin/bash
Related