如何从Java连接到DigitalOcean Kubernetes集群?

时间:2019-09-16 18:07:46

标签: kubernetes

我在DigitalOcean上有一个Kubernetes集群,我尝试使用Kubernetes Java API列出其中的Pod。

String kubeConfigPath = "~/.kube/config";

KubeConfig config = KubeConfig.loadKubeConfig(new FileReader(kubeConfigPath));

ApiClient client = ClientBuilder
        .kubeconfig(config)
        .build();

Configuration.setDefaultApiClient(client);

CoreV1Api api = new CoreV1Api();

V1PodList list = api.listNamespacedPod("default", true, null,
        null, null, null,
        null, null, null,
        null
);

for (V1Pod item : list.getItems()) {
    System.out.println(item.getMetadata().getName());
}

但是当我运行它时,出现以下错误

Exception in thread "main" io.kubernetes.client.ApiException: Forbidden
    at io.kubernetes.client.ApiClient.handleResponse(ApiClient.java:886)
    at io.kubernetes.client.ApiClient.execute(ApiClient.java:802)
    at io.kubernetes.client.apis.CoreV1Api.listNamespacedPodWithHttpInfo(CoreV1Api.java:16949)
    at io.kubernetes.client.apis.CoreV1Api.listNamespacedPod(CoreV1Api.java:16926)
    at com.workfront.k8s.KubeConfigFileClientExample.main(KubeConfigFileClientExample.java:30)

当我使用minikube时,它工作正常。

0 个答案:

没有答案