How to Use Vultr Container Registry with Kubernetes
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
- Deploy a Vultr Container Registry.
- Deploy a Vultr Kubernetes Engine (VKE).
- Deploy an instance with Ubuntu to use as the management workstation.
- Access the server using SSH.
- Install and configure Kubectl to access the VKE cluster.
Generate the Vultr Container Registry Kubernetes Credentials
Open the Vultr Customer Portal.
Click Products and select Container Registry on the left navigation bar.
Click your Vultr Container Registry to open the registry management panel.
Navigate to the Docker/Kubernetes tab and find the Docker Credentials for Kubernetes section.
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.
Click Generate Kubernetes YAML to create a new Secret YAML configuration.
Select all generated YAML configurations and copy them to your clipboard.
Switch to your server SSH session to apply the contents in a new file.
Create a new Secret resource file
vcr-secret.yaml
using a text editor such as Nano.console$ nano vcr-secret.yaml
Add all generated Vultr Container Registry YAML configuration contents to the file.
yamlapiVersion: 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.Deploy the resource to your VKE cluster.
console$ kubectl apply -f secret.yaml
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.
Pull the Nginx container image from DockerHub.
console$ docker pull nginx
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
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
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
Push the container image to your Vultr Container Registry.
console$ docker push sjc.vultrcr.com/examplereg/nginx
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
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
Create a new deployment resource file
deployment.yaml
.console$ nano deployment.yaml
Add the following contents to the file. Replace
sjc.vultrcr.com/exampleregistry/nginx
with your actual Vultr Container Registry repository URL.yamlapiVersion: 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 thevultr-cr-credentials
Secret resource for deployment to thenginx-app
pod containers.Deploy the resource to your VKE cluster.
console$ kubectl apply -f deployment.yaml
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
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.
Create a new Service resource file
service.yaml
.console$ nano service.yaml
Add the following contents to the file.
yamlapiVersion: 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.Apply the resource to your VKE cluster.
console$ kubectl apply -f service.yaml
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 yournginx-app
application on the target port80
.Visit your external resource IP address using a web browser such as Chrome.
http://192.0.2.100
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.