[kubernetes] 쿠버네티스 클러스터에 nodejs 로 웹 열고 외부에서 접근하기

쿠버네티스 > 서버 설치

develop, kubernetes
written byzuhern1zuhern

in

2021. 06. 12


목표

쿠버네티스 클러스터 서버에 파드 올리고 외부에서 접속하기

1. 방화벽 해제

$ systemctl stop firewalld
$ systemctl disable firewalld 

2. 어떤 걸 올려도 pending 상태

# 모든 파드 보기
$ kubectl get pods --all-namespaces
# 노드 보기
$ kubectl get node
# 노드 상태 확인하기
$ kubectl describe nodes <node name>
Taints:             node.kubernetes.io/not-ready:NoSchedule    # 해당 설정한 노드에는 pod이 스케줄되지 않음
# taint 설정 
$ kubectl taint node <node name> key1=value1:NoSchedule
# taint 해제
$ kubectl taint node <node name> key1=value1:NoSchedule-
# 기본적으로 Master노드에는 pod이 스케줄되지않게 taint가 걸려있음
# master taint 해제
kubectl taint nodes <node name> node-role.kubernetes.io/master-

이 문제 아니였음

2.1 원인

CNI 설정이 안되어 있음

$ kubectl apply -f https://docs.projectcalico.org/v3.11/manifests/calico.yaml
$ systemctl restart kubelet
$ kubectl get node

3. 다시 시작

설정 파일

# nodejs.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nodejs-app
  namespace: zuhern
  labels:
    name: nodejs-app
spec:
  containers:
  - name: nodejs-app
    image: heroku/nodejs-hello-world
    ports:
    - containerPort: 3000

실행

# pod 생성 후 expose
$ kubectl apply -f nodejs.yaml
$ kubectl get pod -n zuhern
$ kubectl expose pod nodejs-app -n zuhern
service/nodejs-app exposed
# ingress
$ kubectl apply -f nodejs-ingress
# /etc/hosts 에 127.0.0.1 foo.bar.com (ingress에 지정한 도메인) 추가
$ curl http://foo.bar.com:30739/nodejs

로컬 pc 에서 서버 호출할 때
로컬 hosts 에도 127.0.0.1 foo.bar.com 등록 후
http://foo.bar.com:30739/nodejs
로 호출해야 접속 가능

4. deployment로 expose하는 것으로 처음부터 다시

설정 파일

# nodejs-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nodejs-app
  namespace: zuhern
  labels:
    app: nodejs-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nodejs-app
  template:
    metadata:
      namespace: zuhern
      labels:
        app: nodejs-app
    spec:
      containers:
      - name: nodejs-app
        image: uphiller/nodejs-hello-world
        ports:
        - containerPort: 3000

실행

# deployment 생성
$ kubectl apply -f deployment-nodejs.yaml
# expose NodePort 지정 안하면 ClusterIP가 default
$ kubectl expose deploy nodejs-app --type=NodePort -n zuhern
sevice/nodejs-app exposed
$ kubectl get svc nodejs-app
# 확인 
$ curl http://127.0.0.1:32475

외부에서 IP:32475 로 접근 가능 확인

5. service로 하는 걸로 PORT=31001로 지정해서 처음부터 다시

설정 파일

# nodejs-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nodejs-app
  namespace: zuhern
  labels:
    app: nodejs-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nodejs-app
  template:
    metadata:
      namespace: zuhern
      labels:
        app: nodejs-app
    spec:
      containers:
      - name: nodejs-app
        image: uphiller/nodejs-hello-world
        ports:
        - containerPort: 3000
# node-app-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: nodejs-app
  namespace: zuhern
spec:
  selector:
    app: nodejs-app
  ports:
    - protocol: TCP
      port: 31001
      targetPort: 3000
  type: NodePort

실행

$ kubectl apply -f nodejs-deployment.yaml
$ kubectl apply -f node-app-service.yaml
service/nodejs-app configured
$ kubectl get svc -n zuhern
NAME         TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)           AGE
nodejs-app   NodePort   10.108.48.12   <none>        31001:32177/TCP   109s
$ curl http://127.0.0.1:32177
Hello World!

외부에서 IP:32177 로 접근 가능 확인

문제..
nodejs-app 말고 다른걸로 바꾸고 싶은데..
아직 뭐가 뭔지 모름. ㅠ