Kubernetes Service
Concept
![[images/截屏2026-03-24 14.50.56.png]]
A Service provides a single point of entry for accessing one or more Pods.
Can’t rely on Pod IP
- Pods are motral and may only live a short time. (ephemeral)
- Pod IP address may not stay the same.
- Pods can be horizontally scaled so each Pod gets its own IP address.
- A Pod gets an IP address after it has been scheduled (no way for clients to know IP ahead of time).
Service Do
- Services abstract Pod IP addresses from consumers.
- Load balances between Pods.
- Relies on labels to associate a Service with a Pod.
- Node’s kube-proxy creates a virtual IP for Services
- Layer 4 (TCP/UDP over IP)
- Services are not ephemeral.
- Creates endpoints which sit betwwen a Service and Pod.
Calling Service
![[images/截屏2026-03-24 15.06.21.png]]
Service uses label to associate with the Pods.
Service Types
Services can be defined in different ways:
- ClusterIP: Expose the service on a cluster-internal IP (default) (微服务之间调用)
- NodePort: Expose the service on each Node’s IP at a static port. (本地学习、测试)
- LoadBalancer: Provision an external IP to act as a load balancer for the service. (云环境,基本由云服务厂商提供)
- ExternalName: Maps a service to a DNS name. (几乎不用)
ClusterIP Service
- Serivce IP is exposed internally within the cluster.
- Only Pods within the cluster can talk to the Service.
- Allows Pods to talk to other Pods.
NodePort Service
- Exposes the Service on each Node’s IP at a static port.
- Allocates a port from a range (default is 30000-32767).
- Each Node proxies the allocated port.
LoadBalancer Service
- Exposes a Service externally.
- Useful when combined with a cloud provider’s load balancer.
- NodePort and ClusterIP Services are created.
- Each Node proxies the allocated port.
![[images/截屏2026-03-24 15.16.27.png]]
ExternalName Service
- Service that acts as an alias for an external service.
- Allows a Service to act as the proxy for an external service.
- External service details are hidden from cluster (easier to change).
- Only to change the ExternalName Service but not need to change the Pods in cluster.
![[images/截屏2026-03-24 15.19.11.png]]
Create Service
Port Forward
Use kubectl port-forward to forward a local port to a Pod port.
# Listen on port 8080 locally and forward to port 80 in Pod
kubectl port-forward pod/[pod_name] 8080:80
# Listen on port 8080 locally and forward to Deployment's Pod
kubectl port-forward deployment/[deployment_name] 8080
# Listen on port 8080 locally and forward to Service's Pod
kubectl port-forward service/[service_name] 8080
kubectl port-forward is used on local test or debugging. So Service is needed to forward external requests.
YAML
![[images/截屏2026-03-24 16.03.03.png]]
Connect
metadata:
name: frontend # name of Service. Each Service gets a DNS entry
...
metadata:
name: backend
A frontend Pod can access a backend Pod using backend:port
NodePort
apiVersion: v1
kind: Service
metadata:
name: nginx-nodeport
spec:
type: NodePort
selector:
app: my-nginx
ports:
- port: 80
targetPort: 80
nodePort: 31000 # optionally set NodePort value (defaults between 30000-32767)
ClusterIP
apiVersion: v1
kind: Service
metadata:
name: nginx-clusterip
spec:
type: ClusterIP
selector:
app: my-nginx
ports:
- port: 8080
targetPort: 80
LoadBalancer
apiVersion: v1
kind: Service
metadata:
name: nginx-loadbalancer
spec:
type: LoadBalancer
selector:
app: my-nginx
ports:
- name: "80"
port: 80
targetPort: 80
- name: "443"
port: 443
targetPort: 443
ExternalName
apiVersion: v1
kind: Service
metadata:
name: external-service # Other Pods can use this FQDN to access the external service
spec:
type: ExternalName # Set type to ExternalName
externalName: api.musuyin.com # Service will proxy to FQDN
ports:
- port: 9000 # can be called with external-service:9000
kubectl
kubectl create -f [yaml_name.yaml]
kubectl apply -f [yaml_name.yaml]
kubectl delete -f [yaml_name.yaml]
# Shell into a Pod and test a URL. Add -c [containerID] in cases where multiple containers are running in the Pod
kubectl exec [pod_name] -- curl -s http://podIP:port
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 木素音的小站!