[클라우드/K8S 기본(11) -Ingress, MetalLB 실습(2)(/,/shop,/blog)]

SooYeon Yeon·2022년 9월 13일
0

클라우드 K8S

목록 보기
13/18

실습하기 - LB-ingress-service를 이용한 웹서비스 환경 배포

실습환경

로드밸런서

apiVersion: v1
kind: ConfigMap
metadata:
  namespace: metallb-system
  name: config
data:
  config: | # 이게 없다면 그대로 찍어내지 못하게 됨i
    address-pools:
    - name: default
      protocol: layer2
      addresses:
      - 211.183.3.201-211.183.3.210
    - name: kia
      protocol: layer2
      addresses:
      - 211.183.3.211-211.183.3.220
    - name: sk
      protocol: layer2
      addresses:
      - 211.183.3.221-211.183.3.230
root@manager:~/k8slab/lab1# kubectl apply -f lb-config.yaml
configmap/config created

ingress-config

root@manager:~/k8slab/lab2# touch ingress-config.yaml
root@manager:~/k8slab/lab2# kubectl api-resources | grep Ingress
ingresses                         ing          extensions/v1beta1                     true         Ingress
ingressclasses                                 networking.k8s.io/v1                   false        IngressClass
ingresses                         ing          networking.k8s.io/v1                   true         Ingress
  • ingress-config.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-nginx
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - http:
      paths:
        - path: /
          pathType: Prefix
          backend:
            service:
              name: default
              port:
                number: 80 # 서비스(nodeport)의 포트
        - path: /shop
          pathType: Prefix
          backend:
            service:
              name: shop
              port:
                number: 80
        - path: /blog
          pathType: Prefix
          backend:
            service:
              name: blog
              port:
                number: 80

www.test.com/ip → 실제 서비스 제공하는 포드의 기본 디렉토리로 이동

ingress-controller

  • ingress-controller(인그레스 컨트롤러) 배포
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.2/deploy/static/provider/cloud/deploy.yaml

service deploy

  • 배포할 service, deploy 정의
root@manager:~/k8slab/lab2# touch shop-deploy-svc.yaml
root@manager:~/k8slab/lab2# touch blog-deploy-svc.yaml
root@manager:~/k8slab/lab2# touch default-deploy-svc.yaml
  • default-deploy-svc.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: default
  template:
    metadata:
      name: default # pod 이름
      labels:
        app: default
    spec:
      containers:
      - name: default
        image: nginx
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: default
spec:
  ports:
  - name: default-port
    port: 80 # 'service'의 port
    targetPort: 80 # Pod's port
    nodePort: 30000
  selector:
    app: default
  type: NodePort
  • shop-deploy-svc.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: shop
spec:
  replicas: 3
  selector:
    matchLabels:
      app: shop
  template:
    metadata:
      name: shop # pod 이름
      labels:
        app: shop
    spec:
      containers:
      - name: shop
        image: nginx
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: shop
spec:
  ports:
  - name: shop-port
    port: 80 # 'service'의 port
    targetPort: 80 # Pod's port
    nodePort: 30001
  selector:
    app: shop
  type: NodePort
  • blog-deploy-svc.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: blog
spec:
  replicas: 3
  selector:
    matchLabels:
      app: blog
  template:
    metadata:
      name: blog # pod 이름
      labels:
        app: blog
    spec:
      containers:
      - name: blog
        image: httpd
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: blog
spec:
  ports:
  - name: blog-port
    port: 80 # 'service'의 port
    targetPort: 80 # Pod's port
    nodePort: 30002
  selector:
    app: blog
  type: NodePort
  • 배포하기
root@manager:~/k8slab/lab2# kubectl apply -f default-deploy-svc.yaml
deployment.apps/default created
service/default created
root@manager:~/k8slab/lab2# kubectl apply -f shop-deploy-svc.yaml
deployment.apps/shop created
service/shop created
root@manager:~/k8slab/lab2# kubectl apply -f blog-deploy-svc.yaml
deployment.apps/blog created
service/blog created
  • 현재까지 내용 확인하기 위한 방법

master/manager에서 curl을 이용하여 노드포트를 통해 각 포드에 접속할 수 있는 지 여부를 확인한다. 이는 클러스터 환경 외부(인터넷)에서의 접속을 확인하는 것은 아니고 내부적으로 정상 동작 여부를 판단한 것이다.

root@manager:~/k8slab/lab2# curl http://211.183.3.101:30000
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
root@manager:~/k8slab/lab2# curl http://211.183.3.
  • 위에서 작성한 ingress-config.yaml을 배포한 뒤 kubectl get ingress를 확인
root@manager:~/k8slab/lab2# kubectl apply -f ingress-config.yaml
ingress.networking.k8s.io/ingress-nginx created
root@manager:~/k8slab/lab2# kubectl get ingress
NAME            CLASS    HOSTS   ADDRESS         PORTS   AGE
ingress-nginx   <none>   *       211.183.3.201   80      44s

해당 주소는 LB를 통해 IP를 불러온다.

각 회사별로 namespace 제공

