Skip to content

Lanzar un Cluster de EKS usando Cloud9 con SSM, 3 zonas de AZ y KMS

1. Crear un ambiente de cloud9

  • Crear ambiente de Cloud9

  • Crear Role con acceso admin y agregarlo a la instancia ec2 de cloud9

  • Cambiar la configuracion de Cloud9 de accesos temporales de AWS

  • Eliminar archivo .aws/credentials:

rm -vf ${HOME}/.aws/credentials

2. Instalar dependencias

  • Install kubectl
sudo curl --silent --location -o /usr/local/bin/kubectl \
   https://amazon-eks.s3.us-west-2.amazonaws.com/1.17.11/2020-09-18/bin/linux/amd64/kubectl
sudo chmod +x /usr/local/bin/kubectl
  • Update awscli

Upgrade AWS CLI according to guidance in AWS documentation.

sudo pip install --upgrade awscli && hash -r
Install jq, envsubst (from GNU gettext utilities) and bash-completion
sudo yum -y install jq gettext bash-completion moreutils
  • Install yq for yaml processing
echo 'yq() {
  docker run --rm -i -v "${PWD}":/workdir mikefarah/yq "$@"
}' | tee -a ~/.bashrc && source ~/.bashrc
Verify the binaries are in the path and executable
for command in kubectl jq envsubst aws
  do
    which $command &>/dev/null && echo "$command in path" || echo "$command NOT FOUND"
  done
  • Habilitar kubectl para tener bash_completion
kubectl completion bash >>  ~/.bash_completion
. /etc/profile.d/bash_completion.sh
. ~/.bash_completion

set the AWS Load Balancer Controller version

echo 'export LBC_VERSION="v2.2.0"' >>  ~/.bash_profile
.  ~/.bash_profile

3. Configurar y establecer nuestras variables de entorno

  • Cuenta, region y Zonas de disponibilidad
export ACCOUNT_ID=$(aws sts get-caller-identity --output text --query Account)
export AWS_REGION=$(curl -s 169.254.169.254/latest/dynamic/instance-identity/document | jq -r '.region')
export AZS=($(aws ec2 describe-availability-zones --query 'AvailabilityZones[].ZoneName' --output text --region $AWS_REGION))
  • revisar la region:
test -n "$AWS_REGION" && echo AWS_REGION is "$AWS_REGION" || echo AWS_REGION is not set
  • Guardar en el bash profile
echo "export ACCOUNT_ID=${ACCOUNT_ID}" | tee -a ~/.bash_profile
echo "export AWS_REGION=${AWS_REGION}" | tee -a ~/.bash_profile
echo "export AZS=(${AZS[@]})" | tee -a ~/.bash_profile
aws configure set default.region ${AWS_REGION}
aws configure get default.region
  • Validar el rol que creamos, en este caso se llama "ecsInstanceRole":
aws sts get-caller-identity --query Arn | grep ecsInstanceRole -q && echo "IAM role valid" || echo "IAM role NOT valid"

4. CLonar los microservicios de ejemplo

cd ~/environment
git clone https://github.com/brentley/ecsdemo-frontend.git
git clone https://github.com/brentley/ecsdemo-nodejs.git
git clone https://github.com/brentley/ecsdemo-crystal.git

5. Configurar nuestra llave de cifrado con AWS KMS

  • Crear una llave KMS:
aws kms create-alias --alias-name alias/eksworkshop --target-key-id $(aws kms create-key --query KeyMetadata.Arn --output text)
  • Exportar la variable:
export MASTER_ARN=$(aws kms describe-key --key-id alias/eksworkshop --query KeyMetadata.Arn --output text)
  • agregarlo al bash profile
echo "export MASTER_ARN=${MASTER_ARN}" | tee -a ~/.bash_profile

6. LANZAR EL CLUSTER EKS

6.1 Instalar eksctl

  • bajamos de este repo:
curl --silent --location "https://github.com/weaveworks/eksctl/releases/latest/download/eksctl_$(uname -s)_amd64.tar.gz" | tar xz -C /tmp
sudo mv -v /tmp/eksctl /usr/local/bin
  • verificamos:
eksctl version
  • agregamos al bash profile
eksctl completion bash >> ~/.bash_completion
. /etc/profile.d/bash_completion.sh
. ~/.bash_completion

6.2 CREAR CLUSTER CON UN ARCHIVO YAML

  • Correr el siguiente comando para crear la configuracion del cluster y el nombre del cluster

  • Puedes cambiar el nombre en el parametro "name"

  • Asegurate que las variables funcionen y esten establecidas en el bash profile con los pasos anteriores.

cat << EOF > eksworkshop.yaml
---
apiVersion: eksctl.io/v1alpha5
kind: ClusterConfig

metadata:
  name: eksworkshop-eksctl
  region: ${AWS_REGION}
  version: "1.17"

availabilityZones: ["${AZS[0]}", "${AZS[1]}", "${AZS[2]}"]

managedNodeGroups:
- name: nodegroup
  desiredCapacity: 3
  instanceType: t3.small
  ssh:
    enableSsm: true

# To enable all of the control plane logs, uncomment below:
# cloudWatch:
#  clusterLogging:
#    enableTypes: ["*"]

secretsEncryption:
  keyARN: ${MASTER_ARN}
EOF
  • Ejecutar el archivo con eksctl
eksctl create cluster -f eksworkshop.yaml
  • Obtenemos informacion de los nodos para verificar que todo este funcionando correctamente:
kubectl get nodes
  • Opcional: Exportamos una variable que nos ayudara mas adelante.
STACK_NAME=$(eksctl get nodegroup --cluster eksworkshop-eksctl -o json | jq -r '.[].StackName')
ROLE_NAME=$(aws cloudformation describe-stack-resources --stack-name $STACK_NAME | jq -r '.StackResources[] | select(.ResourceType=="AWS::IAM::Role") | .PhysicalResourceId')
echo "export ROLE_NAME=${ROLE_NAME}" | tee -a ~/.bash_profile