Deploying a docker registry on Kubernetes

Deploying a docker registry on Kubernetes

This document will detail the procedure to deploy a private docker registry as a pod on Kubernetes. Having your own private repository for docker images gives absolute control over storage options, tightening access control and implementing authentication practices, all customized per your needs.

A Kubernetes docker registry is a docker registry running as a Kubernetes pod. Like any storage service, there will be a volume attached to the pod that stores your private docker images, and you can set up access controls for the registry via the access controls of the Kubernetes pod.

Good examples of a docker registry are the public Dockerhub, there is also Amazon Elastic Container RegistryAzure Container Registry, and Artifact Registry from Google. There are also tools like Sonatype’s Nexus, JFrog’s Artifactory, and VMware Harbor that are popularly used tools for systems that prefer staying cloud agnostic.

Namespace

We will deploy everything in a namespace to make management easier and cleaner.

Security

We will start with authentication to have basic security.

htpasswd

We will generate the user / password for authentication. For this, we will use htpasswd to generate a file that will be used to access the registry. For this demonstration, we will use myuser as the username and mypasswd as the password.

Generic secret

Here is the generic secret created in the cluster

Back to the top ↑

Persistent Storage

Now that we have basic security done, we need to create storage for our registry. For this, we will use Persistent Volume and Claim.

At Leaseweb, we will use the default storage class cloudstack-custom. We will not need to specify it during deployment, as this is the default and only storage class on the new cluster. This might differ if you are using S3 or another storage class and would need to be specified.

Click here to read more about PV and PVC. 

For this, we will need to first create a manifest and then deploy it on the cluster.

Manifest

In a file called “registry-pvc.yml”, we will define a PersistentVolumeClaim. You can change the storage size to your needs. We do not need to specify the StorageClassName as the CloudStack CSI is defined as the default on all deployed clusters at Leaseweb.

Deployment

We then apply this to our cluster

Now, we have a PVC to use the volume resources from. Let us start using this volume.

Back to the top ↑

Registry

We will use Helm to deploy the registry. Helm is the package manager for Kubernetes, focused on automating the installation of all kinds of Kubernetes applications. We will add the repo, then create a values.yml to configure it and then deploy the registry.

Adding 

First, let’s add the repo twuni/docker-registry, which is the successor of the original stable docker registry helm chart, and then update the local cache to make sure we have the latest charts.

pod.yml

We can take a look and see the version we will install.

Configuring

We will now create the values.yml that will be used to deploy the registry. Again, here we do not need to specify the storageClass as it is defaulted to the cloudstack csi

values.yml

Back to the top ↑

Deployment

We then deploy to our cluster:

Usage

Now that everything has been deployed, we will go into how to use this.

For this, we will use a simple nginx image to pull and push to our new private Docker registry.

Login

We will log in to our new registry:

We pull an image, for example, nginx:latest

We will now tag this with the private docker registry domain name and a custom tag

And finally, we push on the private docker registry our newly tag image. 

Now we have an image in our private docker registry hosted on our Kubernetes cluster. Let’s use this in a deployment.

Back to the top ↑

Using Docker Registry To Pull Images In Your Kubernetes Cluster

As you have a Docker registry deployed in your Kubernetes cluster, you can start using it by pulling previously pushed images for your Kubernetes Pods.

To learn how a private Docker registry can be used for pulling images, you will create a simple Kubernetes pod in a newly created test namespace. This Kubernetes Pod will use the previously pushed image registry.example.org/my-nginx.

Namespace

First, you have to create a test Kubernetes namespace

Secrets

Kubernetes Secret 

We will reuse the username and password that were created earlier in this demonstration to create a Kubernetes secret that will be used by the deployment to access the registry.

Be mindful of the namespace where you deploy the secret, as this might break your deployment.

We create the secret in the test namespace that we created a minute ago.

Back to the top ↑

Deployment

We will now deploy a pod to test all of this.

Manifest

We will create a pod manifest name test-nginx.yml

test-nginx.yml

Deploy

We then apply our manifest

We look at the deployment

Hopefully, this article gave you a good overview of how to set up a private Docker registry in your Kubernetes cluster. Keep in mind that having a Private Docker registry is essential if deploying Docker services that are not Open Source or need more security.

Back to the top ↑