Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
    • Switch to GitLab Next
  • Sign in / Register
  • k33g_org.gitlab.io k33g_org.gitlab.io
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
    • Locked Files
  • Issues 62
    • Issues 62
    • List
    • Boards
    • Service Desk
    • Milestones
    • Requirements
  • Merge requests 0
    • Merge requests 0
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
    • Test Cases
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Container Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Code review
    • Insights
    • Issue
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • k33g_orgk33g_org
  • k33g_org.gitlab.iok33g_org.gitlab.io
  • Issues
  • #82
Closed
Open
Issue created Sep 01, 2021 by Philippe Charrière@k33g🎱Owner

Set up a GitLab.com Civo Kubernetes integration with GitPod (from everywhere)

IMG_20210826_095309

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:

  • One to create the cluster on Civo: https://gitlab.com/k33g_org/project.templates/civo-cluster
  • The other to set up the Kubernetes integration: https://gitlab.com/k33g_org/project.templates/gitlab-integration-civo-cluster

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
Screenshot_2021-09-01_at_13.44.55
Import the project
Screenshot_2021-09-01_at_13.45.26
The project must be private
Screenshot_2021-09-01_at_13.45.41
Wait for some seconds
Screenshot_2021-09-01_at_13.45.49

Once the project is imported, open it with GitPod

Open the project
Screenshot_2021-09-01_at_13.46.01
Your project inside the GitPod IDE
Screenshot_2021-09-01_at_13.53.53

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
Screenshot_2021-09-01_at_14.04.50

At the same time, you can check the provisioning on the Civo side:

Building the cluster
Screenshot_2021-09-01_at_14.05.13
The cluster is created and running
Screenshot_2021-09-01_at_14.06.14

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
Screenshot_2021-09-01_at_14.09.44

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
Screenshot_2021-09-01_at_14.17.01

Or even run K9S with this command k9s --all-namespaces:

K9S
Screenshot_2021-09-01_at_14.17.21

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
Screenshot_2021-09-01_at_15.09.44
Open the project with GitPod
Screenshot_2021-09-01_at_15.11.15

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
Screenshot_2021-09-01_at_15.23.25

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"
Screenshot_2021-09-01_at_15.26.31
Choose the panel "Connect existing cluster"
Screenshot_2021-09-01_at_15.26.45
And fill the information thanks to the previously generated files
Screenshot_2021-09-01_at_15.28.49
And finally click on "Add Kubernetes cluster"
Screenshot_2021-09-01_at_15.29.00
On the "Details" panel set the "Base domain" and 🖐️ click on "Save changes"
Screenshot_2021-09-01_at_15.29.57

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"
Screenshot_2021-09-01_at_15.31.14
Don't forget to save the changes
Screenshot_2021-09-01_at_15.31.41

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
Screenshot_2021-09-01_at_16.08.21

And in the cluster management project set the option "Enable shared runners for this group" to true

Project Runners section
Screenshot_2021-09-01_at_16.04.05

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
Screenshot_2021-09-01_at_15.52.01

Now we are ready to commit (and push) our changes and trigger the pipeline of the project.

The CI/CD pipeline
Screenshot_2021-09-01_at_16.10.18

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
Screenshot_2021-09-01_at_16.10.34

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
Screenshot_2021-09-01_at_16.20.06

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
Screenshot_2021-09-01_at_16.26.56

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
Screenshot_2021-09-01_at_16.19.21
Add a new gitlab-ci.yml file
Screenshot_2021-09-01_at_16.25.17
Commit your changes, it will trigger the CI Pipeline
Screenshot_2021-09-01_at_16.25.37
The pipeline is running
Screenshot_2021-09-01_at_16.28.58

If you return to GitPod, you can notice that a new pod is running to execute the CI job:

A new pod is running
Screenshot_2021-09-01_at_16.30.01

And you can check the logs of the runner

Logs
Screenshot_2021-09-01_at_16.30.15

That's all 🎉

You can find the 2 template projects here:

  • Create the cluster on Civo: https://gitlab.com/k33g_org/project.templates/civo-cluster
  • Setup the Kubernetes integration: https://gitlab.com/k33g_org/project.templates/gitlab-integration-civo-cluster

👋

  • 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
Edited Sep 01, 2021 by Philippe Charrière
Assignee
Assign to
Time tracking