26 Ocak 2022 Çarşamba

Kubernetes kind: Service İle LoadBalancer Service - Podlar arasında Yük Dengeler

Giriş
kind : Service 
spec/type: LoadBalancer
şeklinde kullanılır.

spec/selector/app ile servisler veya pod'lar belirtilir. targetPort ile gelen istek belirtilen porta gönderilir.

Bu servis yaratılınca otomatik olarak NodePort ve ClusterIP de yaratılır. Açıklaması şöyle
Exposes the Service externally using a cloud provider's load balancer. NodePort and ClusterIP Services, to which the external load balancer routes, are automatically created.
Örnek
Şöyle yaparız
apiVersion: v1
kind: Service
metadata:
  name: app-users
  labels:
    app: app-users
spec:
  type: LoadBalancer
  ports:
    - port: 8080
      protocol: TCP
      targetPort: 8080
  selector:
    app: app-users
Örnek
Şöyle yaparız
apiVersion: v1
kind: Service
metadata:
  name: redis-service
  labels:
    app: redis
spec:
  selector:
    app: redis
  ports:
  - port: 80
    targetPort: 6379
    protocol: "TCP"
    name: redis
  type: LoadBalancer
Örnek
Şöyle yaparız. Burada adv-vtgate-az1 isimli NodePort servisini dış dünyaya açıyoruz.
kubectl expose service -n rlwy03 adv-vtgate-az1 --type=LoadBalancer --name=my-service
Dış dünyaya açılan servis şöyledir
$ kubectl describe services my-service -n rlwy03
Name:                     my-service
Namespace:                rlwy03
Labels:                   app.kubernetes.io/managed-by=Helm
                          planetscale.com/cell=az1
                          planetscale.com/cluster=adv-vitess-cluster
                          planetscale.com/component=vtgate
Annotations:              <none>
Selector:                 planetscale.com/cell=az1,planetscale.com/cluster=adv-vitess-cluster,planetscale.com/component=vtgate
Type:                     LoadBalancer
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       172.30.97.99
IPs:                      172.30.97.99
LoadBalancer Ingress:     34.68.182.24

Port:                     port-1  15000/TCP
TargetPort:               15000/TCP
NodePort:                 port-1  30879/TCP
Endpoints:                10.129.2.37:15000,10.131.2.31:15000

Port:                     port-2  15999/TCP
TargetPort:               15999/TCP
NodePort:                 port-2  30963/TCP
Endpoints:                10.129.2.37:15999,10.131.2.31:15999

Port:                     port-3  3306/TCP
TargetPort:               3306/TCP
NodePort:                 port-3  31515/TCP
Endpoints:                10.129.2.37:3306,10.131.2.31:3306

Session Affinity:         None
External Traffic Policy:  Cluster
Events:
  Type    Reason                Age   From                Message
  ----    ------                ----  ----                -------
  Normal  EnsuringLoadBalancer  120m  service-controller  Ensuring load balancer
  Normal  EnsuredLoadBalancer   119m  service-controller  Ensured load balancer
Doyasıyla şöyle bağlanabilirim
$ mysql -h 34.68.182.24 -u myuser -pmypassword
Örnek
Şöyle yaparız. port dış dünyaya açılır. Gelen isteği targetPort'a gönderir
namespace: default
  labels:
    app: my-example
spec:
  type: LoadBalancer
  ports:
  - port: 8080
    targetPort: 8080
    name: http
  - port: 22
    targetPort: 22
    name: ssh
    protocol: TCP
  selector:
    app: my-example
Örnek 
Şöyle yaparız
apiVersion: v1
kind: Service              # 1
metadata:
  name: sa-frontend-lb
spec:
  type: LoadBalancer       # 2
  ports:
  - port: 80               # 3
    protocol: TCP          # 4
    targetPort: 80         # 5
  selector:                # 6
    app: sa-frontend       # 7
Açıklaması şöyle
1. Kind: A service.
2. Type: Specification type, we choose LoadBalancer because we want to balance the load between the pods.
3. Port: Specifies the port in which the service gets requests.
4. Protocol: Defines the communication.
5. TargetPort: The port at which incoming requests are forwarded.
6. Selector: Object that contains properties for selecting pods.
7. app: sa-frontend Defines which pods to target, only pods that are labeled with “app: sa-frontend”
Örnek
Deployment şöyledir
apiVersion: apps/v1
kind: Deployment
metadata:
  name: springboot
  namespace: springboot-project
  labels:
    app: springboot
spec:
  replicas: 3
  selector:
    matchLabels:
      app: springboot
  template:
    metadata:
      labels:
        app: springboot
    spec:
      containers:
        - name: springboot
          image: byckles/jpa_project:2.0.0
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 8082
          env:
            - name: SPRING_DATASOURCE_URL
              valueFrom:
                configMapKeyRef:
                  name: mysql-configmap
                  key: database_url
            - name: SPRING_DATASOURCE_USERNAME
              valueFrom:
                secretKeyRef:
                  key: mysql-user-username
                  name: mysql-secret
            - name: SPRING_DATASOURCE_PASSWORD
              valueFrom:
                secretKeyRef:
                  key: mysql-user-password
                  name: mysql-secret
Service şöyledir
apiVersion: v1
kind: Service
metadata:
  name: springboot-service
  namespace: springboot-project
spec:
  selector:
    app: springboot
  type: LoadBalancer
  ports:
    - protocol: TCP
      port: 8082
      targetPort: 8082
      nodePort: 30001
Örnek 
Şöyle  yaparız. pod tarafında spec/selector/matchLabels ile service tarafında spec/selector/app uyuşmalıdır
apiVersion: v1 # Kubernetes API version
kind: Service # Kubernetes resource kind we are creating metadata: # Metadata of the resource kind we are creating name: student-kubernetes-demo spec: selector: app: student-kubernetes-demo ports: - protocol: "TCP" port: 8080 # The port that the service is running on in the cluster targetPort: 8080 # The port exposed by the service type: LoadBalancer # LoadBalancer indicates that our service will be external. --- apiVersion: apps/v1 kind: Deployment # Kubernetes resource kind we are creating metadata: name: student-kubernetes-demo spec: selector: matchLabels: app: student-kubernetes-demo replicas: 2 # Number of replicas that will be created for this deployment template: metadata: labels: app: student-kubernetes-demo spec: containers: - name: student-kubernetes-demo image: student-kubernetes-demo imagePullPolicy: IfNotPresent ports:             - containerPort: 8080 # port that the container is running on in the cluster
loadBalancerIP Alanı
Örnek
Şöyle yaparız. Burada loadBalancer tarafından kullanılacak IP belirtiliyor.
apiVersion: v1
kind: Service
metadata:
  name: my-frontend-service
spec:
  type: LoadBalancer
  clusterIP: 10.0.171.123
  loadBalancerIP: 123.123.123.123
  selector:
    app: web
  ports:
  - name: http
    protocol: TCP
    port: 80
    targetPort: 8080
Örnek
Şöyle yaparız
apiVersion: v1
kind: Service
metadata:
  name: order-service
  namespace: default
  labels:
    app: order-service
spec:
  selector:
    app: order-deployment
  ports:
    - name: order-service-port
      protocol: TCP
      port: 8181
  type: LoadBalancer
  loadBalancerIP: ""



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