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

相关文章

闲置电脑也能活出第二春?鲁大师AiNAS让你动动手指就能轻松部署

对于大多数人而言,在这个“数据爆炸”的时代或多或少都遇到过存储告急的情况,这使得“存储焦虑”不再是个别现象,而将会是随着软件的不断臃肿而越来越普遍的情况。从不少手机厂商都开始将存储上限提升至1TB可以见得,我们似乎正处在互联网信息飞速增长的阶段,对于存储的需求也将会不断扩大。对于苹果用户而言,这一问题愈发严峻,毕竟512GB和1TB版本的iPhone可不是人人都消费得起的,因此成熟的外置存储方案开

JVM 的类初始化机制

前言 当你在 Java 程序中new对象时,有没有考虑过 JVM 是如何把静态的字节码(byte code)转化为运行时对象的呢,这个问题看似简单,但清楚的同学相信也不会太多,这篇文章首先介绍 JVM 类初始化的机制,然后给出几个易出错的实例来分析,帮助大家更好理解这个知识点。 JVM 将字节码转化为运行时对象分为三个阶段,分别是:loading 、Linking、initialization

Spring Security 基于表达式的权限控制

前言 spring security 3.0已经可以使用spring el表达式来控制授权,允许在表达式中使用复杂的布尔逻辑来控制访问的权限。 常见的表达式 Spring Security可用表达式对象的基类是SecurityExpressionRoot。 表达式描述hasRole([role])用户拥有制定的角色时返回true (Spring security默认会带有ROLE_前缀),去

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti

Spring Security--Architecture Overview

1 核心组件 这一节主要介绍一些在Spring Security中常见且核心的Java类,它们之间的依赖,构建起了整个框架。想要理解整个架构,最起码得对这些类眼熟。 1.1 SecurityContextHolder SecurityContextHolder用于存储安全上下文(security context)的信息。当前操作的用户是谁,该用户是否已经被认证,他拥有哪些角色权限…这些都被保

Spring Security基于数据库验证流程详解

Spring Security 校验流程图 相关解释说明(认真看哦) AbstractAuthenticationProcessingFilter 抽象类 /*** 调用 #requiresAuthentication(HttpServletRequest, HttpServletResponse) 决定是否需要进行验证操作。* 如果需要验证,则会调用 #attemptAuthentica

Spring Security 从入门到进阶系列教程

Spring Security 入门系列 《保护 Web 应用的安全》 《Spring-Security-入门(一):登录与退出》 《Spring-Security-入门(二):基于数据库验证》 《Spring-Security-入门(三):密码加密》 《Spring-Security-入门(四):自定义-Filter》 《Spring-Security-入门(五):在 Sprin

Java架构师知识体认识

源码分析 常用设计模式 Proxy代理模式Factory工厂模式Singleton单例模式Delegate委派模式Strategy策略模式Prototype原型模式Template模板模式 Spring5 beans 接口实例化代理Bean操作 Context Ioc容器设计原理及高级特性Aop设计原理Factorybean与Beanfactory Transaction 声明式事物

这15个Vue指令,让你的项目开发爽到爆

1. V-Hotkey 仓库地址: github.com/Dafrok/v-ho… Demo: 戳这里 https://dafrok.github.io/v-hotkey 安装: npm install --save v-hotkey 这个指令可以给组件绑定一个或多个快捷键。你想要通过按下 Escape 键后隐藏某个组件,按住 Control 和回车键再显示它吗?小菜一碟: <template

Java进阶13讲__第12讲_1/2

多线程、线程池 1.  线程概念 1.1  什么是线程 1.2  线程的好处 2.   创建线程的三种方式 注意事项 2.1  继承Thread类 2.1.1 认识  2.1.2  编码实现  package cn.hdc.oop10.Thread;import org.slf4j.Logger;import org.slf4j.LoggerFactory