Kubernetes - Service (서비스)

category DevOps/Kubernetes 2021. 11. 30. 13:37
728x90
반응형

Service (서비스)

정의

  • 동일한 서비스를 제공하는 Pod 그룹의 단일 진입점을 제공
  • 파드들의 하나의 IP로 묶어서 관리를 해줌

Service 종류

이미지 출처 : https://medium.com/devops-mojo/kubernetes-service-types-overview-introduction-to-k8s-service-types-what-are-types-of-kubernetes-services-ea6db72c3f8c

ClusterIP

  • selector의 label가 동일한 파드들의 그룹으로 묶어 단일 진입점 (Virtual_IP)을 생성
  • 클러스터 내부에서만 사용가능
  • type 생략 시 default 값으로 10.96.0.0/12 범위에서 할당됨

생성

사전에 Deployment를 먼저 생성하고 그 다음에 Service를 생성하여 ClusterIP로 묶어준다.

root@master:~# cat > deploy-nginx.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: webui
spec:
  replicas: 3
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      name: nginx-pod
      labels:
        app: webui
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.14
root@master:~# kubectl create -f deploy-nginx.yaml 
deployment.apps/webui created
root@master:~# kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP          NODE                NOMINATED NODE   READINESS GATES
webui-6d4c4cc4b8-b6f72   1/1     Running   0          24s   10.44.0.3   node2.example.com   <none>           <none>
webui-6d4c4cc4b8-hngvt   1/1     Running   0          24s   10.36.0.2   node1.example.com   <none>           <none>
webui-6d4c4cc4b8-llgbx   1/1     Running   0          24s   10.36.0.3   node1.example.com   <none>           <none>

root@master:~# cat > clusterip-nginx.yaml
apiVersion: v1
kind: Service
metadata:
  name: clusterip-service
spec:
  type: ClusterIP
  clusterIP: 10.100.100.100
  selector:
    app:  webui
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
root@master:~# kubectl create -f clusterip-nginx.yaml 
service/clusterip-service created

조회

root@master:~# kubectl get svc -o wide
NAME                TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)   AGE   SELECTOR
clusterip-service   ClusterIP   10.100.100.100   <none>        80/TCP    6s    app=webui
kubernetes          ClusterIP   10.96.0.1        <none>        443/TCP   18d   <none>

root@master:~# kubectl describe svc clusterip-service 
Name:              clusterip-service
Namespace:         default
Labels:            <none>
Annotations:       <none>
Selector:          app=webui
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.100.100.100
IPs:               10.100.100.100
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.36.0.1:80,10.44.0.1:80,10.44.0.2:80
Session Affinity:  None
Events:            <none>

삭제

root@master:~# kubectl delete svc clusterip-service 
service "clusterip-service" deleted

 

NodePort

  • 모든 노드를 대상으로 외부 접속 가능한 포트를 예약
  • Default NodePort 범위: 30000-32767
  • ClusterIP를 생성 후 NodePort 예약

생성

사전에 Deployment를 먼저 생성하고 그 다음에 Service를 생성하여 ClusterIP로 묶어준다.

root@master:~# cat > deploy-nginx.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: webui
spec:
  replicas: 3
  selector:
    matchLabels:
      app: webui
  template:
    metadata:
      name: nginx-pod
      labels:
        app: webui
    spec:
      containers:
      - name: nginx-container
        image: nginx:1.14
root@master:~# kubectl create -f deploy-nginx.yaml
deployment.apps/webui created
root@master:~# kubectl get pods -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP          NODE                NOMINATED NODE   READINESS GATES
webui-6d4c4cc4b8-l4zzf   1/1     Running   0          7s    10.44.0.1   node2.example.com   <none>           <none>
webui-6d4c4cc4b8-whlxm   1/1     Running   0          7s    10.36.0.1   node1.example.com   <none>           <none>
webui-6d4c4cc4b8-z44b4   1/1     Running   0          7s    10.44.0.2   node2.example.com   <none>           <none>

