本文分享自华为云社区《spring-cloud-kubernetes官方demo运行实战》,作者: 程序员欣宸 。
spring-cloud-kubernetes是springcloud官方推出的开源项目,用于将Spring Cloud和Spring Boot应用运行在kubernetes环境,并且提供了通用的接口来调用kubernetes服务,GitHub上官方地址是:https://github.com/spring-cloud/spring-cloud-kubernetes
官方提供了简单的demo用于快速了解spring-cloud-kubernetes,我们就来一起将此demo源码编译构建,然后在kubernetes环境运行;
本次实战的环境和版本信息如下:
上面提到的linux、minikube、java、maven,请确保已全部准备好,关于linux环境下minikube的安装和启动请参考《Linux安装minikube指南 》
官方demo包含在整个spring-cloud-kubernetes开源项目中,因此要下载整个开源项目,由于主干的提交一直很活跃,因此最好下载个release版本,我这里下载的是v1.0.1.RELEASE,如下图红框所示,地址是:https://github.com/spring-cloud/spring-cloud-kubernetes/releases
需要事先修改maven的设置,否则编译构建的时候会报错:
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-xml"><span style="color:#e2777a"><span style="color:#e2777a"><span style="color:#cccccc"><</span>pluginGroups</span><span style="color:#cccccc">></span></span>
<span style="color:#e2777a"><span style="color:#e2777a"><span style="color:#cccccc"><</span>pluginGroup</span><span style="color:#cccccc">></span></span>io.fabric8<span style="color:#e2777a"><span style="color:#e2777a"><span style="color:#cccccc"></</span>pluginGroup</span><span style="color:#cccccc">></span></span>
<span style="color:#e2777a"><span style="color:#e2777a"><span style="color:#cccccc"><</span>pluginGroup</span><span style="color:#cccccc">></span></span>org.springframework.boot<span style="color:#e2777a"><span style="color:#e2777a"><span style="color:#cccccc"></</span>pluginGroup</span><span style="color:#cccccc">></span></span>
<span style="color:#e2777a"><span style="color:#e2777a"><span style="color:#cccccc"></</span>pluginGroups</span><span style="color:#cccccc">></span></span>
</code></span></span>
如果您想了解该错误的细节,请参考文章《使用fabric8-maven-plugin插件的错误处理(No plugin found for prefix ‘fabric8’)》
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-shell"><span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Reactor Summary <span style="color:#cc99cd">for</span> Spring Cloud Kubernetes <span style="color:#f08d49">1.0</span>.1.RELEASE:
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Dependencies <span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span> SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">0.077</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes <span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span> SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">2.575</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Core <span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span> SUCCESS <span style="color:#cccccc">[</span>01:51 min<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Config <span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span> SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">21.357</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Discovery <span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span>. SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">6.473</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Ribbon <span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span> SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">31.616</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Starter <span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span>. SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">0.558</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Starter :: Config <span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span>. SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">0.569</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Starter :: Ribbon <span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span>. SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">0.595</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Starter :: All <span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span> SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">0.571</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Examples <span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span> SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">0.558</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Examples :: Reload ConfigMap SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">9.077</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Examples :: Hello World <span style="color:#cc99cd">.</span> SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">1.323</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Leader <span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span> SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">7.395</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Examples :: Leader Election SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">0.594</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Istio <span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span>. SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">12.788</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Integration Tests <span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span>. SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">0.574</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Integration Tests :: Simple Core SUCCESS <span style="color:#cccccc">[</span>02:14 min<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Integration Tests :: Simple Configmap SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">0.646</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Integration Tests :: Istio SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">0.623</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Integration Tests :: Discovery Parent SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">0.564</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Integration Tests :: Discovery Service A SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">0.605</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Integration Tests :: Discovery Service B SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">0.625</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Integration Tests :: Discovery Client SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">0.608</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes :: Integration Tests :: Discovery Tests SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">1.440</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Spring Cloud Kubernetes Docs <span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span><span style="color:#cccccc">..</span>. SUCCESS <span style="color:#cccccc">[</span> <span style="color:#f08d49">0.583</span> s<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> ------------------------------------------------------------------------
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> BUILD SUCCESS
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> ------------------------------------------------------------------------
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Total time: 05:53 min
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Finished at: <span style="color:#f08d49">2019</span>-06-08T19:32:19+08:00
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> ------------------------------------------------------------------------
</code></span></span>
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-shell">mvn clean package fabric8:deploy -Pkubernetes
</code></span></span>
构建和部署完成后,控制台输出以下信息:
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-shell"><span style="color:#cccccc">..</span>.
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Installing /usr/local/work/demo/spring-cloud-kubernetes-1.0.1.RELEASE/spring-cloud-kubernetes-examples/kubernetes-hello-world-example/target/kubernetes-hello-world-1.0.1.RELEASE-sources.jar to /root/.m2/repository/org/springframework/cloud/kubernetes-hello-world/1.0.1.RELEASE/kubernetes-hello-world-1.0.1.RELEASE-sources.jar
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> <span style="color:#67cdcc"><<<</span> fabric8-maven-plugin:3.5.37:deploy <span style="color:#cccccc">(</span>default-cli<span style="color:#cccccc">)</span> <span style="color:#67cdcc"><</span> <span style="color:#f08d49">install</span> @ kubernetes-hello-world <span style="color:#67cdcc"><<<</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> --- fabric8-maven-plugin:3.5.37:deploy <span style="color:#cccccc">(</span>default-cli<span style="color:#cccccc">)</span> @ kubernetes-hello-world ---
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> F8: Using Kubernetes at https://192.168.121.133:8443/ <span style="color:#cc99cd">in</span> namespace default with manifest /usr/local/work/demo/spring-cloud-kubernetes-1.0.1.RELEASE/spring-cloud-kubernetes-examples/kubernetes-hello-world-example/target/classes/META-INF/fabric8/kubernetes.yml
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Using namespace: default
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Creating a Service from kubernetes.yml namespace default name kubernetes-hello-world
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Created Service: spring-cloud-kubernetes-examples/kubernetes-hello-world-example/target/fabric8/applyJson/default/service-kubernetes-hello-world.json
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Using namespace: default
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Creating a Deployment from kubernetes.yml namespace default name kubernetes-hello-world
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Created Deployment: spring-cloud-kubernetes-examples/kubernetes-hello-world-example/target/fabric8/applyJson/default/deployment-kubernetes-hello-world.json
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> F8: HINT: Use the <span style="color:#cc99cd">command</span> <span style="color:#7ec699"><span style="color:#7ec699">`</span>kubectl get pods -w<span style="color:#7ec699">`</span></span> to <span style="color:#f08d49">watch</span> your pods start up
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> ------------------------------------------------------------------------
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> BUILD SUCCESS
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> ------------------------------------------------------------------------
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Total time: <span style="color:#f08d49">16.047</span> s
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Finished at: <span style="color:#f08d49">2019</span>-06-08T19:50:50+08:00
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> ------------------------------------------------------------------------
</code></span></span>
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-shell"><span style="color:#cccccc">[</span>root@minikube kubernetes-hello-world-example<span style="color:#cccccc">]</span><span style="color:#999999"># kubectl get services</span>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT<span style="color:#cccccc">(</span>S<span style="color:#cccccc">)</span> AGE
kubernetes ClusterIP <span style="color:#f08d49">10.96</span>.0.1 <span style="color:#67cdcc"><</span>none<span style="color:#67cdcc">></span> <span style="color:#f08d49">443</span>/TCP 10h
kubernetes-hello-world NodePort <span style="color:#f08d49">10.108</span>.214.207 <span style="color:#67cdcc"><</span>none<span style="color:#67cdcc">></span> <span style="color:#f08d49">8080</span>:30700/TCP 4m
</code></span></span>
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-shell"><span style="color:#cccccc">[</span>root@minikube kubernetes-hello-world-example<span style="color:#cccccc">]</span><span style="color:#999999"># kubectl get deployments</span>
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-hello-world <span style="color:#f08d49">0</span>/1 <span style="color:#f08d49">1</span> <span style="color:#f08d49">0</span> 4m46s
</code></span></span>
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-shell"><span style="color:#cccccc">[</span>root@minikube kubernetes-hello-world-example<span style="color:#cccccc">]</span><span style="color:#999999"># kubectl get pods</span>
NAME READY STATUS RESTARTS AGE
kubernetes-hello-world-7578f45c5d-hr4r7 <span style="color:#f08d49">0</span>/1 Running <span style="color:#f08d49">1</span> 6m
</code></span></span>
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-java"><span style="color:#cccccc">@RestController</span>
<span style="color:#cc99cd">public</span> <span style="color:#cc99cd">class</span> <span style="color:#f8c555">HelloController</span> <span style="color:#cccccc">{</span>
<span style="color:#cc99cd">private</span> <span style="color:#cc99cd">static</span> <span style="color:#cc99cd">final</span> <span style="color:#f8c555">Log</span> log <span style="color:#67cdcc">=</span> <span style="color:#f8c555">LogFactory</span><span style="color:#cccccc">.</span><span style="color:#f08d49">getLog</span><span style="color:#cccccc">(</span><span style="color:#f8c555">HelloController</span><span style="color:#cccccc">.</span><span style="color:#cc99cd">class</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#cccccc">@Autowired</span>
<span style="color:#cc99cd">private</span> <span style="color:#f8c555">DiscoveryClient</span> discoveryClient<span style="color:#cccccc">;</span>
<span style="color:#cccccc">@RequestMapping</span><span style="color:#cccccc">(</span><span style="color:#7ec699">"/"</span><span style="color:#cccccc">)</span>
<span style="color:#cc99cd">public</span> <span style="color:#f8c555">String</span> <span style="color:#f08d49">hello</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span> <span style="color:#cccccc">{</span>
<span style="color:#cc99cd">return</span> <span style="color:#7ec699">"Hello World"</span><span style="color:#cccccc">;</span>
<span style="color:#cccccc">}</span>
<span style="color:#cccccc">@RequestMapping</span><span style="color:#cccccc">(</span><span style="color:#7ec699">"/services"</span><span style="color:#cccccc">)</span>
<span style="color:#cc99cd">public</span> <span style="color:#f8c555">List</span><span style="color:#cccccc"><</span><span style="color:#f8c555">String</span><span style="color:#cccccc">></span> <span style="color:#f08d49">services</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span> <span style="color:#cccccc">{</span>
<span style="color:#cc99cd">return</span> <span style="color:#cc99cd">this</span><span style="color:#cccccc">.</span>discoveryClient<span style="color:#cccccc">.</span><span style="color:#f08d49">getServices</span><span style="color:#cccccc">(</span><span style="color:#cccccc">)</span><span style="color:#cccccc">;</span>
<span style="color:#cccccc">}</span>
<span style="color:#cccccc">}</span>
</code></span></span>
现在真相大白了:部署到minikube上的pod,配置了探针地址是/health,但是服务中并没有此路径,因此探针检查一直无法通过;
搞清楚问题之后就可以动手解决问题了,这里有两种解决方式:
第一种,修改HelloController.java,增加一个方法,对应的地址是/health的服务;
第二种,修改deployment的配置,将探针地址改为现有的服务,例如"/",这是个可用的服务;
第一种方法很简单,留给读者您来完成吧,我们来试试第二种:
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-shell">kubectl edit deployment kubernetes-hello-world
</code></span></span>
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-shell"><span style="color:#cccccc">[</span>root@minikube examples<span style="color:#cccccc">]</span><span style="color:#999999"># kubectl get pods</span>
NAME READY STATUS RESTARTS AGE
kubernetes-hello-world-6c5f75ff74-dnm2q <span style="color:#f08d49">1</span>/1 Running <span style="color:#f08d49">0</span> 15s
</code></span></span>
官方的demo无法在minikube上正常运行,还要我们自己去修改配置或者源码,官方的demo不应该会这样,在kubernetes-hello-world-example工程内的README.md文档中发现了对此问题的说明,如下图红框所示,fabric8的maven插件在生成探针配置的是时候配错了URL,因此官方建议我们去修改deployment的配置,将探针的地址从"/health"改为"/actuator/heath",这个问题已经被提交到了fabric8社区,并且贴出了链接:
刚才我们看过了HelloController.java的源码,里面还有个路径为"/services"的接口,在minikube环境下访问此接口可以成功返回,内容是当前minikube环境的服务信息,但是如果部署在正式的kubernetes环境,访问此接口会返回以下错误:
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-shell">Message: Forbidden<span style="color:#67cdcc">!</span>Configured <span style="color:#f08d49">service</span> account doesn't have access. Service account may have been revoked. services is forbidden: User <span style="color:#7ec699">"system:serviceaccount:default:default"</span> cannot list resource <span style="color:#7ec699">"services"</span> <span style="color:#cc99cd">in</span> API group <span style="color:#7ec699">""</span> <span style="color:#cc99cd">in</span> the namespace <span style="color:#7ec699">"default"</span>
</code></span></span>
也就是说当前的system:serviceaccount账号是没有权限通过API server访问"services"资源的,此时最快的解决办法是提升账号权限:
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-shell">kubectl create clusterrolebinding permissive-binding <span style="color:#cccccc">\</span>
--clusterrole<span style="color:#67cdcc">=</span>cluster-admin <span style="color:#cccccc">\</span>
--user<span style="color:#67cdcc">=</span>admin <span style="color:#cccccc">\</span>
--user<span style="color:#67cdcc">=</span>kubelet <span style="color:#cccccc">\</span>
--group<span style="color:#67cdcc">=</span>system:serviceaccounts
</code></span></span>
注意:以上办法只能用于开发和测试环境,不要用在生产环境,在生产环境应该参考Kubernetes的RBAC授权相关设置来处理。
如果您想尝试修改demo的源码并且部署上去,在编译阶段可能遇到以下问题:
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-shell"><span style="color:#cccccc">[</span>root@minikube kubernetes-hello-world-example<span style="color:#cccccc">]</span><span style="color:#999999"># mvn clean package fabric8:deploy -Pkubernetes</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Scanning <span style="color:#cc99cd">for</span> projects<span style="color:#cccccc">..</span>.
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> ----------<span style="color:#67cdcc"><</span> org.springframework.cloud:kubernetes-hello-world <span style="color:#67cdcc">></span>----------
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Building Spring Cloud Kubernetes :: Examples :: Hello World <span style="color:#f08d49">1.0</span>.1.RELEASE
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> --------------------------------<span style="color:#cccccc">[</span> jar <span style="color:#cccccc">]</span>---------------------------------
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> --- maven-clean-plugin:2.5:clean <span style="color:#cccccc">(</span>default-clean<span style="color:#cccccc">)</span> @ kubernetes-hello-world ---
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Deleting /usr/local/work/demo/spring-cloud-kubernetes-1.0.1.RELEASE/spring-cloud-kubernetes-examples/kubernetes-hello-world-example/target
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> --- maven-checkstyle-plugin:3.0.0:check <span style="color:#cccccc">(</span>checkstyle-validation<span style="color:#cccccc">)</span> @ kubernetes-hello-world ---
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> 开始检查……
<span style="color:#cccccc">[</span>ERROR<span style="color:#cccccc">]</span> /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: 当前行匹配非法表达式: <span style="color:#7ec699">'Trailing whitespace'</span>。 <span style="color:#cccccc">[</span>Regexp<span style="color:#cccccc">]</span>
检查完成。
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> ------------------------------------------------------------------------
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> BUILD FAILURE
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> ------------------------------------------------------------------------
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Total time: <span style="color:#f08d49">2.976</span> s
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> Finished at: <span style="color:#f08d49">2019</span>-06-08T22:15:37+08:00
<span style="color:#cccccc">[</span>INFO<span style="color:#cccccc">]</span> ------------------------------------------------------------------------
<span style="color:#cccccc">[</span>ERROR<span style="color:#cccccc">]</span> Failed to execute goal org.apache.maven.plugins:maven-checkstyle-plugin:3.0.0:check <span style="color:#cccccc">(</span>checkstyle-validation<span style="color:#cccccc">)</span> on project kubernetes-hello-world: Failed during checkstyle execution: There is <span style="color:#f08d49">1</span> error reported by Checkstyle <span style="color:#f08d49">8.12</span> with checkstyle.xml ruleset. -<span style="color:#67cdcc">></span> <span style="color:#cccccc">[</span>Help <span style="color:#f08d49">1</span><span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>ERROR<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>ERROR<span style="color:#cccccc">]</span> To see the full stack trace of the errors, re-run Maven with the -e switch.
<span style="color:#cccccc">[</span>ERROR<span style="color:#cccccc">]</span> Re-run Maven using the -X switch to <span style="color:#cc99cd">enable</span> full debug logging.
<span style="color:#cccccc">[</span>ERROR<span style="color:#cccccc">]</span>
<span style="color:#cccccc">[</span>ERROR<span style="color:#cccccc">]</span> For <span style="color:#f08d49">more</span> information about the errors and possible solutions, please <span style="color:#cc99cd">read</span> the following articles:
<span style="color:#cccccc">[</span>ERROR<span style="color:#cccccc">]</span> <span style="color:#cccccc">[</span>Help <span style="color:#f08d49">1</span><span style="color:#cccccc">]</span> http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
</code></span></span>
出现上述问题的原因是maven-checkstyle-plugin插件检查代码的style没有通过,我试过在mvn命令中添加skip参数,也试过在pom.xml中添加maven-checkstyle-plugin节点并且配置为skip,结果都没有用,最终用以下方法成功规避了此问题:
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-shell"><span style="color:#67cdcc"><</span>properties<span style="color:#67cdcc">></span>
<span style="color:#67cdcc"><</span>maven-checkstyle-plugin.failsOnError<span style="color:#67cdcc">></span>false<span style="color:#67cdcc"><</span>/maven-checkstyle-plugin.failsOnError<span style="color:#67cdcc">></span>
<span style="color:#67cdcc"><</span>maven-checkstyle-plugin.failsOnViolation<span style="color:#67cdcc">></span>false<span style="color:#67cdcc"><</span>/maven-checkstyle-plugin.failsOnViolation<span style="color:#67cdcc">></span>
<span style="color:#67cdcc"><</span>maven-checkstyle-plugin.includeTestSourceDirectory<span style="color:#67cdcc">></span>false<span style="color:#67cdcc"><</span>/maven-checkstyle-plugin.includeTestSourceDirectory<span style="color:#67cdcc">></span><span style="color:#67cdcc"><</span>/properties<span style="color:#67cdcc">></span>
</code></span></span>
至此,官方demo的部署和运行都完成了,对spring-cloud-kubernetes算是有了初步认识,接下来的实战中,我们一起去深入的了解spring-cloud-kubernetes,看看kubernetes上的springcloud应用怎么开发;
感谢jason9211的提醒,如果读者您也遇到类似问题,请参考如下:
<span style="background-color:#2d2d2d"><span style="color:#cccccc"><code class="language-shell">如果在deploy时遇到 “the server could not <span style="color:#f08d49">find</span> the requested resource”问题,
建议把fabric8.maven.plugin的版本升级到4.2.0即可。
<span style="color:#67cdcc"><</span>fabric8.maven.plugin.version<span style="color:#67cdcc">></span><span style="color:#f08d49">4.2</span>.<span style="color:#67cdcc"><span style="color:#cc99cd"><strong>0</strong></span><</span>/fabric8.maven.plugin.version<span style="color:#67cdcc">></span></code></span></span>
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://huaweicloud.blog.csdn.net/article/details/123567135
内容来源于网络,如有侵权,请联系作者删除!