๋กœ์ผ“๐Ÿพ
article thumbnail

์ด๋ฒˆ ๊ธ€์„ ์ง„ํ–‰ํ•˜๋ ค๋ฉด ๋จผ์ € ์„ ํ–‰๋˜์–ด์•ผ ํ•  ์ž‘์—…์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋˜ํ•œ aws, k8s์— ๋Œ€ํ•œ ๊ธฐ์ดˆ ์ง€์‹์ด ์—†์œผ๋ฉด ์–ด๋ ค์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

-k8s 1.19๋ฒ„์ „

-aws cli ์„ค์น˜ ํ›„ aws์— ์•ก์„ธ์Šค ํ•˜๊ธฐ

-eksctl ์„ค์น˜

-์•„๋ž˜์™€ ๊ฐ™์€ vpc ๊ตฌ์ถ•

 

์œ„ 3๊ฐ€์ง€ ์„ ํ–‰์ž‘์—…์ด ๋˜์—ˆ๋‹ค๋Š” ๊ฐ€์ •ํ•˜์— ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

k8s ํด๋Ÿฌ์Šคํ„ฐ ์ƒ์„ฑ

์šฐ์„  ํผ๋ธ”๋ฆฟ ์„œ๋ธŒ๋„ท 3๊ฐœ๋ฅผ ๊ฐ€์ง€๋Š” VPC๋ฅผ ํ†ตํ•ด EKS๋ฅผ ์ด์šฉํ•˜์—ฌ k8s ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ๋งŒ๋“ค์–ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

$ eksctl create cluster -f create-cluster.yaml

๋ช…๋ น์–ด๋ฅผ ์ด์šฉํ•˜์—ฌ k8s ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

 

* create-cluster.yaml 

apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig

metadata:
  name: "ํด๋Ÿฌ์Šคํ„ฐ ์ด๋ฆ„"
  region: ap-northeast-2
  version: "1.19"

vpc:
  subnets:
    public:
      ap-northeast-2a: { id: "์„œ๋ธŒ๋„ท id" }
      ap-northeast-2b: { id: "์„œ๋ธŒ๋„ท id" }
      ap-northeast-2c: { id: "์„œ๋ธŒ๋„ท id" }

 

๋งŒ์•ฝ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ VPC ๊ตฌ์ถ•์ด ๋˜์—ˆ๊ณ , ์˜ฌ๋ฐ”๋ฅธ ์„œ๋ธŒ๋„ท ์•„์ด๋””๋ฅผ ๋„ฃ์—ˆ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ์ƒ์„ฑํ•˜๋Š”๋ฐ ์•ฝ 20๋ถ„ ์ •๋„๊ฐ€ ์†Œ๋ชจ๋˜๋ฏ€๋กœ ๊ธฐ๋‹ค๋ ค์ค๋‹ˆ๋‹ค. ์ž˜ ์ƒ์„ฑ๋˜์—ˆ๋‹ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฉ”์‹œ์ง€๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

 

์ƒ์„ฑํ•œ ํด๋Ÿฌ์Šคํ„ฐ๋‚ด์— ๋ฐ์ดํ„ฐ ํ”Œ๋ ˆ์ธ ์ƒ์„ฑํ•˜๊ธฐ

 

์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ๋ฐฉ๊ธˆ ์ƒ์„ฑํ•œ ํด๋Ÿฌ์Šคํ„ฐ์— ๋ฐ์ดํ„ฐ ํ”Œ๋ ˆ์ธ 3๊ฐœ๋ฅผ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค.

$ eksctl create nodegroup -f create-nodegroup.yaml

*create-nodegroup.yaml

apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig

metadata:
  name: "ํด๋Ÿฌ์Šคํ„ฐ ์ด๋ฆ„"
  region: ap-northeast-2

vpc:
  id: "vpc ์•„์ด๋””"
  cidr: "vpc ์‚ฌ์ด๋”"
  subnets:
    public:
      ap-northeast-2a:
        id: "์„œ๋ธŒ๋„ท ์•„์ด๋””"
        cidr: "์„œ๋ธŒ๋„ท ์‚ฌ์ด๋”"
      ap-northeast-2b:
        id: "์„œ๋ธŒ๋„ท ์•„์ด๋””"
        cidr: "์„œ๋ธŒ๋„ท ์‚ฌ์ด๋”"
      ap-northeast-2c:
        id: "์„œ๋ธŒ๋„ท ์•„์ด๋””"
        cidr: "์„œ๋ธŒ๋„ท ์‚ฌ์ด๋”"

