ft_services - nginx

jiholee·2021년 4월 18일
0

ft_services

목록 보기
3/4

nginx container 요구사항

  • nginx 서버 컨테이너는 80번 포트와 443번 포트를 listening 합니다. http로 연결되는 80번 포트로 접근할 경우, https 인 443번 포트로 301 리다이렉션 되게 해야합니다.

  • 페이지는 http 오류만 아니라면 어떻게 보이든 상관없습니다.

  • 이 컨테이너는 /wordpress 경로로 접근하면 IP:WPPORT 로 307 리다이렉트 되어야합니다.

  • /phpmyadmin 경로로 접근하면 reverse proxy 를 이용해 IP:PMAPORT 로 접근할 수 있어야합니다.

  • WPPORT : WordPress 포트

  • PMAPORT : PhpMyAdmin 포트

nginx 컨테이너를 위한 dockerfile

FROM alpine:3.12.0

RUN apk update \
	&& apk --update --no-cache add \
	nginx \
	openssl

COPY ./nginx.sh /usr/sbin/nginx.sh
RUN chmod +x /usr/sbin/nginx.sh
COPY ./default.conf /etc/nginx/conf.d/

EXPOSE 80 443

ENTRYPOINT ["/usr/sbin/nginx.sh"]

컨테이너 내부 실행 쉘 파일 (nginx.sh)

#!/bin/sh

# ssl configuration
mkdir -p /etc/nginx/ssl
openssl req -new -x509 -nodes -newkey rsa:4096 -keyout localhost-nginx.key -out localhost-nginx.crt -days 365 -subj "/C=KR/ST=Seoul/L=Seoul/O=42Seoul/CN=localhost"
mv localhost-nginx.key /etc/nginx/ssl
mv localhost-nginx.crt /etc/nginx/ssl

mkdir -p /run/nginx
echo "<h1>hello FT_SERVICES NGINX INDEX.HTML</h1>" >> /var/www/index.html

/usr/sbin/nginx -g "daemon off;"
  • daemon
    멀티태스킹 운영 체제에서 데몬은 사용자가 직접적으로 제어하지 않고, 백그라운드에서 돌면서 여러 작업을 하는 프로그램을 말한다. daemon off면 nginx 서버를 foreground에서 실행할 수 있게 해준다
  • nginx 서버를 foreground로 돌리지 않으면 컨테이너를 background로 실행해도 컨테이너 안의 서버가 실행이 안된 상태이기 때문에 컨테이너가 상태가 exited가 된다. 참고

nginx config 파일 (default.conf)

# redirect http to https
server {
	listen 80 default_server;
	listen [::]:80 default_server;
	return 301 https://$host$request_uri;
}

server {
	listen 443 ssl;
	listen [::]:443 ssl;

	root /var/www;
	index index.html index.php;

	server_name _;
	ssl_certificate /etc/nginx/ssl/localhost-nginx.crt;
	ssl_certificate_key /etc/nginx/ssl/localhost-nginx.key;

	location / {
		try_files $uri $uri/ =404;
	}
    
	# IP:WPPORT 로 307 리다이렉트
	location /wordpress {
		return 307		http://$host:5050/;
	}
	
    	# reverse proxy 를 이용해 IP:PMAPORT 로 접근
	location /phpmyadmin/ {
		proxy_pass			http://192.168.99.100:5000/;
		proxy_set_header    Host $host;
		proxy_set_header    X-Real-IP $host;
		proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
		proxy_set_header    X-Forwarded-Proto $scheme;
		proxy_set_header    X-Forwarded-Host $host;
	}

	location /index.php {
		return 301 https://$host/phpmyadmin/;
	}
}

Same Load Balancer, Multiple Services

  • 서비스가 동일한 포트를 사용하지 않는 한 여러 서비스에서 동일한 Load Balancer를 공유 할 수 있습니다. 참고
  • 서비스가 Load Balancer를 공유하려면 영향을받는 모든 서비스에 metallb.universe.tf/allow-shared-ip : 의 동일한 annotation 버전이 필요합니다.

nginx.yaml 파일

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - image: nginx:latest
        imagePullPolicy: Never
        name: nginx 
        ports:
        - containerPort: 80
        - containerPort: 443
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
  annotations:
    metallb.universe.tf/allow-shared-ip: "shared"
  labels:
    app: nginx
spec:
  type: LoadBalancer
  loadBalancerIP: 192.168.99.100
  selector:
    app: nginx
  ports:
  - name: http
    port: 80
    protocol: TCP
    targetPort: 80
  - name: https
    port: 443
    protocol: TCP
    targetPort: 443
  • 레이블을 사용하여 오브젝트를 선택하고, 특정 조건을 만족하는 오브젝트 컬렉션을 찾을 수 있다.
  • 포트는 tcp를 이용한다. 서비스는 80포트로 서비스 하고 (port:80), 서비스의 80포트의 요청을 컨테이너의 80포트로 연결(targetPort:80)해서 서비스를 제공한다.
#nginx
cd ./srcs/nginx
# "nginx image build"
docker build -t nginx:latest .
# "apply yaml"
kubectl apply -f ./nginx.yaml

참고 :

0개의 댓글