외부에서 app에 접근하려면 주소 필요 (public ip)

하나의 클러스터의 app끼리는 필요X (private ip, 별도의 서브넷 주소가 만들어짐) → 이 프라이빗 ip와 각 큐블렛 vm의 하드웨어 ip 주소는 다름

컨테이너 엔진은 하드웨어와 app을 연결

Connecting Applications with Services

Use a Service to Access an Application in a Cluster

vi run-my-nginx.yaml
## 위 공식문서에 있는 yaml파일 붙여넣기

kubectrl apply -f run-my-nginx.yaml ## nginx 이미지가 없으므로 docker 이미지를 가져온다

kubectl get po ## pod 2개 만들어진거 확인가능 (name: my-nginx, replicas: 2)

kubectl get po -o wide 
## 여기서 나오는 IP주소는 무엇인가? -> POD들만의 네트워크에 배정된 IP 주소 (큐브시스템이 해줌)
## 그런데 이 IP 주소를 워커 노드에 일일이 가서 입력해줘야 접속할 수 있다 . . .
## 요 서브넷 안에서만 쓸 수 있는 주소인 것

vi nginx-svc.yaml
## 위 공식문서에 있는 yaml파일 붙여넣기

kubectrl apply -f nginx-svc.yaml

kubectl get svc my-nginx ## my-nginx라는 서비스 이름을 가진 서비스 조회

Untitled

## type이 ClusterIP (우리는 지정한 적 없지만 default이다.)
## CLUSTER-IP는 무엇을 의미하는가? -> 외부에서 해당 쿠버네티스 클러스터에 접근할 때 요 클러스터 IP:포트번호로 접근할 수 있다는 뜻이다.

kubectrl get svc 
## 요렇게 입력하면 kubernetes라는 서비스 역시 확인할 수 있다. 큐브시스템이 만든 default 시스템!!

curl <http://10.108.172.121:80> ## 위에서 뜬 my-nginx의 CLUSTER-IP임. 입력하면 nginx 디폴트 페이지 html이 출력된다.

## 그런데! 10.X.X.X 는 외부 접근이 불가능한 private IP 주소이다.
## 어떻게 접근할까? 두가지 방법이 더 있다.
## 로드밸런스, 노드 포트

# 아래는 노트 포트 방식 -> ClusterIP와 달리 외부에서 접근가능

kubectl apply -f <https://k8s.io/examples/service/access/hello-application.yaml>
## 물론 vi로 yaml파일 만들어서 해도 되지만, 요건 쿠버네티스에서 올려놓은 공식 파일을 apply하는것

kubectl get deployments hello-world
kubectl describe deployments hello-world

kubectl get replicasets
kubectl describe replicasets

kubectl expose deployment hello-world --type=NodePort --name=example-service
## expose란, 서비스로 만들고 공개하라고 하는 명령어.
## 그런데 아까와 달리 type을 NodePort로 지정하였다. (아까는 입력 안해서 디폴트인 ClusterIP가 들어감)

kubectl describe services example-service
## 여기서 나오는 IP 주소는 해당 "서비스"의 private network IP 주소이다!
## 아래 코드가 결과물
Name:                   example-service
Namespace:              default
Labels:                 run=load-balancer-example
Annotations:            <none>
Selector:               run=load-balancer-example
Type:                   NodePort
IP:                     10.32.0.16
Port:                   <unset> 8080/TCP
TargetPort:             8080/TCP
NodePort:               <unset> 31496/TCP
Endpoints:              10.200.1.4:8080, 10.200.2.5:8080
Session Affinity:       None
Events:                 <none>

## 그냥 노트북에서 control plane의 public 주소에 접속하면...
curl http://<컨트롤플레인 VM 주소>:31496
## Hello Kubernetes! 가 찍힘
## 브라우저에 주소 입력해도 잘 찍히는 것을 확인 가능
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  selector:
    matchLabels:
      run: my-nginx
  replicas: 2
  template:
    metadata:
      labels:
        run: my-nginx
    spec:
      containers:
      - name: my-nginx
        image: nginx
        ports:
        - containerPort: 80
apiVersion: v1
kind: Service
metadata:
  name: my-nginx
  labels:
    run: my-nginx
spec:
  ports:
  - port: 80
    protocol: TCP
  selector:
    run: my-nginx

# service/access/hello-application.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world
spec:
  selector:
    matchLabels:
      run: load-balancer-example
  replicas: 2
  template:
    metadata:
      labels:
        run: load-balancer-example
    spec:
      containers:
        - name: hello-world
          image: gcr.io/google-samples/node-hello:1.0
          ports:
            - containerPort: 8080
              protocol: TCP