spring-cloud-kubernetes官方demo運行實戰

關於spring-cloud-kubernetes

spring-cloud-kubernetes是springcloud官方推出的開源項目,用於將Spring Cloud和Spring Boot應用運行在kubernetes環境,而且提供了通用的接口來調用kubernetes服務,GitHub上官方地址是:https://github.com/spring-cloud/spring-cloud-kubernetesjava

系列文章列表

本文是《spring-cloud-kubernetes實戰系列》的第一篇,全文連接以下:linux

  1. 《spring-cloud-kubernetes官方demo運行實戰》
  2. 《你好spring-cloud-kubernetes》
  3. 《spring-cloud-kubernetes背後的三個關鍵知識點》
  4. 《spring-cloud-kubernetes的服務發現和輪詢實戰(含熔斷)》
  5. 《spring-cloud-kubernetes與SpringCloud Gateway》
  6. 《spring-cloud-kubernetes與k8s的configmap》

官方demo

官方提供了簡單的demo用於快速瞭解spring-cloud-kubernetes,可是成功運行此demo須要作一些設置和修改,這也是此文的意義所在,接下來咱們一塊兒實戰這個demo;git

環境信息

本次實戰的環境和版本信息以下:程序員

  1. 操做系統:CentOS Linux release 7.6.1810
  2. minikube:1.1.1
  3. Java:1.8.0_191
  4. Maven:3.6.0
  5. fabric8-maven-plugin插件:3.5.37
  6. spring-cloud-kubernetes:1.0.1.RELEASE

上面提到的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/releases在這裏插入圖片描述spring

修改maven設置

須要事先修改maven的設置,不然編譯構建的時候會報錯:apache

  1. 打開maven的配置文件settings.xml,完整路徑是apache-maven-3.6.0/conf/settings.xm;
  2. 在settings.xml文件內,找到pluginGroups節點,在裏面增長兩行,修改完成後效果以下:
<pluginGroups>
    <pluginGroup>io.fabric8</pluginGroup>
    <pluginGroup>org.springframework.boot</pluginGroup>
  </pluginGroups>複製代碼
  1. 修改完畢保存退出,maven設置完成;

若是您想了解該錯誤的細節,請參考文章《使用fabric8-maven-plugin插件的錯誤處理(No plugin found for prefix 'fabric8')》json

編譯項目源碼

  1. 將上圖中的源碼下載解壓,獲得一個新的文件夾spring-cloud-kubernetes-1.0.1.RELEASE
  2. 進入文件夾spring-cloud-kubernetes-1.0.1.RELEASE,執行命令mvn clean compile -U,徹底編譯整個工程,因爲要下載大量依賴庫因此較爲耗時,我這裏是二十分鐘以上(這一步不是必須的,看我的愛好吧),構建經過後以下所示:
[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] ------------------------------------------------------------------------
複製代碼
  1. 進入目錄spring-cloud-kubernetes-1.0.1.RELEASE/spring-cloud-kubernetes-examples/kubernetes-hello-world-example,這裏面就是官方的入門demo,執行如下命令開始構建而且會部署到minikube:
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] ------------------------------------------------------------------------複製代碼
  1. 查看服務,已經建立了,類型是NodePort ,而且將8080端口映射到宿主機的30700端口,說明能夠用http://宿主機IP:30700來訪問此服務:
[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複製代碼
  1. 查看部署,發現始終未能進入READY狀態
[root@minikube kubernetes-hello-world-example]# kubectl get deployments
NAME                     READY   UP-TO-DATE   AVAILABLE   AGE
kubernetes-hello-world   0/1     1            0           4m46s複製代碼
  1. 查看pod,發現新建的pod始終未能進入READY狀態
[root@minikube kubernetes-hello-world-example]# kubectl get pods
NAME                                      READY   STATUS    RESTARTS   AGE
kubernetes-hello-world-7578f45c5d-hr4r7   0/1     Running   1          6m複製代碼
  1. 從上面的信息能夠看出,部署雖然已經完成,可是pod是不可用的,訪問網頁試試,以下圖,果真沒法訪問:
    在這裏插入圖片描述

檢查問題

  1. 執行命令kubectl describe pod kubernetes-hello-world-7578f45c5d-hr4r7檢查pod的具體狀況,以下圖紅框所示,兩個探針檢查都失敗了:
    在這裏插入圖片描述
  2. 再看看控制檯輸出的pod基本狀況,裏面有探針的信息,以下圖所示,兩個探針的地址都是/health
    在這裏插入圖片描述
  3. 打開demo的源碼,以下所示,根本就沒有路徑爲/health的服務:
@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的配置,將探針地址改成現有的服務,例如"/",這是個可用的服務;

第一種方法很簡單,留給讀者您來完成吧,咱們來試試第二種:

  1. 執行如下命令,開始編輯deployment:
kubectl edit deployment kubernetes-hello-world複製代碼
  1. 在編輯頁面上找到兩個探針的配置,都從"/health"改爲"/",以下圖兩個紅框所示:
    在這裏插入圖片描述
  2. 修改完畢後,像普通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複製代碼
  1. 訪問地址http://192.168.121.133: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] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException複製代碼

出現上述問題的緣由是maven-checkstyle-plugin插件檢查代碼的style沒有經過,我試過在mvn命令中添加skip參數,也試過在pom.xml中添加maven-checkstyle-plugin節點而且配置爲skip,結果都沒有用,最終用如下方法成功規避了此問題:

  1. 打開pom.xml文件;
  2. 找到節點properties(若是沒有就建立),增長如下三個屬性配置,這樣配置的做用是在style檢查失敗、校驗失敗、單元測試代碼檢查失敗這三種狀況下,都不會致使整個maven構建的失敗:
<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應用怎麼開發;

歡迎關注個人公衆號:程序員欣宸

在這裏插入圖片描述

相關文章
相關標籤/搜索