iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🚢

Pulling Images from Private gcr.io Registries in Kubernetes

に公開
2023/04/15

Greetings

I've been wanting to write something on the trending platform Zenn, but I found myself with nothing to write about.
However, after starting to study Kubernetes and getting stuck for a while, I finally figured out where I was going wrong, so I'm leaving this here as a note.

Lately, I've finally started studying Kubernetes.
I feel like I've understood the concepts. At least, I intended to. But actually, I don't understand it at all.

Introduction

Currently, I'm trying out various things while taking advantage of the Google Cloud free trial period. When pulling a container image hosted on Google Container Registry (gcr.io) into a local environment built with minikube, authentication is required if the repository is private.

Kubernetes naturally takes this into account and provides a mechanism that complies with the login authentication used by Docker registries.

However, for some reason, the authentication wasn't working correctly. Even when I specified the repository path normally, I would get a PullImageError.

Looking at the error logs, I saw a message telling me to check the authentication method (paraphrased) along with a link to a page guiding through authentication methods in GCR. I checked it and tried to follow the steps, but it still didn't work. 🤔

Prerequisites

  • I want to use an image hosted in GCP's Google Container Registry
  • Authentication is required because the container registry is private
  • Pushing/Pulling images to the registry using the docker command works without any issues

I was stuck like this for about two weeks, and even when searching for information, I couldn't find anything that could resolve this specific case... 😞

Manifest file

I described the deployment in the manifest file as follows:

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  name: app
  namespace: default
spec:
  selector:
    matchLabels:
      name: app
  replicas: 2
  template:
    spec:
      containers:
        - name: app-php
          image: gcr.io/<GCP_PROJECT_ID>/<IMAGE_NAME>:latest
          ports:
            - containerPort: 9000
          imagePullPolicy: IfNotPresent
      imagePullSecrets:
          - name: gcr-secret
    metadata:
      labels:
        name: app
      namespace: default

Conclusion

I referred to this article:

Using Google Container Registry with Kubernetes
https://blog.container-solutions.com/using-google-container-registry-with-kubernetes

I was able to clear this issue by pulling using an "Access Token" as described in that article.

kubectl create secret docker-registry gcr-access-token \
 --docker-server=gcr.io \
 --docker-username=oauth3accesstoken \
 --docker-password="$(gcloud auth print-access-token)" \
 --docker-email=your-email@example.com
kubectl patch serviceaccount default \
 -p '{"imagePullSecrets": [{"name": "gcr-json-key"}]}'

The second command, kubectl patch serviceaccount, was something I hadn't executed before, so it's possible that authentication was failing because I hadn't done this.

After performing the above, I ran kubectl apply -f ./deployment.yaml, and the status of all containers successfully changed to Running! 🎉

watching-on-k9s.gif

Also, while investigating this problem, I learned that authentication information for pulling from private container registries also uses secrets on Kubernetes, which has piqued my interest even more.
The app is still not running as it is, so it's only one step forward, but I will continue to study and use it while overcoming hurdles.

Discussion