nodeGroups:
  - name: ng-1 
    instanceType: m5.large #์ธ์Šคํ„ด์Šค ์œ ํ˜•
    desiredCapacity: 3 #์ตœ์†Œ ๋…ธ๋“œ์ˆ˜ 
    minSize: 2 #์ตœ์†Œ ๋…ธ๋“œ์ˆ˜
    maxSize: 5 #์ตœ๋Œ€ ๋…ธ๋“œ์ˆ˜
    ssh: # ssh๋ฅผ ์œ„ํ•œ ๊ณต๊ฐœํ‚ค ์„ค์ • ์ด ์ž‘์—…์€ aws๋กœ ๊ฐœ์ธํ‚ค๋ฅผ ๋งŒ๋“  ํ›„ ๊ณต๊ฐœํ‚ค๋กœ ๋งŒ๋“ค์–ด์ค˜์•ผํ•œ๋‹ค
      publicKeyPath: ~/public.pem

 

์ปจํŠธ๋กค ํ”Œ๋ ˆ์ธ(ํด๋Ÿฌ์Šคํ„ฐ)์™€ ๋ฐ์ดํ„ฐ ํ”Œ๋ ˆ์ธ ํ™•์ธํ•˜๊ธฐ

 

์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ํ˜„์žฌ k8s ์ปจํ…์ŠคํŠธ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 

$ kubectl config current-context

 

์•„๋ž˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ ํ”Œ๋ ˆ์ธ์ด ์ž˜ ์ƒ์„ฑ๋œ ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

$ kubectl get nodes

 

alb ์„ค์ •ํ•˜๊ธฐ

 

Installation Guide - AWS Load Balancer Controller

Load Balancer Controller Installation Kubernetes version requirements AWS Load Balancer Controller v2.0.0~v2.1.3 requires Kubernetes 1.15+ AWS Load Balancer Controller v2.2.0+ requires Kubernetes 1.16+ Existing AWS ALB Ingress Controller users AWS ALB Ingr

kubernetes-sigs.github.io

์™ธ๋ถ€์—์„œ ํด๋Ÿฌ์Šคํ„ฐ ๋‚ด๋ถ€์— ์ ‘๊ทผํ•˜๊ธฐ ์œ„ํ•œ type์€ ์—ฌ๋Ÿฌ๊ฐ€์ง€๊ฐ€ ์žˆ์ง€๋งŒ ์—ฌ๊ธฐ์„œ๋Š” ingress๋ฅผ ์ด์šฉํ•˜์—ฌ service, nodeport๋ฅผ ์„ค์ •ํ•ด์ค„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๋˜ํ•œ alb๋Š” Instance ๋ชจ๋“œ์ž…๋‹ˆ๋‹ค.

 

์ž๊ฒฉ ์ฆ๋ช… ๋งŒ๋“ค๊ธฐ

eksctl utils associate-iam-oidc-provider \
    --region <region-code> \
    --cluster <your-cluster-name> \
    --approve

 

ALB์— ๋Œ€ํ•œ ์ •์ฑ… ์ƒ์„ฑ

curl -o iam-policy.json https://raw.githubusercontent.com/kubernetes-sigs/aws-load-balancer-controller/v2.2.1/docs/install/iam_policy.json

์œ„์—์„œ ๋ฐ›์€ json์„ ์ด์šฉํ•˜์—ฌ ์•„๋ž˜์™€ ๊ฐ™์ด ํ•ด์ค๋‹ˆ๋‹ค.

aws iam create-policy \
    --policy-name AWSLoadBalancerControllerIAMPolicy \
    --policy-document file://iam-policy.json

 

ํด๋Ÿฌ์Šคํ„ฐ์— ๋Œ€ํ•œ serviceAccount ์ƒ์„ฑํ•˜๊ธฐ

eksctl create iamserviceaccount \
--cluster=<cluster-name> \
--namespace=kube-system \
--name=aws-load-balancer-controller \
--attach-policy-arn=arn:aws:iam::<AWS_ACCOUNT_ID>:policy/AWSLoadBalancerControllerIAMPolicy \
--override-existing-serviceaccounts \
--approve