우리 회사는 기본 페이지 → /* → default → Pod : 10~15(scale) , 0.25개/16M(resource)

                                     → /blog → blog → pod : 5~10(scale) , 0.25/16M(resource)

                                     → /shop → shop → pod : 10~20(scale) , 0.5/32M(resource)

최종적으로 우리 회사 DNS 서버에 아래의 정보를 등록시켜 둔다.

test.com → zone file → test.com.db

www IN A 211.183.3.99 → www.test.com

user1 IN A 211,183.3.201 → user1.test.com

별도 도메인을 구매하여 해당 도메인과 211.183.3.201을 매핑

www.kakaorapa.com → 211.183.3.201

                            /shop → 211.183.3.201/shop

configmap

각각의 페이지는 configmap을 이용하여 붙여넣기 해 보기

default(nginx) → https://www.naver.com/ 페이지로

shop(nginx) → https://zigzag.kr/ 페이지로

blog(httpd) → https://www.kakaocorp.com/ 페이지로

root@manager:~/k8slab/lab2# mkdir default
root@manager:~/k8slab/lab2# mkdir blog
root@manager:~/k8slab/lab2# mkdir shop

chmod +x default
chmod +x blog
chmod +x shop

root@manager:~/k8slab/lab2# touch default/index.html
root@manager:~/k8slab/lab2# touch blog/index.html
root@manager:~/k8slab/lab2# touch shop/index.html
cd default
curl -L https://www.naver.com/ > index.html

cd ..
cd shop
curl -L https://zigzag.kr/ > index.html

cd ..
cd blog
curl -L [https://www.kakaocorp.com/](https://www.kakaocorp.com/) > index.html
cd default
kubectl create configmap default-cmap --from-file index.html

cd ..
cd shop
kubectl create configmap shop-cmap --from-file index.html

cd ..
cd blog
kubectl create configmap blog-cmap --from-file index.html
  • 확인
kubectl describe cm [configmap이름]
root@manager:~/k8slab/lab2# touch default/default-configmap.yaml
root@manager:~/k8slab/lab2# touch shop/shop-configmap.yaml
root@manager:~/k8slab/lab2# touch blog/blog-configmap.yaml

default, shop → nginx

blog → httpd

기본디렉토리

httpd /usr/local/apache2/htdocs

nginx /usr/share/nginx/html

  • default-deploy-svc.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: default
spec:
  replicas: 3
  selector:
    matchLabels:
      app: default
  template:
    metadata:
      name: default # pod 이름
      labels:
        app: default
    spec:
      containers:
      - name: default
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - name: default-cmap-volume
          mountPath: /usr/share/nginx/html
      volumes:
      - name: default-cmap-volume
        configMap:
          name: default-cmap
---
apiVersion: v1
kind: Service
metadata:
  name: default
spec:
  ports:
  - name: default-port
    port: 80 # 'service'의 port
    targetPort: 80 # Pod's port
    nodePort: 30000
  selector:
    app: default
  type: NodePort
root@manager:~/k8slab/lab2/default# kubectl apply -f default-deploy-svc.yaml
deployment.apps/default configured
service/default unchanged

  • shop-deploy-svc.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: shop
spec:
  replicas: 3
  selector:
    matchLabels:
      app: shop
  template:
    metadata:
      name: shop # pod 이름
      labels:
        app: shop
    spec:
      containers:
      - name: shop
        image: nginx
        ports:
        - containerPort: 80
        volumeMounts:
        - name: shop-cmap-volume
          mountPath: /usr/share/nginx/html
      volumes:
      - name: shop-cmap-volume
        configMap:
          name: shop-cmap
---
apiVersion: v1
kind: Service
metadata:
  name: shop
spec:
  ports:
  - name: shop-port
    port: 80 # 'service'의 port
    targetPort: 80 # Pod's port
    nodePort: 30001
  selector:
    app: shop
  type: NodePort
root@manager:~/k8slab/lab2/shop# kubectl apply -f shop-deploy-svc.yaml
deployment.apps/shop configured
service/shop unchanged

  • blog-deploy-svc.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: blog
spec:
  replicas: 3
  selector:
    matchLabels:
      app: blog
  template:
    metadata:
      name: blog # pod 이름
      labels:
        app: blog
    spec:
      containers:
      - name: blog
        image: httpd
        ports:
        - containerPort: 80
        volumeMounts:
        - name: blog-cmap-volume
          mountPath: /usr/local/apache2/htdocs
      volumes:
      - name: blog-cmap-volume
        configMap:
          name: blog-cmap
---
apiVersion: v1
kind: Service
metadata:
  name: blog
spec:
  ports:
  - name: blog-port
    port: 80 # 'service'의 port
    targetPort: 80 # Pod's port
    nodePort: 30002
  selector:
    app: blog
  type: NodePort
root@manager:~/k8slab/lab2/blog# kubectl apply -f blog-deploy-svc.yaml
deployment.apps/blog configured
service/blog unchanged

root@manager:~/k8slab/lab2/blog# kubectl get pod
NAME                       READY   STATUS    RESTARTS   AGE
blog-745956ff7f-49mrn      1/1     Running   0          12s
blog-745956ff7f-9nttl      1/1     Running   0          12s
blog-745956ff7f-d545q      1/1     Running   0          12s
default-8675d9fc57-cwqw6   1/1     Running   0          13m
default-8675d9fc57-jn6s7   1/1     Running   0          13m
default-8675d9fc57-kghqk   1/1     Running   0          13m
nfs-pod                    1/1     Running   0          6h29m
shop-749c69fd9c-brdg2      1/1     Running   0          6m42s
shop-749c69fd9c-rkn5b      1/1     Running   0          6m36s
shop-749c69fd9c-xqrzk      1/1     Running   0          6m48s

0개의 댓글