aws alb controller (application load balance controller)
์ฟ ๋ฒ๋คํฐ์ค๋ฅผ ์ง์ ์ค์นํ๊ณ ์ฌ์ฉํ ๋๋ ingress-nginx๋ฅผ ์ฌ์ฉํ์์ผ๋ eks์๋ aws (alb) application load balance๋ฅผ ์ฌ์ฉํ ์ ์๋ ๋ฐฉ๋ฒ์ด ์๋ค.
์ฒ์ ๊ณ ๋ฏผ์ด aws์์ ๋ก๋๋ฐ๋์ค๋ฅผ ์ธํ
ํ๋๊ฒ ๋ฒ๊ฑฐ๋กญ๋ค๋ ๊ณ ๋ฏผ์ด ์์๋๋ฐ ๊ทธ๊ฑธ aws์์ ์๊ณ ์์๋์ง kubernete ์ค์ ํ์ผ์ ์ ์ด๋ง ์ฃผ๋ฉด ์๋์ผ๋ก alb๊ฐ ์์ฑ์ด ๋๋ค.
ALB Controller๋ฅผ ์ค์น๋ฅผ ํด๋๋ฉด ์ฟ ๋ฒ๋คํฐ์ค์ ์ค์ ์ ํ๋ฉด ์ด ์ปจํธ๋กค๋ฌ๊ฐ ALB๋ฅผ ์๋์ผ๋ก ๋ฑ๋กํด์ฃผ๋ ๊ฒ์ด๋ค.
์ด ์ปจํธ๋กค๋ฌ๊ฐ alb์ ์ ์์ด ๊ฐ๋ฅํ๊ฒ ๋์ผ๋๋ฐ ์ด๊ฑธ oidc๋ฅผ ์ฌ์ฉํ๋ค.(์ธ์ฆ)
ALB๋ nodeport ๋ loadbalance๋ง ์ง์์ ํ๋ค.(์ค์)
๋งํฌ์ ์๋ ๋ด์ฉ์ ํด์ฃผ๋ฉด ๋๋ค. ๊ฐ๋จํ๊ฒ ์์ฝํด๋ณด๋ฉด
OIDC
https://docs.aws.amazon.com/eks/latest/userguide/enable-iam-roles-for-service-accounts.html
OIDC Issuer
๊ธฐ๋ณธ์ ์ผ๋ก eksctl๋ก ์์ฑํ๋ฉด ์ด๊ฑด ๊ธฐ๋ณธ๊ฐ์ด ๊ฐ์ด ์๊น
Copy aws eks describe-cluster --name cluster01 --query "cluster.identity.oidc.issuer" --output text
Copy > https://oidc.eks.us-west-1.amazonaws.com/id/295F23831974F59E6DF049E7284078A6
OIDC Provider
Copy aws iam list-open-id-connect-providers | grep 295F23831974F59E6DF049E7284078A6
์๋ฌด๊ฒ๋ ์๋์จ๋ค. ์๋ค๋๊ฑฐ๋ค ๊ทธ๋ฌ๋ฉด ์์ฑ ํด์ค์ผ ํ๋ค. ์์ผ๋ฉด ์์ฑ ๋ถ๋ถ์ ๋์ด๊ฐ๋ฉด ๋๋ค.
์์ผ๋ฉด ์์ฑ
Create an IAM OIDC provider for your cluster
Copy eksctl utils associate-iam-oidc-provider \
--region us-west-1 \
--cluster cluster01 \
--approve
๋ด์ฉ ํ์ธ
Copy aws iam list-open-id-connect-providers | grep 295F231974F59E6DF049E7284078A6
Copy > - Arn: arn:aws:iam::530310009353:oidc-provider/oidc.eks.us-west-1.amazonaws.com/id/295F23831974F59E6DF049E7284078A6
๋ด์ฉ์ด ์๋ค. oidc provider๋ ๋ง๋ค์ด์ก๋ค.
์น์ฌ์ดํธ์์๋ ์์ฑ ํ์ธ ๊ฐ๋ฅ
https://console.aws.amazon.com/iamv2/home#/identity_providers
ALB Controller Install
Create an IAM policy
Copy # download
curl -o iam_policy.json https://raw.githubusercontent.com/kubernetes-sigs/aws-load-balancer-controller/v2.2.0/docs/install/iam_policy.json
aws iam create-policy \
--policy-name AWSLoadBalancerControllerIAMPolicy \
--policy-document file://iam_policy.json
arn์ ๋ณต์ฌํด์ ๋ณด๊ดํด๋๋ค.
Copy Policy :
Arn : arn:aws:iam::530310009353:policy/AWSLoadBalancerControllerIAMPolicy
AttachmentCount : 0
CreateDate : '2021-06-02T22:27:30+00:00'
DefaultVersionId : v1
IsAttachable : true
Path : /
PermissionsBoundaryUsageCount : 0
PolicyId : ANPAXW6HU27ETIAOLPJGG
PolicyName : AWSLoadBalancerControllerIAMPolicy
UpdateDate : '2021-06-02T22:27:30+00:00'
์น์ฌ์ดํธ์์ ํ์ธ
https://console.aws.amazon.com/iam/home#/policies
AWSLoadBalancerControllerIAMPolicy๋ก ๊ฒ์ํด๋ณด๋ฉด ์์ฑ๋ ๊ฒ์ ์์ ์๋ค.
create Role
trusted entity > Web identity
Attach Policy section > AWSLoadBalancerControllerIAMPolicy
Role Name : AmazonEKSLoadBalancerControllerRole > create role
์์ฑ๋๊ฑฐ ํ์ธ
After the role is created, choose the role in the console to open it for editing
Trust relationships > Edit trust relationship
๋ค์ ์ฝ๋๋ก ๋ณ๊ฒฝ
sub": "system:serviceaccount:kube-system:aws-load-balancer-controller"
aws-load-balancer-controller-service-account.yaml
Copy ```yaml
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
app.kubernetes.io/component: controller
app.kubernetes.io/name: aws-load-balancer-controller
name: aws-load-balancer-controller
namespace: kube-system
annotations:
eks.amazonaws.com/role-arn: arn:aws:iam::530310009353:role/AmazonEKSLoadBalancerControllerRole
```
Copy role-arn ์ ๋ณต์ฌํด๋๊ฑธ๋ก ๋ฎ์ด์ด๋ค.
create service account kubectl apply -f aws-load-balancer-controller-service-account.yaml
controller ์ค์น
ํ์ฌ alb controller๊ฐ ์๋์ง ํ์ธํ๋ค. ์์ด์ผ ํ๋ค. ์์ผ๋ฉด ์ง์ด๋ค.
Copy kubectl get deployment -n kube-system alb-ingress-controller
> Error from server (NotFound): deployments.apps "alb-ingress-controller" not found
https://github.com/kubernetes-sigs/aws-load-balancer-controller ์์ ์ต์ ๋ฆด๋ฆฌ์ฆ๋ฅผ ํ์ธํ์ ๋ฒ์ ๋ฑ์ ์์ ํด๋ผ.
cert-manager๊ฐ ๋ํ๋์๊ฐ ๊ฑธ๋ ค์๋ค. ๊ฐ์ด ์ค์นํ์.
Copy kubectl apply -f https://github.com/jetstack/cert-manager/releases/download/v1.3.1/cert-manager.yaml
Copy curl -o v2_2_0_full.yaml https://raw.githubusercontent.com/kubernetes-sigs/aws-load-balancer-controller/v2.2.0/docs/install/v2_2_0_full.yaml
ํ์ผ์ ์์ ํ์.
ServiceAccount ์ญ์
cluster name๋ณ๊ฒฝ
Copy kubectl apply -f v2_2_0_full.yaml
ํ์ธ
Copy kubectl get deployment -n kube-system aws-load-balancer-controller
kubectl logs deploy/aws-load-balancer-controller -n kube-system
์์ํ์ด ๋์ค๋ฉด ์ ๋๊ฒ์ด๋ค.
์๋ฌ๊ฐ ๋์ค๋ฉด ๊ถํ๋ถ๋ถ์ ๋ค์ ํด๋ณด๋๋ก ํ์.
๋ก๊ทธ ํ์ธ
์ ์๋๋ฉด ๋ก๊ทธ๋ฅผ ํ์ธํด๋ด์ผํ๋ค.
Copy kubectl logs aws-load-balancer-controller-7d7f98596-rg8wf -n kube-system
> { "level" : "error" , "ts" :1622646021.3727376, "logger" : "controller" , "msg" : "Reconciler error" , "controller" : "ingress" , "name" : "www" , "namespace" : "default" , "error" : "couldn't auto-discover subnets: UnauthorizedOperation: You are not authorized to perform this operation.\n\tstatus code: 403, request id: 73f7cb4e-c285-4a5a-9068-13e4e6c94f6a" }
์ด๋ฌ๋ฉด Oidc๊ฐ ์ ๋์ํ์ง ์๋ ๊ฒ์ด๋ค.
version์ด ์๋ง์๋ค. policy๋ 2.1.3 controller๋ 2.2.2 ๋ก ๋๋ฒ๋ ธ๋ค.
2.1.3์ผ๋ก ํ๋ฉด ์๋๋ค.
alb controller ์ญ์
Copy kubectl delete -f test-deploy.yml
kubectl delete -f aws-load-balancer-controller-service-account.yaml
kubectl delete -f v2_2_0_full.yaml
delete role : AmazonEKSLoadBalancerControllerRole
delete policy : AWSLoadBalancerControllerIAMPolicy
๊ธฐ๋ณธ ingress ์ฌ์ฉ๋ฒ
Copy ---
apiVersion : apps/v1
kind : Deployment
metadata :
name : www
namespace : default
labels :
app : www
spec :
replicas : 1
selector :
matchLabels :
app : www
template :
metadata :
labels :
app : www
spec :
containers :
- name : www
image : nginx:latest
ports :
- containerPort : 80
---
apiVersion : v1
kind : Service
metadata :
name : www
namespace : default
labels :
app : www
spec :
type : NodePort
selector :
app : www
ports :
- name : http
port : 80
targetPort : 80
---
apiVersion : networking.k8s.io/v1
kind : Ingress
metadata :
name : www
namespace : default
annotations :
kubernetes.io/ingress.class : 'alb'
alb.ingress.kubernetes.io/listen-ports : '[{"HTTP": 80}]'
alb.ingress.kubernetes.io/scheme : internet-facing # ์ธํฐ๋ท(public)์์ ์ ์์ด ๋๊ฒ ํ๋ค.
spec :
rules :
- http :
paths :
- path : /
pathType : Prefix
backend :
service :
name : www
port :
number : 80
์ด๊ฑธ ์ฌ์ฉํ๋ฉด ์๋์ผ๋ก aws application load balance๋ ๋ง๋ค์ด ์ค๋ค.
Copy kubectl apply -f test-deploy.yml
ec2 -> load balance
dns์ ์ถ๊ฐ
route53
http๋ฅผ https๋ก redirect
์์ ์ํ์ ์ ํ๋ ์์ผ๋ ํน๋ณํ ๋ฐ๋ก ์ค๋ช
ํ๋ค.
anotation์ ๋ค์ ์ถ๊ฐ
Copy alb.ingress.kubernetes.io/actions.ssl-redirect : '{"Type": "redirect", "RedirectConfig": { "Protocol": "HTTPS", "Port": "443", "StatusCode": "HTTP_301"}}'
๊ทธ๋ฆฌ๊ณ path์ ๋ค์ ์ถ๊ฐ
Copy - path : /
backend :
service :
name : ssl-redirect
port :
name : use-annotation
์ด๋ฌ๋ฉด http๋ก ์ ๊ทผํ๋ฉด https๋ก ๋ฆฌ๋ค์ด๋ ํธ๋ฅผ ์์ผ์ค๋ค. ๊ผญ ์ด์ค์ ์ด ๋งจ์์ ์์ผํ๋ค.
๊ด๋ จ ๋ด์ฉ์ ์ฌ๊ธฐ๋ฅผ ์ฐธ๊ณ ํ์. https://github.com/kubernetes-sigs/aws-load-balancer-controller/blob/main/docs/guide/tasks/ssl_redirect.md
ssl backend
ํน์ pod๋ ํ๋ก๊ทธ๋จ ์์ฒด์์ ssl๋ก ์ ๊ทผ์ ๋ฐ์์ผํ ํ์๊ฐ ์์๋ alb controller์์๋ ๋ค์์ฒ๋ผ ์ฒ๋ฆฌํ๋ค.
Copy apiVersion : extensions/v1beta1
kind : Ingress
metadata :
name : auth
namespace : auth-staging
annotations :
kubernetes.io/ingress.class : 'alb'
alb.ingress.kubernetes.io/certificate-arn : arn:aws:acm:us-west-2:xxxx:certificate/199b8e95-fe5b-43e6-b499-061e4f133011
alb.ingress.kubernetes.io/listen-ports : '[{"HTTP": 80},{"HTTPS":443}]'
alb.ingress.kubernetes.io/scheme : internet-facing
alb.ingress.kubernetes.io/actions.ssl-redirect : '{"Type": "redirect", "RedirectConfig": { "Protocol": "HTTPS", "Port": "443", "StatusCode": "HTTP_301"}}'
alb.ingress.kubernetes.io/backend-protocol : HTTPS # ์ฌ๊ธฐ ์ถ๊ฐ
spec :
rules :
- host : www.aaa.com
http :
paths :
- path : /
backend :
service :
name : ssl-redirect
port :
name : use-annotation
- path : /
backend :
service :
name : auth
port :
number : 80
anotation์ ๋ค์ ์ถ๊ฐ๋ฅผ ๋ณผ์ ์๋ค.
Copy alb.ingress.kubernetes.io/backend-protocol : HTTPS # ์ฌ๊ธฐ ์ถ๊ฐ
๋ง์ฝ ์ด๊ฑธ ์ถ๊ฐํ์ง ์์ผ๋ฉด ์ด๋ฐ ์๋ฌ๋ฅผ ๋ณผ์๊ฐ ์๋ค.
Copy Getting Handshake failedโฆunexpected packet format
alb๊ฐ ๊ธฐ๋ณธ์ ์ผ๋ก http๋ก ํต์ ์ ์๋ํ๋ฏ๋ก ํฌํธ๋ 443์ ์ฐ๋ฉด์ http๋ฅผ ๋ณด๋ด๊ฒ ๋๋ค๋ณด๋ ์ด๋ฐ ์๋ฌ๊ฐ ๋์จ๋ค.
health check
๋ก๋ ๋ฐ๋์ค๊ฐ ๊ธฐ๋ณธ์ ์ผ๋ก pod๋ฅผ ๋ค ์ฒดํฌํด์ ์๋น์ค๋ฅผ ์ ์งํด์ค๋ค. ํน๋ณํ health check ๊ฒฝ๋ก๋ฅผ ์์ ํ๋ ค๋ฉด ๋ค์์ฒ๋ผ ํ์.
Copy alb.ingress.kubernetes.io/healthcheck-protocol : HTTPS #๊ธฐ๋ณธ๊ฐ http
alb.ingress.kubernetes.io/healthcheck-path : /health
alb.ingress.kubernetes.io/healthcheck-interval-seconds : '60'
pod๊ฐ ssl์ ๊ธฐ๋ํ๊ณ ์์ผ๋ฉด healthcheck-protocol๋ ๋ง๋๊ฐ์ ๋ฃ์ด์ค์ผํ๋ค.
ELB target group์ ๊ฐ๋ฉด ์ ๋ด์ฉ์ ์๋ฃ๋๋ผ๋ ๊ธฐ๋ณธ์ผ๋ก healthcheck๊ฐ ์์ฑ์ด ๋๋ค. ๊ธฐ๋ณธ๊ฐ์ด ์๊ธฐ ๋๋ฌธ์ด๋ค.
nginx app์ alb์ ์คํ
ํ๋ฒ๋ ๋ชจ๋ ๊ฑธ ์ ์ฉํด์ alb๋ฅผ ์ฌ์ฉํด๋ณด์
service.yaml deployment.yaml ingress.yaml
Copy ---
apiVersion : v1
kind : Service
metadata :
name : www
namespace : default
labels :
app : www
spec :
type : NodePort
selector :
app : www
ports :
- name : http
port : 80
targetPort : 80
Copy apiVersion : apps/v1
kind : Deployment
metadata :
name : www
namespace : default
labels :
app : www
spec :
replicas : 1
selector :
matchLabels :
app : www
template :
metadata :
labels :
app : www
spec :
containers :
- name : www
image : nginx
Copy apiVersion : extensions/v1beta1
kind : Ingress
metadata :
name : www
namespace : default
annotations :
kubernetes.io/ingress.class : 'alb'
alb.ingress.kubernetes.io/scheme : internet-facing
alb.ingress.kubernetes.io/listen-ports : '[{"HTTP": 80}, {"HTTPS":443}]'
#alb.ingress.kubernetes.io/certificate-arn: arn:aws:acm:us-west-2:YOURACCOUNT:certificate/a2eb12f7-7e36-4d50-811c-8bxxxxx7
alb.ingress.kubernetes.io/actions.ssl-redirect : '{"Type": "redirect", "RedirectConfig": { "Protocol": "HTTPS", "Port": "443", "StatusCode": "HTTP_301"}}'
spec :
rules :
- host : www.aaa.com
http :
paths :
# - path: /*
# backend:
# serviceName: ssl-redirect
# servicePort: use-annotation
- path : /*
backend :
serviceName : www
servicePort : 80
์ ์ฉํ๋ฉด alb๊ฐ ์๊ธฐ๋๊ฒ์ aws console ์์ ๋ณผ ์ ์๋ค.
ssl๋ ์ ์ฉํ๋ค. cert-arn์ certificate-manager์ ๊ฐ์ ๋ง๋ค๋ฉด ์๊ธด๋ค. ๊ทธ๊ฑธ ์ฌ์ฉ
ssl redirect ์ ์ฉ ์๋ฃ
internet-facing
: ํ์์ด๋ค.
ํฌํธ๋ 80 443์ ๋๋ค ์ด์ด์ฃผ๋ฉด ์ข๋ค.
multiple domain and ssl ์ ์ฉ
rules์ ์ฌ๋ฌ๊ฐ์ ๋๋ฉ์ธ์ ์ถ๊ฐํ๋ค. ๊ทธ๋ฆฌ๊ณ ssl์ ์ปด๋ง๋ก ์ฐ๊ฒฐํ๋ค.
Copy apiVersion : extensions/v1beta1
kind : Ingress
metadata :
name : www
namespace : default
annotations :
...
alb.ingress.kubernetes.io/certificate-arn : arn:aws:acm:us-west-2:YOURACCOUNT:certificate/a2eb12f7-7e36-4d50-811c-8bxxxxx7,arn:aws:acm:us-west-2:YOURACCOUNT:certificate/a2eb12f7-7e36-4d50-811c-8xxxxxxx #comma๋ก ์ฐ๊ฒฐ
spec :
rules :
- host : www.aaa.com
http :
paths :
- path : /*
backend :
serviceName : aaa
servicePort : 80
- host : www.bbb.com
http :
paths :
- path : /*
backend :
serviceName : bbb
servicePort : 80
์ฌ๋ฌ๊ฐ์ ์ธ๊ทธ๋ ์ค์์ albํ๋๋ฅผ ๊ณต์ ํ๊ธฐ
Copy alb.ingress.kubernetes.io/group.name : shared-ingress
Ingress๊ฐ ๋ค ๊ฐ๊ฐ์ name space์ ์๊ธฐ๋๊ฒ์ ๋ง๋ค.
๊ทธ๋ฆฌ๊ณ ๊ทธ๊ฒ๋ค์ด ํ๋์ ๋ก๋๋ฐ๋์ค๋ฅผ ์ฌ์ฉํ๋ค.