使用声明性命令创建 Kubernetes Pod 时出现 ErrImagePull:401 Unauthorized

chu*_*hts 4 kubernetes ibm-cloud

我正在开展一个实验室,展示如何在 IBM Cloud 上设置 Kubernetes 和 CLI。

我有 Kubernetes 集群设置和容器注册表。我在 CLI 上登录到 IBM Cloud 和 Container Registry。镜像已创建并推送。

我可以使用带有命令式命令的图像创建一个 pod:

kubectl create -f hello-world-create.yaml
Run Code Online (Sandbox Code Playgroud)

文件yaml如下所示:

kubectl create -f hello-world-create.yaml
Run Code Online (Sandbox Code Playgroud)

但是当我尝试对运行的同一图像使用声明性命令时

kubectl apply -f hello-world-apply.yaml
Run Code Online (Sandbox Code Playgroud)

文件yaml的样子

apiVersion: v1
kind: Pod
metadata:
  name: hello-world
spec:
  containers:
  - name: hello-world
    image: us.icr.io/earlyprogramimages/hello-world:1
    ports:
    - containerPort: 80
  imagePullSecrets:
  - name: icr

Run Code Online (Sandbox Code Playgroud)

ErrImagePull我获取事件堆栈所在的每个 Pod的状态

Successfully assigned default/hello-world-6fd8bd67dc-79gbz to xx.xx.xx.xx
Pulling image "us.icr.io/earlyprogramimages/hello-world:1

Failed to pull image "us.icr.io/earlyprogramimages/hello-world:1": rpc error: code = Unknown desc = failed to pull and unpack image "us.icr.io/earlyprogramimages/hello-world:1": failed to resolve reference "us.icr.io/earlyprogramimages/hello-world:1": failed to authorize: failed to fetch anonymous token: unexpected status: 401 Unauthorized

Error: ErrImagePull
Run Code Online (Sandbox Code Playgroud)

显然该命令没有对图像的读取访问权限,但我已使用以下命令成功登录

ibmcloud cr login
Run Code Online (Sandbox Code Playgroud)

并可以使用命令式 create 命令部署 pod。

我已经阅读了文档,但无法确定我忽略了哪一步。为声明性应用命令授予适当的访问权限需要哪些额外步骤?

跑步

kubectl get secrets -n default | grep "icr-io"
Run Code Online (Sandbox Code Playgroud)

给出

kubectl get secrets -n default | grep "icr-io"
all-icr-io            kubernetes.io/dockerconfigjson        1      167m
default-au-icr-io     kubernetes.io/dockerconfigjson        1      167m
default-de-icr-io     kubernetes.io/dockerconfigjson        1      167m
default-icr-io        kubernetes.io/dockerconfigjson        1      167m
default-jp-icr-io     kubernetes.io/dockerconfigjson        1      167m
default-uk-icr-io     kubernetes.io/dockerconfigjson        1      167m
default-us-icr-io     kubernetes.io/dockerconfigjson        1      167m
Run Code Online (Sandbox Code Playgroud)

Vid*_*lli 6

这就是我所做的并按预期工作,

\n

如您所见,all-icr-io这是集群中提供的默认镜像拉取密钥。不确定你为什么使用icr

\n
\n

默认情况下,IBM Cloud Kubernetes 集群设置为通过使用all-icr-io默认名称空间中的密钥,\n仅从 IBM Cloud Container Registry 中的帐户\xe2\x80\x99s 名称空间提取映像。

\n
\n

检查此处的文档,将现有的镜像拉取机密复制到非默认命名空间

\n

所以,我的hello-world-create样子是这样的

\n
apiVersion: v1\nkind: Pod\nmetadata:\n  name: hello-world\nspec:\n  containers:\n  - name: hello-world\n    image: us.icr.io/mods15/hello-world:1\n    ports:\n    - containerPort: 80\n  imagePullSecrets:\n  - name: all-icr-io\n
Run Code Online (Sandbox Code Playgroud)\n

我的hello-world-apply.yaml

\n
apiVersion: apps/v1\nkind: Deployment\nmetadata:\n  generation: 1\n  labels:\n    run: hello-world\n  name: hello-world\nspec:\n  replicas: 3\n  selector:\n    matchLabels:\n      run: hello-world\n  strategy:\n    rollingUpdate:\n      maxSurge: 1\n      maxUnavailable: 1\n    type: RollingUpdate\n  template:\n    metadata:\n      labels:\n        run: hello-world\n    spec:\n      containers:\n      - image: us.icr.io/mods15/hello-world:1\n        imagePullPolicy: Always\n        name: hello-world\n        ports:\n        - containerPort: 80\n          protocol: TCP\n      imagePullSecrets:\n      - name: all-icr-io\n      dnsPolicy: ClusterFirst\n      restartPolicy: Always\n      securityContext: {}\n      terminationGracePeriodSeconds: 30\n
Run Code Online (Sandbox Code Playgroud)\n

这是yaml文件配置成功后的结果\n在此输入图像描述

\n

  • @YacineLazaar 该工具是 https://github.com/kubernetes-sigs/kui (3认同)