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
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
Açıklaması şöyleapiVersion: v1kind: Service # 1metadata:name: sa-frontend-lbspec:type: LoadBalancer # 2ports:- port: 80 # 3protocol: TCP # 4targetPort: 80 # 5selector: # 6app: sa-frontend # 7
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/v1kind: Deploymentmetadata:name: springbootnamespace: springboot-projectlabels:app: springbootspec:replicas: 3selector:matchLabels:app: springboottemplate:metadata:labels:app: springbootspec:containers:- name: springbootimage: byckles/jpa_project:2.0.0imagePullPolicy: IfNotPresentports:- containerPort: 8082env:- name: SPRING_DATASOURCE_URLvalueFrom:configMapKeyRef:name: mysql-configmapkey: database_url- name: SPRING_DATASOURCE_USERNAMEvalueFrom:secretKeyRef:key: mysql-user-usernamename: mysql-secret- name: SPRING_DATASOURCE_PASSWORDvalueFrom:secretKeyRef:key: mysql-user-passwordname: mysql-secret
Service şöyledir
apiVersion: v1kind: Servicemetadata:name: springboot-servicenamespace: springboot-projectspec:selector:app: springboottype: LoadBalancerports:- protocol: TCPport: 8082targetPort: 8082nodePort: 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 versionkind: 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
Örnek
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