By containerizing an application, you bundle your application together with all the dependencies necessary to run your application. This allows you to easily host your application on any platform that supports containers.
Once you have containerized your application and hosted it publicly or on a private container image registry, you will want to be able to use it, for example on a Kubernetes platform. In this tutorial, we will show you how to do this using a Kubernetes Deployment based on a .yaml object.
If you have previously created a Kubernetes Deployment via a .yaml object, deploying your own container image from your own repository will bring few surprises.
Step 1
If your container image is on Docker Hub, proceed to step 2.
If you host your container image on a private registry, you will need a 'secret', provided you haven't already created one when creating that private registry. If you already did so, also proceed to step 2.
A secret stores sensitive information such as login credentials used by Pods to access services, such as a database. For this guide, you will create a secret to store the login credentials for your private container registry. There are multiple ways to create a secret, but perhaps the easiest is with Kubectl.
Execute the following command on the computer where you manage your Kubernetes cluster and replace the following information (note the scroll bar):
- mykey: A name for your secret. It's useful to use a name that indicates what the secret is for, such as my_harbor_server.
- PRIVATE_SERVER: The hostname/URL of your container image server.
- USER: A user with access rights to your container image server.
- PASSWORD: The password for this user.
- mail@example.com: The email address of the user.
kubectl create secret docker-registry mykey --docker-server=PRIVATE_SERVER --docker-username=USER --docker-password=PASSWORD --docker-email=mail@example.com
You can find more information about secrets here and more information about creating and managing secrets using Kubectl here.
Step 2
Create a .yaml file for your Deployment on the computer from which you manage your Kubernetes cluster, for example:
nano my-website.yaml
Step 3
How the contents of the file should look depends on whether you are hosting the container on your own image registry or publicly on Docker Hub:
Private registry:
Give the file the following content, adjusting the following values:
- my-website: Replace with the name of your application as you pushed it to your own registry in the previous paragraph.
- replicas: Change to the number of pods you want to create.
- image: Change this to the web address where your image is located, see for example our Harbor guide.
- imagePullSecrets: Change to the secret you created in step 1 to access the image on your registry.
- For more options for customizing the deployment below, see our tutorial on creating your own Kubernetes .yaml objects.
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-website
spec:
replicas: 1
selector:
matchLabels:
app: my-website
template:
metadata:
labels:
app: my-website
spec:
containers:
- name: my-website
image: yourregistrysaddress.com/my-website:latest
ports:
- containerPort: 80
imagePullSecrets:
- name: mykey
Public registry:
Give the file the following content, adjusting the following values:
- my-website: Replace with the name of your application as you pushed it to Docker Hub.
- replicas: Change to the number of pods you want to create.
- image: Change the value to the name of your container image.
- For more options for customizing the deployment below, see our tutorial on creating your own Kubernetes .yaml objects.
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-website
spec:
replicas: 1
selector:
matchLabels:
app: my-website
template:
metadata:
labels:
app: my-website
spec:
containers:
- name: my-website
image: my-website:latest
ports:
- containerPort: 80
Step 4
Create the deployment with the command:
kubectl apply -f my-website.yaml
Replace my-website.yaml here with the name of the file you created in step 1.
Congratulations! Your containerized application is now hosted on your Kubernetes cluster. Please note that for exposing your application to the outside world, you will need a loadbalancer service, see this guide.