Skip to main content
Redhat Developers  Logo
  • AI

    Get started with AI

    • Red Hat AI
      Accelerate the development and deployment of enterprise AI solutions.
    • AI learning hub
      Explore learning materials and tools, organized by task.
    • AI interactive demos
      Click through scenarios with Red Hat AI, including training LLMs and more.
    • AI/ML learning paths
      Expand your OpenShift AI knowledge using these learning resources.
    • AI quickstarts
      Focused AI use cases designed for fast deployment on Red Hat AI platforms.
    • No-cost AI training
      Foundational Red Hat AI training.

    Featured resources

    • OpenShift AI learning
    • Open source AI for developers
    • AI product application development
    • Open source-powered AI/ML for hybrid cloud
    • AI and Node.js cheat sheet

    Red Hat AI Factory with NVIDIA

    • Red Hat AI Factory with NVIDIA is a co-engineered, enterprise-grade AI solution for building, deploying, and managing AI at scale across hybrid cloud environments.
    • Explore the solution
  • Learn

    Self-guided

    • Documentation
      Find answers, get step-by-step guidance, and learn how to use Red Hat products.
    • Learning paths
      Explore curated walkthroughs for common development tasks.
    • Guided learning
      Receive custom learning paths powered by our AI assistant.
    • See all learning

    Hands-on

    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.
    • Interactive labs
      Learn by doing in these hands-on, browser-based experiences.
    • Interactive demos
      Click through product features in these guided tours.

    Browse by topic

    • AI/ML
    • Automation
    • Java
    • Kubernetes
    • Linux
    • See all topics

    Training & certifications

    • Courses and exams
    • Certifications
    • Skills assessments
    • Red Hat Academy
    • Learning subscription
    • Explore training
  • Build

    Get started

    • Red Hat build of Podman Desktop
      A downloadable, local development hub to experiment with our products and builds.
    • Developer Sandbox
      Spin up Red Hat's products and technologies without setup or configuration.

    Download products

    • Access product downloads to start building and testing right away.
    • Red Hat Enterprise Linux
    • Red Hat AI
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Featured

    • Red Hat build of OpenJDK
    • Red Hat JBoss Enterprise Application Platform
    • Red Hat OpenShift Dev Spaces
    • Red Hat Developer Toolset

    References

    • E-books
    • Documentation
    • Cheat sheets
    • Architecture center
  • Community

    Get involved

    • Events
    • Live AI events
    • Red Hat Summit
    • Red Hat Accelerators
    • Community discussions

    Follow along

    • Articles & blogs
    • Developer newsletter
    • Videos
    • Github

    Get help

    • Customer service
    • Customer support
    • Regional contacts
    • Find a partner

    Join the Red Hat Developer program

    • Download Red Hat products and project builds, access support documentation, learning content, and more.
    • Explore the benefits

Getting started with the fabric8 Kubernetes Java client

