Istio Java SDK API - 连接访问

环境

  • kubernetes:1.18
  • Docker:19.03
  • Istio:1.4.4
  • IntelliJ Java:2020
  • kubernetes-client:4.10.1
  • istio-client:1.1.1
  • Java:OpenJDK13
  • Spring Boot:2.1.14.RELEASE 

 

工程说明

Java访问Istio和访问Kubernetes一样,通过istio-java-api 访问。下面的例子为,链接https的kubernetes。其他方式类似。

 

注意点

Java版本JDK9及其以上,此处用的13

IP和端口号

证书内容需要Base64编码

 

工程依赖



    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.1.14.RELEASE
         
    
    com.you.micro
    istio-demo
    0.0.1
    istio-demo
    Istio Demo project for Spring Boot

    
        1.13
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            io.fabric8
            kubernetes-client
            4.10.1
        

        
            me.snowdrop
            istio-client
            1.1.1
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    


 

工程源码

package com.you.micro.istiodemo.test;

import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.ConfigBuilder;

import me.snowdrop.istio.client.DefaultIstioClient;
import me.snowdrop.istio.client.IstioClient;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.core.io.ClassPathResource;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Base64;

public class ConnectIstio {

    public static void main(String[] args) {
        ConnectIstio connectIstio = new ConnectIstio();
        IstioClient demoIstioClient = connectIstio.getIstioClient();
        if (demoIstioClient != null) {
            String hostString = demoIstioClient.getMasterUrl().getHost();
            System.out.println("k8s host: " + hostString);
            connectIstio.closeIstioClient(demoIstioClient);
        } else {
            System.out.println("istio client is null");
        }

    }

    public IstioClient getIstioClient() {
        //文件为 /etc/kubernetes/pki 证书文件, 此处放在 工程的 resources 目录下
        String kubeCaPath = "ca.crt";
        String clientCertPath = "apiserver-kubelet-client.crt";
        String clientKeyPath = "apiserver-kubelet-client.key";
        String masterUrl = "https://ip:port";

        ClassPathResource pathResource = new ClassPathResource(kubeCaPath);
        String caData = readClassPathResourceFile(pathResource);
        pathResource = new ClassPathResource(clientCertPath);
        String certData = readClassPathResourceFile(pathResource);
        pathResource = new ClassPathResource(clientKeyPath);
        String keyData = readClassPathResourceFile(pathResource);

        IstioClient istioClient = null;

        if (StringUtils.isEmpty(caData) || StringUtils.isEmpty(certData) || StringUtils.isEmpty(keyData)) {
            return istioClient;
        }

        Config kubeConfig = new ConfigBuilder()
                .withMasterUrl(masterUrl)
                .withCaCertData(Base64.getEncoder().encodeToString(caData.getBytes()))
                .withClientCertData(Base64.getEncoder().encodeToString(certData.getBytes()))
                .withClientKeyData(Base64.getEncoder().encodeToString(keyData.getBytes()))
                .build();

        istioClient = new DefaultIstioClient(kubeConfig);
        return istioClient;

    }

    public String readClassPathResourceFile(ClassPathResource pathResource) {
        String fileContent = "";
        if (pathResource == null) {
            return fileContent;
        }
        boolean fileIsExists = pathResource.exists();
        if (!fileIsExists) {
            return fileContent;
        } else {
            try {
                fileContent = IOUtils.toString(new FileInputStream(pathResource.getFile()));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return fileContent;
    }

    public void closeIstioClient(IstioClient istioClient) {
        if (istioClient != null) {
            istioClient.close();
        }
    }

}

 

你可能感兴趣的:(istio,java)