ํด๋Ÿฌ์Šคํ„ฐ์— ์•„๋ฌด๋‚˜ ์ ‘๊ทผํ•˜๋ฉด ์•ˆ๋˜๋ฏ€๋กœ serviceAccount๋ฅผ ๋งŒ๋“ค์–ด์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค.

cert-manager ์„ค์น˜

kubectl apply --validate=false -f https://github.com/jetstack/cert-manager/releases/download/v1.0.2/cert-manager.yaml

๋งŒ์•ฝ k8s ๋ฒ„์ „์ด 1.16๋ณด๋‹ค ๋‚ฎ์œผ๋ฉด ๋‹ค๋ฅธ ๋ฒ„์ „์˜ cert-manager๋ฅผ ์ด์šฉํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

ALB controller ์ƒ์„ฑํ•˜๊ธฐ

wget https://raw.githubusercontent.com/kubernetes-sigs/aws-load-balancer-controller/v2.2.1/docs/install/v2_2_1_full.yaml

์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์ „์— ๋ฐ˜๋“œ์‹œ ํ•ด์•ผํ•˜๋Š” ์ผ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

apiVersion: apps/v1
kind: Deployment
. . . 
name: aws-load-balancer-controller
namespace: kube-system
spec:
    . . . 
    template:
        spec:
            containers:
                - args:
                    - --cluster-name=<INSERT_CLUSTER_NAME>

--cluster-name์˜ ๋ถ€๋ถ„์„ ๋ฐ˜๋“œ์‹œ ์ƒ์„ฑํ•œ ํด๋Ÿฌ์Šคํ„ฐ์˜ ์ด๋ฆ„์œผ๋กœ ๋ณ€๊ฒฝํ•ด์ค๋‹ˆ๋‹ค. 

 

๋˜ ์šฐ๋ฆฌ๋Š” serviceAccount๋ฅผ ๋ฏธ๋ฆฌ ์ƒ์„ฑํ–ˆ์œผ๋ฏ€๋กœ ์•„๋ž˜๋กœ ์ด๋™ํ•˜์—ฌ kind:ServiceAccount ๋ถ€๋ถ„์„ ๋ชจ๋‘ ์‚ญ์ œํ•ด์ค๋‹ˆ๋‹ค.

apiVersion: v1
kind: ServiceAccount

 

๊ทธ๋Ÿผ ์ด์ œ alb controller๋ฅผ ์„ค์น˜ํ•ด์ค๋‹ˆ๋‹ค.

kubectl apply -f v2_2_1_full.yaml

 

*์ฃผ์˜ : cert-manager ๊ด€๋ จ ์˜ค๋ธŒ์ ํŠธ๋“ค์ด ๋‹ค ์„ค์น˜๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ์‹คํ–‰ํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์ƒ์„ฑ๋˜๊ธฐ์ „์— ์œ„ ๋ช…๋ น์–ด๋ฅผ ์‹คํ–‰ํ•˜๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด k8s ํด๋Ÿฌ์Šคํ„ฐ์— ingress๋ฅผ ์œ„ํ•œ alb controller๋ฅผ ์„ค์น˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

๊ทธ๋Ÿผ ๊ฐ„๋‹จํžˆ ์™ธ๋ถ€์— ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ nginx๋ฅผ ๋ฐฐํฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

deployment -> service -> ingress ์ˆœ์œผ๋กœ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋“œ์‹œ ์„ ํ–‰ ์˜ค๋ธŒ์ ํŠธ๊ฐ€ ์ œ๋Œ€๋กœ ์ƒ์„ฑ๋˜๊ณ  ์‹คํ–‰๋˜๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ๋‹ค์Œ ์˜ค๋ธŒ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜์‹œ๊ธฐ ๋ฐ”๋ž๋‹ˆ๋‹ค. ์•ˆ๊ทธ๋Ÿฌ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

 

Deployment

$ kubectl apply -f nginx-deployment.yaml

*nginx-deployment.yaml

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

Service

$ kubectl apply -f nginx-service.yaml

*nginx-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  annotations:
    alb.ingress.kubernetes.io/healthcheck-path: "/healthy"
