添加ImagePullSecrets到ServiceAccount

如果镜像仓库需要认证,您需要将对应的 ImagePullSecrets 添加到应用使用的 ServiceAccount 中。这样可以确保应用能够成功从私有仓库拉取镜像。

创建 ImagePullSecret

要创建 ImagePullSecret,请参考创建 Secret了解创建 ImagePullSecret 的详细步骤。

将 ImagePullSecret 添加到 ServiceAccount

如果您的应用 Pod 使用的是 ServiceAccount example,您可以将 ImagePullSecret 添加到应用所在命名空间中的 example ServiceAccount

使用 patch 命令编辑 ServiceAccount example

kubectl patch serviceaccount example -p '{"imagePullSecrets": [{"name": "my-registry-creds"}]}' -n <namespace>

<namespace> 替换为应用所在的命名空间,将 my-registry-creds 替换为您创建的 ImagePullSecret 的名称。

您可以通过描述 ServiceAccount 来验证 ImagePullSecret 是否添加成功:

kubectl describe serviceaccount example -n <namespace>
Name:                example
Namespace:           <namespace>
Labels:              <none>
Annotations:         <none>
Image pull secrets:  my-registry-creds
Mountable secrets:   <none>
Tokens:              <none>
Events:              <none>

您应该能看到 Image pull secrets 一栏显示了已添加的 secret。

NOTE

注意:如果您的 Pod 没有指定 ServiceAccount,默认会使用命名空间中的 default ServiceAccount。您可以用同样的方式将 ImagePullSecret 添加到 default ServiceAccount

验证新建 Pod 是否设置了 imagePullSecrets

当您创建一个使用 ServiceAccount example 的新 Pod 时,该 Pod 会自动使用 ServiceAccount 中指定的 ImagePullSecrets

您可以通过运行以下命令进行验证:

kubectl get pod <pod-name> -n <namespace> -o=jsonpath='{.spec.imagePullSecrets}'