쿠버네티스 Ingress 예제 그대로 따라하기

2024. 4. 26. 16:42it

반응형
반응형

 

해당 환경은 윈도우 WSL Ubuntu에서 작업을 수행했습니다.

 

1. 준비 파일 

1-1. index.html 준비 

vi index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello World</title>
</head>
<body>
    <h1>Hello World!</h1>
    <p>This is my first Docker-based web page.</p>
</body>
</html>

 

 

1-2. docker file 작성

vi Dockerfile
# 사용할 베이스 이미지 지정
FROM nginx:alpine

# /usr/share/nginx/html 디렉토리에 index.html 파일을 추가
COPY index.html /usr/share/nginx/html/index.html

# 컨테이너가 80 포트에서 실행되도록 설정
EXPOSE 80

 

 

1-3. deployment.yaml 작성

vi deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world-web
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello-world-web
  template:
    metadata:
      labels:
        app: hello-world-web
    spec:
      containers:
      - name: hello-world-web
        image: hello-world-web
        ports:
        - containerPort: 80

 

 

1-4. service.yaml 작성

vi service.yaml
apiVersion: v1
kind: Service
metadata:
  name: hello-world-web
spec:
  type: NodePort
  ports:
  - port: 80
    nodePort: 30007
    protocol: TCP
  selector:
    app: hello-world-web

 

 

1-5. ingress.yaml 작성

vi ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: hello-world-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
    kubernets.io/ingress.class: "nginx"
spec:
  rules:
    - http:
        paths:
          - path: /hello(/|$)(.*)
            pathType: Prefix
            backend:
              service:
                name: hello-world-web
                port:
                  number: 80

 

 

 

2. minikube 실행 

minikube start --driver=docker

 

 

3. docker 빌드 

 eval $(minikube docker-env)
 docker build -t hello-world-web .

 

 

4. pod 배포

kubectl apply -f deployment.yaml
kubectl apply -f service.yaml

 

 

5. ingress 배포

minikube addons enable ingress
kubectl apply -f ingress.yaml

 

 

6. ingress 확인 

minikube ip

 

결과 : 192.168.49.2

 

curl 192.168.49.2/hello

 

 

 

7. 웹 브라우저 확인

728x90

 

 

 

반응형