leaseweb knowledge base logo - white color on the orange background

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 need.

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 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 clean.

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

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 new cluster. This might differ if you are using S3 or other storage class and would need to be specified.

A Persistent Volume (PV) is a piece of volume and a cluster resource that you provision just like the nodes of the cluster. 

A Persistent Volume Claim (PVC) is a request for storage by a user in the cluster. Just like a Pod consumes node resources, PVCs request and consume PV resources. 

– From Kubernetes documentation

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 need. We do not need to specify the StorageClassName as the cloudstack csi is define as default on all deployed cluster 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.

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

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 in our new private docker registry.

Login

We will log on 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.

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 

A Secret is an object that contains a small amount of sensitive data such as a password, a token, or a key. Such information might otherwise be put in a Pod specification or in a container image. Using a Secret means that you don’t need to include confidential data in your application code. Because Secrets can be created independently of the Pods that use them, there is less risk of the Secret (and its data) being exposed during the workflow of creating, viewing, and editing Pods. Kubernetes, and applications that run in your cluster, can also take additional precautions with Secrets, such as avoiding writing sensitive data to nonvolatile storage. So we will now create a TLS type of secret and a Generic type of secret to mount our certificate and password, respectively.

– Excerpt from the official Kubernetes documentation

We will reuse the username and password that was 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 create a minute ago.

Deployment

We now will 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.