New to KubeDB? Please start here.

Using private Docker registry

KubeDB operator supports using private Docker registry. This tutorial will show you how to use KubeDB to run MongoDB database using private Docker images.

Before You Begin

  • Read concept of MongoDB Version Catalog to learn detail concepts of MongoDBVersion object.

  • you need to have a Kubernetes cluster, and the kubectl command-line tool must be configured to communicate with your cluster. If you do not already have a cluster, you can create one by using Minikube.

  • You will also need a docker private registry or private repository. In this tutorial we will use private repository of docker hub.

  • You have to push the required images from KubeDB’s Docker hub account into your private registry. For mongodb, push DB_IMAGE, TOOLS_IMAGE, EXPORTER_IMAGE of following MongoDBVersions, where deprecated is not true, to your private registry.

  $ kubectl get mongodbversions -n kube-system  -o=custom-columns=NAME:.metadata.name,VERSION:.spec.version,DB_IMAGE:.spec.db.image,TOOLS_IMAGE:.spec.tools.image,EXPORTER_IMAGE:.spec.exporter.image,DEPRECATED:.spec.deprecated
  NAME      VERSION   DB_IMAGE              TOOLS_IMAGE                 EXPORTER_IMAGE                   DEPRECATED
  3.4       3.4       kubedb/mongo:3.4      kubedb/mongo-tools:3.4      kubedb/operator:0.8.0            true
  3.4-v1    3.4       kubedb/mongo:3.4-v1   kubedb/mongo-tools:3.4-v1   kubedb/mongodb_exporter:v1.0.0   <none>
  3.6       3.6       kubedb/mongo:3.6      kubedb/mongo-tools:3.6      kubedb/operator:0.8.0            true
  3.6-v1    3.6       kubedb/mongo:3.6-v1   kubedb/mongo-tools:3.6-v1   kubedb/mongodb_exporter:v1.0.0   <none>

Docker hub repositories:

  apiVersion: catalog.kubedb.com/v1alpha1
  kind: MongoDBVersion
  metadata:
    name: "3.4-v1"
    labels:
      app: kubedb
  spec:
    version: "3.4"
    db:
      image: "PRIVATE_DOCKER_REGISTRY/mongo:3.4-v1"
    exporter:
      image: "PRIVATE_DOCKER_REGISTRY/mongodb_exporter:v1.0.0"
    tools:
      image: "PRIVATE_DOCKER_REGISTRY/mongo-tools:3.4-v1"
  

Create ImagePullSecret

ImagePullSecrets is a type of a Kubernete Secret whose sole purpose is to pull private images from a Docker registry. It allows you to specify the url of the docker registry, credentials for logging in and the image name of your private docker image.

Run the following command, substituting the appropriate uppercase values to create an image pull secret for your private Docker registry:

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

If you wish to follow other ways to pull private images see official docs of kubernetes.

NB: If you are using kubectl 1.9.0, update to 1.9.1 or later to avoid this issue.

Install KubeDB operator

When installing KubeDB operator, set the flags --docker-registry and --image-pull-secret to appropriate value. Follow the steps to install KubeDB operator properly in cluster so that to points to the DOCKER_REGISTRY you wish to pull images from.

Create Demo namespace

To keep things isolated, this tutorial uses a separate namespace called demo throughout this tutorial. Run the following command to prepare your cluster for this tutorial:

$ kubectl create ns demo
namespace "demo" created

$ kubectl get ns
NAME          STATUS    AGE
demo          Active    10s

Deploy MongoDB database from Private Registry

While deploying MongoDB from private repository, you have to add myregistrykey secret in MongoDB spec.imagePullSecrets. Below is the MongoDB CRD object we will create.

apiVersion: kubedb.com/v1alpha1
kind: MongoDB
metadata:
  name: mgo-pvt-reg
  namespace: demo
spec:
  version: "3.4-v1"
  storage:
    storageClassName: "standard"
    accessModes:
    - ReadWriteOnce
    resources:
      requests:
        storage: 50Mi
  podTemplate:
    spec:
      imagePullSecrets:
      - name: myregistrykey

Now run the command to deploy this MongoDB object:

$ kubedb create -f https://raw.githubusercontent.com/kubedb/cli/0.9.0-rc.1/docs/examples/mongodb/private-registry/demo-2.yaml
mongodb.kubedb.com/mgo-pvt-reg created

To check if the images pulled successfully from the repository, see if the MongoDB is in running state:

$ kubectl get pods -n demo -w
NAME            READY     STATUS              RESTARTS   AGE
mgo-pvt-reg-0   0/1       Pending             0          0s
mgo-pvt-reg-0   0/1       Pending             0          0s
mgo-pvt-reg-0   0/1       ContainerCreating   0          0s
mgo-pvt-reg-0   1/1       Running             0          5m


$ kubedb get mg -n demo
NAME          STATUS    AGE
mgo-pvt-reg   Running   1m

Snapshot

You can specify imagePullSecret for Snapshot objects in spec.podTemplate.spec.imagePullSecrets field of Snapshot object. If you are using scheduled backup, you can also provide imagePullSecret in backupSchedule.podTemplate.spec.imagePullSecrets field of MongoDB crd. KubeDB also reuses imagePullSecret for Snapshot object from spec.podTemplate.spec.imagePullSecrets field of MongoDB crd.

Cleaning up

To cleanup the Kubernetes resources created by this tutorial, run:

kubectl patch -n demo mg/mgo-pvt-reg -p '{"spec":{"terminationPolicy":"WipeOut"}}' --type="merge"
kubectl delete -n demo mg/mgo-pvt-reg

kubectl patch -n demo drmn/mgo-pvt-reg -p '{"spec":{"wipeOut":true}}' --type="merge"
kubectl delete -n demo drmn/mgo-pvt-reg

kubectl delete ns demo

Next Steps

Subscribe to our free technical newsletter!

Join thousands of subscribers and stay up-to-date on AppsCode.