k8s环境部署的集成arthas-spring-boot-starter spingboot项目无法访问控制台

本文主要是介绍k8s环境部署的集成arthas-spring-boot-starter spingboot项目无法访问控制台,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

k8s环境部署的集成arthas-spring-boot-starter项目无法访问控制台,springboot项目集成arthas-spring-boot-starter 会自带个控制台 供我们访问 但是当使用k8s环境部署后 这个页面就无法访问了

分析

首先看下arthas对应的配置 arthas-spring-boot-starter 中配置类 见 com.alibaba.arthas.spring.ArthasProperties

@ConfigurationProperties(prefix = "arthas")
public class ArthasProperties {private String ip;private int telnetPort;private int httpPort;private String tunnelServer;private String agentId;private String appName;/*** report executed command*/private String statUrl;/*** session timeout seconds*/private long sessionTimeout;private String username;private String password;private String home;/*** when arthas agent init error will throw exception by default.*/private boolean slientInit = false;/*** disabled commands,default disable stop command*/private String disabledCommands;private static final String DEFAULT_DISABLEDCOMMANDS = "stop";
}	

可以看到 的IP 以及端口配置 都是空的

实际的初始化 是在 com.taobao.arthas.agent.attach.ArthasAgent#init类中进行初始化的

public void init() throws IllegalStateException {// 尝试判断arthas是否已在运行,如果是的话,直接就退出try {Class.forName("java.arthas.SpyAPI"); // 加载不到会抛异常if (SpyAPI.isInited()) {return;}} catch (Throwable e) {// ignore}try {if (instrumentation == null) {instrumentation = ByteBuddyAgent.install();}// 检查 arthasHomeif (arthasHome == null || arthasHome.trim().isEmpty()) {// 解压出 arthasHomeURL coreJarUrl = this.getClass().getClassLoader().getResource("arthas-bin.zip");if (coreJarUrl != null) {File tempArthasDir = createTempDir();ZipUtil.unpack(coreJarUrl.openStream(), tempArthasDir);arthasHome = tempArthasDir.getAbsolutePath();} else {throw new IllegalArgumentException("can not getResources arthas-bin.zip from classloader: "+ this.getClass().getClassLoader());}}// find arthas-core.jarFile arthasCoreJarFile = new File(arthasHome, ARTHAS_CORE_JAR);if (!arthasCoreJarFile.exists()) {throw new IllegalStateException("can not find arthas-core.jar under arthasHome: " + arthasHome);}AttachArthasClassloader arthasClassLoader = new AttachArthasClassloader(new URL[] { arthasCoreJarFile.toURI().toURL() });/*** <pre>* ArthasBootstrap bootstrap = ArthasBootstrap.getInstance(inst);* </pre>*/Class<?> bootstrapClass = arthasClassLoader.loadClass(ARTHAS_BOOTSTRAP);Object bootstrap = bootstrapClass.getMethod(GET_INSTANCE, Instrumentation.class, Map.class).invoke(null,instrumentation, configMap);boolean isBind = (Boolean) bootstrapClass.getMethod(IS_BIND).invoke(bootstrap);if (!isBind) {String errorMsg = "Arthas server port binding failed! Please check $HOME/logs/arthas/arthas.log for more details.";throw new RuntimeException(errorMsg);}} catch (Throwable e) {errorMessage = e.getMessage();if (!slientInit) {throw new IllegalStateException(e);}}
}

可以看到会直接通过类加载器加载ARTHAS_BOOTSTRAP 常量 对应的 com.taobao.arthas.core.server.ArthasBootstrap 类 获取并调用其getInstance方法 然后在获取反射调用其bind方法

断点可以看到配置ip 为127.0.0.1
在这里插入图片描述
而127.0.0.1 对应的 host 即为localhost
当绑定的ip是localhost时 只能使用localhost 或者127.0.0.1 访问
没配置绑定ip的话 就会导致我们只能使用localhost 或者127.0.0.1 访问
在这里插入图片描述

另外 Arthas 默认启用了基于 HTTP Basic Authentication 的简单访问控制,目的是防止将 Arthas 的 Web 控制台暴露在公网上,避免安全隐患。

当你使用 arthas-spring-boot-starter 时,它会自动为你启用 Arthas 的访问控制功能。如果你没有显式配置用户名和密码,Arthas 会自动生成一个随机的用户名和密码,并将其打印在启动日志中。
这个是官网文档中并没有找到说明。当IP没进行配置时 默认是不需要鉴权的 使用本机的IP 也无须鉴权,当k8s 应用部署后 每次IP的地址都不一样 最简单的就是将IP配置成0.0.0.0 但是需要配置默认的密码 不然就会默认生成一个密码 打印到控制台 参见 SecurityAuthenticatorImpl的构造方法 密码不为空 就会设置一个默认的账号 。

SecurityAuthenticatorImpl的构造中有两个逻辑

  • 用户名不为空 密码为空 生成一个32位的随机密码
  • 用户为空 密码不为空 使用默认的用户名

源码位置 com.taobao.arthas.core.server.ArthasBootstrap#bind

