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.
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:
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.
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>.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.
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:
NodePort Service:
apiVersion: v1 kind: Service metadata: name: web-app-service spec: type: NodePort selector: app: web-app ports: - port: 80 targetPort: 80This will expose the web application on a random port on each Node's IP address.
LoadBalancer Service:
apiVersion: v1 kind: Service metadata: name: web-app-service spec: type: LoadBalancer selector: app: web-app ports: - port: 80 targetPort: 80This will provision a load balancer in the cloud environment and expose the web application using the load balancer's IP address.
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: 80Ingress 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:
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.
- Access the Service using
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.
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.


