All Products
Search
Document Center

Alibaba Cloud Service Mesh:Manage Istio resources with the Java SDK

Last Updated:Mar 11, 2026

When you manage Istio resources such as VirtualServices and Gateways across multiple clusters, applying YAML files manually through kubectl becomes repetitive and error-prone. The fabric8io/istio-client SDK lets you create, query, update, and delete Istio resources directly from Java code, so you can automate these operations in your deployment pipelines.

This guide covers creating a VirtualService and a Gateway in Alibaba Cloud Service Mesh (ASM) using istio-client 6.0.0-RC1 with Java 11.

Quick start

The following example creates a VirtualService in the default namespace by loading a YAML file. It connects to the ASM control plane through the default kubeconfig at $HOME/.kube/config.

import io.fabric8.istio.api.networking.v1beta1.VirtualService;
import io.fabric8.istio.client.DefaultIstioClient;
import io.fabric8.istio.client.IstioClient;

public class QuickStartExample {
    public static void main(String[] args) {
        try {
            IstioClient istioClient = new DefaultIstioClient();
            VirtualService vs = istioClient.v1beta1().virtualServices()
                .load(QuickStartExample.class.getResourceAsStream("/virtualService.yaml"))
                .get();
            istioClient.v1beta1().virtualServices()
                .inNamespace("default")
                .resource(vs)
                .create();
            System.out.println("VirtualService created successfully.");
            istioClient.close();
        } catch (Exception e) {
            System.err.println("Failed to create VirtualService: " + e.getMessage());
            System.exit(1);
        }
    }
}

For the YAML files used in these examples, see Use ASM SDK to manage Istio resources in code.

Prerequisites

Before you begin, make sure that you have:

Set up your project

Add the SDK dependency

Add the istio-client dependency to your Maven pom.xml:

<dependencies>
  <dependency>
    <groupId>io.fabric8</groupId>
    <artifactId>istio-client</artifactId>
    <version>6.0.0-RC1</version>
  </dependency>
</dependencies>

Connect to the ASM instance

The istio-client SDK reads your kubeconfig to connect to the ASM control plane. Copy the kubeconfig from the ASM console to your local machine:

  1. Log on to the ASM console.

  2. In the left-side navigation pane, choose Service Mesh > Mesh Management.

  3. On the Mesh Management page, find your ASM instance and click its name, or click Manage in the Actions column.

  4. In the left-side navigation pane, click Base Information. On the Basic Information page, click Connection.

  5. In the Connection panel, copy the system kubeconfig file to $HOME/.kube/config on your machine.

By default, the SDK reads $HOME/.kube/config automatically. To use a different kubeconfig file, see Connect with a custom kubeconfig.

Prepare YAML configuration files

Save the virtualService.yaml and gateway.yaml files from Use ASM SDK to manage Istio resources in code to the Maven static resource folder (src/main/resources) in your project.

Create a VirtualService

You can create Istio resources by loading a YAML file or by building the resource programmatically with method chaining.

Load from a YAML file

The following example loads virtualService.yaml from the project resource folder and creates the VirtualService in the default namespace:

import java.io.FileReader;
import java.io.IOException;

import io.fabric8.istio.api.networking.v1beta1.VirtualService;
import io.fabric8.istio.client.DefaultIstioClient;
import io.fabric8.istio.client.IstioClient;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.utils.IOHelpers;

public class VirtualServiceExample {
    public static void main(String[] args) {
        try {
            IstioClient istioClient = new DefaultIstioClient();
            final String NAMESPACE = "default"; // Target namespace for the resource
            VirtualService virtualService = istioClient.v1beta1().virtualServices().load(
                VirtualServiceExample.class.getResourceAsStream("/virtualService.yaml")
                ).get();
            istioClient.v1beta1().virtualServices().inNamespace(NAMESPACE).resource(virtualService).create();
            printAllVirtualServices(istioClient);
            istioClient.close();
        } catch (Exception e) {
            System.err.println("Failed to create VirtualService: " + e.getMessage());
            System.exit(1);
        }
    }

    // List all virtual services in the ASM instance
    static void printAllVirtualServices(IstioClient istioClient) {
        istioClient.v1beta1().virtualServices().list().getItems().forEach(System.out::println);
    }
}

Build with method chaining

Use VirtualServiceBuilder to construct the resource in code instead of loading YAML. The following example creates a Bookinfo VirtualService that routes traffic from the bookinfo-gateway to the productpage service on port 9080.

import io.fabric8.istio.api.networking.v1beta1.VirtualServiceBuilder;
import io.fabric8.istio.client.DefaultIstioClient;
import io.fabric8.istio.client.IstioClient;

