You will now deploy an Amazon EKS cluster in order to roll out your newly built application into it. In this lab you will use Terraform by HashiCorp for this purpose. However, you can also do it using AWS CloudFormation, AWS CDK or eksctl.
The Terraform files are located in ~/environment/amazon-ec2-spot-cicd-workshop/gitlab-spot/eks-cluster
directory of your Cloud9 environment.
kubectl
that you will be using to work with the Kubernetes cluster:cd ~/environment
export KUBECTL_VERSION=v1.23.7
sudo curl --silent --location -o /usr/local/bin/kubectl https://storage.googleapis.com/kubernetes-release/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl
sudo chmod +x /usr/local/bin/kubectl
cd ~/environment/amazon-ec2-spot-cicd-workshop/gitlab-spot/eks-cluster
terraform init
TOKEN=$(curl -s -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 600")
export TF_VAR_aws_region=$(curl -s -H "X-aws-ec2-metadata-token: $TOKEN" http://169.254.169.254/latest/meta-data/placement/region)
curl -o iam-policy.json https://raw.githubusercontent.com/kubernetes-sigs/aws-load-balancer-controller/main/docs/install/iam_policy.json
export TF_VAR_alb_policy=$(aws iam get-policy --policy-arn arn:aws:iam::$(aws sts get-caller-identity --output text --query Account):policy/AWSLoadBalancerControllerIAMPolicy --query Policy.Arn --output text 2>/dev/null || aws iam create-policy --policy-name AWSLoadBalancerControllerIAMPolicy --policy-document file://iam-policy.json --query Policy.Arn --output text)
terraform apply
1.23
echo VPC ID = $VPC
yes
, and press Enter. It should take approximately 15 minutes to deploy the cluster.kubeconfig
, using the information from Amazon EKS:aws eks update-kubeconfig --region $TF_VAR_aws_region --name gitlab-spot-workshop
kubectl get all -A
kubectl get nodes
curl -sSL https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3 | bash
helm repo add eks https://aws.github.io/eks-charts
helm install aws-load-balancer-controller eks/aws-load-balancer-controller --set clusterName=gitlab-spot-workshop -n kube-system
spot_group
one-by-one and check the value of the Lifecycle field in the description tab below or enable its display in the main table by choosing the cogwheel icon in the top-right corner, searching for Instance lifecycle
in the Attribute columns field, enabling the toggle next to it, and choosing Confirm:By default, the IAM user or role that created Amazon EKS cluster gets access to its Kubernetes API. However, as you have different roles assigned to the Cloud9 environment and the GitLab runners, you need to add the latter to Kubernetes Role Based Access Control (RBAC).
You will do it by manually modifying Kubernetes ConfigMap called aws-auth
. You could have done it directly in Terraform, but in this workshop we suggest that you do it manually to better understand the concepts.
aws-auth.yaml
file:cd ~/environment
kubectl get configmap -n kube-system aws-auth -o yaml > aws-auth.yaml
export ACCOUNT_ID=$(aws sts get-caller-identity --output text --query Account)
cat << EoF
- rolearn: arn:aws:iam::${ACCOUNT_ID}:role/gitlab-runner
groups:
- system:masters
username: gitlab-runner
EoF
aws-auth.yaml
by double-clicking it, and finally add the previous command’s output that you copied before into mapRoles
section. You should have something similar to the below screenshot:kubectl apply -f aws-auth.yaml
You are now ready for the final steps to deploy your demo application into the cluster in Installing the demo app into Amazon EKS.
Instead of using Terraform try deploying the Amazon EKS cluster with eksctl.
You can find step-by-step instructions in the Containers with EKS workshop, but make sure you modify them to use the VPC you created in this workshop.