spring-cloud-kubernetes combat official demo run

About spring-cloud-kubernetes

springcloud-kubernetes is springcloud official launch of the open source project for Spring Cloud Spring Boot and applications running in kubernetes environment, and provides a common interface to call kubernetes services on GitHub official address is: https: // github. com / spring-cloud / spring-cloud-kubernetes

Series list

This article is a "spring-cloud-kubernetes combat series' first article, the full text of the following links:

  1. "Spring-cloud-kubernetes official demo run real"
  2. "Hello spring-cloud-kubernetes"
  3. "Three key knowledge points spring-cloud-kubernetes behind"
  4. "Spring-cloud-kubernetes actual service discovery and polling (including fused)"
  5. 《spring-cloud-kubernetes与SpringCloud Gateway》
  6. "Spring-cloud-kubernetes and k8s of configmap"

The official demo

Official provides a simple demo for a quick overview of spring-cloud-kubernetes, but successfully run this demo needs to do some settings and modifications, and this is the significance of this article, then together we combat this demo;

Environmental Information

The real environmental and version information is as follows:

  1. Operating System: 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 plugin: 3.5.37
  6. spring-cloud-kubernetes:1.0.1.RELEASE

Linux mentioned above, minikube, java, maven, make sure you have all ready on minikube the linux environment installation and startup refer to "Linux Installation Guide minikube" .

Download Source

The official demo includes the entire spring-cloud-kubernetes open source project, so you want to download the entire open-source project, because the trunk submission has been very active, it is best to download a release version, I am here to download the v1.0.1.RELEASE , as shown below , the address is a red box: https: //github.com/spring-cloud/spring-cloud-kubernetes/releases
Here Insert Picture Description

Modify settings maven

Requires prior maven modify settings, or the compiler will complain when the building:

  1. Open the configuration file maven settings.xml, the full path apache-maven-3.6.0 / conf / settings.xm;
  2. In settings.xml file, found pluginGroups node, add two lines which, after editing results are as follows:
<pluginGroups>
    <pluginGroup>io.fabric8</pluginGroup>
    <pluginGroup>org.springframework.boot</pluginGroup>
  </pluginGroups>
  1. Save and exit the modification is completed, maven setup is complete;

If you want to know the details of the error, please refer to the article "Using error handling fabric8-maven-plugin plugin (No plugin found for prefix 'fabric8')" ;

Compile the project source code

  1. The figure above source code download, unzip, get a new folder the Spring-Cloud-Kubernetes-1.0.1.RELEASE ;
  2. Into the folder spring-cloud-kubernetes-1.0.1.RELEASE, execute the command mvn Clean the compile -U , fully compile the entire project, due to be downloaded to rely heavily on libraries and it is more time-consuming, I was here more than 20 minutes (this step is not must see personal hobby), constructed after the adoption of the following:
[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. Enter the directory the Spring-Cloud-Kubernetes-1.0.1.RELEASE / the Spring-Cloud-Kubernetes-examples / Kubernetes-the Hello-world-Example , there is the official entry demo, execute the following command to start building and will be deployed to minikube:
mvn clean package fabric8:deploy -Pkubernetes

After the build and deploy complete, the console outputs the following information:

...
[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. View service, has been created, type NodePort, and maps port 8080 to port 30700 host, indicating that you can use http: // host IP: 30700 to access this service:
[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. View deployment, found that has been unable to enter the READY state :
[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. View pod, discover new pod has been unable to enter the READY state :
[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. As can be seen from the above information, although the deployment has been completed, but the pod is not available, try to access a Web page, as shown below, and she can not access:
    Here Insert Picture Description

    Check for problems

  2. Run kubectl describe pod kubernetes-hello-world -7578f45c5d-hr4r7 Check details pod, as shown in FIG red box, check two probes have failed:
    Here Insert Picture Description
  3. Look substantially console output pod, the inside information of the probe, as shown, two probes are shown below addresses / Health :
    Here Insert Picture Description
  4. Open demo source code, as shown below, there is no path / health services:
@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();
        }
}

Now the truth is out: the pod is deployed to the minikube, configure the probe address / Health , but the service and without this path, probe check has been unable to pass;

Solve the problem

After figuring out the problem can begin to solve the problem, there are two solutions:
first, modify HelloController.java, add a method, the corresponding address / health services;
second, to modify the deployment configuration, probe address to existing services, e.g., "/", which is available on the service;

The first method is very simple, you are left to the reader to complete it, let's try the second:

  1. Execute the following command to start editing deployment:
kubectl edit deployment kubernetes-hello-world
  1. Found on the page layout editing two probes are from "/ Health" to "/", as shown in FIG two red box:
    Here Insert Picture Description
  2. After modification, the same as ordinary operating vim "wq" to save and exit configuration takes effect immediately, wait a minute look at the pod, the pod found the name has changed, and the state has become Ready, prove the old pod has been destroyed, a new the pod is created and tested by the probe:
[root@minikube examples]# kubectl get pods
NAME                                      READY   STATUS    RESTARTS   AGE
kubernetes-hello-world-6c5f75ff74-dnm2q   1/1     Running   0          15s
  1. Access address http://192.168.121.133:30700, normal service (192.168.121.133 is the IP host address), as shown below:
    Here Insert Picture Description

    The official explanation

    The official demo not work properly on minikube, but also our own to modify the source code or configuration, the official demo this should not be found to this problem in README.md documents within kubernetes-hello-world-example project Description, red box shown in the following figure, fabric8 the maven plugin configuration is generated when the probe with the wrong URL, therefore the official suggested we go to modify the deployment configuration of the probe from the address "/ health" to "/ actuator / heath ", this issue has been submitted to the fabric8 community, and posted a link:
    Here Insert Picture Description

    Permissions issue

    We have just seen HelloController.java source, there is also a path "/ services" interface, this interface can be accessed successfully returned in minikube environment, the contents of the current service information minikube environment, but if deployed in a formal kubernetes environment, access to this interface will return the following error:
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"

That the current system: serviceaccount account is not have access to "services" resources through API server, this time the quickest solution is to upgrade the account permissions:

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

Note: The above approach can only be used for development and test environments, do not use in a production environment, the authorization settings in a production environment should reference Kubernetes of RBAC to handle.

Modify the source code encountered an error how to avoid the

If you want to try to modify the source code and deploy up demo, you may encounter the following problems at compile time:

[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

The reason for the above problem is style maven-checkstyle-plugin plugin check code does not pass, I tried to add a skip parameter in the mvn command, also tried to add maven-checkstyle-plugin in pom.xml node and configured to skip, the results are not used, the ultimate success of the following methods to circumvent this problem:

  1. Open the pom.xml file;
  2. Find the node properties (if not created), add the following three attributes configuration, the role of such a configuration is a failure in the style check, the check fails, check the unit test code fails all three cases, it will not cause the entire maven build failure:
<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>

So far, the deployment and operation of official demo are completed, to be springcloud-kubernetes have a preliminary understanding, the next actual combat, we go in-depth understanding springcloud-kubernetes, take a look at how springcloud application on kubernetes development;

I welcome the attention of the public numbers: programmer Chen Xin

Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/bolingcavalry/p/11445732.html