3 Nisan 2023 Pazartesi

Headless Service - Load-balancer'a Uğramadan Direkt DNS Kaydı İle Bir POD'a Erişilebilir

Giriş
Açıklaması şöyle. Böylece load-balancer'a uğramadan direkt DNS kaydı ile bir POD'a erişilebilir.
A headless service in Kubernetes can be a useful tool for creating distributed applications. It allows you to directly access the individual pods in a service.
Örnek
Şöyle yaparız
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  clusterIP: None
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376
Açıklaması şöyle
To create a headless service in Kubernetes, we need to define a service with the clusterIP field set to "None". 

In this example, we’ve defined a headless service named “my-service”. We’ve set the clusterIP field to "None" to indicate that we want a headless service. We've also specified a selector to associate pods with the service. The ports field specifies the ports that the service will forward traffic to.

Once you’ve created a headless service, you can access each pod associated with the service through DNS. The DNS record for each pod will be in the format <pod-name>.<headless-service-name>.<namespace>.svc.cluster.local.
Örnek - StatefulSet
Elimizde şöyle bir headless service olsun
apiVersion: v1
kind: Service
metadata:
  name: my-db-service
spec:
  clusterIP: None
  selector:
    app: my-db
  ports:
    - protocol: TCP
      port: 3306
      targetPort: 3306
Şöyle yaparız
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: my-db-statefulset
spec:
  serviceName: my-db-service
  replicas: 3
  selector:
    matchLabels:
      app: my-db
  template:
    metadata:
      labels:
        app: my-db
    spec:
      containers:
      - name: my-container
        image: my-db-image
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: my-password
  volumeClaimTemplates:
  - metadata:
      name: my-pvc
    spec:
      accessModes: [ "ReadWriteOnce" ]
      resources:
        requests:
          storage: 10Gi
Örnek - StatefulSet
Şöyle yaparız
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: kafka
spec:
  selector:
    matchLabels:
      app: kafka
  serviceName: kafka
  replicas: 3
  updateStrategy:
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: kafka
    spec:
      hostname: kafka
      containers:
        - name: kafka
          image: <kafka-image>
          env:
            - name: KAFKA_ZOOKEEPER_CONNECT
              value: <zookeeper-endpoints>
            - name: KAFKA_ADVERTISED_LISTENERS
              value: PLAINTEXT://$(hostname -f):9092
          ports:
            - containerPort: 9092
              name: kafka
          volumeMounts:
            - name: data
              mountPath: /var/lib/kafka/data
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: kafka-data
  volumeClaimTemplates:
    - metadata:
        name: kafka-data
      spec:
        accessModes:
          - ReadWriteOnce
        resources:
          requests:
            storage: 10Gi

---

apiVersion: v1
kind: Service
metadata:
  name: kafka
spec:
  clusterIP: None
  ports:
    - name: kafka
      port: 9092
      targetPort: 9092
  selector:
    app: kafka


Örnek - Deployment
Şöyle yaparız
apiVersion: v1
kind: Service
metadata:
  name: my-service-a
spec:
  clusterIP: None
  selector:
    app: my-service-a
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-service-a-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-service-a
  template:
    metadata:
      labels:
        app: my-service-a
    spec:
      containers:
      - name: my-container
        image: my-service-a-image
        ports:
        - containerPort: 8080

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