如何在 kubernetes 上挂载 Kerberised NFS?

sy_*_*001 5 authorization nfs kerberos kubernetes

有没有办法以创建 pod 的用户身份在 Kubernetes pod 内挂载经过 Kerberos 身份验证的 NFS 服务器?

我们使用 FreeIPA 进行用户管理,并且我们有一个 Kubernetes 集群设置来训练我们的深度学习模型。我们在 NFS 上拥有我们的数据,它使用 Kerberos 进行身份验证。这是我们正在努力实现的目标:

  1. 在 pod 中挂载 Kerberized NFS。
  2. NFS 应该以与部署 pod 的用户相同的权限挂载。
  3. 用户不应该能够执行到其他用户部署的 pod 并访问他们的数据。

我们为 kubernetes 使用 GKE,我们的 NFS 在同一个 VPC 中。

Ape*_*ant 6

我就是这样做的。

对于我的方法,你需要:

  • 工作人员加入到 kerberso 领域。(NFS 通过主机 TGT 安装在工作线程中)
  • 密钥表存储在安全的地方,我使用保管库和保管库代理注入器
  • 将管理凭证的 Sidecar 容器
  • 服务器和 Pod 之间共享 KCM,共享 KCM 套接字,避免其他 Pod 访问存储的 TGT
  • Krb5 作为 configmap 存储在命名空间中
  • NFS 导出数据的适当权限,由 IPA 中存在的用户拥有,并且哪些 uids/gids 将用于运行 pod 中的容器。

采用这种方法的原因是:

  • 保管库中的信息受到保护
  • 秘密从存储中删除并仅存储在内存中
  • 每个容器管理一个进程或任务。

考虑到这一切,您首先写下 krb5-sidecar 的 Dockerfile。命令:[“/bin/sh”] 参数:[“-c”,“/usr/bin/sleep 3600000”]

    FROM centos:centos7
    
    # install the kerberos client tools
    RUN yum install -y krb5-workstation && \
     mkdir /krb5 && chmod 755 /krb5
    
    # add resources, the kinit script and the default krb5 configuration
    ADD entrypoint.sh /entrypoint.sh
    RUN chmod +x /krb-sidecar-entrypoint.sh
    # Little trick here that will allow my container to remove 
    # the vault secrets without root
    RUN chmod u+s /usr/bin/rm
    ENTRYPOINT ["/entrypoint.sh"]
Run Code Online (Sandbox Code Playgroud)

这是管理的入口点脚本

  1. Keytab 加载到 KCM 内存中
  2. /vault/secrets从密钥表的共享文件中删除
  3. 根据您的 Krb5 政策更新 kerberos 票证

# Default value for renewing the TGT ticket
KERBEROS_RENEWAL_TIME=86400 # One day

# Move the keytab into keytabfile
echo  "Generating keytab file"
cat /vault/secrets/${USERNAME}.keytab  | cut -d' ' -f2 | base64 -d > /etc/${USERNAME}.keytab

# Get the TGT
echo "Loading keytab"
kinit -kt /etc/${USERNAME}.keytab ${USERNAME}@${REALM}

# Remove secrets for security reasons
rm -rf /vault/secrets/*
rm -rf /etc/${USERNAME}.keytab
echo "Secrets removed from tmpfs"
while :;
do
  kinit -R
  sleep ${KERBEROS_RENEWAL_TIME}
done
Run Code Online (Sandbox Code Playgroud)

当然,您需要为部署创建 PersistentVolumes 和 PersistentVolumeClaims。

持续量

apiVersion: v1
kind: PersistentVolume
metadata:
  name: NFS-vol
spec:
  volumeMode: Filesystem
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Recycle
  storageClassName: slow
  mountOptions:
    - sec=krb5
  nfs:
    path: /exports
    server: nfs.server.test
Run Code Online (Sandbox Code Playgroud)

持久体积声明:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nfsvol
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 3Gi
Run Code Online (Sandbox Code Playgroud)

最后是部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: deployment-user
spec:
  selector:
    matchLabels:
      test: test
  template:
    metadata:
      labels:
        test: test
      annotations:
        vault.hashicorp.com/agent-inject: 'true'
        vault.hashicorp.com/agent-inject-secret-userKeytab: 'user/keytabs/user'
        vault.hashicorp.com/role: 'nfs'
        vault.hashicorp.com/ca-cert: 'certs/ca.crt'
        vault.hashicorp.com/tls-secret: 'tls-ca'
        vault.hashicorp.com/agent-pre-populate-only: "true"
    spec:
      securityContext:
        # Here we defined the user uid, this user must be present in the NFS server
        runAsUser: 2500
        runAsGroup: 2500
      # This may be needed or not depending on your DNS setup
      hostAliases:
        - ip: "192.168.111.130"
          hostnames:
            - "IPA"
            - "IPA.server"
        - ip: "192.168.111.131"
          hostnames:
            - "nfs"
            - "nfs.serer"
      restartPolicy: Always
      volumes:
      - name: nfs-user
        persistentVolumeClaim:
          claimName: nfs-vol
      - name: krb5
        configMap:
          name: keos-kerberos-config
      - name: kcmsocket
        hostPath:
          path: /var/run/.heim_org.h5l.kcm-socket
          type: File
      containers:
      - name: krb5-sidecar
        image: krb5-sidecar:0.1.0
        env:
        - name: KRB5CCNAME
          value: "KCM:"
        - name: USERNAME
          value: user
        - name: REALM
          value: server
        volumeMounts:
        - name: krb5
          mountPath: "/etc/krb5.conf"
          subPath: "krb5.conf"
        - name: kcmsocket
          mountPath: "/var/run/.heim_org.h5l.kcm-socket"
        lifecycle:
          preStop:
            exec:
              command: ["/usr/bin/kdestroy"]
      - name: mount-nfs-container
        image: nfs-centos:0.2.0
        env:
        - name: KRB5CCNAME
          value: "KCM:"
        volumeMounts:
        - name: nfs-user
          mountPath: "/nfs"
        - name: krb5
          mountPath: "/etc/krb5.conf"
          subPath: "krb5.conf"
        - name: kcmsocket
          mountPath: "/var/run/.heim_org.h5l.kcm-socket"

Run Code Online (Sandbox Code Playgroud)