首页 > 解决方案 > 如何以 root 用户身份登录到 Azure Kubernetes pod

问题描述

我在 Azure Kubernetes 服务 (AKS) 上部署了 Postgresql。它工作正常。但是当我使用 登录 pod 时kubectl exec -it pod_name bash,它会自动使用“ postgres ”用户登录,我无法切换到“ root ”用户。

如果我能够使用 ssh 登录 Kubernetes 的节点,我可以使用docker exec -it -u root image_idroot ”用户登录,但我知道这在 Azure 上是不可能的。

如何在 AKS 上以“root”用户身份登录 pod?

谢谢!

标签: azurekubernetesrootazure-aks

解决方案


您可以添加 pod securityContext。您可以在其中设置UID 0哪个是root用户。默认情况下,Pod 将以 root 用户身份运行。参考

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

或者,如果您只想以 root 身份运行 pod 的 postgres 容器,那么您需要使用容器的安全上下文。

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


推荐阅读