想了解 spring-cloud-kubernetes,那就先來實戰一把官方demo

華為雲開發者社群發表於2022-03-18
摘要:官方提供了簡單的demo用於快速瞭解spring-cloud-kubernetes,我們就來一起將此demo原始碼編譯構建,然後在kubernetes環境執行。

本文分享自華為雲社群《spring-cloud-kubernetes官方demo執行實戰》,作者: 程式設計師欣宸 。

關於spring-cloud-kubernetes

spring-cloud-kubernetes是spring cloud官方推出的開源專案,用於將Spring Cloud和Spring Boot應用執行在kubernetes環境,並且提供了通用的介面來呼叫kubernetes服務,GitHub上官方地址是:https://github.com/spring-cloud/spring-cloud-kubernetes

官方demo

官方提供了簡單的demo用於快速瞭解spring-cloud-kubernetes,我們就來一起將此demo原始碼編譯構建,然後在kubernetes環境執行;

環境資訊

本次實戰的環境和版本資訊如下:

  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指南 》

下載原始碼

官方demo包含在整個spring-cloud-kubernetes開源專案中,因此要下載整個開源專案,由於主幹的提交一直很活躍,因此最好下載個release版本,我這裡下載的是v1.0.1.RELEASE,如下圖紅框所示,地址是:https://github.com/spring-cloud/spring-cloud-kubernetes/releases

想了解 spring-cloud-kubernetes,那就先來實戰一把官方demo

修改maven設定

需要事先修改maven的設定,否則編譯構建的時候會報錯:

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>

3、修改完畢儲存退出,maven設定完成;

如果您想了解該錯誤的細節,請參考文章《使用fabric8-maven-plugin外掛的錯誤處理(No plugin found for prefix ‘fabric8’)》

編譯專案原始碼

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] ------------------------------------------------------------------------

3、進入目錄spring-cloud-kubernetes-1.0.1.RELEASE/spring-cloud-kubernetes-examples/kubernetes-hello-world-example,這裡面就是官方的入門demo,執行以下命令開始構建並且會部署到minikube:

mvn clean package fabric8:deploy -Pkubernetes

構建和部署完成後,控制檯輸出以下資訊:

...
[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] ------------------------------------------------------------------------

4、檢視服務,已經建立了,型別是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

5、檢視部署,發現始終未能進入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

6、檢視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

7、從上面的資訊可以看出,部署雖然已經完成,但是pod是不可用的,訪問網頁試試,如下圖,果然無法訪問:

想了解 spring-cloud-kubernetes,那就先來實戰一把官方demo

檢查問題

1、執行命令kubectl describe pod kubernetes-hello-world-7578f45c5d-hr4r7檢查pod的具體情況,如下圖紅框所示,兩個探針檢查都失敗了:

想了解 spring-cloud-kubernetes,那就先來實戰一把官方demo

2、再看看控制檯輸出的pod基本情況,裡面有探針的資訊,如下圖所示,兩個探針的地址都是/health:

想了解 spring-cloud-kubernetes,那就先來實戰一把官方demo

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,但是服務中並沒有此路徑,因此探針檢查一直無法通過;

解決問題

搞清楚問題之後就可以動手解決問題了,這裡有兩種解決方式:
第一種,修改HelloController.java,增加一個方法,對應的地址是/health的服務;
第二種,修改deployment的配置,將探針地址改為現有的服務,例如"/",這是個可用的服務;

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

1、執行以下命令,開始編輯deployment:

kubectl edit deployment kubernetes-hello-world

2、在編輯頁面上找到兩個探針的配置,都從"/health"改成"/",如下圖兩個紅框所示:

想了解 spring-cloud-kubernetes,那就先來實戰一把官方demo

3、修改完畢後,像普通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

4、訪問地址http://192.168.121.133:30700 ,服務正常(192.168.121.133是宿主機IP地址),如下圖:

想了解 spring-cloud-kubernetes,那就先來實戰一把官方demo

官方解釋

官方的demo無法在minikube上正常執行,還要我們自己去修改配置或者原始碼,官方的demo不應該會這樣,在kubernetes-hello-world-example工程內的README.md文件中發現了對此問題的說明,如下圖紅框所示,fabric8的maven外掛在生成探針配置的是時候配錯了URL,因此官方建議我們去修改deployment的配置,將探針的地址從"/health"改為"/actuator/heath",這個問題已經被提交到了fabric8社群,並且貼出了連結:

想了解 spring-cloud-kubernetes,那就先來實戰一把官方demo

許可權問題

剛才我們看過了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應用怎麼開發;

問題處理

感謝jason9211的提醒,如果讀者您也遇到類似問題,請參考如下:

如果在deploy時遇到 “the server could not find the requested resource”問題,
建議把fabric8.maven.plugin的版本升級到4.2.0即可。 
<fabric8.maven.plugin.version>4.2.0</fabric8.maven.plugin.version>

 

點選關注,第一時間瞭解華為雲新鮮技術~

相關文章