Skip to content

Latest commit

ย 

History

History
261 lines (234 loc) ยท 5.16 KB

kubernetes-service.md

File metadata and controls

261 lines (234 loc) ยท 5.16 KB

Kubernetes ์„œ๋น„์Šค์™€ ๋„คํŠธ์›Œํ‚น์— ๋Œ€ํ•œ ๊ธฐ์ˆ  ๋ฉด์ ‘ ๋‹ต๋ณ€

์ฃผ์š” ์งˆ๋ฌธ

"Kubernetes์˜ ์„œ๋น„์Šค ํƒ€์ž…๋“ค๊ณผ ๊ฐ๊ฐ์˜ ์šฉ๋„์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”. ๋˜ํ•œ Pod ๊ฐ„ ํ†ต์‹ ์ด ์–ด๋–ป๊ฒŒ ์ด๋ฃจ์–ด์ง€๋Š”์ง€ ์„ค๋ช…ํ•ด์ฃผ์„ธ์š”."

1. ์„œ๋น„์Šค ํƒ€์ž…

1.1 ClusterIP

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: ClusterIP
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080

1.2 NodePort

apiVersion: v1
kind: Service
metadata:
  name: my-nodeport-service
spec:
  type: NodePort
  selector:
    app: my-app
  ports:
    - port: 80
      targetPort: 8080
      nodePort: 30007  # 30000-32767 ๋ฒ”์œ„

1.3 LoadBalancer

apiVersion: v1
kind: Service
metadata:
  name: my-lb-service
spec:
  type: LoadBalancer
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
  externalTrafficPolicy: Local

1.4 ExternalName

apiVersion: v1
kind: Service
metadata:
  name: my-database
spec:
  type: ExternalName
  externalName: database.example.com

2. ๋„คํŠธ์›Œํฌ ์ •์ฑ…

2.1 ๊ธฐ๋ณธ ๋„คํŠธ์›Œํฌ ์ •์ฑ…

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: api-allow
spec:
  podSelector:
    matchLabels:
      app: api
  policyTypes:
    - Ingress
    - Egress
  ingress:
    - from:
        - podSelector:
            matchLabels:
              role: frontend
      ports:
        - protocol: TCP
          port: 8080
  egress:
    - to:
        - podSelector:
            matchLabels:
              role: database
      ports:
        - protocol: TCP
          port: 5432

2.2 CIDR ๊ธฐ๋ฐ˜ ์ •์ฑ…

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-external-traffic
spec:
  podSelector:
    matchLabels:
      app: web
  policyTypes:
    - Ingress
  ingress:
    - from:
        - ipBlock:
            cidr: 172.17.0.0/16
            except:
              - 172.17.1.0/24

3. Ingress ์„ค์ •

3.1 ๊ธฐ๋ณธ Ingress

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: minimal-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  ingressClassName: nginx
  rules:
  - host: myapp.example.com
    http:
      paths:
      - path: /api
        pathType: Prefix
        backend:
          service:
            name: api-service
            port:
              number: 80
      - path: /web
        pathType: Prefix
        backend:
          service:
            name: web-service
            port:
              number: 80

3.2 TLS ์„ค์ •

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: tls-ingress
spec:
  tls:
  - hosts:
    - myapp.example.com
    secretName: tls-secret
  rules:
  - host: myapp.example.com
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: web-service
            port:
              number: 80

4. ์„œ๋น„์Šค ๋””์Šค์ปค๋ฒ„๋ฆฌ์™€ DNS

4.1 CoreDNS ์„ค์ •

apiVersion: v1
kind: ConfigMap
metadata:
  name: coredns
  namespace: kube-system
data:
  Corefile: |
    .:53 {
        errors
        health
        kubernetes cluster.local in-addr.arpa ip6.arpa {
            pods insecure
            upstream
            fallthrough in-addr.arpa ip6.arpa
        }
        prometheus :9153
        forward . /etc/resolv.conf
        cache 30
        loop
        reload
        loadbalance
    }

๊ทผ๊ฑฐ ์ž๋ฃŒ

1. ๊ณต์‹ ๋ฌธ์„œ

2. ๋ชจ๋ฒ” ์‚ฌ๋ก€

์‹ค๋ฌด ๊ด€๋ จ ์ถ”๊ฐ€ ์งˆ๋ฌธ

  1. "์„œ๋น„์Šค ๋ฉ”์‹œ(Service Mesh)๋ฅผ ์‚ฌ์šฉํ•ด๋ณด์…จ๋‚˜์š”? ์žฅ๋‹จ์ ์€ ๋ฌด์—‡์ธ๊ฐ€์š”?"

  2. "๋ฉ€ํ‹ฐ ํด๋Ÿฌ์Šคํ„ฐ ํ™˜๊ฒฝ์—์„œ ์„œ๋น„์Šค ๋””์Šค์ปค๋ฒ„๋ฆฌ๋Š” ์–ด๋–ป๊ฒŒ ๊ตฌํ˜„ํ•˜์‹œ๋‚˜์š”?"

  3. "Ingress ์ปจํŠธ๋กค๋Ÿฌ ์„ ํƒ ์‹œ ๊ณ ๋ ค์‚ฌํ•ญ์€ ๋ฌด์—‡์ธ๊ฐ€์š”?"

  4. "๋„คํŠธ์›Œํฌ ์ •์ฑ…์œผ๋กœ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ๊ฐ„ ํ†ต์‹ ์„ ์–ด๋–ป๊ฒŒ ์ œ์–ดํ•˜์‹œ๋‚˜์š”?"

์‹ค์ œ ์‚ฌ์šฉ ์˜ˆ์‹œ

์„œ๋น„์Šค ๋ชจ๋‹ˆํ„ฐ๋ง

# ์„œ๋น„์Šค ์—ฐ๊ฒฐ์„ฑ ํ…Œ์ŠคํŠธ
kubectl run -i --tty --rm debug \
  --image=busybox --restart=Never \
  -- wget -O- http://my-service:80

# ์—”๋“œํฌ์ธํŠธ ํ™•์ธ
kubectl get endpoints my-service

ํŠธ๋ž˜ํ”ฝ ์ œ์–ด

# ํŠธ๋ž˜ํ”ฝ ๋ถ„ํ•  ์˜ˆ์‹œ
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
  name: my-service-split
spec:
  hosts:
  - my-service
  http:
  - route:
    - destination:
        host: my-service-v1
        subset: v1
      weight: 90
    - destination:
        host: my-service-v2
        subset: v2
      weight: 10

์‹ค์ œ ๋ฉด์ ‘์—์„œ๋Š” ์ด๋ก ์ ์ธ ์ง€์‹๊ณผ ํ•จ๊ป˜ ๋„คํŠธ์›Œํฌ ๊ตฌ์„ฑ ๊ฒฝํ—˜, ํŠธ๋Ÿฌ๋ธ”์ŠˆํŒ… ๊ฒฝํ—˜, ์„ฑ๋Šฅ ์ตœ์ ํ™” ๊ฒฝํ—˜ ๋“ฑ์„ ๊ตฌ์ฒด์ ์œผ๋กœ ์„ค๋ช…ํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.