How to Use Vultr Container Registry with Kubernetes

Updated on April 24, 2024

Header Image

Introduction

Kubernetes is an open-source container orchestration platform that automates the deployment, scaling, and management of containerized applications. It allows developers to focus on the development and deployment of applications by abstracting away all underlying infrastructure management tasks.

This article explains how to use the Vultr Container Registry with Kubernetes and deploy containerized applications.

Prerequisites

Generate the Vultr Container Registry Kubernetes Credentials

  1. Open the Vultr Customer Portal.

  2. Click Products and select Container Registry on the left navigation bar.

    Manage a Vultr Container Registry

  3. Click your Vultr Container Registry to open the registry management panel.

  4. Navigate to the Docker/Kubernetes tab and find the Docker Credentials for Kubernetes section.

  5. Enter your desired credential expiry time in the Expires (in seconds) field and verify the Push Access option to enable or disable push privileges from your Kubernetes cluster.

  6. Click Generate Kubernetes YAML to create a new Secret YAML configuration.

    Generate new Kubernetes YAML Secret Registry Configurations

  7. Select all generated YAML configurations and copy them to your clipboard.

  8. Switch to your server SSH session to apply the contents in a new file.

  9. Create a new Secret resource file vcr-secret.yaml using a text editor such as Nano.

    console
    $ nano vcr-secret.yaml
    
  10. Add all generated Vultr Container Registry YAML configuration contents to the file.

    yaml
    apiVersion: v1
    kind: Secret
    metadata:
      name: vultr-cr-credentials
    data:
      .dockerconfigjson: example-config-values
    type: kubernetes.io/dockerconfigjson
    

    Save and close the file.

    The above configuration creates a new Secret resource vultr-cr-credentials that authenticates with your Vultr Container Registry to interact with referenced container images in your Kubernetes cluster.

  11. Deploy the resource to your VKE cluster.

    console
    $ kubectl apply -f secret.yaml
    
  12. View your cluster Secrets and verify that the new resource is available.

    console
    $ kubectl get secrets
    

    Output:

    NAME                   TYPE                             DATA   AGE
    vultr-cr-credentials   kubernetes.io/dockerconfigjson   1      7s

    You have created and deployed a Vultr Container Registry authentication configuration to your cluster. All cluster references to the Vultr Container Registry authenticate using the secret resource to pull container repositories from your registry. Activate the Push Access option when generating a new YAML configuration to enable push privileges from your cluster to the registry.

Build and Push a Container Image to the Vultr Container Registry

Build a new container image to verify that your VKE cluster can pull and deploy applications from your Vultr Container Registry using the cluster Secret resource. Follow the steps below to clone an existing Nginx container image from DockerHub and publish it to the Vultr Container Registry for deployment in your VKE cluster.

  1. Pull the Nginx container image from DockerHub.

    console
    $ docker pull nginx
    
  2. Log in to the Vultr Container Registry using Docker CLI. Replace examplereg, exampleuser, registrypassword with your actual Vultr Container Registry credentials.

    console
    $ docker login https://sjc.vultrcr.com/example -u exampleuser -p registrypassword
    

    Output:

    Login Succeeded
  3. Tag the Nginx container image with your Vultr Container Registry repository tag. For example, sjc.vultrcr.com/examplereg/nginx

    console
    $ docker tag nginx sjc.vultrcr.com/examplereg/nginx
    
  4. View all Docker images on the server and verify that the tagged image is available.

    console
    $ docker images
    

    Output:

    REPOSITORY                                TAG       IMAGE ID       CREATED          SIZE
    nginx                                     latest    c613f16b6642   2 months ago     187MB
    sjc.vultrcr.com/examplereg/nginx             latest    c613f16b6642   2 months ago     187MB
  5. Push the container image to your Vultr Container Registry.

    console
    $ docker push sjc.vultrcr.com/examplereg/nginx
    
  6. Run the container image on the host port 80 to verify that it works correctly.

    console
    $ docker run -d -p 80:80 sjc.vultrcr.com/examplereg/nginx
    
  7. Test access to the host port 80 using the Curl utility and verify that your output includes the default Nginx web page contents.

    console
    $ curl 127.0.0.1:80
    

    Output:

    .....
    <body>
    <h1>Welcome to nginx!</h1>
    <p>If you see this page, the nginx web server is successfully installed and
    working. Further configuration is required.</p>
    
    .........
    
    <p><em>Thank you for using nginx.</em></p>
    </body>
    </html>

