27 Temmuz 2023 Perşembe

Fabric8 Kubernetes Client

Maven
Örnek - BOM
Şu satırı dahil ederiz
<dependency>
  <groupId>io.fabric8</groupId>
  <artifactId>kubernetes-client-bom</artifactId>
  <version>6.9.2</version>
  <type>pom</type>
  <scope>import</scope>
</dependency>
Örnek
Şu satırı dahil ederiz
<dependency>
<groupId>io.fabric8</groupId> <artifactId>kubernetes-client</artifactId> <version>6.9.2</version> </dependency>
constructor
Şöyle yaparız
KubernetesClient kubernetesClient = new DefaultKubernetesClient().inNamespace(namespace);
namespaces metodu
Örnek
Şöyle yaparız
import io.fabric8.kubernetes.api.model.Namespace;
import io.fabric8.kubernetes.api.model.NamespaceBuilder;

public void createOrReplaceNamespace(String namespaceName) {
  Namespace namespace = new NamespaceBuilder()
    .withNewMetadata()
    .withName(namespaceName)
    .endMetadata()
    .build();
  kubernetesClient.namespaces().createOrReplace(namespace);
}
Örnek
Şöyle yaparız
public void deleteNamespace(String namespaceName) {
  kubernetesClient.namespaces().withName(namespaceName).delete();
}
pods metodu
Örnek
Şöyle yaparız
import io.fabric8.kubernetes.api.model.Pod;

public List<Pod> allPodsStartingWith(String prefix) {
  return kubernetesClient.pods().list().getItems().stream()
    .filter(p -> p.getMetadata().getName().startsWith(prefix))
    .collect(Collectors.toList());
}
Örnek
Şöyle yaparız
public void deletePod(String podName) {
  kubernetesClient.pods().withName(podName).delete();
}

public void deletePodForcefully(String podName) {
  kubernetesClient.pods().withName(podName).withGracePeriod(0).delete();
}
Örnek
Şöyle yaparız
public String getPodLog(String podName) {
  return kubernetesClient.pods().withName(podName).getLog();
}

public String getPodStatus(String podName) {
  Pod pod = kubernetesClient.pods().withName(podName).get();
  return pod.getStatus().getPhase();
}
services metodu
Örnek
Şöyle yaparız
import io.fabric8.kubernetes.api.model.LoadBalancerIngress;
import io.fabric8.kubernetes.api.model.Service;
import io.fabric8.kubernetes.api.model.apps.StatefulSetStatus;


public String getServiceExternalIp(String serviceName) {
  Service service = kubernetesClient.services().withName(serviceName).get();
  if (service == null) {
    throw new IllegalStateException("Service '" + serviceName + "' not found");
   }
   return getIngressIp(service);
}

private static String getIngressIp(Service service) {
  List<LoadBalancerIngress> ingress = service.getStatus().getLoadBalancer().getIngress();
  if (ingress != null && !ingress.isEmpty()) {
    return ingress.get(0).getIp();
  }
  throw new IllegalStateException("Ingress for '" + 
    service.getMetadata().getName() + "' not found");
}




Hiç yorum yok:

Yorum Gönder

Cluster Propotional Autoscaler - ReplicaSet Ekler/Siler

Giriş Açıklaması şöyle CPA aims to horizontally scale the number of Pod replicas based on the cluster’s scale. A common example is DNS ser...