Exposing Services
Kubernetes Cluster: Exposing Services
ClusterIP
ClusterIP is the default Kubernetes Service type. It exposes the application only to other workloads inside the Kubernetes cluster and does not provide external access.
kind: Service
apiVersion: v1
metadata:
name: nginx1
namespace: test
spec:
type: ClusterIP
selector:
app: nginx
ports:
- port: 80
NodePort
NodePort is the most basic method for exposing a Kubernetes Service externally. Kubernetes opens a port on the cluster nodes and forwards traffic received on that port to the target Service.
By default, Kubernetes assigns a NodePort from the range 30000–32767.
kind: Service
apiVersion: v1
metadata:
name: nginx1
namespace: test
labels:
run: nginx
spec:
type: NodePort
selector:
run: nginx
ports:
- port: 80
targetPort: 80
If a specific NodePort is required, define it explicitly:
ports:
- port: 80
targetPort: 80
nodePort: 30984
nodePort must be within the allowed 30000–32767 range and must be unique so it does not collide with another Service.
If a public IP is already attached to the Kubernetes worker nodes, no additional platform-side exposure is required for the selected NodePort.
Expose NodePort through a Platform Endpoint
If the Kubernetes worker does not have a public IP, expose the NodePort through a platform endpoint.
- Open the Kubernetes environment.
- Navigate to Settings > Endpoints.
- Click Add.
- Node — select any worker node.
- Name — enter a preferred endpoint name.
- Private Port — enter the NodePort configured for the Service.
- Protocol — select TCP.
After clicking Add, allow a few minutes for the platform to expose the port and begin redirecting requests to the NodePort Service.
LoadBalancer
LoadBalancer is the commonly used Service type for publishing a Kubernetes application directly on the Internet. It requires a public IP attached to a Kubernetes worker node.
kind: Service
apiVersion: v1
metadata:
name: nginx1
namespace: test
spec:
type: LoadBalancer
selector:
app: nginx
ports:
- port: 80
targetPort: 8080
In this example, incoming Internet traffic arrives on port 80 and is forwarded to application port 8080.
Production Considerations
NodePort is simple but has limitations, including one Service per exposed port and a restricted high-port range. It is therefore more suitable for demonstrations, testing, or temporary applications.
