How to use kubectl to deploy applications on Kubernetes

Are you ready to take your Kubernetes game to the next level? Do you want to learn how to deploy applications on Kubernetes using kubectl? If so, you've come to the right place! In this article, we'll show you how to use kubectl to deploy applications on Kubernetes, step by step.

What is kubectl?

Before we dive into the details of deploying applications on Kubernetes using kubectl, let's first understand what kubectl is. Kubectl is a command-line tool that allows you to interact with Kubernetes clusters. It is used to deploy, inspect, and manage applications on Kubernetes. Kubectl is a powerful tool that can help you automate your Kubernetes workflows and make your life easier.

Prerequisites

Before we get started, there are a few things you'll need to have in place:

If you don't have a Kubernetes cluster set up yet, you can use a managed Kubernetes service like Google Kubernetes Engine (GKE), Amazon Elastic Kubernetes Service (EKS), or Microsoft Azure Kubernetes Service (AKS). These services make it easy to set up and manage Kubernetes clusters.

To install kubectl on your local machine, you can follow the instructions in the Kubernetes documentation. Once you have kubectl installed, you can use it to interact with your Kubernetes cluster.

Deploying an application using kubectl

Now that we have our prerequisites in place, let's get started with deploying an application on Kubernetes using kubectl. In this example, we'll be deploying a simple web application that displays a "Hello, World!" message.

Step 1: Create a deployment

The first step in deploying an application on Kubernetes is to create a deployment. A deployment is a Kubernetes object that manages a set of replicas of your application. It ensures that the desired number of replicas are running at all times, and it can automatically replace any replicas that fail.

To create a deployment, we'll use the kubectl create command. Here's an example:

kubectl create deployment hello-world --image=gcr.io/google-samples/hello-app:1.0

This command creates a deployment called hello-world and specifies the Docker image to use (gcr.io/google-samples/hello-app:1.0). This image contains our "Hello, World!" web application.

Step 2: Verify the deployment

Once we've created our deployment, we can use the kubectl get command to verify that it's running:

kubectl get deployments

This command should output something like this:

NAME          READY   UP-TO-DATE   AVAILABLE   AGE
hello-world   1/1     1            1           10s

This output tells us that our hello-world deployment is running and that there is one replica available.

Step 3: Expose the deployment

Now that our deployment is running, we need to expose it so that we can access it from outside the cluster. To do this, we'll create a Kubernetes service.

A service is a Kubernetes object that provides a stable IP address and DNS name for accessing your application. It can also load balance traffic between multiple replicas of your application.

To create a service, we'll use the kubectl expose command. Here's an example:

kubectl expose deployment hello-world --type=LoadBalancer --port=8080

This command creates a service called hello-world and exposes it as a load balancer on port 8080. This will allow us to access our "Hello, World!" web application from outside the cluster.

Step 4: Verify the service

Once we've created our service, we can use the kubectl get command to verify that it's running:

kubectl get services

This command should output something like this:

NAME          TYPE           CLUSTER-IP     EXTERNAL-IP     PORT(S)          AGE
hello-world   LoadBalancer   10.0.0.1       35.184.10.101   8080:32617/TCP   10s

This output tells us that our hello-world service is running and that it has been assigned an external IP address (35.184.10.101). We can now access our "Hello, World!" web application by visiting http://35.184.10.101:8080 in a web browser.

Updating an application using kubectl

Now that we've deployed our "Hello, World!" web application, let's see how we can update it using kubectl. In this example, we'll update the application to display a "Hello, Kubernetes!" message instead.

Step 1: Update the Docker image

The first step in updating our application is to update the Docker image. We'll create a new Docker image that contains our updated "Hello, Kubernetes!" web application.

docker build -t gcr.io/google-samples/hello-app:2.0 .
docker push gcr.io/google-samples/hello-app:2.0

This command builds a new Docker image and pushes it to the Google Container Registry. We'll use this new image to update our deployment.

Step 2: Update the deployment

To update our deployment, we'll use the kubectl set image command. Here's an example:

kubectl set image deployment/hello-world hello-world=gcr.io/google-samples/hello-app:2.0

This command updates the hello-world deployment to use the new Docker image (gcr.io/google-samples/hello-app:2.0).

Step 3: Verify the update

Once we've updated our deployment, we can use the kubectl rollout status command to verify that the update has been applied:

kubectl rollout status deployment/hello-world

This command should output something like this:

deployment "hello-world" successfully rolled out

This output tells us that our update has been successfully applied. We can now visit http://35.184.10.101:8080 in a web browser to see our updated "Hello, Kubernetes!" message.

Conclusion

In this article, we've shown you how to use kubectl to deploy and update applications on Kubernetes. We've covered the basics of creating a deployment and exposing it as a service, as well as updating the application by updating the Docker image and rolling out the update.

Kubectl is a powerful tool that can help you automate your Kubernetes workflows and make your life easier. By mastering kubectl, you'll be able to deploy and manage applications on Kubernetes with ease. So what are you waiting for? Start using kubectl today and take your Kubernetes game to the next level!

Editor Recommended Sites

AI and Tech News
Best Online AI Courses
Classic Writing Analysis
Tears of the Kingdom Roleplay
Quick Home Cooking Recipes: Ideas for home cooking with easy inexpensive ingredients and few steps
Labaled Machine Learning Data: Pre-labeled machine learning data resources for Machine Learning engineers and generative models
Tree Learn: Learning path guides for entry into the tech industry. Flowchart on what to learn next in machine learning, software engineering
Developer Painpoints: Common issues when using a particular cloud tool, programming language or framework
Crypto Payments - Accept crypto payments on your Squarepace, WIX, etsy, shoppify store: Learn to add crypto payments with crypto merchant services