본문 바로가기
Kubernetes

테스트용 Nginx 띄우기

by Nirah 2023. 11. 3.

 약 7개월간 K8S를 다루면서 많은 구축을 해보았는데,
그 중 겪는 곤란한 일 중에 하나는 정상 작동 여부를 검증하는 일이었다.

검증의 방법 중 하나로 테스트용 nginx를 띄우는 법에 대해 작성한다.


nginx를  쓰는 이유는 간단하다

1. 웹페이지가 뜨기 때문에 가시적인 성공여부 확인이 되고

2. 관련 자료가 많다

복잡하게 작성한 yaml이 작동 안할때 nginx로 이미지를 바꿔서 테스트해서 트슛한 적도 있고,
argocd 구축에서도 잘 배포되는지 여부 확인에 유용하게 쓰였고,

이후 앱구동 상태를 전송하는 webhook 테스트라던지,

ingress 테스트라던지, ELK 로그를 전송하는 app 역할이라던지

인증서나 토큰 Secret 테스트용이라던지...
등등 신세를 많이 졌다.

 

 

아래 띄우는 방법들은 번호별로 전부 독립적이다. 필요한 부분만 찝어 쓰자.

 

 

1. docker 이미지를 사용해서 간단 생성

 

kubectl create deployment <디플로이먼트 이름> --image=<이미지 이름>:<태그>

$ kubectl create deployment hello-nginx --image=nginx:latest
deployment.apps/hello-nginx created

 

 

 

2. 서비스

$ kubectl create service clusterip hello-nginx --tcp=80:80
service/hello-nginx created

 

 

3. 포워딩 (필수 아님, 노드포트 설정이나 미니쿠베등의 환경에서 자체 머신 포워딩이 귀찮을시 임시로 사용하기 좋음 )

 

kubectl port-forward <리소스 종류/이름> <로컬 포트>:<리모트 포트>

$ kubectl port-forward service/hello-nginx 8000:80
Forwarding from 127.0.0.1:8000 -> 80
Forwarding from [::1]:8000 -> 80

 

 

4. 노드포트 서비스 

 

$ kubectl expose deployment/nginx-deployment --type="NodePort" --port 80 --target-port=80 --protocol="TCP"

# 해당 명령은 현재까지는 아직 nodeport를 지정할 수 있는 파라미터가 없어 30000~32767 사이 포트가 랜덤으로 부여된다

 

 

5. yaml 로 배포

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

 

만들었던 deployment.yaml 파일을 apply

$ kubectl apply -f nginx-deployment.yaml

 

 

서비스

apiVersion: v1
kind: Service
metadata:
  name: my-nginx
  labels:
    run: my-nginx
spec:
  type: NodePort     # 서비스 타입
  ports:
  - port: 8080       # 서비스 포트
    targetPort: 80   # 타켓, 즉 pod의 포트
    nodePort: 30003
    protocol: TCP
    name: http
  selector:
    app: nginx

 

 

 

6. configmap으로 nginx에 띄울 index.html 지정해서 배포

$ echo "Hello, world!" > index.html
$ kubectl -n 네임스페이스 create configmap index.html --from-file index.html
configmap/index.html created

 

nginx pod 에서 해당 컨피그맵 지목해서 적절한 위치(/usr/share/nginx/html/)에 마운트 해주면 된다

---
apiVersion: v1
kind: Service
metadata:
  name: nginx

spec:
  ports:
  - port: 80
  selector:
    app: nginx
  type: NodePort

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment

spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
        volumeMounts:
        - name: htmlcontent
          mountPath: "/usr/share/nginx/html/"
          readOnly: true
      volumes:
      - name: htmlcontent
        configMap:
          name: index.html
          items:
          - key: index.html
            path: index.html

 

 

 

'Kubernetes' 카테고리의 다른 글

init-container와 empty-dir  (0) 2023.11.30
Ingress  (1) 2023.11.30
NodePort, port, targetPort  (0) 2023.05.24
파드의 동작 보증 기능  (0) 2023.03.27
Liveness Probe / Readiness probe 공부  (0) 2023.03.23