CKA-Debug Services
CKA-Debug Services
Tasks
HOMEGETTING STARTEDCONCEPTSTASKSTUTORIALSREFERENCECONTRIBUTE
Search
Debug Services
An issue that comes up rather frequently for new installations of Kubernetes is that a Service is not working properly. You’ve run your
Deployment and created a Service , but you get no response when you try to access it. This document will hopefully help you to gure out
Conventions
Running commands in a Pod
Setup
Does the Service exist?
Does the Service work by DNS?
Does the Service work by IP?
Is the Service correct?
Does the Service have any Endpoints?
Are the Pods working?
Is the kube-proxy working?
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/ 1/22
08/02/2020 Debug Services - Kubernetes
Seek help
What's next
Conventions
Throughout this doc you will see various commands that you can run. Some commands need to be run within a Pod , others on a Kubernetes
Node , and others can run anywhere you have kubectl and credentials for the cluster. To make it clear what is expected, this document will use
u@pod$ COMMAND
OUTPUT
u@node$ COMMAND
OUTPUT
kubectl ARGS
OUTPUT
For many steps here you will want to see what a Pod running in the cluster sees. The simplest way to do this is to run an interactive alpine Pod :
If you already have a running Pod that you prefer to use, you can run a command in it using:
Setup
For the purposes of this walk-through, let’s run some Pods . Since you’re probably debugging your own Service you can substitute your own
details, or you can follow along and get a second data point.
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/ 3/22
08/02/2020 Debug Services - Kubernetes
kubectl commands will print the type and name of the resource created or mutated, which can then be used in subsequent commands.
Note:
This is the same as if you started the Deployment with the following YAML:
apiVersion: apps/v1
kind: Deployment
metadata:
name: hostnames
spec:
selector:
matchLabels:
app: hostnames
replicas: 3
template:
metadata:
labels:
app: hostnames
spec:
containers:
- name: hostnames
image: k8s.gcr.io/serve_hostname
ports:
- containerPort: 9376
protocol: TCP
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/ 4/22
08/02/2020 Debug Services - Kubernetes
The astute reader will have noticed that we did not actually create a Service yet - that is intentional. This is a step that sometimes gets
forgotten, and is the rst thing to check.
So what would happen if I tried to access a non-existent Service ? Assuming you have another Pod that consumes this Service by name you
would get something like:
So we have a culprit, let’s create the Service . As before, this is for the walk-through - you can use your own Service ’s details here.
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/ 5/22
08/02/2020 Debug Services - Kubernetes
As before, this is the same as if you had started the Service with YAML:
apiVersion: v1
kind: Service
metadata:
name: hostnames
spec:
selector:
app: hostnames
ports:
- name: default
protocol: TCP
port: 80
targetPort: 9376
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/ 6/22
08/02/2020 Debug Services - Kubernetes
Name: hostnames
Address 1: 10.0.1.175 hostnames.default.svc.cluster.local
If this fails, perhaps your Pod and Service are in different Namespaces , try a namespace-quali ed name:
Name: hostnames.default
Address 1: 10.0.1.175 hostnames.default.svc.cluster.local
If this works, you’ll need to adjust your app to use a cross-namespace name, or run your app and Service in the same Namespace . If this still
fails, try a fully-quali ed name:
Name: hostnames.default.svc.cluster.local
Address 1: 10.0.1.175 hostnames.default.svc.cluster.local
Note the su x here: “default.svc.cluster.local”. The “default” is the Namespace we’re operating in. The “svc” denotes that this is a Service . The
“cluster.local” is your cluster domain, which COULD be different in your own cluster.
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/ 7/22
08/02/2020 Debug Services - Kubernetes
Name: hostnames.default.svc.cluster.local
Address: 10.0.1.175
If you are able to do a fully-quali ed name lookup but not a relative one, you need to check that your /etc/resolv.conf le is correct.
The nameserver line must indicate your cluster’s DNS Service . This is passed into kubelet with the --cluster-dns ag.
The search line must include an appropriate su x for you to nd the Service name. In this case it is looking for Services in the local
Namespace ( default.svc.cluster.local ), Services in all Namespaces ( svc.cluster.local ), and the cluster ( cluster.local ).
Depending on your own install you might have additional records after that (up to 6 total). The cluster su x is passed into kubelet with the
--cluster-domain ag. We assume that is “cluster.local” in this document, but yours might be different, in which case you should change that
in all of the commands above.
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/ 8/22
08/02/2020 Debug Services - Kubernetes
The options line must set ndots high enough that your DNS client library considers search paths at all. Kubernetes sets this to 5 by default,
which is high enough to cover all of the DNS names it generates.
Name: kubernetes.default
Address 1: 10.0.0.1 kubernetes.default.svc.cluster.local
If this fails, you might need to go to the kube-proxy section of this doc, or even go back to the top of this document and start over, but instead of
debugging your own Service , debug DNS.
Assuming we can con rm that DNS works, the next thing to test is whether your Service works at all. From a node in your cluster, access the
Service ’s IP (from kubectl get above).
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/ 9/22
08/02/2020 Debug Services - Kubernetes
If your Service is working, you should get correct responses. If not, there are a number of things that could be going wrong. Read on.
It might sound silly, but you should really double and triple check that your Service is correct and matches your Pod ’s port. Read back your
Service and verify it:
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/ 10/22
08/02/2020 Debug Services - Kubernetes
{
"kind": "Service",
"apiVersion": "v1",
"metadata": {
"name": "hostnames",
"namespace": "default",
"uid": "428c8b6c-24bc-11e5-936d-42010af0a9bc",
"resourceVersion": "347189",
"creationTimestamp": "2015-07-07T15:24:29Z",
"labels": {
"app": "hostnames"
}
},
"spec": {
"ports": [
{
"name": "default",
"protocol": "TCP",
"port": 80,
"targetPort": 9376,
"nodePort": 0
}
],
"selector": {
"app": "hostnames"
},
"clusterIP": "10.0.1.175",
"type": "ClusterIP",
"sessionAffinity": "None"
},
"status": {
"loadBalancer": {}
}
}
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/ 11/22
08/02/2020 Debug Services - Kubernetes
Is the targetPort correct for your Pods (many Pods choose to use a different port than the Service )?
If you meant it to be a named port, do your Pods expose a port with the same name?
If you got this far, we assume that you have con rmed that your Service exists and is resolved by DNS. Now let’s check that the Pods you ran
Earlier we saw that the Pods were running. We can re-check that:
The “AGE” column says that these Pods are about an hour old, which implies that they are running ne and not crashing.
The -l app=hostnames argument is a label selector - just like our Service has. Inside the Kubernetes system is a control loop which
evaluates the selector of every Service and saves the results into an Endpoints object.
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/ 12/22
08/02/2020 Debug Services - Kubernetes
This con rms that the endpoints controller has found the correct Pods for your Service . If the hostnames row is blank, you should check that
the spec.selector eld of your Service actually selects for metadata.labels values on your Pods . A common mistake is to have a typo or
other error, such as the Service selecting for run=hostnames , but the Deployment specifying app=hostnames .
At this point, we know that your Service exists and has selected your Pods . Let’s check that the Pods are actually working - we can bypass
the Service mechanism and go straight to the Pods .
Note: These commands use the Pod port (9376), rather than the Service port (80).
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/ 13/22
08/02/2020 Debug Services - Kubernetes
We expect each Pod in the Endpoints list to return its own hostname. If this is not what happens (or whatever the correct behavior is for your
own Pods ), you should investigate what’s happening there. You might nd kubectl logs to be useful or kubectl exec directly to your Pods
and check service from there.
Another thing to check is that your Pods are not crashing or being restarted. Frequent restarts could lead to intermittent connectivity issues.
If the restart count is high, read more about how to debug pods.
If you get here, your Service is running, has Endpoints , and your Pods are actually serving. At this point, the whole Service proxy
mechanism is suspect. Let’s con rm it, piece by piece.
Is kube-proxy running?
Con rm that kube-proxy is running on your Nodes . You should get something like the below:
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/ 14/22
08/02/2020 Debug Services - Kubernetes
Next, con rm that it is not failing something obvious, like contacting the master. To do this, you’ll have to look at the logs. Accessing the logs
depends on your Node OS. On some OSes it is a le, such as /var/log/kube-proxy.log, while other OSes use journalctl to access logs. You
should see something like:
If you see error messages about not being able to contact the master, you should double-check your Node con guration and installation steps.
One of the possible reasons that kube-proxy cannot run correctly is that the required conntrack binary cannot be found. This may happen on
some Linux systems, depending on how you are installing the cluster, for example, you are installing Kubernetes from scratch. If this is the case,
you need to manually install the conntrack package (e.g. sudo apt install conntrack on Ubuntu) and then retry.
The kube-proxy can run in “userspace” mode, “iptables” mode or “ipvs” mode. Hopefully you are using the “iptables” mode or “ipvs” mode. You
should see one of the following cases.
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/ 15/22
08/02/2020 Debug Services - Kubernetes
Userspace
There should be 2 rules for each port on your Service (just one in this example) - a “KUBE-PORTALS-CONTAINER” and a “KUBE-PORTALS-
HOST”. If you do not see these, try restarting kube-proxy with the -v ag set to 4, and then look at the logs again.
Almost nobody should be using the “userspace” mode any more, so we won’t spend more time on it here.
Iptables
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/ 16/22
08/02/2020 Debug Services - Kubernetes
There should be 1 rule in KUBE-SERVICES , 1 or 2 rules per endpoint in KUBE-SVC-(hash) (depending on SessionAffinity ), one
KUBE-SEP-(hash) chain per endpoint, and a few rules in each KUBE-SEP-(hash) chain. The exact rules will vary based on your exact con g
IPVS
IPVS proxy will create a virtual server for each service address(e.g. Cluster IP, External IP, NodePort IP, Load Balancer IP etc.) and some
corresponding real servers for endpoints of the service, if any. In this example, service hostnames( 10.0.1.175:80 ) has 3 endpoints(
10.244.0.5:9376 , 10.244.0.6:9376 , 10.244.0.7:9376 ) and you’ll get results similar to above.
Is kube-proxy proxying?
Assuming you do see the above rules, try again to access your Service by IP:
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/ 17/22
08/02/2020 Debug Services - Kubernetes
If this fails and you are using the userspace proxy, you can try accessing the proxy directly. If you are using the iptables proxy, skip this section.
Look back at the iptables-save output above, and extract the port number that kube-proxy is using for your Service . In the above
examples it is “48577”. Now connect to that:
If this still fails, look at the kube-proxy logs for speci c lines like:
If you don’t see those, try restarting kube-proxy with the -v ag set to 4, and then look at the logs again.
themselves if they try to access their own Service VIP. The hairpin-mode ag must either be set to hairpin-veth or promiscuous-bridge .
Con rm hairpin-mode is set to hairpin-veth or promiscuous-bridge . You should see something like the below. hairpin-mode is set
to promiscuous-bridge in the following example.
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/ 18/22
08/02/2020 Debug Services - Kubernetes
root 3392 1.1 0.8 186804 65208 ? Sl 00:51 11:11 /usr/local/bin/kubelet --enable-debugging-handlers
Con rm the effective hairpin-mode . To do this, you’ll have to look at kubelet log. Accessing the logs depends on your Node OS. On some
OSes it is a le, such as /var/log/kubelet.log, while other OSes use journalctl to access logs. Please be noted that the effective hairpin
mode may not match --hairpin-mode ag due to compatibility. Check if there is any log lines with key word hairpin in kubelet.log. There
should be log lines indicating the effective hairpin mode, like something below.
If the effective hairpin mode is hairpin-veth , ensure the Kubelet has the permission to operate in /sys on node. If everything works
properly, you should see something like:
1
1
If the effective hairpin mode is promiscuous-bridge , ensure Kubelet has the permission to manipulate linux bridge on node. If cbr0
bridge is used and con gured properly, you should see:
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/ 19/22
08/02/2020 Debug Services - Kubernetes
Seek help
If you get this far, something very strange is happening. Your Service is running, has Endpoints , and your Pods are actually serving. You
have DNS working, iptables rules installed, and kube-proxy does not seem to be misbehaving. And yet your Service is not working. You
should probably let us know, so we can help investigate!
What's next
Feedback
Yes No
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/ 20/22
08/02/2020 Debug Services - Kubernetes
Page last modi ed on January 03, 2020 at 12:13 PM PST by add missing backquote (#18413) (Page History)
Home
Blog
Partners
Community
Case Studies
Contribute
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/ 21/22
08/02/2020 Debug Services - Kubernetes
https://kubernetes.io/docs/tasks/debug-application-cluster/debug-service/ 22/22