Deploying Your First Application on Kubernetes

Deploying an application on Kubernetes can seem daunting, but it's a structured process that can be broken down into manageable steps. By the end of this article, you will have a basic application running in a Kubernetes cluster, complete with configuration and deployment using YAML files and the kubectl command line tool.

Prerequisites

Before we dive into deployment, make sure you have the following in place:

  1. Kubernetes Cluster: You should have access to a running Kubernetes cluster. This can be a local setup using Minikube, or a managed cluster on platforms like Google Kubernetes Engine (GKE), Amazon EKS, or Azure AKS.

  2. kubectl Installed: Ensure you have the Kubernetes command-line tool, kubectl, installed and configured to communicate with your cluster. You can verify this by running:

    kubectl version --client
    
  3. A Sample Application: For this guide, we’ll deploy a simple web application based on Docker that serves a "Hello, World!" message. You can find a basic Docker image for our demo at nginxdemos/hello.

Step 1: Create a Deployment YAML File

Kubernetes deployments are typically defined in YAML files. Here, we'll set up a deployment configuration for our sample application.

Create a file named deployment.yaml and add the following content:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: hello-world
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
      - name: hello-world
        image: nginxdemos/hello
        ports:
        - containerPort: 80

Breakdown of the YAML File

  • apiVersion: Specifies which version of the Kubernetes API you’re using; apps/v1 is the standard for deployments.

  • kind: Defines the type of resource we’re creating, which in this case is a Deployment.

  • metadata: Contains data that helps uniquely identify the deployment, including its name.

  • spec: Here, we define the desired state of our deployment:

    • replicas: The number of pod replicas you want to run; we’re starting with 2.
    • selector: A label query over pods that should match the replica count.
    • template: Describes the pods that will be created; includes labels and the container specification.

Step 2: Create a Service YAML File

To expose our application to the outside world, we’ll define a Service resource. Create a file named service.yaml with the following content:

apiVersion: v1
kind: Service
metadata:
  name: hello-world-service
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: hello-world

Breakdown of the Service YAML File

  • apiVersion: Here as well, we indicate the API version, which is v1 for Services.

  • kind: We specify that this resource is of type Service.

  • metadata: Contains the name of our service which will be used to reference it.

  • spec: Specifies how the service behaves:

    • type: LoadBalancer will create an external-facing LoadBalancer that routes traffic to the pods.
    • ports: Maps port 80 of the service to port 80 of the pods, where our application is running.
    • selector: Identifies the pods that this service will route traffic to.

Step 3: Deploying to the Kubernetes Cluster

Now that we have our deployment and service YAML files ready, it's time to deploy them to the Kubernetes cluster using kubectl.

Apply the Deployment

Run the following command to apply the deployment configuration:

kubectl apply -f deployment.yaml

You should see output indicating that the deployment has been created.

Apply the Service

Next, apply the service configuration:

kubectl apply -f service.yaml

Once again, you should see confirmation that the service has been created.

Step 4: Verify Your Deployment

To check the status of your application, you can use the following commands:

Check Pods

To list the pods that were created, run:

kubectl get pods

You should see your hello-world pods running. The output might look something like this:

NAME                                         READY   STATUS    RESTARTS   AGE
hello-world-deployment-5b76fb8444-d75hd    1/1     Running   0          1m
hello-world-deployment-5b76fb8444-l2q5s    1/1     Running   0          1m

Check Services

To list services and find the external IP for your hello-world-service, run:

kubectl get services

You should see something like this:

NAME                   TYPE           CLUSTER-IP      EXTERNAL-IP    PORT(S)        AGE
hello-world-service    LoadBalancer   10.96.171.135   <pending>      80:30000/TCP   1m

If <pending> appears for the EXTERNAL-IP, it might take a few moments for the LoadBalancer to provision an external IP address. This typically depends on your cloud provider.

Step 5: Access Your Application

Once you have the external IP address, you can access your application. Open your web browser and navigate to http://<EXTERNAL-IP>. You should see the "Hello, World!" message served by your application.

Step 6: Clean Up

When you're done testing, it's a good idea to clean up the resources you created. You can delete the deployment and service with the following commands:

kubectl delete -f service.yaml
kubectl delete -f deployment.yaml

This will remove everything that you deployed from your Kubernetes cluster.

Conclusion

Congratulations! You have successfully deployed your first application on Kubernetes. You’ve learned how to create the necessary YAML files for deployments and services, how to use kubectl for applying configurations and managing resources, and how to access your application.

With this foundational knowledge, you're now ready to explore more complicated deployments, scale your applications, and take full advantage of the robust features Kubernetes has to offer. Happy deploying!