if (IPUtils.isAllZeroIP(configure.getIp()) && StringUtils.isBlank(configure.getPassword())) {// 当 listen 0.0.0.0 时,强制生成密码,防止被远程连接String errorMsg = "Listening on 0.0.0.0 is very dangerous! External users can connect to your machine! "+ "No password is currently configured. " + "Therefore, a default password is generated, "+ "and clients need to use the password to connect!";AnsiLog.error(errorMsg);configure.setPassword(StringUtils.randomString(64));AnsiLog.error("Generated arthas password: " + configure.getPassword());logger().error(errorMsg);logger().info("Generated arthas password: " + configure.getPassword());
}this.securityAuthenticator = new SecurityAuthenticatorImpl(configure.getUsername(), configure.getPassword());

源码位 com.taobao.arthas.core.security.SecurityAuthenticatorImpl#SecurityAuthenticatorImpl

public SecurityAuthenticatorImpl(String username, String password) {if (username != null && password == null) {password = StringUtils.randomString(32);logger.info("\nUsing generated security password: {}\n", password);}if (username == null && password != null) {username = ArthasConstants.DEFAULT_USERNAME;}this.username = username;this.password = password;subject = new Subject();
}

解决

  • 针对IP 绑定不上的位置 我们可以直接设置绑定的IP 为0.0.0.0 同时显示的设置用户名和密码 (如果未配置账号密码 就会使用默认的用户名 和 随机生成64位的密码) 由于k8s部署 每次重启后IP会发生变更 这样导致使用的地址得频繁修改 (推荐使用入下方式)
arthas:ip: 0.0.0.0  username: adminpassword: xxx
  • 使用k8s中 service (服务(Service)提供一种抽象的方法,将运行在容器组(Pod)上的应用程序公开为网络服务。
    arthas配置也是需要添加的
arthas:ip: 0.0.0.0  username: adminpassword: xxx

在这里插入图片描述
提供内部或者外部访问 具体使用可以直接=使用图形化工具创建
内部访问: 虚拟IP + 具体的端口 默认8563
外部访问: nodeIp+ 外部访问端口

good day !!!

这篇关于k8s环境部署的集成arthas-spring-boot-starter spingboot项目无法访问控制台的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/989052

相关文章

Java计算经纬度距离的示例代码

《Java计算经纬度距离的示例代码》在Java中计算两个经纬度之间的距离,可以使用多种方法(代码示例均返回米为单位),文中整理了常用的5种方法,感兴趣的小伙伴可以了解一下... 目录1. Haversine公式(中等精度,推荐通用场景)2. 球面余弦定理(简单但精度较低)3. Vincenty公式(高精度,

使用Java将实体类转换为JSON并输出到控制台的完整过程

《使用Java将实体类转换为JSON并输出到控制台的完整过程》在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用JSON格式,用Java将实体类转换为J... 在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用j

Java实现视频格式转换的完整指南

《Java实现视频格式转换的完整指南》在Java中实现视频格式的转换,通常需要借助第三方工具或库,因为视频的编解码操作复杂且性能需求较高,以下是实现视频格式转换的常用方法和步骤,需要的朋友可以参考下... 目录核心思路方法一:通过调用 FFmpeg 命令步骤示例代码说明优点方法二:使用 Jaffree(FF

Java实现图片淡入淡出效果

《Java实现图片淡入淡出效果》在现代图形用户界面和游戏开发中,**图片淡入淡出(FadeIn/Out)**是一种常见且实用的视觉过渡效果,它可以用于启动画面、场景切换、轮播图、提示框弹出等场景,通过... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细

Java如何用乘号来重复字符串的功能

《Java如何用乘号来重复字符串的功能》:本文主要介绍Java使用乘号来重复字符串的功能,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java乘号来重复字符串的功能1、利用循环2、使用StringBuilder3、采用 Java 11 引入的String.rep

Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例

《Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例》本文介绍Nginx+Keepalived实现Web集群高可用负载均衡的部署与测试,涵盖架构设计、环境配置、健康检查、... 目录前言一、架构设计二、环境准备三、案例部署配置 前端 Keepalived配置 前端 Nginx

SpringBoot中HTTP连接池的配置与优化

《SpringBoot中HTTP连接池的配置与优化》这篇文章主要为大家详细介绍了SpringBoot中HTTP连接池的配置与优化的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、HTTP连接池的核心价值二、Spring Boot集成方案方案1:Apache HttpCl

Spring Boot项目打包和运行的操作方法

《SpringBoot项目打包和运行的操作方法》SpringBoot应用内嵌了Web服务器,所以基于SpringBoot开发的web应用也可以独立运行,无须部署到其他Web服务器中,下面以打包dem... 目录一、打包为JAR包并运行1.打包为可执行的 JAR 包2.运行 JAR 包二、打包为WAR包并运行

Java进行日期解析与格式化的实现代码

《Java进行日期解析与格式化的实现代码》使用Java搭配ApacheCommonsLang3和Natty库,可以实现灵活高效的日期解析与格式化,本文将通过相关示例为大家讲讲具体的实践操作,需要的可以... 目录一、背景二、依赖介绍1. Apache Commons Lang32. Natty三、核心实现代

Spring Boot 常用注解整理(最全收藏版)

《SpringBoot常用注解整理(最全收藏版)》本文系统整理了常用的Spring/SpringBoot注解,按照功能分类进行介绍,每个注解都会涵盖其含义、提供来源、应用场景以及代码示例,帮助开发... 目录Spring & Spring Boot 常用注解整理一、Spring Boot 核心注解二、Spr