May 20, 2020
Rohan Kumar
Related topics:
JavaCI/CDDevOpsKubernetes

    Fabric8 has been available as a Java client for Kubernetes since 2015, and today is one of the most popular client libraries for Kubernetes. (The most popular is client-go, which is the client library for the Go programming language on Kubernetes.) In recent years, fabric8 has evolved from a Java client for the Kubernetes REST API to a full-fledged alternative to the kubectl command-line tool for Java-based development.

    Fabric8 is much more than a simple Java Kubernetes REST client. Its features include a rich domain-specific language (DSL), a model for advanced code handling and manipulation, extension hooks, a mock server for testing, and many client-side utilities. In addition to hooks for building new extensions, the fabric8 Kubernetes Java client has extensions for Knative, Tekton, Kubernetes Service Catalog, Red Hat OpenShift Service Catalog, and Kubernetes Assertions.

    Kubernetes client

    <dependency>  
      <groupId>io.fabric8</groupId>  
      <artifactId>kubernetes-client</artifactId>
      <version>4.10.3</version>
    </dependency>
    

    OpenShift client

    <dependency>  
      <groupId>io.fabric8</groupId>  
      <artifactId>openshift-client</artifactId>
      <version>4.10.3</version>
    </dependency>
    

    Tekton client

    <dependency>  
      <groupId>io.fabric8</groupId>  
      <artifactId>tekton-client</artifactId>
      <version>4.10.3</version>
    </dependency>
    

    Knative client 

    <dependency>  
      <groupId>io.fabric8</groupId>  
      <artifactId>knative-client</artifactId>
      <version>4.10.3</version>
    </dependency>
    

    Istio client

    <dependency>  
      <groupId>me.snowdrop</groupId>
      <artifactId>istio-client</artifactId>  
      <version>1.6.5-Beta2</version>
    </dependency>
    
    Service Catalog client
    <dependency> 
      <groupId>io.fabric8</groupId>  
      <artifactId>servicecatalog-client</artifactId>  
      <version>4.10.3</version>  
      <type>bundle</type>
    </dependency>
    

    Note: The Istio client is not a direct part of the fabric8 repository, but is based on fabric8).

    Also, many popular projects use fabric8 Kubernetes client extensions, including Quarkus, Apache Camel, Apache Spark, and many more. See which projects work with this Kubernetes and OpenShift Java client here.

    Using fabric8 with Kubernetes

    Using fabric8 is straightforward, especially because it offers an API for accessing Kubernetes resources. To get started with the Java client, you just add it as a dependency in your Maven pom.xml:

      <dependency>
        <groupId>io.fabric8</groupId>
        <artifactId>kubernetes-client</artifactId>
        <version>4.10.3</version>
      </dependency>
    

    Alternatively, you could use build.gradle:

    dependencies {
        compile 'io.fabric8:kubernetes-client:4.10.3'
    }
    

    Next, we'll look at a couple of common examples.

    Example 1: Listing pods in a namespace

    Here's an example of listing all of the client pods in a namespace:

    try (KubernetesClient client = new DefaultKubernetesClient()) {
    
        client.pods().inNamespace("default").list().getItems().forEach(
                pod -> System.out.println(pod.getMetadata().getName())
        );
    
    } catch (KubernetesClientException ex) {
        // Handle exception
        ex.printStackTrace();
    }
    

    Example 2: Server authentication

    When you use DefaultKubernetesClient, it will try to read the ~/.kube/config file in your home directory and load information required for authenticating with the Kubernetes API server. You can override this configuration with the system property KUBECONFIG.

    If you are using DefaultKubernetesClient from inside a Pod, it will load ~/.kube/config from the ServiceAccount volume mounted inside the Pod. For a more complex configuration, you can simply pass a Config object inside DefaultKubernetesClient, like this:

    Config config = new ConfigBuilder()
            .withMasterUrl("https://api.rh-idev.openshift.com:443")
            .build();
    try (KubernetesClient client = new DefaultKubernetesClient(config)) {
    
        client.pods().inNamespace("default").list().getItems().forEach(
                pod -> System.out.println(pod.getMetadata().getName())
        );
    
    } catch (KubernetesClientException ex) {
        // Handle exception
        ex.printStackTrace();
    }
    

    Example 3: Creating a Simple Deployment:

    Suppose you want to build up a quick Deployment object and apply it onto Kubernetes Cluster. You can easily leverage on rich builder classes provided by fabric8 to construct your Kubernetes resources on the fly. Here is an example of building up a simple NginxDeployment:

    try (KubernetesClient client = new DefaultKubernetesClient()) {
        Deployment deployment = new DeploymentBuilder()
                .withNewMetadata()
                   .withName("nginx-deployment")
                   .addToLabels("app", "nginx")
                .endMetadata()
                .withNewSpec()
                   .withReplicas(1)
                   .withNewSelector()
                       .addToMatchLabels("app", "nginx")
                   .endSelector()
                   .withNewTemplate()
                       .withNewMetadata()
                          .addToLabels("app", "nginx")
                       .endMetadata()
                       .withNewSpec()
                          .addNewContainer()
                              .withName("nginx")
                              .withImage("nginx:1.7.9")
                              .addNewPort().withContainerPort(80).endPort()
                          .endContainer()
                       .endSpec()
                   .endTemplate()
                .endSpec()
                .build();
    
        client.apps().deployments().inNamespace("default").createOrReplace(deployment);
    }
    

     

    Example 4: Loading your Kubernetes resource YAMLs into Java Objects:

    With Fabric8 Kubernetes Client, you can easily load your resource manifests into Java objects provided by it's Kubernetes Model. Suppose you have a ServiceYAML like this one:

    apiVersion: v1
    kind: Service
    metadata:
      name: my-service
    spec:
      selector:
        app: MyApp
      ports:
        - protocol: TCP
          port: 80
          targetPort: 9376

    Now in order to load this YAML object into a Kubernetes Service object. You need to do something like this:

    Service service = client.services()
            .load(LoadServiceYaml.class.getResourceAsStream("/test-svc.yml"))
            .get();

    Example 5: Doing CRUD operations for Kubernetes resource using Client:

    You can easily create, replace, edit, or delete your Kubernetes resources using Fabric8 Kubernetes Client API. We provide a rich DSL to achieve these operations. Here is an example of basic CRUD operations of a Deployment object:

        // Create
        client.apps().deployments().inNamespace("default").create(deployment);
    
        // Get
        Deployment deploy = client.apps().deployments()
                .inNamespace("default")
                .withName("deploy1")
                .get();
    
        // Update, adding dummy annotation
        Deployment updatedDeploy = client.apps().deployments()
                .inNamespace("default")
                .withName("deploy1")
                .edit()
                .editMetadata().addToAnnotations("foo", "bar").endMetadata()
                .done();
    
        // Deletion
        Boolean isDeleted = client.apps().deployments()
                .inNamespace("default")
                .withName("deploy1")
                .delete();
    
        // Deletion with some propagation policy
        Boolean bDeleted = client.apps().deployments()
                .inNamespace("default")
                .withName("deploy1")
                .withPropagationPolicy(DeletionPropagation.BACKGROUND)
                .delete();
    

     

    Learn more about fabric8

    Fabric8's development team consists of mostly Java developers, so a Java developer's perspective heavily influences this client. In this article, I've demonstrated just a few of fabric8's features for using Kubernetes APIs in a Java environment. For more examples, see the Kubernetes Java client examples repository. And for a deep dive into using fabric8, visit the Fabric8 Kubernetes Java Client Cheat Sheet.

    Last updated: July 17, 2020

    Recent Posts

    • Trusted software factory: Building trust in the agentic AI era

    • Build a zero trust AI pipeline with OpenShift and RHEL CVMs

    • Red Hat Hardened Images: Top 5 benefits for software developers

    • How EvalHub manages two-layer Kubernetes control planes

    • Tekton joins the CNCF as an incubating project

    Red Hat Developers logo LinkedIn YouTube Twitter Facebook

    Platforms

    • Red Hat AI
    • Red Hat Enterprise Linux
    • Red Hat OpenShift
    • Red Hat Ansible Automation Platform
    • See all products

    Build

    • Developer Sandbox
    • Developer tools
    • Interactive tutorials
    • API catalog

    Quicklinks

    • Learning resources
    • E-books
    • Cheat sheets
    • Blog
    • Events
    • Newsletter

    Communicate

    • About us
    • Contact sales
    • Find a partner
    • Report a website issue
    • Site status dashboard
    • Report a security problem

    RED HAT DEVELOPER

    Build here. Go anywhere.

    We serve the builders. The problem solvers who create careers with code.

    Join us if you’re a developer, software engineer, web designer, front-end designer, UX designer, computer scientist, architect, tester, product manager, project manager or team lead.

    Sign me up

    Red Hat legal and privacy links

    • About Red Hat
    • Jobs
    • Events
    • Locations
    • Contact Red Hat
    • Red Hat Blog
    • Inclusion at Red Hat
    • Cool Stuff Store
    • Red Hat Summit
    © 2026 Red Hat

    Red Hat legal and privacy links

    • Privacy statement
    • Terms of use
    • All policies and guidelines
    • Digital accessibility

    Chat Support

    Please log in with your Red Hat account to access chat support.