k3s 에 배포할 docker container  준비

node.js 의 next.js 기본 생성 앱을 docker image 로 생성한다.

# docker 설치 (on windows 10)
# https://docs.microsoft.com/ko-kr/virtualization/windowscontainers/quick-start/set-up-environment?tabs=Windows-10-and-11


# next.js 서버
npx create-next-app@latest
What is your project named? ... hi-nextjs

# 서버 구동 후 http://localhost:3000/api/hello 접속
cd hi-nextjs
npm run build
npm run start

# Dockerfile 작성
FROM node:16-alpine

EXPOSE 3000

WORKDIR /usr/src/next
COPY *.* /usr/src/next

RUN npm install
COPY . /usr/src/next

RUN npm run build
CMD [ "npm" , "run" , "start"]


# docker image 생성
docker build -t  kyulity/hi-next.js:latest .
docker images

# docker container 테스트 실행
# http://localhost:3000/api/hello  접속
docker run -itd -p 3000:3000 --name hi-next kyulity/hi-next.js:latest

# docker container 삭제
docker stop hi-next
docker rm hi-next

# docker hub 에 이미지 push
docker login
docker push kyulity/hi-next.js:latest

 

 

deployment, service yaml 작성(hi-next.yaml)

---
apiVersion: v1
kind: Service
metadata:
  name: hi-next-service
spec:
  selector:
    app: hi-next
  # type: NodePort
  type: LoadBalancer
  ports:
    - protocol: TCP
      nodePort: 30000 # 각 노드로 접근 가능 포트
      port: 3000 # 서비스 포트
      targetPort: 3000 # 실제 app 포트


---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hi-next-deployment
  labels:
    app: hi-next
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hi-next
  template:
    metadata:
      labels:
        app: hi-next
    spec:
      containers:
        - name:  hi-next
          image: kyulity/hi-next.js:latest
          ports:
            - containerPort: 3000
          resources:
            requests:
              memory: "64Mi"
              cpu: "250m"
            limits:
              memory: "128Mi"
              cpu: "500m"

 

 

 

 

k3s docker container 띄우기

# namespace 생성
kubectl create namespace test-ns

# pod 생성
kubectl apply -f hi-next.yaml -n test-ns

# pod 확인
kubectl get all -n test-ns

# pod 로그 확인
kubectl logs -f hi-next-deployment-96f9665d-58b28 -n test-ns


# pod 배포된 node 확인
> kubectl get po -n test-ns -o wide
NAME                                READY   STATUS    RESTARTS   AGE   IP          NODE        NOMINATED NODE   READINESS GATES
hi-next-deployment-96f9665d-58b28   1/1     Running   0          18m   10.42.1.9   k3s-node1   <none>           <none>
hi-next-deployment-96f9665d-lrp4f   1/1     Running   0          18m   10.42.2.8   k3s-node2   <none>           <none>

# node ip 확인
> kubectl get node -n test-ns -o wide
NAME         STATUS   ROLES                  AGE   VERSION        INTERNAL-IP      EXTERNAL-IP   OS-IMAGE       KERNEL-VERSION      CONTAINER-RUNTIME
k3s-node2    Ready    <none>                 16d   v1.22.7+k3s1   172.27.158.171   <none>        Ubuntu 21.10   5.13.0-40-generic   containerd://1.5.9-k3s1
k3s-node1    Ready    <none>                 16d   v1.22.7+k3s1   172.27.151.105   <none>        Ubuntu 21.10   5.13.0-40-generic   containerd://1.5.9-k3s1
k3s-node3    Ready    <none>                 16d   v1.22.7+k3s1   172.27.146.161   <none>        Ubuntu 21.10   5.13.0-35-generic   containerd://1.5.9-k3s1
k3s-master   Ready    control-plane,master   16d   v1.22.7+k3s1   172.27.153.108   <none>        Ubuntu 21.10   5.13.0-40-generic   containerd://1.5.9-k3s1

# 접속 주소
모든 노드 INTERNAL-IP 30000 포트로 접근 가능 (이렇게 되는게 맞나?)
ex> http://172.27.151.105:30000

외부에서 접속하려면 EXTERNAL-IP 를 지정하던가

# kube 마스터 접속 주소
> kubectl get ep
NAME         ENDPOINTS             AGE
kubernetes   172.27.153.108:6443   16d

# hi-next-service 내부 접근 주소
> kubectl get ep -n test-ns
NAME              ENDPOINTS                       AGE
hi-next-service   10.42.1.9:3000,10.42.2.8:3000   30m