前言

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();
        }

        // 检查 arthasHome
        if (arthasHome == null || arthasHome.trim().isEmpty()) {
            // 解压出 arthasHome
            URL 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.jar
        File 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: admin
  password: xxx
  • 使用k8s中 service (服务(Service)提供一种抽象的方法,将运行在容器组(Pod)上的应用程序公开为网络服务。
    arthas配置也是需要添加的
arthas:
  ip: 0.0.0.0  
  username: admin
  password: xxx

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

good day !!!

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