[EKS] AWS EKS๋ก k8s ํด๋ฌ์คํฐ ์์ฑ ํ ALB๋ก ๋ฐฐํฌํ๊ธฐ
์ด๋ฒ ๊ธ์ ์งํํ๋ ค๋ฉด ๋จผ์ ์ ํ๋์ด์ผ ํ ์์ ์ด ์์ต๋๋ค. ๋ํ 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