spring-cloud-kubernetes是springcloud官方推出的開源項目,用於將Spring Cloud和Spring Boot應用運行在kubernetes環境,而且提供了通用的接口來調用kubernetes服務,GitHub上官方地址是:https://github.com/spring-cloud/spring-cloud-kubernetesjava
本文是《spring-cloud-kubernetes實戰系列》的第一篇,全文連接以下:linux
官方提供了簡單的demo用於快速瞭解spring-cloud-kubernetes,可是成功運行此demo須要作一些設置和修改,這也是此文的意義所在,接下來咱們一塊兒實戰這個demo;git
本次實戰的環境和版本信息以下:程序員
上面提到的linux、minikube、java、maven,請確保已所有準備好,關於linux環境下minikube的安裝和啓動請參考《Linux安裝minikube指南 》。github
官方demo包含在整個spring-cloud-kubernetes開源項目中,所以要下載整個開源項目,因爲主幹的提交一直很活躍,所以最好下載個release版本,我這裏下載的是v1.0.1.RELEASE,以下圖紅框所示,地址是:https://github.com/spring-cloud/spring-cloud-kubernetes/releasesspring
須要事先修改maven的設置,不然編譯構建的時候會報錯:apache
<pluginGroups>
<pluginGroup>io.fabric8</pluginGroup>
<pluginGroup>org.springframework.boot</pluginGroup>
</pluginGroups>複製代碼
若是您想了解該錯誤的細節,請參考文章《使用fabric8-maven-plugin插件的錯誤處理(No plugin found for prefix 'fabric8')》;json
[INFO] Reactor Summary for Spring Cloud Kubernetes 1.0.1.RELEASE:
[INFO]
[INFO] Spring Cloud Kubernetes :: Dependencies ............ SUCCESS [ 0.077 s]
[INFO] Spring Cloud Kubernetes ............................ SUCCESS [ 2.575 s]
[INFO] Spring Cloud Kubernetes :: Core .................... SUCCESS [01:51 min]
[INFO] Spring Cloud Kubernetes :: Config .................. SUCCESS [ 21.357 s]
[INFO] Spring Cloud Kubernetes :: Discovery ............... SUCCESS [ 6.473 s]
[INFO] Spring Cloud Kubernetes :: Ribbon .................. SUCCESS [ 31.616 s]
[INFO] Spring Cloud Kubernetes :: Starter ................. SUCCESS [ 0.558 s]
[INFO] Spring Cloud Kubernetes :: Starter :: Config ....... SUCCESS [ 0.569 s]
[INFO] Spring Cloud Kubernetes :: Starter :: Ribbon ....... SUCCESS [ 0.595 s]
[INFO] Spring Cloud Kubernetes :: Starter :: All .......... SUCCESS [ 0.571 s]
[INFO] Spring Cloud Kubernetes :: Examples ................ SUCCESS [ 0.558 s]
[INFO] Spring Cloud Kubernetes :: Examples :: Reload ConfigMap SUCCESS [ 9.077 s]
[INFO] Spring Cloud Kubernetes :: Examples :: Hello World . SUCCESS [ 1.323 s]
[INFO] Spring Cloud Kubernetes :: Leader .................. SUCCESS [ 7.395 s]
[INFO] Spring Cloud Kubernetes :: Examples :: Leader Election SUCCESS [ 0.594 s]
[INFO] Spring Cloud Kubernetes :: Istio ................... SUCCESS [ 12.788 s]
[INFO] Spring Cloud Kubernetes :: Integration Tests ....... SUCCESS [ 0.574 s]
[INFO] Spring Cloud Kubernetes :: Integration Tests :: Simple Core SUCCESS [02:14 min]
[INFO] Spring Cloud Kubernetes :: Integration Tests :: Simple Configmap SUCCESS [ 0.646 s]
[INFO] Spring Cloud Kubernetes :: Integration Tests :: Istio SUCCESS [ 0.623 s]
[INFO] Spring Cloud Kubernetes :: Integration Tests :: Discovery Parent SUCCESS [ 0.564 s]
[INFO] Spring Cloud Kubernetes :: Integration Tests :: Discovery Service A SUCCESS [ 0.605 s]
[INFO] Spring Cloud Kubernetes :: Integration Tests :: Discovery Service B SUCCESS [ 0.625 s]
[INFO] Spring Cloud Kubernetes :: Integration Tests :: Discovery Client SUCCESS [ 0.608 s]
[INFO] Spring Cloud Kubernetes :: Integration Tests :: Discovery Tests SUCCESS [ 1.440 s]
[INFO] Spring Cloud Kubernetes Docs ....................... SUCCESS [ 0.583 s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 05:53 min
[INFO] Finished at: 2019-06-08T19:32:19+08:00
[INFO] ------------------------------------------------------------------------
複製代碼
mvn clean package fabric8:deploy -Pkubernetes複製代碼
構建和部署完成後,控制檯輸出如下信息:vim
...
[INFO] 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
[INFO]
[INFO] <<< fabric8-maven-plugin:3.5.37:deploy (default-cli) < install @ kubernetes-hello-world <<<
[INFO]
[INFO]
[INFO] --- fabric8-maven-plugin:3.5.37:deploy (default-cli) @ kubernetes-hello-world ---
[INFO] F8: Using Kubernetes at https://192.168.121.133:8443/ in 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
[INFO] Using namespace: default
[INFO] Creating a Service from kubernetes.yml namespace default name kubernetes-hello-world
[INFO] Created Service: spring-cloud-kubernetes-examples/kubernetes-hello-world-example/target/fabric8/applyJson/default/service-kubernetes-hello-world.json
[INFO] Using namespace: default
[INFO] Creating a Deployment from kubernetes.yml namespace default name kubernetes-hello-world
[INFO] Created Deployment: spring-cloud-kubernetes-examples/kubernetes-hello-world-example/target/fabric8/applyJson/default/deployment-kubernetes-hello-world.json
[INFO] F8: HINT: Use the command `kubectl get pods -w` to watch your pods start up
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 16.047 s
[INFO] Finished at: 2019-06-08T19:50:50+08:00
[INFO] ------------------------------------------------------------------------複製代碼
[root@minikube kubernetes-hello-world-example]# kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 10h
kubernetes-hello-world NodePort 10.108.214.207 <none> 8080:30700/TCP 4m複製代碼
[root@minikube kubernetes-hello-world-example]# kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-hello-world 0/1 1 0 4m46s複製代碼
[root@minikube kubernetes-hello-world-example]# kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-hello-world-7578f45c5d-hr4r7 0/1 Running 1 6m複製代碼
@RestController
public class HelloController {
private static final Log log = LogFactory.getLog(HelloController.class);
@Autowired
private DiscoveryClient discoveryClient;
@RequestMapping("/")
public String hello() {
return "Hello World";
}
@RequestMapping("/services")
public List<String> services() {
return this.discoveryClient.getServices();
}
}複製代碼
如今真相大白了:部署到minikube上的pod,配置了探針地址是/health,可是服務中並無此路徑,所以探針檢查一直沒法經過;app
搞清楚問題以後就能夠動手解決問題了,這裏有兩種解決方式:第一種,修改HelloController.java,增長一個方法,對應的地址是/health的服務;第二種,修改deployment的配置,將探針地址改成現有的服務,例如"/",這是個可用的服務;
第一種方法很簡單,留給讀者您來完成吧,咱們來試試第二種:
kubectl edit deployment kubernetes-hello-world複製代碼
[root@minikube examples]# kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-hello-world-6c5f75ff74-dnm2q 1/1 Running 0 15s複製代碼
官方的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] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException複製代碼
出現上述問題的緣由是maven-checkstyle-plugin插件檢查代碼的style沒有經過,我試過在mvn命令中添加skip參數,也試過在pom.xml中添加maven-checkstyle-plugin節點而且配置爲skip,結果都沒有用,最終用如下方法成功規避了此問題:
<properties>
<maven-checkstyle-plugin.failsOnError>false</maven-checkstyle-plugin.failsOnError>
<maven-checkstyle-plugin.failsOnViolation>false</maven-checkstyle-plugin.failsOnViolation>
<maven-checkstyle-plugin.includeTestSourceDirectory>false</maven-checkstyle-plugin.includeTestSourceDirectory></properties>複製代碼
至此,官方demo的部署和運行都完成了,對spring-cloud-kubernetes算是有了初步認識,接下來的實戰中,咱們一塊兒去深刻的瞭解spring-cloud-kubernetes,看看kubernetes上的springcloud應用怎麼開發;