使用声明性命令创建Kubernetes Pod时获取ErrImagePull:401未经授权

问题描述

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

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

我可以使用命令通过以下命令使用图像来创建Pod:

kubectl create -f hello-world-create.yaml

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

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

kubectl apply -f hello-world-apply.yaml

yaml文件的外观

apiVersion: apps/v1
kind: Deployment
Metadata:
  generation: 1
  labels:
    run: hello-world
  name: hello-world
spec:
  replicas: 3
  selector:
    matchLabels:
      run: hello-world
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    Metadata:
      labels:
        run: hello-world
    spec:
      containers:
      - image: us.icr.io/earlyprogramimages/hello-world:1
        imagePullPolicy: Always
        name: hello-world
        ports:
        - containerPort: 80
          protocol: TCP
      imagePullSecrets:
      - name: icr
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      securityContext: {}
      terminationGracePeriodSeconds: 30

对于事件堆栈所在的每个Pod,我的状态为ErrImagePull

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

很明显,该命令没有读取图像的权限,但是我已经成功使用

登录
ibmcloud cr login

,并且可以使用命令性create命令部署pod。

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

运行

kubectl get secrets -n default | grep "icr-io"

给予

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

解决方法

请查看https://cloud.ibm.com/docs/containers?topic=containers-registry#cluster_registry_auth,以获取有关可能出问题的详细信息。要检查的一些事情:

  1. 您是否已制定允许您访问容器注册表的IAM策略?
  2. kubectl get secrets -n default | grep "icr-io"是否显示任何拉式机密?如果没有,请按照上面的文档链接进行修复。
,

这就是我的工作和预期的工作,

您会看到all-icr-io是群集中提供的默认映像拉密钥。 不确定您为什么使用icr

默认情况下,IBM Cloud Kubernetes集群设置为提取映像 仅从您帐户在IBM Cloud Container Registry中的名称空间访问 在默认名称空间中使用机密all-icr-io

选中documentation here,将现有图像提取密钥复制到非默认名称空间

所以,我的hello-world-create看起来像这样

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

我的hello-world-apply.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  generation: 1
  labels:
    run: hello-world
  name: hello-world
spec:
  replicas: 3
  selector:
    matchLabels:
      run: hello-world
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      labels:
        run: hello-world
    spec:
      containers:
      - image: us.icr.io/mods15/hello-world:1
        imagePullPolicy: Always
        name: hello-world
        ports:
        - containerPort: 80
          protocol: TCP
      imagePullSecrets:
      - name: all-icr-io
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      securityContext: {}
      terminationGracePeriodSeconds: 30

成功配置yaml文件后,结果如下 enter image description here