In a Kubernetes cluster, sooner or later, you often create a Deployment. As part of a Deployment, one or more Pods (a group of one or more containers) are created. In practice, Kubernetes (YAML) objects are usually used for this, or a package manager like Helm.
In this tutorial, we'll show you how to create a Kubernetes Deployment using Kubernetes objects. For more information on Kubernetes objects and how to create them, check out our articles 'What are Kubernetes objects' and 'Creating Kubernetes (YAML) objects'.
For the steps in this tutorial, it is important to have Kubectl installed and to have downloaded your Kubeconfig file.
Creating a Deployment using a Kubernetes objectv
An Kubernetes object contains the specifications of the desired state of a Kubernetes cluster, such as the used Kubernetes API version, the container image, number of replicas, etc. In this guide, we use an example object from the official Kubernetes organization, which can be found at https://k8s.io/examples/application/deployment.yaml. You are free to use your own object for this.
Step 1
Create a Deployment with the command below. If you want to deploy a different object than this example, replace the address https://k8s.io/examples/application/deployment.yaml with the address where the desired object is hosted, or the location of the .yaml file on your computer.
kubectl apply -f https://k8s.io/examples/application/deployment.yaml
You will see the following confirmation:
deployment.apps/nginx-deployment created
Your deployment based on a .yaml object is now ready for usage. We will immediately go through some useful management commands that allow you to check the status of different aspects of your Deployment.
Checking the status of your Deployment
Check the status of your new Deployment immediately with the command:
kubectl get deployments
If you host multiple Deployments, you will see them all with this command. If this is your only Deployment, the output looks like this:
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 2/2 2 2 21s
Checking the status of your Pods
Next, check the status of your Pods:
kubectl get pods
The output looks like this. No matter how many Pods you have or which Deployments they belong to, you will see them all here. You can recognize your Deployment by the fact that the name of your Pod always starts with the name of your Deployment.
NAME READY STATUS RESTARTS AGE
nginx-deployment-9456bbbf9-m25sr 1/1 Running 0 52s
nginx-deployment-9456bbbf9-wwczr 1/1 Running 0 52s
There are many more commands available. An overview of some of the most useful for managing your cluster can be found in this tutorial.
Step 2
By default, the Pod is only accessible from the internal virtual network of your Kubernetes cluster and not via the public internet. To make your hello-node container accessible to the public internet, you 'expose' the Pod as a Kubernetes Service of type 'LoadBalancer'.
kubectl expose deployment nginx-deployment --type=LoadBalancer --port=8080
- The addition --type=LoadBalancer indicates that you want to create a LoadBalancer Service and expose your Pods to the public internet. For this, an HA-IP load balancer is automatically created for your Deployment in our systems.
- The Nginx application listens to traffic on port 80, see the layout of this specific type of object in our article 'What are Kubernetes objects'. If you use a different type of object for your Deployment, check the used port number and adjust it in this command accordingly.
You will see a clear confirmation:
service/nginx-deployment exposed
You can see the status of your LoadBalancer Service (and all other Kubernetes Services on your cluster) with the command:
kubectl get services
For a new cluster, the output looks something like this:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 42d
nginx-deployment LoadBalancer 10.123.234.123 <pending> 80:31867/TCP 15s
Step 3
Finally, there is one more thing to do: testing your hello node Deployment.
Open a browser and go to the IP address you see at the end of step 2 under 'EXTERNAL-IP' with the addition of :80 for the port on which the service listens, for example 86.123.234.123.haip.transip.net:80
If you used the example object to create a Deployment, you will now see an Nginx welcome page.
Additional steps are required to actually use your own website: you can either put your website in a container and deploy it, or deploy an FTP server alongside nginx. We will explain these options in a separate article in the future.
Removing the Deployment and associated services
Now that you have finished testing a hello node Deployment, you may want to remove it. This consists of two parts: deleting the created services and deleting the actual Deployment.
kubectl delete service nginx-deployment kubectl delete deployment nginx-deployment
If you are not sure anymore about the name of your Service(s) or Deployment, you can retrieve them again with the following commands respectively:
kubectl get services kubectl get deployments
This brings us to the end of this tutorial on creating a Kubernetes Deployment using an object.