쿠버네티스 서비스 ClusterIP, SessionAffinity,Endpoint, nodeport

신상우·2024년 1월 2일

쿠버네티스

목록 보기
21/26
post-thumbnail

https://kubernetes.io/docs/concepts/services-networking/service/

sessionAffinity - 로그인시 다른세션접속시 로그인처리가 안되어있을수있어서 세션을 고정 시킴

kubectl get svc
kubectl describe svc 이름 -세부사항 볼수있음

실습 GCP

kubectl run --image=gasbugs/http-go http-go --port=8080 --dry-run -o yaml > http-go-deploy.yaml

apiVersion: v1
kind: Service
metadata:
  name: my-service이름
spec:
  selector:
    run: 이름
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376

추가
kubectl create -f http-go-deploy.yaml

kubectl get all 조회
kubectl get pod -o wide 에서 ip 기억해두기
kubectl describe svc
kubectl edit svc http-go-svc 후
SessionAffinity: None > SessionAffinity:ClientIP로 변경하면 하나의 포드에 접속하면 그ip로만 접속가능 wq 저장후 나감

확인을 위해서
kubectl run it --rm --image=busybox bash
wget -o- -q ip 같은 ip로 가는거 확인가능

엔드포인트

"---" 하고

apiVersion: discovery.k8s.io/v1
kind: EndpointSlice
metadata:
  name: my-service-1 # by convention, use the name of the Service
                     # as a prefix for the name of the EndpointSlice
  labels:
    # You should set the "kubernetes.io/service-name" label.
    # Set its value to match the name of the Service
    kubernetes.io/service-name: my-service
addressType: IPv4
ports:
  - name: '' # empty because port 9376 is not assigned as a well-known
             # port (by IANA)
    appProtocol: http
    protocol: TCP
    port: 9376
endpoints:
  - addresses:
      - "10.4.5.6"
  - addresses:
      - "10.1.2.3"

추가

nodeport

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: NodePort
  selector:
    app.kubernetes.io/name: MyApp
  ports:
    - port: 80
      # By default and for convenience, the `targetPort` is set to
      # the same value as the `port` field.
      targetPort: 80
      # Optional field
      # By default and for convenience, the Kubernetes control plane
      # will allocate a port from a range (default: 30000-32767)
      nodePort: 30001

지정 안하면 30000~32767 랜덤으로 지정

구글클라우드플랫폼에서 방화벽 열어주기
예제) gcloud compute firewall-rules create http-go-svc-rule --allow=tcp:30001

LoadBalancer

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app.kubernetes.io/name: MyApp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 9376
  clusterIP: 10.0.171.239
  type: LoadBalancer
status:
  loadBalancer:
    ingress:
    - ip: 192.0.2.127

실습 톰캣 nodeport 만들기
kubectl run tomcat --image=tomcat --port=8080 --dry-run -o yaml > tomcat-deploy-np-lb.yaml

vim tomcat-deploy-np-lb.yaml
들어가서 --- 밑에

apiVersion: v1
kind: Service
metadata:
  name: tomcat-np
spec:
  type: NodePort
  selector:
    run: tomact
  ports:
    - port: 80
      # By default and for convenience, the `targetPort` is set to
      # the same value as the `port` field.
      targetPort: 8080
      # Optional field
      # By default and for convenience, the Kubernetes control plane
      # will allocate a port from a range (default: 30000-32767)
      nodePort: 30002
 ---
 apiVersion: v1
kind: Service
metadata:
  name: tomcat-lb
spec:
  type: LoadBalancer
  selector:
    run: tomact
  ports:
    - port: 80
      targetPort: 8080

kubectl create -f tomcat-deploy-np-lb.yaml

kubectl get svc
ip 접속 후 구글클라우드플랫폼 방화벽 > gcloud compute firewall-rules create tomcat-svc-rule --allow=tcp:30002

profile
기록 남기기

0개의 댓글