KubernetesClientException when trying to start Spring Boot microservice on Kubernetes
I am getting this exception when trying to start a Spring Boot microservice (which is Spring Boot Zuul gateway, but I don't think it matters too much) on Kubernetes:
o.s.c.kubernetes.StandardPodUtils.internalGetPod(66) - Failed to get pod with name:[podname-nqm46]. You should look into this if things aren't working as you expect. Are you missing serviceaccount permissions?
io.fabric8.kubernetes.client.KubernetesClientException: Failure executing: GET at: https://kubernetes.default.svc/api/v1/namespaces/default/pods/podname-nqm46. Message: Forbidden!Configured service account doesn't have access. Service account may have been revoked. pods "podname-nqm46" is forbidden: User "system:serviceaccount:default:default" cannot get pods in the namespace "default".
Any idea why?
This most likely has to do with RBAC control in Kubernetes.
It seems like you have not specified a service account for the Pod that the application is running in (and hence default is being used). To see how to configure a service account for a Pod check out this.
Furthermore the default service account in the namespace you are running the Pod in (which is the default namespace) does not have any extra Roles assigned to it (which is the default behavior).
Although you could grant the roles to the default user in the default namespace, a better approach would be to create a specific ServiceAccount, use that in the Pod and grant that service account the necessary roles.
Check out this for all the details.
Thank you. Working now. I made the proper configuration to my setup.
You're welcome, goood to hear it's working!
It seems I have another one that I didn't pick up from the log in the first place:
org.springframework.context.ApplicationContextException: Failed to start bean 'kubernetesDiscoveryLifecycle'; nested exception is io.fabric8.kubernetes.client.KubernetesClientException: Operation: [list] for kind: [Service] with name: [null] in namespace: [null] failed.
Maybe some additional configuration in the YAML?
Can you please provide some more information about what you are doing that results in such an error?
I want to start it up. Below more info from stacktrace.
13-07-2018 12:55:07.329 [35m[main][0;39m [1;31mERROR[0;39m o.s.boot.SpringApplication.reportFailure(771) - Application startup failed
org.springframework.context.ApplicationContextException: Failed to start bean 'kubernetesDiscoveryLifecycle'; nested exception is io.fabric8.kubernetes.client.KubernetesClientException: Operation: [list] for kind: [Service] with name: [null] in namespace: [null] failed.
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178)
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:50)
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:348)
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:151)
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:114)
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:880)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.finishRefresh(EmbeddedWebApplicationContext.java:144)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:546)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:693)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:360)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1118)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1107)
at ro.noian.pago.EdgeApplication.main(EdgeApplication.java:20)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)
Caused by: io.fabric8.kubernetes.client.KubernetesClientException: Operation: [list] for kind: [Service] with name: [null] in namespace: [null] failed.
at io.fabric8.kubernetes.client.KubernetesClientException.launderThrowable(KubernetesClientException.java:62)
at io.fabric8.kubernetes.client.KubernetesClientException.launderThrowable(KubernetesClientException.java:71)
at io.fabric8.kubernetes.client.dsl.base.BaseOperation.list(BaseOperation.java:585)
at io.fabric8.kubernetes.client.dsl.base.BaseOperation.list(BaseOperation.java:68)
at org.springframework.cloud.kubernetes.discovery.KubernetesDiscoveryClient.getServices(KubernetesDiscoveryClient.java:93)
at org.springframework.cloud.client.discovery.composite.CompositeDiscoveryClient.getServices(CompositeDiscoveryClient.java:61)
at org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator.locateRoutes(DiscoveryClientRouteLocator.java:116)
at org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator.locateRoutes(DiscoveryClientRouteLocator.java:43)
at org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator.doRefresh(SimpleRouteLocator.java:167)
at org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator.refresh(DiscoveryClientRouteLocator.java:166)
at org.springframework.cloud.netflix.zuul.filters.CompositeRouteLocator.refresh(CompositeRouteLocator.java:75)
at org.springframework.cloud.netflix.zuul.web.ZuulHandlerMapping.setDirty(ZuulHandlerMapping.java:78)
at org.springframework.cloud.netflix.zuul.ZuulProxyAutoConfiguration$ZuulDiscoveryRefreshListener.reset(ZuulProxyAutoConfiguration.java:231)
at org.springframework.cloud.netflix.zuul.ZuulProxyAutoConfiguration$ZuulDiscoveryRefreshListener.onApplicationEvent(ZuulProxyAutoConfiguration.java:211)
at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:172)
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:165)
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:393)
at org.springframework.context.support.AbstractApplicationContext.publishEvent(AbstractApplicationContext.java:347)
at org.springframework.cloud.kubernetes.discovery.KubernetesDiscoveryLifecycle.start(KubernetesDiscoveryLifecycle.java:44)
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:175)
... 22 common frames omitted
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1964)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:328)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:322)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1614)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:1052)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:987)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1072)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1385)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1413)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1397)
at okhttp3.internal.connection.RealConnection.connectTls(RealConnection.java:281)
at okhttp3.internal.connection.RealConnection.establishProtocol(RealConnection.java:251)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:151)
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:195)
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:121)
at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:100)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at io.fabric8.kubernetes.client.utils.HttpClientUtils$2.intercept(HttpClientUtils.java:95)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:185)
at okhttp3.RealCall.execute(RealCall.java:69)
at io.fabric8.kubernetes.client.dsl.base.OperationSupport.handleResponse(OperationSupport.java:377)
at io.fabric8.kubernetes.client.dsl.base.OperationSupport.handleResponse(OperationSupport.java:343)
at io.fabric8.kubernetes.client.dsl.base.OperationSupport.handleResponse(OperationSupport.java:327)
at io.fabric8.kubernetes.client.dsl.base.BaseOperation.list(BaseOperation.java:583)
I see Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target which makes suspicious... Are you sure that the certificates in your cluster are setup properly?
I noticed that, but I don't understand what endpoint is it trying to connect to. The cluster was spinned up with Rancher, and there is a SSL certificate setup, but I don't understand exactly what endpoint is it trying to connect to.
This also worries me: Operation: [list] for kind: [Service] with name: [null] in namespace: [null] failed.
A call is being made to the Kubernetes API.
Have you tried deploying the application to a test cluster created for example by Minikube?
Do you have a sample project that is exhibiting this behavior that I could try?
It works on local Minikube, but checking the .kube/config file on Minikube and in the cluster I see that they are quite different. Locally there is a minikube user with client-certificate and client-key. On the cluster however, there is an user with some token trying to connect.
Unfortunately I can't setup a sample project to test now. I will try to put one up shortly.
Ok, thanks
Here you go @geoand try this one: https://github.com/angheladrianclaudiu/zuul-kubernetes
Great, I'll take a look on Monday most likely, thanks
@angheladrianclaudiu I tried the sample locally and it worked for me without problems.
@angheladrianclaudiu @geoand I have these projects with some extra roles, role bindings and service accounts configurations to be able to interact with the Kube API from inside a pod: https://github.com/Salaboy/s1p_gateway/tree/master/charts/s1p-gateway/templates
Important to notice the deployment.yaml file descriptor that includes a reference to the service account. This is working as expected in different Kubernetes Flavours (GKE, PKS, AWS + Kops)
@spencergibb I think that we can close this issue and open a documentation issue to cover this extra descriptors that are needed.
It is still an issue for me. Tried everything mentioned in the comments. I have created a new issue as this has been closed. #560