Exposing a Deployment as a Service
In Kubernetes, a Deployment is a higher-level abstraction that manages the lifecycle of a set of Pods, which are the basic units of deployment. To make the application running inside the Pods accessible from outside the Kubernetes cluster, you need to expose the Deployment as a Service.
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 network endpoint for your application, allowing other parts of your application to easily find and communicate with it.
There are several types of Services in Kubernetes:
-
ClusterIP: This is the default Service type. It exposes the Service on a cluster-internal IP address, which means the Service is only accessible from within the cluster.
-
NodePort: This type of Service exposes the application on a static port on each Node's IP address. This makes the application accessible from outside the cluster using
<NodeIP>:<NodePort>
. -
LoadBalancer: This type of Service provisions a load balancer for your application in the cloud provider's infrastructure. It makes the application 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 you to do any manual DNS configuration.
Exposing a Deployment as a Service
To expose a Deployment as a Service, you can follow these steps:
- Create a Deployment: First, you need to create a Deployment that defines the application you want to expose. Here's an example Deployment for a simple web application:
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: nginx:latest
ports:
- containerPort: 80
- Create a Service: Next, you need to create a Service that will expose the Deployment. Here's an example Service that exposes the web-app Deployment using a NodePort type:
apiVersion: v1
kind: Service
metadata:
name: web-app-service
spec:
type: NodePort
selector:
app: web-app
ports:
- port: 80
targetPort: 80
In this example, the Service selects Pods with the app=web-app
label, and forwards traffic from port 80 on the Service to port 80 on the Pods.
- Access the Application: Once the Service is created, you can access the application from outside the cluster using the Node's IP address and the assigned NodePort. For example, if the NodePort is 30123, you can access the application at
http://<NodeIP>:30123
.
Here's a Mermaid diagram that illustrates the relationship between the Deployment and the Service:
The key points to remember are:
- Deployments manage the lifecycle of your application Pods.
- Services provide a stable network endpoint to access your application.
- Different Service types (ClusterIP, NodePort, LoadBalancer) allow you to expose your application in different ways.
- By creating a Service that selects the Pods from your Deployment, you can make your application accessible from outside the cluster.
This approach allows you to decouple the application deployment from the way it is exposed, making it easier to manage and scale your application in a Kubernetes environment.