Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

즉, Service discovery 방식으로 해결하게 되는데, …to do즉 도메인 방식으로 접근한다.
기본적으로 K8s를 Initialize하게 되면, coredns pod가 생성되며, 생성된 모든 pod는 여기 등록되어 아래와 같이 kube-dns 서비스로 dns 쿼리하여 pod IP를 알아낸 후 통신을 연결하게 된다.

Code Block
$ kubectl get svc -A
NAMESPACE              NAME                        TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                  AGE
default                kubernetes                  ClusterIP      10.96.0.1        <none>        443/TCP                  13d
kube-system            kube-dns                    ClusterIP      10.96.0.10       <none>        53/UDP,53/TCP,9153/TCP   13d

정리하자면, 각각의 Pod는 언제든 IP가 변경될 수 있으며 할당된 IP는 label selector로 지정된 서비스를 통해 도메인으로 통신하게 된다.

이제 아래 예제를 통해 이해를 높여보자.

외부에서의 Pod 접속 테스트

nginx-deployment.yaml

...

Code Block
languageyaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: test
  labels:
    app: nginx
spec:
  replicas: 2
  selectorselector:
    matchLabels:
      app: nginx #  label을 matchLabels:Service object에 지정해야 서비스 도메인을 통한 통신 app:연결이 nginx수행됨.
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:stable-alpine3.17-slim
        ports:
        - containerPort: 80

...

Code Block
languageyaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
  namespace: test
spec:
  selector:
    app: nginx #  matchLabels에 지정되었던 label 지정.
  type: ClusterIP
  ports:
    - name: http
      port: 80 # targetPort를 지정하지 않을 경우, targetPort=port 설정 값.
  externalIPs:
    - 10.20.2.236
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc-lb
  namespace: test
spec:
  selector:
    app: nginx #  matchLabels에 지정되었던 label 지정.
  type: LoadBalancer
  ports:
    - name: http
      port: 8080
      protocol: TCP
      targetPort: 80
      #nodePort: 30036 # 생략 시, 임의값 지정됨.(30000-32767)
  externalIPs:
    - 10.20.2.235
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc-np
  namespace: test
spec:
  selector:
    app: nginx # 위 matchLabels에 지정되었던 label 지정.
  type: NodePort
  ports:
    - name: http
      port: 8080
      protocol: TCP
      targetPort: 80
      #nodePort: 30037 # 생략 시, 임의값 지정됨.(30000-32767)
  externalIPs:
    - 10.20.2.236

...