You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java K8s客户端应用抛出java.lang.NumberFormatException: null错误求助

解决Kubernetes Java客户端NumberFormatException: null问题

我开发了一个用于列出Kubernetes集群所有Pod的Java应用,运行时抛出java.lang.NumberFormatException: null异常,完整异常栈信息如下:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "main" java.lang.IllegalStateException: java.lang.NumberFormatException: null
    at io.kubernetes.client.util.ClientBuilder.setBasePath(ClientBuilder.java:261)
    at io.kubernetes.client.util.ClientBuilder.cluster(ClientBuilder.java:247)
    at Example.main(Example.java:16)
Caused by: java.lang.NumberFormatException: null
    at java.base/java.lang.Integer.parseInt(Integer.java:614)
    at java.base/java.lang.Integer.valueOf(Integer.java:983)
    at io.kubernetes.client.util.ClientBuilder.setBasePath(ClientBuilder.java:257)
    ... 2 more

我的Java主类代码:

import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.ApiException;
import io.kubernetes.client.openapi.Configuration;
import io.kubernetes.client.openapi.apis.CoreV1Api;
import io.kubernetes.client.openapi.models.V1Pod;
import io.kubernetes.client.openapi.models.V1PodList;
import io.kubernetes.client.util.ClientBuilder;
import java.io.IOException;

public class Example {
    public static void main(String[] args) throws IOException, ApiException {
        ApiClient client = ClientBuilder.cluster().build();

        Configuration.setDefaultApiClient(client);

        CoreV1Api api = new CoreV1Api();

        V1PodList list =
                api.listPodForAllNamespaces(null, null, null, null, null, null, null, null, null, null);
        for (V1Pod item : list.getItems()) {
            System.out.println(item.getMetadata().getName());
        }
    }
}

pom.xml仅添加了client-java依赖:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>JavaK8sClient</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>io.kubernetes</groupId>
            <artifactId>client-java</artifactId>
            <version>15.0.1</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>

</project>

更新:经排查,错误是由于端口和主机为null导致的,请问该如何解决?


解决方案

1. 应用在Kubernetes集群内部运行

当应用以Pod形式部署在集群内时,ClientBuilder.cluster()本应自动从环境变量和挂载的ServiceAccount配置读取集群信息,但如果配置未正确加载,会出现主机/端口null的问题。可以手动指定集群内部API Server地址:
修改ApiClient创建代码:

// 集群内部API Server默认地址
String apiServerUrl = "https://kubernetes.default.svc:443";
ApiClient client = ClientBuilder.cluster()
        .setBasePath(apiServerUrl)
        .build();

同时确保Pod绑定了具备Pod列表访问权限的ServiceAccount,并配置了正确的RBAC规则。

2. 应用在Kubernetes集群外部运行(本地开发)

本地运行时,需要加载本地kubeconfig文件(默认路径为~/.kube/config):
修改ApiClient创建逻辑:

import io.kubernetes.client.util.KubeConfig;
import java.io.FileReader;

// 加载本地kubeconfig配置
ApiClient client = ClientBuilder.kubeconfig(KubeConfig.loadKubeConfig(new FileReader(System.getProperty("user.home") + "/.kube/config"))).build();

此方式会从kubeconfig中读取集群主机、端口及认证信息,避免null值问题。

额外处理SLF4J警告

当前出现的SLF4J警告是因为缺少日志实现,可在pom.xml中添加logback依赖解决:

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.11</version>
    <scope>runtime</scope>
</dependency>

内容的提问来源于stack exchange,提问作者marjan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 19:04:07