Skip to content

Commit bd87251

Browse files
jcvillalta03Ryan Baxter
authored and
Ryan Baxter
committed
update docs yaml examples, and consistently document code in asciidoc (spring-cloud#280)
1 parent df1d442 commit bd87251

File tree

1 file changed

+30
-42
lines changed

1 file changed

+30
-42
lines changed

README.adoc

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -18,38 +18,35 @@ to fetch the list of the endpoints defined for an application to be load balance
1818

1919
This is something that you get for free just by adding the following dependency inside your project:
2020

21-
```xml
21+
[source,xml]
2222
<dependency>
2323
<groupId>org.springframework.cloud</groupId>
2424
<artifactId>spring-cloud-starter-kubernetes</artifactId>
2525
<version>${latest.version}</version>
2626
</dependency>
27-
```
2827

2928
To enable loading of the `DiscoveryClient`, add `@EnableDiscoveryClient` to the according configuration or application class like this:
3029

31-
```java
30+
[source,java]
3231
@SpringBootApplication
3332
@EnableDiscoveryClient
3433
public class Application {
3534
public static void main(String[] args) {
3635
SpringApplication.run(Application.class, args);
3736
}
3837
}
39-
```
4038

4139
Then you can inject the client in your code simply by:
4240

43-
```java
41+
[source,java]
4442
@Autowired
4543
private DiscoveryClient discoveryClient;
46-
```
4744

4845
If for any reason you need to disable the `DiscoveryClient` you can simply set the following property in `application.properties`:
4946

50-
```
47+
----
5148
spring.cloud.kubernetes.discovery.enabled=false
52-
```
49+
----
5350

5451
Some Spring Cloud components use the `DiscoveryClient` in order to obtain info about the local service instance. For
5552
this to work you need to align the Kubernetes service name with the `spring.application.name` property.
@@ -77,7 +74,7 @@ However, more advanced configuration are possible where multiple ConfigMaps can
7774
This is made possible by the `spring.cloud.kubernetes.config.sources` list.
7875
For example one could define the following ConfigMaps
7976

80-
```yaml
77+
[source,yaml]
8178
spring:
8279
application:
8380
name: cloud-k8s-app
@@ -94,7 +91,6 @@ spring:
9491
# Spring Cloud Kubernetes will lookup a ConfigMap named c3 in namespace n3
9592
- namespace: n3
9693
name: c3
97-
```
9894

9995
In the example above, it `spring.cloud.kubernetes.config.namespace` had not been set,
10096
then the ConfigMap named `c1` would be looked up in the namespace that the application runs
@@ -122,20 +118,19 @@ configuration.
122118

123119
This can be externalized to config map in `yaml` format:
124120

125-
```yaml
121+
[source,yaml]
126122
kind: ConfigMap
127123
apiVersion: v1
128124
metadata:
129125
name: demo
130126
data:
131127
pool.size.core: 1
132128
pool.size.max: 16
133-
```
134129

135130
Individual properties work fine for most cases but sometimes embedded `yaml` is more convenient. In this case we will
136131
use a single property named `application.yaml` to embed our `yaml`:
137132

138-
```yaml
133+
[source,yaml]
139134
kind: ConfigMap
140135
apiVersion: v1
141136
metadata:
@@ -146,11 +141,10 @@ data:
146141
size:
147142
core: 1
148143
max:16
149-
```
150144

151145
The following also works:
152146

153-
```yaml
147+
[source,yaml]
154148
kind: ConfigMap
155149
apiVersion: v1
156150
metadata:
@@ -161,14 +155,13 @@ data:
161155
size:
162156
core: 1
163157
max:16
164-
```
165158

166159
Spring Boot applications can also be configured differently depending on active profiles which will be merged together
167160
when the ConfigMap is read. It is possible to provide different property values for different profiles using an
168161
`application.properties|yaml` property, specifying profile-specific values each in their own document
169162
(indicated by the `---` sequence) as follows:
170163

171-
```yaml
164+
[source,yaml]
172165
kind: ConfigMap
173166
apiVersion: v1
174167
metadata:
@@ -191,22 +184,20 @@ data:
191184
profiles: production
192185
greeting:
193186
message: Say Hello to the Ops
194-
```
195187

196188
In the above case, the configuration loaded into your Spring Application with the `development` profile will be:
197-
```yaml
189+
[source,yaml]
198190
greeting:
199191
message: Say Hello to the Developers
200192
farewell:
201193
message: Say Goodbye to the Developers
202-
```
194+
203195
whereas if the `production` profile is active, the configuration will be:
204-
```yaml
196+
[source,yaml]
205197
greeting:
206198
message: Say Hello to the Ops
207199
farewell:
208200
message: Say Goodbye
209-
```
210201

211202
If both profiles are active, the property which appears last within the configmap will overwrite preceding values.
212203

@@ -215,7 +206,7 @@ To tell to Spring Boot which `profile` should be enabled at bootstrap, a system
215206
command launching your Spring Boot application using an env variable that you will define with the OpenShift
216207
`DeploymentConfig` or Kubernetes `ReplicationConfig` resource file as follows:
217208

218-
```yaml
209+
[source,yaml]
219210
apiVersion: v1
220211
kind: DeploymentConfig
221212
spec:
@@ -228,7 +219,6 @@ spec:
228219
value: /deployments
229220
- name: JAVA_OPTIONS
230221
value: -Dspring.profiles.active=developer
231-
```
232222

233223
**Notes:**
234224
- check the security configuration section, to access config maps from inside a pod you need to have the correct
@@ -266,13 +256,13 @@ If the secrets are found their data is made available to the application.
266256
Let's assume that we have a spring boot application named ``demo`` that uses properties to read its database
267257
configuration. We can create a Kubernetes secret using the following command:
268258

269-
```
259+
----
270260
oc create secret generic db-secret --from-literal=username=user --from-literal=password=p455w0rd
271-
```
261+
----
272262

273263
This would create the following secret (shown using `oc get secrets db-secret -o yaml`):
274264

275-
```yaml
265+
[source,yaml]
276266
apiVersion: v1
277267
data:
278268
password: cDQ1NXcwcmQ=
@@ -286,14 +276,12 @@ metadata:
286276
selfLink: /api/v1/namespaces/default/secrets/db-secret
287277
uid: 63c89263-6099-11e7-b3da-76d6186905a8
288278
type: Opaque
289-
```
290-
291279

292280
Note that the data contains Base64-encoded versions of the literal provided by the create command.
293281

294282
This secret can then be used by your application for example by exporting the secret's value as environment variables:
295283

296-
```yaml
284+
[source,yaml]
297285
apiVersion: v1
298286
kind: Deployment
299287
metadata:
@@ -313,7 +301,6 @@ spec:
313301
secretKeyRef:
314302
name: db-secret
315303
key: password
316-
```
317304

318305
You can select the Secrets to consume in a number of ways:
319306

@@ -381,7 +368,8 @@ Example:
381368

382369
Assuming that the reload feature is enabled with default settings (*`refresh`* mode), the following bean will be refreshed when the config map changes:
383370

384-
```java
371+
[source,java]
372+
----
385373
@Configuration
386374
@ConfigurationProperties(prefix = "bean")
387375
public class MyConfig {
@@ -391,11 +379,12 @@ public class MyConfig {
391379
// getter and setters
392380
393381
}
394-
```
382+
----
395383

396384
A way to see that changes effectively happen is creating another bean that prints the message periodically.
397385

398-
```java
386+
[source,java]
387+
----
399388
@Component
400389
public class MyBean {
401390
@@ -407,19 +396,20 @@ public class MyBean {
407396
System.out.println("The message is: " + config.getMessage());
408397
}
409398
}
410-
```
399+
----
411400

412401
The message printed by the application can be changed using a `ConfigMap` as follows:
413402

414-
```yaml
403+
[source,java]
404+
----
415405
apiVersion: v1
416406
kind: ConfigMap
417407
metadata:
418408
name: reload-example
419409
data:
420410
application.properties: |-
421411
bean.message=Hello World!
422-
```
412+
----
423413

424414
Any change to the property named `bean.message` in the `ConfigMap` associated to the pod will be reflected in the
425415
output. More generally speaking, changes associated to properties prefixed with the value defined by the `prefix`
@@ -465,20 +455,18 @@ about such endpoints.
465455

466456
The implementation is part of the following starter that you can use by adding its dependency to your pom file:
467457

468-
```xml
458+
[source,xml]
469459
<dependency>
470460
<groupId>org.springframework.cloud</groupId>
471461
<artifactId>spring-cloud-starter-kubernetes-netflix</artifactId>
472462
<version>${latest.version}</version>
473463
</dependency>
474-
```
475464

476465
When the list of the endpoints is populated, the Kubernetes client will search the registered endpoints living in
477466
the current namespace/project matching the service name defined using the Ribbon Client annotation:
478467

479-
```java
468+
[source,java]
480469
@RibbonClient(name = "name-service")
481-
```
482470

483471
You can configure Ribbon's behavior by providing properties in your `application.properties` (via your application's
484472
dedicated `ConfigMap`) using the following format: `<name of your service>.ribbon.<Ribbon configuration key>` where:
@@ -542,7 +530,7 @@ The Kubernetes health indicator which is part of the core module exposes the fol
542530
## Namespace
543531
Most 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.
544532
For earlier version it needs to be specified as an env var to the pod. A quick way to do this is:
545-
533+
[source,yaml]
546534
env:
547535
- name: "KUBERNETES_NAMESPACE"
548536
valueFrom:

0 commit comments

Comments
 (0)