27 Kasım 2023 Pazartesi

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 services. CPA can dynamically adjust the number of DNS instances based on the current cluster scale, which can be either the number of nodes or the overall CPU capacity.

2 Ekim 2023 Pazartesi

Kubernetes kind: Role

Örnek
Elimizde şöyle bir Role olsun. Bu Role pods ve configmaps kaynaklarını watch, get vs yapabilir. Yani izleyebilir.
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: leader
  labels:
    app: kubernetes-leader-election-example
    group: org.springframework.cloud
rules:
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - watch
  - get
- apiGroups:
  - ""
  resources:
  - configmaps
  verbs:
  - watch
  - get
  - update
  # resourceNames:
  #   - <config-map name>
Bu Rolü kendime atarım
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  labels:
    app: kubernetes-leader-election-example
    group: org.springframework.cloud
  name: leader
roleRef:
  apiGroup: ""
  kind: Role
  name: leader
subjects:
- kind: ServiceAccount
  name: default
  apiGroup: ""


18 Eylül 2023 Pazartesi

Kubernetes kind: Pod dnsConfig

Giriş
Açıklaması şöyle
To change the behavior of a pod’s DNS resolver, you can change the DNS config of a pod
Örnek
Şöyle yaparız
apiVersion: v1
kind: Pod
metadata:
  namespace: default
  name: dns-example
spec:
  containers:
    - name: test
      image: nginx
  dnsPolicy: "None"
  dnsConfig:
    nameservers:
      - 1.2.3.4
    searches:
      - ns1.svc.cluster-domain.example
      - my.dns.search.suffix
    options:
      - name: ndots
        value: "2"
      - name: edns0
Açıklaması şöyle
In the example above, the dnsPolicy is set to "None", which means that the pod will not use the default DNS settings provided by the cluster. Instead, the dnsConfig field is used to specify custom DNS settings for the pod.

The nameservers field is used to specify the DNS servers that the pod should use for DNS lookups. The searches field is used to specify the search domains that should be used for incomplete domains.

The options field is used to specify custom options for the DNS resolver, such as the ndots and edns0 options in the example above.

These settings will be used by the pod’s DNS resolver instead of the default settings provided by the cluster. For more information on pod DNS configuration, see the official docs.

31 Temmuz 2023 Pazartesi

fieldRef'i Environment Olarak Kullanma

Giriş
Deployment içindeki bazı değerleri environment variable olarak eklemek mümkün
1. Pod valueFrom/fieldRef/fieldPath ile istenilen field değerini environment variable olarak yükler.

Örnek
Şöyle yaparız
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deployment
spec:
  replicas: 2
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
        - name: my-app-container
          image: my-app-image:latest
          ports:
            - containerPort: 8081
          env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
SpringBoot uygulamasından okumak için şöyle yaparız. Böylece POD ismine ortam değişkeni olarak erişebiliriz.
@RestController
@RequestMapping("/example")
public class MyController {

  @Value("${POD_NAME}")
  private String podName;

  @GetMapping("/getPodName")
  public void printPodName() {
    System.out.println(podName);
  }
}


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");
}




Fabric8 Kubernetes Mock Server

Giriş
Bu kütüphane WireMock ile de kullanılabilir. Bir örnek burada

Bir yazı burada

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-server-mock</artifactId>
  <version>${fabric8.version}</version>
  <scope>test</scope>
</dependency>

14 Temmuz 2023 Cuma

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...