Connecting to EKS cluster from GitLab CI/CD

Creating a small GitLab CI/CD pipeline for deploying applications to Amazon EKS (Elastic Kubernetes Service) involves several steps

First we need to install GitLab agents in EKS cluster so that GitLab can able to communicate to EKS cluster

  • In GitLab go to your subgroup and create new repository for storing config files.

  • Folder structure : reponame/.gitlab/agents/cluster_name/config.yaml

gitops:
  manifest_projects:
  - id: rijojoseph/gitlab-agents
    default_namespace: default

ci_access:
  groups:
    - id: rijojoseph

You can able to see that rijojoseph is the subgroup name and gitlab-agents is the repository for storing the config files. Using above config file the EKS cluster which is going to connect is accessible across the subgroup named rijojoseph

  • Go to operate --> Kubernetes clusters and click on connect to cluster

  • Select the config folder and click on register

  • Now you will get the steps to run after authenticating to EKS cluster from your local machine
helm repo add gitlab https://charts.gitlab.io
helm repo update
helm upgrade --install test-cluster gitlab/gitlab-agent \
    --namespace gitlab-agent-test-cluster \
    --create-namespace \
    --set image.tag=v16.9.0-rc2 \
    --set config.token=glagent-XXXXXXXXXXX \
    --set config.kasAddress=wss://kas.gitlab.com
  • After running above steps you will see the eks cluster is connected in GitLab

  • Now you can able to use the context variable to connect to the EKS cluster from each of the repository in your subgroup

  • You just need to add a CI/CD variable like this

KUBE_CONTEXT_CLUSTER_NAME : rijojoseph/gitlab-agents:test-cluster
  • Now you can use this variable in GitLab CI/CD like this
kubectl config use-context ${KUBE_CONTEXT_CLUSTER_NAME}
  • Now the GitLab CI/CD can able to run kubectl/helm commands to apply the resources to handle the deployment