How to Access Service in Kubernetes: Simple Guide
To access a
Service in Kubernetes, you use its type like ClusterIP for internal access, NodePort to expose it on a node port, or LoadBalancer to get an external IP. You connect to the service using its cluster IP, node IP with port, or external IP depending on the service type.Syntax
A Kubernetes Service is defined in YAML with a spec.type that controls how it is accessed:
- ClusterIP: Accessible only inside the cluster via
clusterIP. - NodePort: Opens a port on each node to access the service externally.
- LoadBalancer: Requests a cloud provider to provision an external IP.
The key fields are spec.selector to match pods, spec.ports to define ports, and spec.type to set access method.
yaml
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: ClusterIPExample
This example shows a NodePort service exposing a pod on port 30007 of each node. You can access the service externally by connecting to any node's IP at port 30007.
yaml
apiVersion: v1
kind: Service
metadata:
name: example-nodeport
spec:
selector:
app: example-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
nodePort: 30007
type: NodePortOutput
kubectl get svc example-nodeport
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
example-nodeport NodePort 10.96.0.123 <none> 80:30007/TCP 5m
Common Pitfalls
Common mistakes when accessing services include:
- Using
ClusterIPservice type but trying to access it from outside the cluster. - Not specifying
nodePortwhen usingNodePortand expecting a fixed port. - For
LoadBalancertype, assuming an external IP appears immediately; it may take time or require cloud provider support. - Incorrect
selectorlabels causing no pods to be matched.
yaml
apiVersion: v1
kind: Service
metadata:
name: wrong-service
spec:
selector:
app: wrong-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
type: ClusterIP
# This service cannot be accessed externally because ClusterIP is internal only.
---
apiVersion: v1
kind: Service
metadata:
name: correct-nodeport
spec:
selector:
app: correct-app
ports:
- protocol: TCP
port: 80
targetPort: 8080
nodePort: 30080
type: NodePort
# This service is accessible externally on node IP at port 30080.Quick Reference
Here is a quick summary of service types and how to access them:
| Service Type | Access Method | Use Case |
|---|---|---|
| ClusterIP | Internal cluster IP only | Internal communication between pods |
| NodePort | Node IP + nodePort | Expose service on each node for external access |
| LoadBalancer | External IP from cloud provider | Public access with cloud load balancer |
| ExternalName | DNS name | Map service to external DNS name |
Key Takeaways
Use ClusterIP for internal-only service access inside the cluster.
Use NodePort to expose a service on a static port on each node for external access.
Use LoadBalancer to get an external IP from your cloud provider for public access.
Always check your service selector matches the pod labels correctly.
Access methods depend on the service type and network setup.