Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access private Docker Hub repository from Kubernetes on Vagrant

I am failing to pull from my private Docker Hub repository into my local Kubernetes setup running on Vagrant:

Container "hellonode" in pod "hellonode-n1hox" is waiting to start: image can't be pulled

Failed to pull image "username/hellonode": Error: image username/hellonode:latest not found

I have set up Kubernetes locally via Vagrant as described here and created a secret named "dockerhub" with kubectl create secret docker-registry dockerhub --docker-server=https://registry.hub.docker.com/ --docker-username=username --docker-password=... --docker-email=... which I supplied as the image pull secret.

I am running Kubernetes 1.2.0.

like image 424
André Avatar asked Mar 26 '16 07:03

André


People also ask

How do I connect Docker Hub to Kubernetes?

Select Kubernetes from the left sidebar. Next to Enable Kubernetes, select the checkbox. Select Apply & Restart to save the settings and then click Install to confirm. This instantiates images required to run the Kubernetes server as containers, and installs the /usr/local/bin/kubectl command on your machine.

Is Docker Hub private repository?

You get one private repository for free with your Docker Hub user account (not usable for organizations you're a member of). If you need more private repositories for your user account, upgrade your Docker Hub plan from your Billing Information page.

Can Kubernetes Pull my Docker images?

If your Docker images are in a public repository such as DockerHub, Kubernetes can pull them right away. In most cases however your images are in a private Docker registry and Kubernetes must be given explicit access to it.

How do I access a docker registry from a Kubernetes cluster?

Accessing a Docker registry from your Kubernetes cluster. Kubernetes deployments are based on a “pull” approach. When you deploy your application to a Kubernetes cluster you don’t upload the application itself (which usually happens with traditional deployments). Instead, Kubernetes will pull the Docker images to its nodes on its own.

How to create a private repository on dockerhub?

To work with a private repository on DockerHub, you need to add one by using the Add Repository Procedure (Steps 1 - 3 from Create a public image repository on DockerHub ). Then select Private at Step 3. If you want to use the Node Example from this tutorial, download it and cd into it, else cd into your work directory:

How to pull a private image from a Kubernetes cluster?

A Kubernetes cluster uses the Secret of kubernetes.io/dockerconfigjson type to authenticate with a container registry to pull a private image. If you already ran docker login, you can copy that credential into Kubernetes:


2 Answers

To pull a private DockerHub hosted image from a Kubernetes YAML:

Run these commands:

DOCKER_REGISTRY_SERVER=docker.io
DOCKER_USER=Type your dockerhub username, same as when you `docker login`
DOCKER_EMAIL=Type your dockerhub email, same as when you `docker login`
DOCKER_PASSWORD=Type your dockerhub pw, same as when you `docker login`

kubectl create secret docker-registry myregistrykey \
  --docker-server=$DOCKER_REGISTRY_SERVER \
  --docker-username=$DOCKER_USER \
  --docker-password=$DOCKER_PASSWORD \
  --docker-email=$DOCKER_EMAIL

If your username on DockerHub is DOCKER_USER, and your private repo is called PRIVATE_REPO_NAME, and the image you want to pull is tagged as latest, create this example.yaml file:

apiVersion: v1
kind: Pod
metadata:
  name: whatever
spec:
  containers:
    - name: whatever
      image: DOCKER_USER/PRIVATE_REPO_NAME:latest
      imagePullPolicy: Always
      command: [ "echo", "SUCCESS" ]
  imagePullSecrets:
    - name: myregistrykey

Then run:

kubectl create -f example.yaml
like image 64
Rich Kuzsma Avatar answered Oct 19 '22 01:10

Rich Kuzsma


Create k8 Secret:

apiVersion: v1
kind: Secret
metadata:
  name: repositorySecretKey
data:
  .dockerconfigjson: <base64 encoded docker auth config>
type: kubernetes.io/dockerconfigjson

Then in pod or rc config mention the secret. Example :

apiVersion: v1
kind: Pod
metadata:
  name: test-pod
spec:
  containers:
    - name: test-pod
      image: quay.io/example/hello:1.1
  imagePullSecrets:
    - name: repositorySecretKey

Docker auth config

{
   "https://quay.io": {
    "email": ".",
    "auth": "<base64 encoded auth token>"
  }
}

Or

kubectl create secret docker-registry myregistrykey \
    --docker-server=DOCKER_REGISTRY_SERVER \
    --docker-username=DOCKER_USER \
    --docker-password=DOCKER_PASSWORD \
    --docker-email=DOCKER_EMAIL
like image 9
Phagun Baya Avatar answered Oct 19 '22 01:10

Phagun Baya