public class VirtualServiceChainingExample {
    public static void main(String[] args) {
        try {
            IstioClient istioClient = new DefaultIstioClient();
            final String NAMESPACE = "default";
            istioClient.v1beta1().virtualServices().inNamespace(NAMESPACE).resource(
                new VirtualServiceBuilder()
                .withNewMetadata()
                .withName("bookinfo")
                .endMetadata()
                .withNewSpec()
                    .addToHosts("*")
                    .addToGateways("bookinfo-gateway")
                    .addNewHttp()
                        .addNewMatch().withNewUri().withNewStringMatchExactType("/productpage").endUri().endMatch()
                        .addNewMatch().withNewUri().withNewStringMatchPrefixType("/static").endUri().endMatch()
                        .addNewMatch().withNewUri().withNewStringMatchExactType("/login").endUri().endMatch()
                        .addNewMatch().withNewUri().withNewStringMatchExactType("/logout").endUri().endMatch()
                        .addNewMatch().withNewUri().withNewStringMatchPrefixType("/api/v1/products").endUri().endMatch()
                        .addNewRoute()
                            .withNewDestination()
                                .withHost("productpage")
                                .withNewPort()
                                    .withNumber(9080)
                                .endPort()
                            .endDestination()
                        .endRoute()
                    .endHttp()
                .endSpec()
                .build()
                ).create();
            printAllVirtualServices(istioClient);
            istioClient.close();
        } catch (Exception e) {
            System.err.println("Failed to create VirtualService: " + e.getMessage());
            System.exit(1);
        }
    }

    // List all virtual services in the ASM instance
    static void printAllVirtualServices(IstioClient istioClient) {
        istioClient.v1beta1().virtualServices().list().getItems().forEach(System.out::println);
    }
}

Builder-to-YAML mapping

The builder methods map directly to VirtualService YAML fields. The following table shows the mapping for common fields:

Builder methodYAML equivalentDescription
.withName("bookinfo")metadata.name: bookinfoResource name
.addToHosts("*")spec.hosts: ["*"]Target hosts for traffic
.addToGateways("bookinfo-gateway")spec.gateways: ["bookinfo-gateway"]Gateway to bind
.withNewStringMatchExactType("/productpage")match.uri.exact: "/productpage"Exact URI match
.withNewStringMatchPrefixType("/static")match.uri.prefix: "/static"Prefix URI match
.withHost("productpage").withNewPort().withNumber(9080)destination.host: productpage, destination.port.number: 9080Route destination

For the full VirtualService specification, see the Istio VirtualService API reference.

Create a Gateway

The following example creates a Gateway in the default namespace from the gateway.yaml file. It also demonstrates how to connect with a custom kubeconfig file instead of the default $HOME/.kube/config:

import java.io.FileReader;
import java.io.IOException;

import io.fabric8.istio.api.networking.v1beta1.Gateway;
import io.fabric8.istio.client.DefaultIstioClient;
import io.fabric8.istio.client.IstioClient;
import io.fabric8.kubernetes.client.Config;
import io.fabric8.kubernetes.client.utils.IOHelpers;

public class GateWayExample {
    public static void main(String[] args) {
        try {
            // Read a custom kubeconfig file
            String kubeconfigContents = null;
            FileReader reader = new FileReader("<path-to-kubeconfig>"); // Replace with the actual path
            kubeconfigContents = IOHelpers.readFully(reader);
            Config config = Config.fromKubeconfig(null, kubeconfigContents, null);

            IstioClient istioClient = new DefaultIstioClient(config);
            final String NAMESPACE = "default"; // Target namespace for the resource
            Gateway gateway = istioClient.v1beta1().gateways().load(
                GateWayExample.class.getResourceAsStream("/gateway.yaml")
            ).get();
            istioClient.v1beta1().gateways().inNamespace(NAMESPACE).resource(gateway).create();
            printAllGateways(istioClient);
            istioClient.close();
        } catch (Exception e) {
            System.err.println("Failed to create Gateway: " + e.getMessage());
            System.exit(1);
        }
    }

    // List all gateways in the ASM instance
    static void printAllGateways(IstioClient istioClient) {
        istioClient.v1beta1().gateways().list().getItems().forEach(System.out::println);
    }
}

Replace the following placeholder with your actual value:

PlaceholderDescriptionExample
<path-to-kubeconfig>Absolute path to your kubeconfig file/home/user/.kube/asm-config

Common SDK operations

Beyond creating resources, the istio-client SDK supports the full range of operations. The following table lists the most common operations for VirtualService. The same pattern applies to Gateway and other Istio resource types.

OperationCode
CreateistioClient.v1beta1().virtualServices().inNamespace(ns).resource(vs).create()
Get by nameistioClient.v1beta1().virtualServices().inNamespace(ns).withName("bookinfo").get()
List allistioClient.v1beta1().virtualServices().inNamespace(ns).list()
UpdateistioClient.v1beta1().virtualServices().inNamespace(ns).resource(vs).update()
DeleteistioClient.v1beta1().virtualServices().inNamespace(ns).withName("bookinfo").delete()

Verify the result

After you create the VirtualService and Gateway, open a browser and go to http://<ingress-gateway-ip>/productpage to access the Bookinfo application. Replace <ingress-gateway-ip> with the IP address of your ingress gateway service.

References