spec:
  selector:
     app: nginx
  type: NodePort
  ports:
  - port: 8080
    protocol: TCP
    targetPort: 80

 

Ingress

$ kubectl apply -f nginx-ingress.yaml

*nginx-ingress.yaml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx-ingress
  annotations:
    kubernetes.io/ingress.class: alb
    alb.ingress.kubernetes.io/scheme: internet-facing
    alb.ingress.kubernetes.io/target-type: instance
spec:
  rules:
    - http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: "nginx-service"
                port:
                  number: 8080

 

$ kubectl describe ingress nginx-ingress

์œ„ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ingrss๊ฐ€ ์•„์ฃผ ์ž˜ ์ƒ์„ฑ๋œ ๊ฑธ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

k8s-default-nginxing-eecf94859d-1802852978.ap-northeast-2.elb.amazonaws.com ๋กœ ์ ‘๊ทผํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์™ธ๋ถ€์—์„œ ๋‚ด๋ถ€๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

alb์˜ ๊ณผ์ •์€ ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ™”์‚ดํ‘œ๋Š” ์ž˜๋ชป๋œ ๊ฒƒ์ด๋ฏ€๋กœ ๋ฌด์‹œํ•ฉ๋‹ˆ๋‹ค.

nodeport๋Š” ์•„๋ž˜์˜ ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. 8080์ด ์„œ๋น„์Šค๋กœ ๊ฐ€๊ธฐ ์œ„ํ•œ ํฌํŠธ์ด๊ณ  ๋’ค์— 32334(์ด๋ฏธ์ง€์™€ ๋‹ค๋ฆ„)์€ alb์˜ ํƒ€๊ฒŸ๊ทธ๋ฃน ํฌํŠธ๋กœ nodeport์ž…๋‹ˆ๋‹ค. ํƒ€๊ฒŸ๊ทธ๋ฃน์˜ ํฌํŠธ ๋ฒˆํ˜ธ๋Š” ๋œฌ๊ธˆ์—†๋Š” 32334์ธ๋ฐ alb์—์„œ ์•Œ์•„์„œ ๋ผ์šฐํŒ…ํ•ด์ค๋‹ˆ๋‹ค.

 

aws eks๋ฅผ ์ด์šฉํ•˜์—ฌ ์ปจํŠธ๋กค ํ”Œ๋ ˆ์ธ๊ณผ ๋ฐ์ดํ„ฐ ํ”Œ๋ ˆ์ธ์„ ์ƒ์„ฑํ•˜์˜€๊ณ , ์™ธ๋ถ€์—์„œ ๋‚ด๋ถ€๋กœ ์ ‘๊ทผํ•˜๊ฒŒ ํ•ด๋ณด์•˜์Šต๋‹ˆ๋‹ค. controller ๊ฐ™์€ ๊ฒฝ์šฐ alb ๋ง๊ณ ๋„ ๋‹ค์–‘ํ•œ ์†Œ์Šค๋“ค์ด ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. ๊ตฌ๊ธ€๋งํ•ด์„œ ์ฐธ๊ณ ํ•ด๋ณด์‹œ๊ธธ ๋ฐ”๋ž๋‹ˆ๋‹ค.

 

 

[K8S] kubeadm ์ด์šฉํ•˜์—ฌ k8s Cluster ๋ฐฐํฌํ•˜๊ธฐ(๊ณต์‹๋ฌธ์„œ ์œ„์ฃผ)

AWS EKS ๋‚˜ GCP GKE๋ฅผ ์ด์šฉํ•˜๋ฉด ์–ด๋ ต์ง€ ์•Š๊ฒŒ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ํผ๋ธ”๋ฆญ ํด๋ผ์šฐ๋“œ๋ฅผ ์ด์šฉํ•˜์ง€ ์•Š๊ณ  ์˜จํ”„๋ ˆ๋ฏธ์Šค ํ™˜๊ฒฝ์—์„œ ์ฟ ๋ฒ„๋„คํ‹ฐ์Šค ํด๋Ÿฌ์Šคํ„ฐ๋ฅผ ๊ตฌ์ถ•ํ•˜๊ธฐ๋ž€ ์‰ฌ์šด ์ผ์€ ์•„๋‹™๋‹ˆ

kingofbackend.tistory.com

 

profile on loading

Loading...