There were some changes in the past months with the GitLab-Kubernetes integration. And the "One-click application management" was removed in GitLab 14.0
, and now we need to use a "cluster management project" to deploy the Kubernetes Executor on a Kubernetes cluster (https://docs.gitlab.com/ee/user/clusters/management_project.html).
I decided to set up this integration with
-
Civo to host my Kubernetes cluster (Civo provides K3S clusters as a service, it's easy to use, not expensive and the support is very nice and friendly)
-
GitLab.com for my projects (but you can use a self-managed GitLab instance of course)
- And GitPod as IDE but also as a management tool of my Kubernetes cluster (GitPod is an amazing IDE as a service with a lot of superpowers)
To help me (and you) I created 2 GitLab projects that I will be able to re-use in the future:
Both projects are GitPod projects. It means that I added the necessary tools (kubectl, k9s, ...) to each project thanks 2 files:
.gitpod.dockerfile
.gitpod.yml
We won't detail the GitPod configuration, just trust me (and read the code)
Prerequisites
You need to have/create:
- A GitLab account
- A GitPod account
- A Civo account
🖐️🖐️🖐️ Important: you need to create an environment variable CIVO_API_KEY
in the settings of your GitPod account and populate it with your Civo key.
This blog post is in 3 parts:
- The creation of the k3s cluster
- The connection to the cluster
- The set up of the Kubernetes integration with GitLab
- The use of all of that to run a CI script
Take a coffee and read it carefully 😃
Cluster creation
Create a new project
First, create a new project on GitLab.com by importing this one https://gitlab.com/k33g_org/project.templates/civo-cluster.git
Create a new project |
|
Import the project |
|
The project must be private |
|
Wait for some seconds |
|
Once the project is imported, open it with GitPod
Open the project |
|
Your project inside the GitPod IDE |
|
Time to create the cluster
Now, you can launch the first script 01-create-cluster.sh
to create the cluster on Civo.com:
run the first script |
|
At the same time, you can check the provisioning on the Civo side:
Building the cluster |
|
The cluster is created and running |
|
Once the cluster is running, you can launch the 2nd script (02-get-config.sh
) to get the "kubeconfig" file (it will be copied in the ./config
directory and named k3s.yaml
)
k3s.yaml |
|
Now, you can connect to your new cluster from the GitPod terminal. Try with this command kubectl get pods --all-namespaces
:
kubectl get pods --all-namespaces |
|
Or even run K9S with this command k9s --all-namespaces
:
K9S |
|
Link my new Civo cluster with my GitLab project
Create the cluster management project
Then we will create a cluster management project by importing this project https://gitlab.com/k33g_org/project.templates/gitlab-integration-civo-cluster.git
🖐️ Of course, we keep this project private
Create a new project |
|
Open the project with GitPod |
|
Connect to the cluster
Now, to be able to connect to the cluster:
- Check the values of the
.env
file
- Run
01-get-config.sh
to get the kubeconfig file (the file will be saved in ./config/k3s.yaml
)
- Run
02-get-data.sh
to get the certificate of the cluster and the URL of the cluster (certificate.txt
and url.txt
will be saved in ./config
)
- Run
03-create-gitlab-service-account.sh
to create the GitLab Service Account
- Run
04-get-access-token.sh
to get the access token of the cluster (it will be saved to ./config/access.token.txy
)
The files are generated in ./config
|
|
Initiate the Cluster Integration for the Group
Return on the GitLab side at the group level and follow the steps below:
Go to the Kubernetes menu of the group and click on "Integrate with a cluster certificate" |
|
Choose the panel "Connect existing cluster" |
|
And fill the information thanks to the previously generated files |
|
And finally click on "Add Kubernetes cluster" |
|
On the "Details" panel set the "Base domain" and 🖐️ click on "Save changes" |
|
Remark: I use the https://nip.io/ for my demos, so my base domain is <cluster-ip>.nip.io
Go to the "Advanced Settings" panel and select your project as the "Cluster management project" |
|
Don't forget to save the changes |
|
Setting the CI
We still need a few more steps. Go to the CI/CD Settings of the group and collapse the Runners section:
- Copy the GitLab URL
- Copy the registration token
-
🖐️ And set the option "Enable shared runners for this group" to false (but allow the projects to override the settings)
Group Runners section |
|
And in the cluster management project set the option "Enable shared runners for this group" to true
Project Runners section |
|
Then use the provided GitLab URL and registration token to update the values of ./applications/gitlab-runner/values.yaml
in our management project:
update of ./applications/gitlab-runner/values.yaml
|
|
Now we are ready to commit (and push) our changes and trigger the pipeline of the project.
The CI/CD pipeline |
|
And if you return to the management project opened with the GitPod IDE and launch K9S, you can see that the GitLab Executor (the runner) and Prometheus have been deployed on the Kubernetes cluster:
The applications have been deployed |
|
And if you return in the CI settings of the group, you can notice that there is a new runner in the list of the runners:
New runner |
|
You need to change the settings of the runner: check to true this option "Indicates whether this runner can pick jobs without tags" and save the changes:
Update the settings |
|
Try the integration with GitLab CI
Now it's time to test GitLab CI with our new Kubernetes Runner (Kubernetes executor). So, in the same group, create a new project:
Create a new project |
|
Add a new gitlab-ci.yml file |
|
Commit your changes, it will trigger the CI Pipeline |
|
The pipeline is running |
|
If you return to GitPod, you can notice that a new pod is running to execute the CI job:
A new pod is running |
|
And you can check the logs of the runner
Logs |
|
That's all 🎉
You can find the 2 template projects here:
👋
- If you loved this "post" (or not), don't forget to use the emojis reactions
- Don't hesitate to add comments and/or ask questions
- You can subscribe to the Rss feed