Ch20 建立環境
安裝 AWS Load Balancer Controller
-
下載 IAM policy。
curl -o iam-policy.json https://raw.githubusercontent.com/kubernetes-sigs/aws-load-balancer-controller/v2.6.0/docs/install/iam_policy.json
-
透過 AWS CLI
aws iam create-policy
命令建立 IAM Policy。$ aws iam create-policy \
--policy-name AWSLoadBalancerControllerIAMPolicy \
--policy-document file://iam-policy.json
{
"Policy": {
"PolicyName": "AWSLoadBalancerControllerIAMPolicy",
"PolicyId": "ANPA5V3NJ2SQS4EUBO3FD",
"Arn": "arn:aws:iam::111111111111:policy/AWSLoadBalancerControllerIAMPolicy",
"Path": "/",
"DefaultVersionId": "v1",
"AttachmentCount": 0,
"PermissionsBoundaryUsageCount": 0,
"IsAttachable": true,
"CreateDate": "2023-08-30T15:57:46+00:00",
"UpdateDate": "2023-08-30T15:57:46+00:00"
}
} -
使用
eksctl
命令建立 IRSA(IAM roles for service accounts) 的 IAM role 及 Service Account。$ eksctl create iamserviceaccount \
--cluster=ironman \
--namespace=kube-system \
--name=aws-load-balancer-controller \
--attach-policy-arn=arn:aws:iam::111111111111:policy/AWSLoadBalancerControllerIAMPolicy \
--override-existing-serviceaccounts \
--region ap-northeast-1 \
--approve
2023-08-30 15:59:50 [ℹ] 3 existing iamserviceaccount(s) (amazon-cloudwatch/cloudwatch-agent,amazon-cloudwatch/fluentd,kube-system/aws-node) will be excluded
2023-08-30 15:59:50 [ℹ] 1 iamserviceaccount (kube-system/aws-load-balancer-controller) was included (based on the include/exclude rules)
2023-08-30 15:59:50 [!] metadata of serviceaccounts that exist in Kubernetes will be updated, as --override-existing-serviceaccounts was set
2023-08-30 15:59:50 [ℹ] 1 task: {
2 sequential sub-tasks: {
create IAM role for serviceaccount "kube-system/aws-load-balancer-controller",
create serviceaccount "kube-system/aws-load-balancer-controller",
} }2023-08-30 15:59:50 [ℹ] building iamserviceaccount stack "eksctl-ironman-addon-iamserviceaccount-kube-system-aws-load-balancer-controller"
2023-08-30 15:59:50 [ℹ] deploying stack "eksctl-ironman-addon-iamserviceaccount-kube-system-aws-load-balancer-controller"
2023-08-30 15:59:50 [ℹ] waiting for CloudFormation stack "eksctl-ironman-addon-iamserviceaccount-kube-system-aws-load-balancer-controller"
2023-08-30 16:00:20 [ℹ] waiting for CloudFormation stack "eksctl-ironman-addon-iamserviceaccount-kube-system-aws-load-balancer-controller"
2023-08-30 16:00:20 [ℹ] created serviceaccount "kube-system/aws-load-balancer-controller" -
AWS Load Balancer Controller 提供 Helm [2] 安裝方式 ,依照 Helm 文件進行安裝,使用最新版本 3.10.0 版本:
$ curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
$ chmod 700 get_helm.sh
$ ./get_helm.sh
[WARNING] Could not find git. It is required for plugin installation.
Downloading https://get.helm.sh/helm-v3.12.3-linux-amd64.tar.gz
Verifying checksum... Done.
Preparing to install helm into /usr/local/bin
helm installed into /usr/local/bin/helm -
新增 Helm repo:
$ helm repo add eks https://aws.github.io/eks-charts
"eks" has been added to your repositories -
於第 3 步驟已經建立 Service Account 過,因此此步驟則無需再次建立 Service Account。
$ helm install aws-load-balancer-controller eks/aws-load-balancer-controller -n kube-system --set clusterName=ironman --set serviceAccount.create=false --set serviceAccount.name=aws-load-balancer-controller
NAME: aws-load-balancer-controller
LAST DEPLOYED: Wed Aug 30 16:03:45 2023
NAMESPACE: kube-system
STATUS: deployed
REVISION: 1
TEST SUITE: None
NOTES:
AWS Load Balancer controller installed!$ kubectl -n kube-system describe deploy aws-load-balancer-controller| grep Image | cut -d "/" -f 3
aws-load-balancer-controller:v2.6.0
測試環境
-
以 Nginx server 作為測試,建立
nginx-demo-ingress.yaml
包含Deployment
、Service
、Ingress
資源。 -
部署此 YAML。
$ kubectl apply -f ./nginx-demo-ingress.yaml
namespace/demo-nginx created
deployment.apps/demo-nginx-deployment created
service/service-demo-nginx created
ingress.networking.k8s.io/ingress-nginx created -
確認 Nginx Pod 皆有正常運作。
$ kubectl -n demo-nginx get ing,svc,po -o wide
NAME CLASS HOSTS ADDRESS PORTS AGE
ingress.networking.k8s.io/ingress-nginx alb * k8s-demongin-ingressn-1ac4841e19-1133650141.ap-northeast-1.elb.amazonaws.com 80 42s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/service-demo-nginx ClusterIP 10.100.100.25 <none> 80/TCP 42s app=demo-nginx
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/demo-nginx-deployment-75645cd467-bch2z 1/1 Running 0 42s 192.168.1.129 ip-192-168-18-171.ap-northeast-1.compute.internal <none> <none>
pod/demo-nginx-deployment-75645cd467-d5dxr 1/1 Running 0 42s 192.168.73.219 ip-192-168-70-74.ap-northeast-1.compute.internal <none> <none>
pod/demo-nginx-deployment-75645cd467-j4kvm 1/1 Running 0 42s 192.168.29.67 ip-192-168-18-171.ap-northeast-1.compute.internal <none> <none>
pod/demo-nginx-deployment-75645cd467-n7szj 1/1 Running 0 42s 192.168.9.76 ip-192-168-18-171.ap-northeast-1.compute.internal <none> <none>
pod/demo-nginx-deployment-75645cd467-ncflk 1/1 Running 0 42s 192.168.93.241 ip-192-168-70-74.ap-northeast-1.compute.internal <none> <none>
pod/demo-nginx-deployment-75645cd467-q44vh 1/1 Running 0 42s 192.168.17.54 ip-192-168-18-171.ap-northeast-1.compute.internal <none> <none>
pod/demo-nginx-deployment-75645cd467-q7t5x 1/1 Running 0 42s 192.168.80.15 ip-192-168-70-74.ap-northeast-1.compute.internal <none> <none>
pod/demo-nginx-deployment-75645cd467-qstst 1/1 Running 0 42s 192.168.90.12 ip-192-168-70-74.ap-northeast-1.compute.internal <none> <none>
pod/demo-nginx-deployment-75645cd467-sms55 1/1 Running 0 42s 192.168.26.4 ip-192-168-18-171.ap-northeast-1.compute.internal <none> <none>
pod/demo-nginx-deployment-75645cd467-tgd2w 1/1 Running 0 42s 192.168.65.125 ip-192-168-70-74.ap-northeast-1.compute.internal <none> <none>