Service Overview
Kubernetes services allow you to expose your applications running on a set of Pods as a network service. They provide a stable endpoint to communicate with these Pods, irrespective of their lifecycle changes.
Service Types
-
ClusterIP:
kubectl expose pod <POD_NAME> --port=<PORT> --name=<SERVICE_NAME> --type=ClusterIP
apiVersion: v1 kind: Service metadata: name: my-clusterip-service spec: selector: app: my-app ports: - protocol: TCP port: 80 targetPort: 8080 type: ClusterIP
-
NodePort:
kubectl expose pod <POD_NAME> --port=<PORT> --name=<SERVICE_NAME> --type=NodePort
apiVersion: v1 kind: Service metadata: name: my-nodeport-service spec: selector: app: my-app ports: - protocol: TCP port: 80 targetPort: 8080 nodePort: 30080 type: NodePort
-
LoadBalancer:
kubectl expose pod <POD_NAME> --port=<PORT> --name=<SERVICE_NAME> --type=LoadBalancer
apiVersion: v1 kind: Service metadata: name: my-loadbalancer-service spec: selector: app: my-app ports: - protocol: TCP port: 80 targetPort: 8080 type: LoadBalancer
Troubleshooting & Common Issues
-
Check Service Details and Events:
kubectl describe service <SERVICE_NAME>
Look for any events or configurations that might seem out of place.
-
Pods Not Matched by Selector: If you’re not getting traffic to your pods, it might be that the service selector doesn’t match any pods.
-
Check Service’s Endpoints:
kubectl get endpoints <SERVICE_NAME>
This will show you which Pods (if any) are receiving traffic from the service.
-
Restrictive Network Policies: Ensure they aren’t too restrictive, blocking traffic to your service’s pods.
-
Validate Network Plugins: Ensure they’re functioning correctly, as they might be misconfigured or encountering errors.
-
Cloud Provider Issues: Check if there are sufficient resources (like IP addresses) in your cloud account.
-
Port Conflicts: For NodePort services, ensure that the designated port isn’t being used by another service on the node.
-
Ping Test:
ping <SERVICE_IP>
From inside a Pod, try to ping the service IP to see if it’s reachable.
-
Use
curl
orwget
:curl <SERVICE_IP>
Inside a Pod, use curl or wget to check the service response.
-
External Connectivity: Test connectivity from outside the cluster. For LoadBalancers, ensure the right firewall rules are in place.
-
Check CoreDNS (or kube-dns) Pods:
kubectl get pods -n kube-system
Ensure DNS pods are running and healthy.
-
DNS Configuration in Pods: Check
/etc/resolv.conf
to see if nameservers and search domains are correctly set up. -
Check Pod Logs:
kubectl logs <POD_NAME>
For services routing traffic to specific pods, check the logs of those pods for any application-specific issues.
-
Monitoring & Metrics: Check metrics for abnormal patterns like increased latency or error rates.
-
Use
netcat
ornc
:nc -l <PORT>
Use these tools to listen on a specific port and check for incoming traffic.
-
Check with
istioctl
: If you’re using Istio, theistioctl
command-line tool can provide a lot of insights and diagnostics.