root@master:~# cat > nodeport-nginx.yaml
apiVersion: v1
kind: Service
metadata:
  name: nodeport-service
spec:
  type: NodePort
  clusterIP: 10.100.100.200
  selector:
    app:  webui
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    nodePort: 30200
root@master:~# kubectl create -f nodeport-nginx.yaml 
service/nodeport-service created

조회

master, node1, node2에서 port를 조회해보면 30200으로 LISTEN 상태인 것을 확인할 수 있다.

root@master:~# kubectl get svc -o wide
NAME               TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE   SELECTOR
kubernetes         ClusterIP   10.96.0.1        <none>        443/TCP        18d   <none>
nodeport-service   NodePort    10.100.100.200   <none>        80:30200/TCP   20s   app=webui

root@master:~# kubectl describe svc nodeport-service 
Name:                     nodeport-service
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=webui
Type:                     NodePort
IP Family Policy:         SingleStack
IP Families:              IPv4
IP:                       10.100.100.200
IPs:                      10.100.100.200
Port:                     <unset>  80/TCP
TargetPort:               80/TCP
NodePort:                 <unset>  30200/TCP
Endpoints:                10.36.0.1:80,10.44.0.1:80,10.44.0.2:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

oot@master:~# netstat -napt | grep 30200
tcp        0      0 0.0.0.0:30200           0.0.0.0:*               LISTEN      3528/kube-proxy     
root@node1:~# netstat -napt | grep 30200
tcp        0      0 0.0.0.0:30200           0.0.0.0:*               LISTEN      1670/kube-proxy     
root@node2:~# netstat -napt | grep 30200
tcp        0      0 0.0.0.0:30200           0.0.0.0:*               LISTEN      1424/kube-proxy

삭제

root@master:~# kubectl delete svc nodeport-service 
service "nodeport-service" deleted

 

LoadBalancer

  • Public 클라우드 (AWS, Azure, GCP 등)에서 운영가능
  • LoadBalancer를 자동으로 구성 요청
  • NodePort를 예약 후 해당 nodeport로 외부 접근을 허용

 

ExternalName

  • 클러스터 내부에서 External(외부)의 도메인을 설정

생성

root@master:~# cat > external-name.yaml
apiVersion: v1
kind: Service
metadata:
  name: externalname-svc
spec:
  type: ExternalName
  externalName: google.com
root@master:~# kubectl create -f external-name.yaml 
service/externalname-svc created

조회

root@master:~# kubectl get svc -o wide
NAME               TYPE           CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE   SELECTOR
externalname-svc   ExternalName   <none>       google.com    <none>    4s    <none>
kubernetes         ClusterIP      10.96.0.1    <none>        443/TCP   18d   <none>

확인

centos pod를 생성하고 파드 안으로 진입하여 curl로 service 이름을 호출해본다.

root@master:~# kubectl run testpod -it --image=centos:7
If you dont see a command prompt, try pressing enter.

[root@testpod /]# curl externalname-svc.default.svc.cluster.local
<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 404 (Not Found)!!1</title>
  <style>
    *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) 0}}@media only screen and (-webkit-min-device-pixel-ratio:2){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat;-webkit-background-size:100% 100%}}#logo{display:inline-block;height:54px;width:150px}
  </style>
  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>404.</b> <ins>That’s an error.</ins>
  <p>The requested URL <code>/</code> was not found on this server.  <ins>That’s all we know.</ins>
728x90
반응형

'DevOps > Kubernetes' 카테고리의 다른 글

Kubernetes - kube-proxy  (0) 2021.11.30
Kubernetes - Headless Service  (0) 2021.11.30
Kubernetes - Deployment  (0) 2021.11.25
Kubernetes - DaemonSet  (0) 2021.11.25
Kubernetes - CronJob  (0) 2021.11.24