Spring Boot Admin On Kubernetes

在目前的部署模式下,通常会将Spring Boot项目部署在Kubernetes容器中; 今天,我们就来细说一下,如何使用Spring Boot Admin监控在Kubernetes上运行的Spring Boot应用程序。

kubernetes下部署的架构往往是这样的: 服务employee-service部署在命名空间a中,department-service部署在命名空间b中,而organization-service部署在命名空间c中, Spring Boot Admin也使用Spring Boot启动,并默认部署在命名空间中。 此时,我们就可以通过kubernetesApi对服务进行管理和监控。 PS:图是我抄的😜

启动Spring Boot Admin Server

引入相关依赖

在SBA中,已经集成了kubernetes,如果需要通过kubernetes来发现服务,我们还需要把kubernetes-client包加入到项目中,SpringCloud已经为我们提供了spring-cloud-starter-kubernetes-client

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?xml version="1.0" encoding="utf-8" ?>
<project>
  <properties>
    <spring-boot.version>2.6.1</spring-boot.version>
    <spring-cloud.version>2021.0.0</spring-cloud.version>
    <!-- SBA 的最新版本 实际情况 和 spring boot 的版本号应该一致,目前没有发现不兼容的情况  -->
    <spring-boot-admin.version>2.5.5</spring-boot-admin.version>
  </properties>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${spring-boot.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>de.codecentric</groupId>
        <artifactId>spring-boot-admin-dependencies</artifactId>
        <version>${spring-boot-admin.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>${spring-cloud.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>de.codecentric</groupId>
      <artifactId>spring-boot-admin-server-cloud</artifactId>
    </dependency>
    <dependency>
      <groupId>de.codecentric</groupId>
      <artifactId>spring-boot-admin-server-ui</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-kubernetes-client</artifactId>
    </dependency>
  </dependencies>
</project>

启动类配置

  • @EnableAdminServer 启用SBA Server服务
  • EnableDiscoveryClient 启用服务发现的能力
  • EnableScheduling 启用定时任务,这个很关键,不然你会发现启动的服务,无法在SBA中显示
1
2
3
4
5
6
7
8
9
@SpringBootApplication
@EnableAdminServer // 启用 SBA Server 服务
@EnableDiscoveryClient // 启用服务发现
@EnableScheduling // 启用定时器
public class OpsAdminCloudServerApplication {
	public static void main(String[] args) {
		SpringApplication.run(OpsAdminCloudServerApplication.class, args);
	}
}

Spring Boot Admin Server 配置

既然我们使用kubernetes来发现服务,那么我们就需要对kubernetes的属性进行配置

  • spring.cloud.kubernetes.discovery.service-labels 通过 Servicesmeta 中匹配对应的标记,不满足的则会被排除
  • spring.cloud.kubernetes.discovery.primary-port-name 当给 NodePort 指定多个多个端口时,通过name来确认使用哪个端口作为主端口
  • spring.cloud.kubernetes.client.namespace 指定服务所在的命名空间
  • spring.cloud.kubernetes.discovery.all-namespaces 当然,如果你不想指定命名空间,也可以设置该属性为true,程序会找到所有命名空间下满足service-labels的实例
  • spring.boot.admin.discovery.converter.management-context-path 如果实例的actuator地址被修改,需要配置该属性,默认值 /actuator; 该配置为全局配置,假设每个服务都有自己的context-path,需要编辑kubernetes文件,metadata.labels中配置management.context-path
  • spring.boot.admin.discovery.converter.health-endpoint-path 如果实例的health地址被修改,需要配置该属性,默认值 /health; 该配置为全局配置,如果每个服务都有自己的健康检查,需要编辑kubernetes文件,metadata.labels中配置health.path
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
spring:
  cloud:
    kubernetes:
      discovery:
        service-labels: # 通过 Services 的 meta 中匹配对应的标记,不满足的则会被排除
          spa-admin: enabled
        primary-port-name: http # nodePort 指定多个多个端口时,通过name来确认使用哪个端口请求
      client:
        namespace: production # 指定命名空间
  boot:
    application:
      name: spring-boot-admin-kubernetes # 服务名
    admin:
      context-path: admin # Spring Boot Admin 的 页面或 api 的路径前缀
      discovery:
        converter:
          management-context-path: /actuator # 访问实例 的 Spring Boot actuator 的根路径
          health-endpoint-path: /health # 访问实例 健康检查的地址

实例配置

编写kubernetes文件

  • metadata.labels属性下添加spa-admin: enabled,方便SPA过滤实例,对应SPA配置中的spring.cloud.kubernetes.discovery.service-labels
  • spec.ports中配置端口信息,name的值为http,对应SPA配置中的spring.cloud.kubernetes.discovery.primary-port-name
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
kind: Service
apiVersion: v1
metadata:
  name: admin-user-server
  namespace: production
  labels:
    name: sadmin-user-server
    app: admin-user-server
    version: production
    spa-admin: enabled
spec:
  type: NodePort
  ports:
    - protocol: TCP
      name: http
      targetPort: 8080
      port: 8080
      nodePort: 32000
  selector:
    app: admin-user-server

加入相关依赖

  • 添加spring-boot-starter-actuator,给实例赋予actuator能力
  • 通过spring-boot-maven-plugin插件,可以为actuatorinfo接口提供更多的信息
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

<project >
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
  </dependencies>
  <build>
    <plugin>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-maven-plugin</artifactId>
      <executions>
        <execution>
          <goals>
            <goal>build-info</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </build>
</project>

配置actuator

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
spring:
  application:
    name: admin-user-server
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: ALWAYS

效果图

写在最后

码字不易,因个人能力有限,难免有疏漏和错误之处,如发现bug或者有更好的建议,欢迎批评指正,不吝感激