Create a Kubernetes Deployment

  1. Create a new deployment resource file deployment.yaml.

    console
    $ nano deployment.yaml
    
  2. Add the following contents to the file. Replace sjc.vultrcr.com/exampleregistry/nginx with your actual Vultr Container Registry repository URL.

    yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: nginx
    spec:
      replicas: 3
      selector:
        matchLabels:
          app: nginx-app
      template:
        metadata:
          labels:
            app: nginx-app
        spec:
          containers:
          - name: nginx-container
            image: sjc.vultrcr.com/examplereg/nginx
            ports:
            - containerPort: 80
          imagePullSecrets:
          - name: vultr-cr-credentials
    

    Save and close the file.

    The above YAML configuration creates a new deployment resource nginx using the application container image from your Vultr Container Registry and the vultr-cr-credentials Secret resource for deployment to the nginx-app pod containers.

  3. Deploy the resource to your VKE cluster.

    console
    $ kubectl apply -f deployment.yaml
    
  4. View the cluster deployments and verify that the new resource is available.

    console
    $ kubectl get deployments
    

    Output:

    NAME    READY   UP-TO-DATE   AVAILABLE   AGE
    nginx   3/3     3            3           16s
  5. View all cluster pods and verify that all nginx-app pods are running.

    console
    $ kubectl get pods
    

    Output:

    NAME                     READY   STATUS    RESTARTS   AGE
    nginx-6b95c688b5-4hxd9   1/1     Running   0          55s
    nginx-6b95c688b5-5mcfh   1/1     Running   0          55s
    nginx-6b95c688b5-xzddj   1/1     Running   0          55s

Test Access to the Kubernetes Cluster Application

The Kubernetes deployment creates all necessary pods and containers in your cluster with automatic scaling functionalities depending on the volume of application requests. However, the application is only accessible within the cluster by default. Follow the steps below to deploy a Service resource to securely expose the cluster application using an external Vultr Load Balancer IP address.

  1. Create a new Service resource file service.yaml.

    console
    $ nano service.yaml
    
  2. Add the following contents to the file.

    yaml
    apiVersion: v1
    kind: Service
    type: LoadBalancer
    metadata:
      name: nginx-service
    spec:
      selector:
        app: nginx-app
      ports:
        - protocol: TCP
          port: 80
          targetPort: 80
    

    Save and close the file.

    The above YAML configuration creates a new cluster Service resource that exposes all nginx-app resources for external access using a Vultr Load Balancer with a unique public IP address.

  3. Apply the resource to your VKE cluster.

    console
    $ kubectl apply -f service.yaml
    
  4. Wait for at least 3 minutes for the Vultr Load Balancer deployment process to complete, then view all cluster services to verify the assigned public IP address.

    console
    $ kubectl get services
    

    Output:

    NAME            TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)        AGE
    kubernetes      ClusterIP      10.96.0.1       <none>          443/TCP        31m
    nginx-service   LoadBalancer   10.104.27.208   192.0.2.100      80:30638/TCP   3m

    The LoadBalancer resource runs with the external IP address 192.0.2.100 based on the above output. Use the IP address to access your nginx-app application on the target port 80.

  5. Visit your external resource IP address using a web browser such as Chrome.

    http://192.0.2.100

    View the Default Web Page From a Vultr Container Registry Container

    Verify that the default Nginx web page displays in your browser session.

Conclusion

You have integrated your Vultr Container Registry to a Vultr Kubernetes Engine (VKE) cluster and deployed a sample Nginx container image from the available repositories. Kubernetes performs auto-scaling and secure node selection functionalities for your application container images allowing you to focus on building more applications with the Vultr Container Registry.