2017-10-26 8 views
0

を使用してKubernetesサービスと起動サービスのポッドを取得:Javaのファブリック8 - 私はFabric8のJava API</p> <p>ConnectionExample.java使用Kubernetesサービスの利用可能ポッドを取得するためのコードの下にしようとしていますFabric8のJava API

package examples; 

import java.util.*; 

import io.fabric8.kubernetes.api.KubernetesClient; 
import io.fabric8.kubernetes.api.KubernetesFactory; 
import io.fabric8.kubernetes.api.model.IntOrString; 
import io.fabric8.kubernetes.api.model.Service; 

public class ConnectionExample { 
    private String ip; 
    private String port; 

    public ConnectionExample(String ip, String port) { 
     this.ip= ip; 
     this.port = port; 
    } 

    public KubernetesClient getConnection() { 
     final String URI = "http://" + ip+ ":" + port; 
     final KubernetesClient kubernetes = new KubernetesClient(new KubernetesFactory(URI)); 

     return kubernetes; 
    } 
} 

App.java私はエラーを下回る

2017-10-26 15:09:04 WARN PhaseInterceptorChain:452 - Interceptor for 
WebClient has thrown exception, unwinding now 
org.apache.cxf.interceptor.Fault: Could not send Message. 
    at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:64) 
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:307) 
    at org.apache.cxf.jaxrs.client.AbstractClient.doRunInterceptorChain(AbstractClient.java:619) 
    at org.apache.cxf.jaxrs.client.ClientProxyImpl.doChainedInvocation(ClientProxyImpl.java:674) 
    at org.apache.cxf.jaxrs.client.ClientProxyImpl.invoke(ClientProxyImpl.java:224) 
    at com.sun.proxy.$Proxy19.getPods(Unknown Source) 
    at io.fabric8.kubernetes.api.KubernetesClient.getPods(KubernetesClient.java:154) 
    at io.fabric8.kubernetes.api.KubernetesClient.getPods(KubernetesClient.java:149) 
を取得しています

package examples; 

/** 
* Hello world! 
* 
*/ 
public class App { 
    public static void main(String[] args) { 
     System.out.println("Hello World!"); 
     ConnectionExample connectionExample = new ConnectionExample("XXX.XXX.XXX.XX", "1234"); 
     System.out.println("Retrun: "+connectionExample.getConnection()); 

     System.out.println("List of Pods: "+connectionExample.getConnection().getPods()); 

     //connectionExample.getConnection().createService(entity, namespace) 
    } 
} 

ユーザ名と私はあなたがauthenitcateする設定ファイルからSSL証明書を使用する必要が

+0

私はあなたが必要以上に抽象度の低いところでこの問題に近づいていると思います。いくつかの例[ここ](https://github.com/fabric8io/kubernetes-client/tree/master/kubernetes-examples/src/main/java/io/fabric8/kubernetes/examples)は、 。 –

答えて

0

fabric8のJava API

を使用してJavaクラスからkubernetesサービスを開始しようとしています

kubernetesサービスのパスワードを渡す方法を疑問に思っ

も。設定ファイルを$HOME/.kube/configにコピーし、このサンプルプログラムを実行してみてください。

/** 
* Copyright (C) 2015 Red Hat, Inc. 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); 
* you may not use this file except in compliance with the License. 
* You may obtain a copy of the License at 
* 
*   http://www.apache.org/licenses/LICENSE-2.0 
* 
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, 
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
* See the License for the specific language governing permissions and 
* limitations under the License. 
*/ 
package io.fabric8.kubernetes.examples; 

import io.fabric8.kubernetes.client.Config; 
import io.fabric8.kubernetes.client.ConfigBuilder; 
import io.fabric8.kubernetes.client.DefaultKubernetesClient; 
import io.fabric8.kubernetes.client.KubernetesClient; 
import io.fabric8.kubernetes.client.KubernetesClientException; 
import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 

public class ListExamples { 

    private static final Logger logger = LoggerFactory.getLogger(ListExamples.class); 

    public static void main(String[] args) { 

    System.setProperty("KUBERNETES_AUTH_TRYKUBECONFIG_SYSTEM_PROPERTY","true"); 
    Config config = Config.autoConfigure(); 


    try (final KubernetesClient client = new DefaultKubernetesClient(config)) { 


     System.out.println(
     client.namespaces().list() 
    ); 


     System.out.println(
     client.namespaces().withLabel("this", "works").list() 
    ); 

     System.out.println(
     client.pods().withLabel("this", "works").list() 
    ); 

    System.out.println(
     client.pods().inNamespace("test").withLabel("this", "works").list() 
    ); 

     System.out.println(
     client.pods().inNamespace("test").withName("testing").get() 
    ); 
    } catch (KubernetesClientException e) { 
     logger.error(e.getMessage(), e); 
    } 
    } 

} 
0
public static void main(String[] args) throws Exception{ 

    try { 

     String url = "cluster_endpoint"; 
     String oathToken = "serviceAccountToken"; 
     Config config = new ConfigBuilder() 
       .withMasterUrl("") 
       .withTrustCerts(true) 
       .withOauthToken(oathToken.replaceAll("(\\r|\\n)", "")) 
       .build(); 

     KubernetesClient client = new DefaultKubernetesClient(config); 
     System.out.println(client.pods().inNamespace("default").list()); 

    } catch (KubernetesClientException kce) { 
     logger.error("KubernetesClientException : {}, {}", KubernetesErrorUtil.getErrorMsg(kce), kce); 
    } catch (Exception e){ 
     logger.error("Exception :"); 
     e.printStackTrace(); 
    } 
} 

これはserviceAccountTokenを使用してクライアントを取得します。 ClusterRoleまたはClusterRoleBindingまたはRoleBindingを作成することにより、サービスアカウントを作成し、それに必要な権限を与えることができます。

また、あなたがクライアントを取得することができ、他の方法があります。API-サーバーに=新しいConfigBuilder

コンフィグ設定を使用している証明書を使用してkubeconfigファイル

  • 2)を使用して

    • 1) (); .withMasterUrl(masterURL) .withClientCertFile(certFile) 。ビルド();

    または証明書の内容ファイル -

    コンフィグ設定が=新しいConfigBuilder() .withMasterUrl(masterURL) .withClientCertData(certfileに) .build()。

  • 関連する問題