# TIPs. \[NHN Cloud] NHN Kubernetes Service(NKS) 의 LoadBalancer 사용법

{% hint style="info" %}
**2022년 NHN Cloud&#x20;**<mark style="color:red;">**무료**</mark>**&#x20;교육일정** : <https://doc.skill.or.kr/2022-NHN-Cloud-Education>
{% endhint %}

{% hint style="info" %}
**NHN Cloud&#x20;**<mark style="color:red;">**사용자 가이드**</mark> : <https://doc.skill.or.kr/nhn-cloud-user-guide>
{% endhint %}

{% hint style="warning" %}
**2022년 NHN Cloud&#x20;**<mark style="color:red;">**행사/프로모션**</mark>**&#x20;정보 공유** : <https://doc.skill.or.kr/2022-NHN-Cloud-Event-Promotion>
{% endhint %}

## TIPs. \[NHN Cloud] NHN Kubernetes Service(NKS)에서 LoadBalancer 사용 방법     &#x20;

### 1. NHN Kubernetes Service(NKS) 에서 LoadBalaner 사용 하기 &#x20;

> Kubernetes 에 서비스 등록 후 외부 공인IP 를 사용하여 접속 하려면 LoadBalancer 를 이용하여야 한.
>
> Service Type 에서는 "ClusterIP", "ExternalName", "LoadBalancer", "NodePort" 값이 있다.&#x20;
>
> * ClusterIP (기본값) - 클러스터 내에서 내부 IP 에 대해 서비스를 노출해준다. 이 방식은 오직 클러스터 내에서만 서비스가 접근될 수 있도록 해준다.
> * NodePort - NAT가 이용되는 클러스터 내에서 각각 선택된 노드들의 동일한 포트에 서비스를 노출시켜준다. `<NodeIP>:<NodePort>`를 이용하여 클러스터 외부로부터 서비스가 접근할 수 있도록 해준다. ClusterIP의 상위 집합이다.
> * LoadBalancer - (지원 가능한 경우) 기존 클라우드에서 외부용 로드밸런서를 생성하고 서비스에 고정된 공인 IP를 할당해준다. NodePort의 상위 집합이다.
> * ExternalName - `CNAME` 레코드 및 값을 반환함으로써 서비스를 `externalName` 필드의 내용(예를 들면, \`foo.bar.example.com\`)에 매핑한다. 어떠한 종류의 프록시도 설정되지 않는다. 이 방식은 `kube-dns` v1.7 이상 또는 CoreDNS 버전 0.0.8 이상을 필요로 한다.

{% code title="LoadBalancer 를 이용하여 공인IP 사용하기    " %}

```
apiVersion: apps/v1
kind: Deployment
metadata:
  name: http-echo
spec:
  replicas: 2
  selector:
    matchLabels:
      app: http-echo
  template:
    metadata:
      labels:
        app: http-echo
    spec:
      containers:
      - name: http-echo
        image: hashicorp/http-echo
        args: ["-text", "hello-world"]
        ports:
        - containerPort: 5678
---
apiVersion: v1
kind: Service
metadata:
  name: http-echo
spec:
  ports:
  - port: 5678
    protocol: TCP
    targetPort: 5678
  selector:
    app: http-echo
  ## NodePort 가 아니라 LoadBalancer 로 설정 해 주어야 함.
  type: LoadBalancer
```

{% endcode %}

### 2. 서비스 중지 시 공인IP 유지 하기!!!!!

> 서비스를 중지 하였는데 `Loadbalancer 에 등록된 공인IP 가 같이 사라졌다!!!!!`
>
> 이를 해결하기 위해 아래와 같은 `loadbalancer.openstack.org/keep-floatingip: "true"` 값이 필요 하.
>
> {% code title="Service 의 metadata 하위에 아래의 annotations 값을 입력" %}
>
> ```
>   annotations:
>     loadbalancer.openstack.org/keep-floatingip: "true"
> ```
>
> {% endcode %}

> 아래와 같은 소스로 실행해 보자.

{% code title="서비스가 중지 되더라도 공인IP 유지하기(Floating ip 유지 설정)" %}

```
apiVersion: apps/v1
kind: Deployment
metadata:
  name: http-echo
spec:
  replicas: 2
  selector:
    matchLabels:
      app: http-echo
  template:
    metadata:
      labels:
        app: http-echo
    spec:
      containers:
      - name: http-echo
        image: hashicorp/http-echo
        args: ["-text", "hello-world"]
        ports:
        - containerPort: 5678
---
apiVersion: v1
kind: Service
metadata:
  name: http-echo
  annotations:                                           ## 오브젝트 선언형 관리
    loadbalancer.openstack.org/keep-floatingip: "true"   ## Floating ip 유지 설정  
spec:
  ports:
  - port: 5678
    protocol: TCP
    targetPort: 5678
  selector:
    app: http-echo
  ## NodePort 가 아니라 LoadBalancer 로 설정 해 주어야 함.
  type: LoadBalancer
```

{% endcode %}

### 3. 사용하고 있는 공인 IP 를 LoadBalancer IP 로 설정해 보자!!!!!   &#x20;

> NHN Cloud Console 에서 Floating IP 를 할당 받고 LoadBalancer 등록 할때 고정 IP 로  yaml 파일에 적용해 보자.
>
> 할당 받은 Floating IP 는 `133.186.215.195` 이다.
>
> 이를 적용하기 위해서는 Service 의 Spec 에서 아래의 같은  값을 `loadBalancerIP: 133.186.215.195` 정보를 입력 한.

{% code title="http-echo.yaml 파일" %}

```
apiVersion: apps/v1
kind: Deployment
metadata:
  name: http-echo
spec:
  replicas: 2
  selector:
    matchLabels:
      app: http-echo
  template:
    metadata:
      labels:
        app: http-echo
    spec:
      containers:
      - name: http-echo
        image: hashicorp/http-echo
        args: ["-text", "hello-world"]
        ports:
        - containerPort: 5678
---
apiVersion: v1
kind: Service
metadata:
  name: http-echo
  annotations:                                           ## 오브젝트 선언형 관리
    loadbalancer.openstack.org/keep-floatingip: "true"   ## Floating ip 설정  
spec:
  ports:
  - port: 5678
    protocol: TCP
    targetPort: 5678
  selector:
    app: http-echo
  ## NodePort 가 아니라 LoadBalancer 로 설정 해 주어야 함.
  type: LoadBalancer
  ## Floating ip 를 지정 하여 Loadbalancer ip 로 설정.
  loadBalancerIP: 133.186.215.195
  
  #####  설명   
  #####  annotations:                                            ## 오브젝트 선언형 관리 
  #####    loadbalancer.openstack.org/keep-floatingip: "true"    ## Floating ip 설정     
  #####  type: LoadBalancer
  #####  loadBalancerIP: 133.186.215.195
```

{% endcode %}

{% file src="<https://674794822-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MXjvaK4Gw77FbwOuHWJ%2F-MbeCMS5gf_fXhaTT0IH%2F-MbeDAs2S8Ap9t_E8Xxi%2Fhttp-echo.yaml?alt=media&token=b638b184-1793-458e-b2bb-9c0d66a3a505>" %}

### 참고 : <https://kubernetes.io/ko/docs/tasks/manage-kubernetes-objects/declarative-config/>

{% hint style="info" %}
**2022년 NHN Cloud&#x20;**<mark style="color:red;">**무료**</mark>**&#x20;교육일정** : <https://doc.skill.or.kr/2022-NHN-Cloud-Education>
{% endhint %}

{% hint style="info" %}
**NHN Cloud&#x20;**<mark style="color:red;">**사용자 가이드**</mark> : <https://doc.skill.or.kr/nhn-cloud-user-guide>
{% endhint %}

{% hint style="warning" %}
**2022년 NHN Cloud&#x20;**<mark style="color:red;">**행사/프로모션**</mark>**&#x20;정보 공유** : <https://doc.skill.or.kr/2022-NHN-Cloud-Event-Promotion>
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://doc.skill.or.kr/nhn-cloud-kubernetes-loadbalancer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
