Versions Compared

Key

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

...

Info
iconfalse

https://dev-k8sref-io.web.app/docs/services/service-v1/

Info
iconfalse



Service 실습

ClusterIP (default)

Info
iconfalse


Info


Code Block
titlenginx-deploy.yaml
linenumberstrue
collapsetrue
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deploy
  labels:
    app: myapp-deploy
    type: front-end
spec:
  template:
    metadata:
      name: nginx
      labels:
        app: myapp
        type: front-end
    spec:
      containers:
        - name: nginx
          image: nginx:1.14.2
          ports:
            - containerPort: 80
  selector:
    matchLabels:
      type: front-end
  replicas: 3
Code Block
titlenginx-svc.yaml
linenumberstrue
apiVersion: v1
kind: Service
metadata:
  labels:
	app: myapp-svc
    type: front-end
  name: nginx-svc
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    type: front-end
Code Block
linenumberstrue
sansae@sansaeAir15m2 k8s-lab-workspace % k apply -f nginx-svc.yaml 
service/nginx-svc created
sansae@sansaeAir15m2 k8s-lab-workspace % k get svc
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.0.0.1       <none>        443/TCP   23h
nginx-svc    ClusterIP   10.0.133.111   <none>        80/TCP    5s
sansae@sansaeAir15m2 k8s-lab-workspace % k describe svc nginx-svc 
Name:              nginx-svc
Namespace:         default
Labels:            app=myapp-svc
                   type=front-end
Annotations:       <none>
Selector:          type=front-end
Type:              ClusterIP
IP Family Policy:  SingleStack
IP Families:       IPv4
IP:                10.0.133.111
IPs:               10.0.133.111
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.244.0.75:80,10.244.0.76:80,10.244.0.77:80
Session Affinity:  None
Events:            <none>


Code Block
titlesooabia/network-utils
linenumberstrue
sansae@sansaeAir15m2 k8s-lab-workspace % k get svc
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.0.0.1       <none>        443/TCP   24h
nginx-svc    ClusterIP   10.0.133.111   <none>        80/TCP    61m

sansae@sansaeAir15m2 k8s-lab-workspace % k run util --image=sooabia/network-utils
pod/util created

sansae@sansaeAir15m2 k8s-lab-workspace % k exec -it util -- bash
root@util:/# curl 10.0.133.111
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>



NodePort

Info
iconfalse


Single Pod on Single Node

Multi Pod on Single Node

Multi Pod on Multi Node

Code Block
titlenginx-svc-nodeport.yaml
linenumberstrue
apiVersion: v1
kind: Service
metadata:
  labels:
    app: myapp-svc
    type: front-end
  name: nginx-svc-nodeport
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30008
  selector:
    type: front-end
Code Block
linenumberstrue
sansae@sansaeAir15m2 k8s-lab-workspace % k apply -f nginx-svc-nodeport.yaml 
service/nginx-svc-nodeport created
sansae@sansaeAir15m2 k8s-lab-workspace % k get svc
NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes           ClusterIP   10.0.0.1       <none>        443/TCP        29h
nginx-svc            ClusterIP   10.0.133.111   <none>        80/TCP         5h38m
nginx-svc-nodeport   NodePort    10.0.159.138   <none>        80:30008/TCP   3s
sansae@sansaeAir15m2 k8s-lab-workspace % k get node -o wide
NAME                                STATUS   ROLES   AGE   VERSION   INTERNAL-IP   EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION      CONTAINER-RUNTIME
aks-agentpool-33019784-vmss000000   Ready    agent   29h   v1.27.9   10.224.0.4    <none>        Ubuntu 22.04.4 LTS   5.15.0-1056-azure   containerd://1.7.7-1
sansae@sansaeAir15m2 k8s-lab-workspace % 
sansae@sansaeAir15m2 k8s-lab-workspace % 
sansae@sansaeAir15m2 k8s-lab-workspace % k exec -it util -- bash
root@util:/# curl 10.224.0.4:30008
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
~~~~~~~~~ 생략 ~~~~~~~~
root@util:/# 

root@util:/# curl 10.0.159.138
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
~~~~~~~~~ 생략 ~~~~~~~~
root@util:/# 


LoadBalancer

Info

Code Block
titlenginx-svc-lb.yaml
linenumberstrue
apiVersion: v1
kind: Service
metadata:
  labels:
    app: myapp-svc
    type: front-end
  name: nginx-svc-lb
spec:
  type: LoadBalancer
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    type: front-end
Code Block
sansae@sansaeAir15m2 k8s-lab-workspace % k apply -f nginx-svc-lb.yaml 
service/nginx-svc-lb created

sansae@sansaeAir15m2 k8s-lab-workspace % k get svc
NAME                 TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes           ClusterIP      10.0.0.1       <none>        443/TCP        29h
nginx-svc            ClusterIP      10.0.133.111   <none>        80/TCP         5h50m
nginx-svc-lb         LoadBalancer   10.0.69.70     <pending>     80:32050/TCP   5s
nginx-svc-nodeport   NodePort       10.0.159.138   <none>        80:30008/TCP   11m

sansae@sansaeAir15m2 k8s-lab-workspace % k get svc
NAME                 TYPE           CLUSTER-IP     EXTERNAL-IP      PORT(S)        AGE
kubernetes           ClusterIP      10.0.0.1       <none>           443/TCP        29h
nginx-svc            ClusterIP      10.0.133.111   <none>           80/TCP         5h50m
nginx-svc-lb         LoadBalancer   10.0.69.70     20.249.176.154   80:32050/TCP   18s
nginx-svc-nodeport   NodePort       10.0.159.138   <none>           80:30008/TCP   11m
sansae@sansaeAir15m2 k8s-lab-workspace % 

...