Amazon EKS 클러스터에서 실행 중인 Kubernetes 서비스를 노출하려면 어떻게 해야 합니까?
Amazon Elastic Kubernetes Service(Amazon EKS) 클러스터에서 실행 중인 Kubernetes 서비스를 노출하고 싶습니다.
해결 방법
클러스터에서 실행 중인 Kubernetes 서비스를 노출하려면 먼저 샘플 애플리케이션을 생성해야 합니다. 그런 다음 ClusterIP, NodePort 또는 LoadBalancer Kubernetes 서비스 유형을 샘플 애플리케이션에 적용합니다. 자세한 내용은 Kubernetes 웹 사이트의 서비스 유형을 참조하십시오.
샘플 애플리케이션 생성
다음 단계를 완료하십시오.
-
Kubernetes에서 배포 파일을 정의하고 적용합니다. 다음 예시 명령은 nginx-deployment.yaml이라는 파일을 생성한 다음 두 개의 nginx 포드를 가동하는 ReplicaSet를 생성합니다.
cat <<EOF > nginx-deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: nginx spec: replicas: 2 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.14.2 ports: - containerPort: 80 EOF
-
배포를 생성하려면 다음 명령을 실행합니다.
kubectl apply -f nginx-deployment.yaml
-
포드가 실행 중이고 자체 내부 IP 주소가 있는지 확인하려면 다음 명령어를 실행합니다.
kubectl get pods -l 'app=nginx' -o wide | awk {'print $1" " $3 " " $6'} | column -t
출력 예시:
NAME STATUS IP nginx-deployment-574b87c764-hcxdg Running 192.168.20.8 nginx-deployment-574b87c764-xsn9s Running 192.168.53.240
서비스 유형 적용
애플리케이션 노출 방법을 결정한 다음 적절한 서비스 유형을 적용합니다. 각 서비스 유형에 대한 자세한 내용은 Kubernetes 웹 사이트의 유형: ClusterIP, 유형: NodePort 및 유형: LoadBalancer를 참조하십시오.
ClusterIP 서비스 유형
다음 단계를 완료하십시오.
-
clusterip.yaml이라는 파일을 생성합니다.
-
다음 예와 같이 유형을 ClusterIP로 설정합니다.
cat <<EOF > clusterip.yaml apiVersion: v1 kind: Service metadata: name: nginx-service-cluster-ip spec: type: ClusterIP selector: app: nginx ports: - protocol: TCP port: 80 targetPort: 80 EOF
-
선언적 또는 명령형 명령을 사용하여 Kubernetes에서 ClusterIP 객체를 생성합니다.
객체를 생성하고 clusterip.yaml 파일을 적용하려면 다음 선언적 명령을 실행합니다.kubectl create -f clusterip.yaml
출력 예시:
service/nginx-service-cluster-ip created>/code>
-또는-
ClusterIP 유형의 배포를 노출하려면 다음 명령형 명령을 실행합니다.
kubectl expose deployment nginx-deployment --type=ClusterIP --name=nginx-service-cluster-ip
참고: expose 명령은 서비스를 생성하지만 YAML 파일을 생성하지는 않습니다. 하지만 kubectl은 명령형 명령을 선언적 Kubernetes 배포 객체로 변환합니다.
출력 예시:service "nginx-service-cluster-ip" exposed
-
CLUSTER-IP 주소를 가져오려면 다음 명령을 실행합니다.
kubectl get service nginx-service-cluster-ip
출력 예시:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx-service-cluster-ip ClusterIP 10.100.12.153 <none> 80/TCP 23s
-
CLUSTER-IP 주소를 사용하여 애플리케이션에 액세스하려면 다음 명령을 실행합니다.
curl -silent 10.100.12.153:80 | grep title
참고: 서비스에 액세스하려면 워커 노드에 로그인해 있거나 포드의 컨테이너 내에 있어야 합니다.
-
ClusterIP 서비스를 삭제하려면 다음 명령을 실행합니다.
kubectl delete service nginx-service-cluster-ip
출력 예시:
service "nginx-service-cluster-ip" deleted
NodePort 서비스 유형
다음 단계를 완료하십시오.
-
nodeport.yaml이라는 파일을 생성합니다.
-
다음 예와 같이 유형을 NodePort로 설정합니다.
cat <<EOF > nodeport.yaml apiVersion: v1 kind: Service metadata: name: nginx-service-nodeport spec: type: NodePort selector: app: nginx ports: - protocol: TCP port: 80 targetPort: 80 EOF
-
선언적 또는 명령형 명령을 사용하여 Kubernetes에서 NodePort 객체를 생성합니다.
객체를 생성하고 nodeport.yaml 파일을 적용하려면 다음 선언적 명령을 실행합니다.kubectl create -f nodeport.yaml
-또는-
NodePort 유형의 배포를 노출하려면 다음 명령형 명령을 실행합니다.
kubectl expose deployment nginx-deployment --type=NodePort --name=nginx-service-nodeport
출력 예시:
service/nginx-service-nodeport exposed
-
nginx-service에 대한 정보를 얻으려면 다음 명령을 실행합니다.
kubectl get service/nginx-service-nodeport
출력 예시:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx-service-nodeport NodePort 10.100.106.151 <none> 80:30994/TCP 27s
중요: NodePort는 클러스터 내에서 액세스할 수 있는 서비스 IP 주소를 생성하고 지정된 포트를 사용하여 모든 노드에 서비스를 노출합니다. 위 명령의 출력은 NodePort 서비스 유형이 사용 가능한 워커 노드의 Amazon Elastic Compute Cloud(Amazon EC2) 인스턴스의 포트 외부에 노출됨을 보여줍니다. 클러스터 외부에서 NodeIP:NodePort에 액세스하기 전에 출력에 나열된 포트를 통해 들어오는 트래픽을 허용하도록 노드의 보안 그룹을 설정합니다.
-
퍼블릭 서브넷에 있고 인터넷에서 연결할 수 있는 노드의 경우 노드의 퍼블릭 IP 주소를 확인합니다.
kubectl get nodes -o wide | awk {'print $1" " $2 " " $7'} | column -t
출력 예시:
NAME STATUS EXTERNAL-IP ip-10-0-3-226.eu-west-1.compute.internal Ready 1.1.1.1 ip-10-1-3-107.eu-west-1.compute.internal Ready 2.2.2.2
-또는-
프라이빗 서브넷에 있고 가상 프라이빗 클라우드(VPC)를 통해서만 연결할 수 있는 노드의 경우 노드의 프라이빗 IP 주소를 확인합니다.
kubectl get nodes -o wide | awk {'print $1" " $2 " " $6'} | column -t
출력 예시:
NAME STATUS INTERNAL-IP ip-10-0-3-226.eu-west-1.compute.internal Ready 10.0.3.226 ip-10-1-3-107.eu-west-1.compute.internal Ready 10.1.3.107
-
노드 IP 및 NodePort를 사용하여 애플리케이션에 액세스하려면 다음 명령을 실행합니다.
curl -silent <Public/PrivateNodeIP>:30994 | grep title
-
NodePort 서비스를 삭제하려면 다음 명령을 실행합니다.
kubectl delete service nginx-service-nodeport
출력 예시:
service "nginx-service-nodeport" deleted
LoadBalancer 서비스 유형
다음 단계를 완료하십시오.
-
loadbalancer.yaml이라는 파일을 생성합니다.
-
다음 예와 같이 유형을 LoadBalancer로 설정합니다.
cat <<EOF > loadbalancer.yaml apiVersion: v1 kind: Service metadata: name: nginx-service-loadbalancer spec: type: LoadBalancer selector: app: nginx ports: - protocol: TCP port: 80 targetPort: 80 EOF
-
loadbalancer.yaml 파일을 적용하려면 다음 명령을 실행합니다.
kubectl create -f loadbalancer.yaml
출력 예시:
service/nginx-service-loadbalancer created
-또는-
LoadBalancer 유형의 배포를 노출하려면 다음 명령을 실행합니다.
kubectl expose deployment nginx-deployment --type=LoadBalancer --name=nginx-service-loadbalancer
출력 예시:
service "nginx-service-loadbalancer" exposed
-
nginx-service에 대한 정보를 얻으려면 다음 명령을 실행합니다.
kubectl get service/nginx-service-loadbalancer | awk {'print $1" " $2 " " $4 " " $5'} | column -t
출력 예시:
NAME TYPE EXTERNAL-IP PORT(S) nginx-service-loadbalancer LoadBalancer *****.eu-west-1.elb.amazonaws.com 80:30039/TCP
-
외부에서 로드 밸런서에 액세스할 수 있는지 확인하려면 다음 명령을 실행합니다.
curl -silent *****.eu-west-1.elb.amazonaws.com:80 | grep title
-
LoadBalancer 서비스를 삭제하려면 다음 명령을 실행합니다.
kubectl delete service nginx-service-loadbalancer
출력 예시:
service "nginx-service-loadbalancer" deleted
참고: 기본적으로 LoadBalancer 서비스 유형은 Classic Load Balancer를 생성합니다.
인스턴스 유형 대상을 사용하여 Network Load Balancer를 만들려면 서비스 매니페스트에 다음 주석을 추가합니다.
service.beta.kubernetes.io/aws-load-balancer-type: nlb
-또는-
IP 대상을 사용하여 네트워크 로드 밸런서를 생성하려면 AWS Load Balancer Controller를 배포한 다음 IP 대상을 사용하는 로드 밸런서를 생성합니다.
관련 콘텐츠
- 질문됨 7일 전lg...
- 질문됨 일 년 전lg...
- AWS 공식업데이트됨 6달 전