server

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>2.3.2.RELEASE</version>
  10. <relativePath />
  11. </parent>
  12. <groupId>com.qizai</groupId>
  13. <artifactId>ops-admin-server</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. <name>ops-admin-server</name>
  16. <description>spring-boot-admin on k8s</description>
  17. <properties>
  18. <java.version>1.8</java.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>de.codecentric</groupId>
  27. <artifactId>spring-boot-admin-starter-server</artifactId>
  28. <version>2.3.1</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-security</artifactId>
  33. </dependency>
  34. <!-- 需要在k8s环境下才能使用 -->
  35. <!-- <dependency>
  36. <groupId>org.springframework.cloud</groupId>
  37. <artifactId>spring-cloud-kubernetes-discovery</artifactId>
  38. <version>1.1.6.RELEASE</version>
  39. </dependency> -->
  40. </dependencies>
  41. <build>
  42. <plugins>
  43. <plugin>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-maven-plugin</artifactId>
  46. <executions>
  47. <execution>
  48. <id>repackage</id>
  49. <goals>
  50. <goal>repackage</goal>
  51. </goals>
  52. </execution>
  53. </executions>
  54. </plugin>
  55. </plugins>
  56. </build>
  57. </project>

ServerApplication.java

  1. package com.qizai.opsserver;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.scheduling.annotation.EnableScheduling;
  5. import de.codecentric.boot.admin.server.config.EnableAdminServer;
  6. @EnableAdminServer
  7. //@org.springframework.cloud.client.discovery.EnableDiscoveryClient //开启k8s
  8. @EnableScheduling // 开启定时任务,不加此注解服务发现不会执行定时刷新
  9. @SpringBootApplication
  10. public class ServerApplication {
  11. public static void main(String[] args) {
  12. SpringApplication.run(ZcOpsServerApplication.class, args);
  13. }
  14. }

SecuritySecureConfig.java

  1. package com.qizai.opsserver;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.security.config.annotation.web.builders.HttpSecurity;
  4. import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
  5. import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
  6. import org.springframework.security.web.csrf.CookieCsrfTokenRepository;
  7. import de.codecentric.boot.admin.server.config.AdminServerProperties;
  8. @Configuration
  9. public class SecuritySecureConfig extends WebSecurityConfigurerAdapter {
  10. private final String adminContextPath;
  11. public SecuritySecureConfig(AdminServerProperties adminServerProperties) {
  12. this.adminContextPath = adminServerProperties.getContextPath();
  13. }
  14. @Override
  15. protected void configure(HttpSecurity http) throws Exception {
  16. SavedRequestAwareAuthenticationSuccessHandler successHandler = new SavedRequestAwareAuthenticationSuccessHandler();
  17. successHandler.setTargetUrlParameter("redirectTo");
  18. successHandler.setDefaultTargetUrl(adminContextPath + "/");
  19. http.authorizeRequests()
  20. // 1.配置所有静态资源和登录页可以公开访问
  21. .antMatchers(adminContextPath + "/assets/**").permitAll()
  22. .antMatchers(adminContextPath + "/login").permitAll()
  23. .antMatchers(adminContextPath + "/actuator/health").permitAll()
  24. .anyRequest().authenticated().and()
  25. // 2.配置登录和登出路径
  26. .formLogin().loginPage(adminContextPath + "/login").successHandler(successHandler).and().logout()
  27. .logoutUrl(adminContextPath + "/logout").and()
  28. // 3.开启http basic支持,admin-client注册时需要使用
  29. .httpBasic().and().csrf()
  30. // 4.开启基于cookie的csrf保护
  31. .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse())
  32. // 5.忽略这些路径的csrf保护以便admin-client注册
  33. .ignoringAntMatchers(adminContextPath + "/instances", adminContextPath + "/actuator/**");
  34. }
  35. }

application.properties

  1. server.port=8080
  2. management.endpoints.web.exposure.include=*
  3. # 不配权限不需要
  4. spring.security.user.name=ops
  5. spring.security.user.password=opsPsw
  6. # client 的用户名和密码
  7. spring.boot.admin.client.instance.metadata.user.name=${spring.security.user.name}
  8. spring.boot.admin.client.instance.metadata.user.password=${spring.security.user.password}
  9. #----以下在k8s下需要设置
  10. # 按设要监控 Service 的端口名称
  11. spring.cloud.kubernetes.discovery.primaryPortName=management
  12. # 设置要监控 Service 的 Label 标签
  13. spring.cloud.kubernetes.discovery.serviceLabels.admin=enabled

