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:
An ASM instance. For more information, see Create an ASM instance
A Container Service for Kubernetes (ACK) cluster added to your ASM instance. For more information, see Add a cluster to an ASM instance
The Bookinfo sample application deployed in the ACK cluster. For more information, see Deploy an application in an ASM instance
An ingress gateway service deployed in the ACK cluster. For more information, see Create an ingress gateway service
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:
Log on to the ASM console.
In the left-side navigation pane, choose Service Mesh > Mesh Management.
On the Mesh Management page, find your ASM instance and click its name, or click Manage in the Actions column.
In the left-side navigation pane, click Base Information. On the Basic Information page, click Connection.
In the Connection panel, copy the system kubeconfig file to
$HOME/.kube/configon 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 method | YAML equivalent | Description |
|---|---|---|
.withName("bookinfo") | metadata.name: bookinfo | Resource 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: 9080 | Route 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:
| Placeholder | Description | Example |
|---|---|---|
<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.
| Operation | Code |
|---|---|
| Create | istioClient.v1beta1().virtualServices().inNamespace(ns).resource(vs).create() |
| Get by name | istioClient.v1beta1().virtualServices().inNamespace(ns).withName("bookinfo").get() |
| List all | istioClient.v1beta1().virtualServices().inNamespace(ns).list() |
| Update | istioClient.v1beta1().virtualServices().inNamespace(ns).resource(vs).update() |
| Delete | istioClient.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
Use ASM SDK to manage Istio resources in code -- source for
virtualService.yamlandgateway.yamlfabric8io/kubernetes-client Istio extension -- full list of supported Istio resource types and operations
Istio networking API reference -- VirtualService and Gateway API specifications