How to expose a Kubernetes web application

KubernetesKubernetesBeginner
Practice Now

Introduction

Kubernetes is a powerful container orchestration platform that simplifies the deployment and management of web applications. In this tutorial, we will explore how to expose a Kubernetes web application, making it accessible to users. We'll cover the key concepts of Kubernetes services and the various methods to configure service access.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/TroubleshootingandDebuggingCommandsGroup(["`Troubleshooting and Debugging Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes(("`Kubernetes`")) -.-> kubernetes/ConfigurationandVersioningGroup(["`Configuration and Versioning`"]) kubernetes/TroubleshootingandDebuggingCommandsGroup -.-> kubernetes/describe("`Describe`") kubernetes/BasicCommandsGroup -.-> kubernetes/expose("`Expose`") kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") kubernetes/BasicCommandsGroup -.-> kubernetes/run("`Run`") kubernetes/ConfigurationandVersioningGroup -.-> kubernetes/config("`Config`") subgraph Lab Skills kubernetes/describe -.-> lab-415006{{"`How to expose a Kubernetes web application`"}} kubernetes/expose -.-> lab-415006{{"`How to expose a Kubernetes web application`"}} kubernetes/get -.-> lab-415006{{"`How to expose a Kubernetes web application`"}} kubernetes/run -.-> lab-415006{{"`How to expose a Kubernetes web application`"}} kubernetes/config -.-> lab-415006{{"`How to expose a Kubernetes web application`"}} end

Understanding Kubernetes Services

What is a Kubernetes Service?

A Kubernetes Service is an abstraction that defines a logical set of Pods and a policy by which to access them. Services provide a stable endpoint for applications to communicate with each other, even as the underlying Pods come and go.

Types of Kubernetes Services

Kubernetes supports several types of Services:

  1. ClusterIP: This is the default Service type. It exposes the Service on a cluster-internal IP address, which means the Service is only reachable from within the cluster.

  2. NodePort: This type of Service exposes the Service on each Node's IP address at a static port. This makes the Service accessible from outside the cluster using <NodeIP>:<NodePort>.

  3. LoadBalancer: This type of Service provisions a load balancer for the Service, typically in cloud environments. This makes the Service accessible from outside the cluster using the load balancer's IP or hostname.

  4. ExternalName: This type of Service maps the Service to a DNS name, without requiring a proxy.

Service Discovery

Kubernetes Services provide built-in service discovery mechanisms, allowing Pods to find and communicate with each other. Pods can discover Services through environment variables or DNS.

graph LR Client --> Service Service --> Pods

Accessing Services

Clients can access Services in different ways:

  • ClusterIP: Clients within the cluster can access the Service using the ClusterIP and the Service port.
  • NodePort: Clients outside the cluster can access the Service using <NodeIP>:<NodePort>.
  • LoadBalancer: Clients outside the cluster can access the Service using the load balancer's IP or hostname.

Exposing a Kubernetes Web Application

Deploying a Web Application to Kubernetes

To expose a web application running in Kubernetes, you first need to deploy the application. Here's an example of a simple web application deployment using a Deployment and a Service:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: web-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: web-app
  template:
    metadata:
      labels:
        app: web-app
    spec:
      containers:
        - name: web-app
          image: labex/web-app:v1
          ports:
            - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: web-app-service
spec:
  selector:
    app: web-app
  ports:
    - port: 80
      targetPort: 80
  type: ClusterIP

Exposing the Web Application

To expose the web application to the outside world, you can use different Service types:

  1. NodePort Service:

    apiVersion: v1
    kind: Service
    metadata:
      name: web-app-service
    spec:
      type: NodePort
      selector:
        app: web-app
      ports:
        - port: 80
          targetPort: 80

    This will expose the web application on a random port on each Node's IP address.

  2. LoadBalancer Service:

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

    This will provision a load balancer in the cloud environment and expose the web application using the load balancer's IP address.

  3. Ingress:

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: web-app-ingress
    spec:
      rules:
        - http:
            paths:
              - path: /
                pathType: Prefix
                backend:
                  service:
                    name: web-app-service
                    port:
                      number: 80

    Ingress provides a more advanced way of exposing multiple web applications with a single external IP address.

Configuring Service Access

Accessing Services within the Cluster

Pods within the same Kubernetes cluster can access a Service using the Service name and the appropriate port. For example, if you have a Service named web-app-service listening on port 80, Pods can access it using http://web-app-service:80.

Accessing Services from Outside the Cluster

To access a Service from outside the Kubernetes cluster, you can use one of the following methods:

  1. NodePort Service:

    • Access the Service using <NodeIP>:<NodePort>, where <NodeIP> is the IP address of any Node in the cluster.
    • This method is suitable for development and testing environments.
  2. LoadBalancer Service:

    • The cloud provider will provision a load balancer and assign it a public IP address.
    • Access the Service using the load balancer's IP address.
    • This method is suitable for production environments.
  3. Ingress:

    • Ingress provides a more advanced way of exposing multiple web applications with a single external IP address.
    • Access the web application using the Ingress hostname or IP address.
    • This method is suitable for production environments with multiple web applications.

Configuring Service Access

You can configure Service access by modifying the Service specification. For example, to change the Service type from ClusterIP to NodePort, update the type field in the Service manifest:

apiVersion: v1
kind: Service
metadata:
  name: web-app-service
spec:
  type: NodePort
  selector:
    app: web-app
  ports:
    - port: 80
      targetPort: 80

After applying the updated manifest, the web application will be accessible from outside the cluster using <NodeIP>:<NodePort>.

Summary

By the end of this tutorial, you will have a comprehensive understanding of how to expose a Kubernetes web application. You will learn about the different types of Kubernetes services, how to configure service access, and best practices for making your web application accessible to users. This knowledge will empower you to effectively deploy and manage your Kubernetes-based web applications.

Other Kubernetes Tutorials you may like