client

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <parent>
  7. <groupId>org.springframework.boot</groupId>
  8. <artifactId>spring-boot-starter-parent</artifactId>
  9. <version>2.3.2.RELEASE</version>
  10. <relativePath />
  11. </parent>
  12. <groupId>com.qizai</groupId>
  13. <artifactId>ops-admin-client</artifactId>
  14. <version>0.0.1-SNAPSHOT</version>
  15. <name>ops-admin-client</name>
  16. <description>spring-boot-client on k8s</description>
  17. <properties>
  18. <java.version>1.8</java.version>
  19. </properties>
  20. <dependencies>
  21. <dependency>
  22. <groupId>org.springframework.boot</groupId>
  23. <artifactId>spring-boot-starter-web</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>de.codecentric</groupId>
  27. <artifactId>spring-boot-admin-starter-client</artifactId>
  28. <version>2.3.1</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.springframework.boot</groupId>
  32. <artifactId>spring-boot-starter-security</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-actuator</artifactId>
  37. </dependency>
  38. <dependency>
  39. <groupId>io.micrometer</groupId>
  40. <artifactId>micrometer-registry-prometheus</artifactId>
  41. </dependency>
  42. </dependencies>
  43. <build>
  44. <plugins>
  45. <plugin>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-maven-plugin</artifactId>
  48. <executions>
  49. <execution>
  50. <id>repackage</id>
  51. <goals>
  52. <goal>repackage</goal>
  53. </goals>
  54. </execution>
  55. </executions>
  56. </plugin>
  57. </plugins>
  58. </build>
  59. </project>

ClientApplication.java

package com.qizai.opsclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(OpsAdminClientApplication.class, args);
    }
}

SpringSecurityActuatorConfig.java

package com.qizai.opsclient;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;

@Configuration
public class SpringSecurityActuatorConfig extends WebSecurityConfigurerAdapter {
    private static Logger log = LoggerFactory.getLogger(SpringSecurityActuatorConfig.class);

    public SpringSecurityActuatorConfig() {
        log.info("SpringSecurityActuatorConfig... start");
    }

    @Override
    public void configure(WebSecurity web) throws Exception {
        // 排除
        web.ignoring().antMatchers("/actuator/health");
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        // 这个配置只针对 /actuator/** 的请求生效
        http.antMatcher("/actuator/**")
                // /actuator/下所有请求都要认证
                .authorizeRequests().anyRequest().authenticated()
                // 启用httpBasic认证模式,当springboot admin-client 配置了密码时,
                // admin-server走httpbasic的认证方式来拉取client的信息
                .and().httpBasic()
                // 禁用csrf
                .and().csrf().disable();
    }
}

application.properties

server.port=8082
spring.application.name=ops-client-k8s
management.endpoints.web.exposure.include=*
# client 的用户名和密码
spring.security.user.name=saAdmin
spring.security.user.password=saPsw
spring.boot.admin.client.instance.metadata.user.name=${spring.security.user.name}
spring.boot.admin.client.instance.metadata.user.password=${spring.security.user.password}
# server 配置
spring.boot.admin.client.url=http://10.0.2.69:8080
spring.boot.admin.client.username=saAdmin
spring.boot.admin.client.password=saPsw
#----以下在k8s下需要设置
# 按设要监控 Service 的端口名称
spring.cloud.kubernetes.discovery.primaryPortName=management
# 设置要监控 Service 的 Label 标签
spring.cloud.kubernetes.discovery.serviceLabels.admin=enabled

https://ica10888.com/2019/01/03/%E5%9C%A8kubernetes%E9%9B%86%E7%BE%A4%E4%B8%AD%E4%BD%BF%E7%94%A8prometheus%E5%AE%9E%E7%8E%B0%E5%AF%B9SpringCloud%E7%9A%84HPA.html
https://github.com/stefanprodan/k8s-prom-hpa
https://zhuanlan.zhihu.com/p/34555654
https://github.com/prometheus-operator/kube-prometheus