spring-cloud-kubernetes官方demo运行实战 (3)

搞清楚问题之后就可以动手解决问题了,这里有两种解决方式:
第一种,修改HelloController.java,增加一个方法,对应的地址是/health的服务;
第二种,修改deployment的配置,将探针地址改为现有的服务,例如"http://www.likecs.com/",这是个可用的服务;

第一种方法很简单,留给读者您来完成吧,我们来试试第二种:

执行以下命令,开始编辑deployment:

kubectl edit deployment kubernetes-hello-world

在编辑页面上找到两个探针的配置,都从"/health"改成"http://www.likecs.com/",如下图两个红框所示:

在这里插入图片描述

修改完毕后,像普通vim操作一样"wq"保存退出,配置会立即生效,稍等一会儿再看pod情况,发现pod的name已经变了,并且状态已经成为Ready,证明旧的pod已经销毁,新的pod被创建并且探针测试通过:

[root@minikube examples]# kubectl get pods NAME READY STATUS RESTARTS AGE kubernetes-hello-world-6c5f75ff74-dnm2q 1/1 Running 0 15s

访问地址:30700 ,服务正常(192.168.121.133是宿主机IP地址),如下图:

在这里插入图片描述

官方解释

官方的demo无法在minikube上正常运行,还要我们自己去修改配置或者源码,官方的demo不应该会这样,在kubernetes-hello-world-example工程内的README.md文档中发现了对此问题的说明,如下图红框所示,fabric8的maven插件在生成探针配置的是时候配错了URL,因此官方建议我们去修改deployment的配置,将探针的地址从"/health"改为"/actuator/heath",这个问题已经被提交到了fabric8社区,并且贴出了链接:

在这里插入图片描述

权限问题 刚才我们看过了HelloController.java的源码,里面还有个路径为"/services"的接口,在minikube环境下访问此接口可以成功返回,内容是当前minikube环境的服务信息,但是如果部署在正式的kubernetes环境,访问此接口会返回以下错误:

Message: Forbidden!Configured service account doesn't have access. Service account may have been revoked. services is forbidden: User "system:serviceaccount:default:default" cannot list resource "services" in API group "" in the namespace "default"

也就是说当前的system:serviceaccount账号是没有权限通过API server访问"services"资源的,此时最快的解决办法是提升账号权限:

kubectl create clusterrolebinding permissive-binding \ --clusterrole=cluster-admin \ --user=admin \ --user=kubelet \ --group=system:serviceaccounts

注意:以上办法只能用于开发和测试环境,不要用在生产环境,在生产环境应该参考Kubernetes的RBAC授权相关设置来处理。

修改源码时遇到的错误怎么规避

如果您想尝试修改demo的源码并且部署上去,在编译阶段可能遇到以下问题:

[root@minikube kubernetes-hello-world-example]# mvn clean package fabric8:deploy -Pkubernetes [INFO] Scanning for projects... [INFO] [INFO] ----------< org.springframework.cloud:kubernetes-hello-world >---------- [INFO] Building Spring Cloud Kubernetes :: Examples :: Hello World 1.0.1.RELEASE [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ kubernetes-hello-world --- [INFO] Deleting /usr/local/work/demo/spring-cloud-kubernetes-1.0.1.RELEASE/spring-cloud-kubernetes-examples/kubernetes-hello-world-example/target [INFO] [INFO] --- maven-checkstyle-plugin:3.0.0:check (checkstyle-validation) @ kubernetes-hello-world --- [INFO] 开始检查…… [ERROR] /usr/local/work/demo/spring-cloud-kubernetes-1.0.1.RELEASE/spring-cloud-kubernetes-examples/kubernetes-hello-world-example/src/main/java/org/springframework/cloud/kubernetes/examples/HelloController.java:33: 当前行匹配非法表达式: 'Trailing whitespace'。 [Regexp] 检查完成。 [INFO] ------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.976 s [INFO] Finished at: 2019-06-08T22:15:37+08:00 [INFO] ------------------------------------------------------------------------ [ERROR] Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:3.0.0:check (checkstyle-validation) on project kubernetes-hello-world: Failed during checkstyle execution: There is 1 error reported by Checkstyle 8.12 with checkstyle.xml ruleset. -> [Help 1] [ERROR] [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. [ERROR] Re-run Maven using the -X switch to enable full debug logging. [ERROR] [ERROR] For more information about the errors and possible solutions, please read the following articles: [ERROR] [Help 1]

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wspgfy.html