본문 바로가기
코드리뷰

쿠바네티스 hello world 페이지 만들기

by 디마드 2024. 12. 8.
반응형

지난 시간에 쿠바네티스 환경 구축 및 간단한 서비스를 만들어 보았다. 이번에는 한 걸음 더 들어가서 웹 사이트에 접속해 hello world가 출력되는 페이지를 만들어 보자. 함께 따라 해보면 쿠바네티스 돌아가는 원리가 보일 것이다. 

쿠바네티스트 환경 구축은 이전 포스트 참고하시라. 

2024.12.01 - [코드리뷰] - 내 PC에 도커와 쿠바네티스 구축하기

 

내 PC에 도커와 쿠바네티스 구축하기

가상화 개발이 기본인 세상이다. 도커와 쿠바네티스를 모른다면 클라우드 개발이 불가능하다는 인식을 가져야 한다. 쉬운 이해를 위해 내 pc에 도커와 쿠바네티스를 설치하고 nginx 웹서버를 구

iamgus.tistory.com

로컬에 쿠바네티스를 올리고 hello world 페이지를 위한 서비스를 등록하자. 

1. Deployment 생성

nginx-deployment.yaml 파일로 저장한다. 쿠바네티스에 올라갈 deploy yaml파일이다. 이 코드에서 핵심은 configMap을 설정하는 거다. 

volumes:

      - name: index-html

        configMap:

          name: nginx-hello-config

 

		apiVersion: apps/v1
		kind: Deployment
		metadata:
		  name: nginx-hello
		spec:
		  replicas: 3
		  selector:
		    matchLabels:
		      app: nginx-hello
		  template:
		    metadata:
		      labels:
		        app: nginx-hello
		    spec:
		      containers:
		      - name: nginx-hello
		        image: nginx:latest
		        ports:
		        - containerPort: 80
		        volumeMounts:
		        - name: index-html
		          mountPath: /usr/share/nginx/html
		      volumes:
		      - name: index-html
		        configMap:
          name: nginx-hello-config

2. configMap 생성

nginx-configmap.yaml 파일로 저장하자. 2개의 html페이지를 포함한다. index.html과 about.html이다. 전자는 웹 사이트 디폴트 페이즈, 즉 도메인 접속했을 때 나오는 첫 번째 페이지다. Hello, Kubernetes World! 를 웹 페이지 출력한다. 그 아래 웹 링크를 추가해 클릭하면 about.html로 이동한다. 

		apiVersion: v1
		kind: ConfigMap
		metadata:
		  name: nginx-hello-config
		data:
		  index.html: |
		    <!DOCTYPE html>
		    <html>
		    <head>
		        <title>Hello World</title>
		        <style>
		            body { 
		                font-family: Arial, sans-serif; 
		                display: flex;
		                justify-content: center;
		                align-items: center;
		                height: 100vh;
		                margin: 0;
		                background-color: #f0f0f0;
		            }
		            .container {
		                text-align: center;
		                background-color: white;
		                padding: 30px;
		                border-radius: 10px;
		                box-shadow: 0 4px 6px rgba(0,0,0,0.1);
		            }
		        </style>
		    </head>
		    <body>
		        <div class="container">
		            <h1>Hello, Kubernetes World!</h1>
		            <p><a href="/about.html">About Us</a></p>
		        </div>
		    </body>
		    </html>
		  about.html: |
		    <!DOCTYPE html>
		    <html>
		    <head>
		        <title>About</title>
		    </head>
		    <body>
		        <h1>About Us</h1>
		        <p>This is the about page.</p>
		    </body>
		    </html>

3. Service 생성

nginx-service.yaml 파일로 저장한다. 웹 페이지 접속을 위한 port를 지정한다. 

		apiVersion: v1
		kind: Service
		metadata:
		  name: nginx-hello-nodeport
		spec:
		  type: NodePort
		  ports:
		  - port: 80
		    targetPort: 80
		    # 30000-32767 범위의 포트를 지정할 수 있습니다.
		    # 명시하지 않으면 쿠버네티스가 자동으로 할당합니다.
		    # nodePort: 30080
		  selector:
		    app: nginx-hello

4. 배포 및 확인 단계

powershell이나 cmd창에서 yaml파일을 등록한다. 실제 쿠바네티스에 리소스를 올리는 단계다. 

# 리소스 생성

kubectl apply -f nginx-deployment.yaml

kubectl apply -f nginx-configmap.yaml

kubectl apply -f nginx-service.yaml

 

# 배포 상태 확인

kubectl get deployments

kubectl get services

kubectl get pods

 

5. Port forwarding 

로컬 pc환경에서 쿠바네티스를 돌리면 필수 작업이다. 명시적으로 지정하지 않으면 접속이 되지 않는다. 

kubectl port-forward service/nginx-hello-nodeport 30805:80

6. 웹 사이트 접속 - 출력 확인

http://localhost:30805 

 

반응형

댓글