在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):fabric8io/spring-cloud-kubernetes开源软件地址(OpenSource Url):https://github.com/fabric8io/spring-cloud-kubernetes开源编程语言(OpenSource Language):Java 88.1%开源软件介绍(OpenSource Introduction):Spring Cloud Incubator.This project has moved to theSpring Cloud KubernetesSpring Cloud integration with Kubernetes Features
DiscoveryClient for KubernetesThis project provides an implementation of Discovery Client for Kubernetes. This allows you to query Kubernetes endpoints (see services) by name. This is something that you get for free just by adding the following dependency inside your project: <dependency>
<groupId>io.fabric8</groupId>
<artifactId>spring-cloud-starter-kubernetes</artifactId>
<version>${latest.version}</version>
</dependency> Then you can inject the client in your cloud simply by: @Autowire
private DiscoveryClient discoveryClient; If for any reason you need to disable the
Some spring cloud components use the Kubernetes PropertySourceThe most common approach to configure your spring boot application is to edit the ConfigMap PropertySourceKubernetes has the notion of ConfigMap for passing configuration to the application. This project provides integration with The
Example: Let's assume that we have a spring boot application named
This can be externalized to config map in yaml format: kind: ConfigMap
apiVersion: v1
metadata:
name: demo
data:
pool.size.core: 1
pool.size.max: 16 Individual properties work fine for most cases but sometimes we yaml is more convinient. In this case we will use a single property named kind: ConfigMap
apiVersion: v1
metadata:
name: demo
data:
application.yaml: |-
pool:
size:
core: 1
max:16 Notes:
Secrets PropertySourceKubernetes has the notion of Secrets for storing sensitive data such as password, OAuth tokens, etc. This project provides integration with The
Please note that by default, consuming Secrets via API (points 2 and 3 above) is not enabled. If the secrets are found theirs data is made available to the application. Example: Let's assume that we have a spring boot application named
This can be externalized to Secrets in yaml format:
You can select the Secrets to consume in a number of ways:
Properties:
Notes:
PropertySource ReloadSome applications may need to detect changes on external property sources and update their internal status to reflect the new configuration. The reload feature of Spring Cloud Kubernetes is able to trigger an application reload when a related ConfigMap or Secret change. This feature is disabled by default and can be enabled using the configuration property The following levels of reload are supported (property
Example: Assuming that the reload feature is enabled with default settings (refresh mode), the following bean will be refreshed when the config map changes: @Configuration
@ConfigurationProperties(prefix = "bean")
public class MyConfig {
private String message = "a message that can be changed live";
// getter and setters
} A way to see that changes effectively happen is creating another bean that prints the message periodically. @Component
public class MyBean {
@Autowired
private MyConfig config;
@Scheduled(fixedDelay = 5000)
public void hello() {
System.out.println("The message is: " + config.getMessage());
}
} The message printed by the application can be changed using a config map like the following one: apiVersion: v1
kind: ConfigMap
metadata:
name: reload-example
data:
application.properties: |-
bean.message=Hello World! Any change to the property named The full example is available in spring-cloud-kubernetes-reload-example. The reload feature supports two operating modes:
Properties:
Notes:
Pod Health IndicatorSpring Boot uses HealthIndicator to expose info about the health of an application. That makes it really useful for exposing health related information to the user and are also a good fit for use as readiness probes. The Kubernetes health indicator which is part of the core modules exposes the following info:
TransparencyAll of the features described above will work equally fine regardless of wether our application is inside Kubernetes or not. This is really helpful for development and troubleshooting. Kubernetes Profile AutoconfigurationWhen the application is run inside Kubernetes a profile named Ribbon discovery in KubernetesA Kubernetes based <dependency>
<groupId>io.fabric8</groupId>
<artifactId>spring-cloud-starter-kubernetes-netflix</artifactId>
<version>${latest.version></version>
</dependency> The ribbon discovery client can be disabled by setting By default the client will detect all endpoints with the configured client name that lives in the current namespace. If the endpoint contains multiple ports, the first port will be used. To fine tune the name of the desired port (if the service is a multiport service) or fine tune the namespace you can use one of the following properties.
Examples that are using this module for ribbon discovery are: Zipkin discovery in KubernetesZipkin is a distributed tracing system and it is also supported by Sleuth. Discovery of the services required by Zipkin (e.g. <dependency>
<groupId>io.fabric8</groupId>
<artifactId>spring-cloud-starter-kubernetes-zipkin</artifactId>
<version>${latest.version></version>
</dependency> This works as an extension of spring-cloud-sleuth-zipkin. Examples of application that are using Zipkin discovery in Kubernetes: ConfigMap Archaius BridgeSection ConfigMap PropertySource provides a brief explanation on how to configure spring boot application via ConfigMap. This approach will aid in creating the configuration properties objects that will be passed in our application. If our application is using Archaius it will indirectly benefit by it. An alternative approach that provides more direct Archaius support without getting in the way of spring configuration properties by using spring-cloud-kubernetes-archaius that is part of the Netflix starter. This module allows you to annotate your application with the TroubleshootingNamespaceMost of the components provided in this project need to know the namespace. For Kubernetes (1.3+) the namespace is made available to pod as part of the service account secret and automatically detected by the client. For earlier version it needs to be specified as an env var to the pod. A quick way to do this is:
Service AccountFor distros of Kubernetes that support more fine-grained role-based access within the cluster, you need to make sure a pod that runs with spring-cloud-kubernetes has access to the Kubernetes API. For example, OpenShift has very comprehensive security measures that are on by default (typically) in a shared cluster. For any service accounts you assign to a deployment/pod, you need to make sure it has the correct roles. For example, you can add
BuildingYou can just use maven to build it from sources:
UsageThe project provides a "starter" module, so you just need to add the following dependency in your project. <dependency>
<groupId>io.fabric8</groupId>
<artifactId>spring-cloud-starter-kubernetes</artifactId>
<version>x.y.z</version>
</dependency> |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论