avatarGökhan Yılmaz Gökün

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

3897

Abstract

onfig files</li><li><b>.gitignore:</b> for ignore some files on a repository.</li><li><b>.gitlab-ci.yml:</b> to creating and manage CI/CD progresses.</li><li><b>Dockerfile:</b> for build docker images.</li></ul><p id="9f50"><b>Build with Dockerfile</b></p><p id="e398">To create a docker image, you must use Dockerfile. Our Dockerfile is like that,</p> <figure id="756c"> <div> <div>

            <iframe class="gist-iframe" src="/gist/gokhangokun/1a336c17cb9c673fd07c5c7b7dbc9bbf.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="cf17">In our Dockerfile, we use alpine Linux distro.</p><p id="2e19"><b>Why we select Alpine as a base Docker image?</b></p><p id="c90e">The reason is in that sentence, Small. Simple. Secure. Alpine Linux is a security-oriented, lightweight Linux distribution based on musl libc and busybox. Also, alpines’ size is very minimal. It is only 3.98MB.</p><p id="b19d"><b>Comparing Linux OS images</b></p><p id="05d2">This chart shows us alpine Linux is very small.</p><figure id="0987"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*Bl7GQI7cLf5w5R6o"><figcaption>Figure 4. Comparing Linux OS images</figcaption></figure><p id="069a">After the base image selection, we have a docker argument for the passing environment to Dockerfile. And we have some environments like KUBECTL_VERSION and KUBE_ENVIRONMENT on our Dockerfile. KUBECTL_VERSION is used for deciding the kubectl package version while installation. Also, we use KUBE_ENVIRONMENT for selecting the kubeconfig environment.</p><p id="b498">Our base image Alpine is very small and simple. For that reason, we should add some missed packages for our build process. These packages are git, gettext, and curl.</p><p id="8263">Let’s continue with kubectl, kubectl is the most important command for Kubernetes deployment. Kubectl is a command-line interface for running commands against Kubernetes clusters. We use this command on the CI/CD pipeline, to deploy our projects to Kubernetes.</p><p id="ac05">Our build steps are almost ready, only one thing is left; to give kubeconfig files to kubectl. After these steps, we are ready to build a Docker base image. Let’s see these steps below.</p>
    <figure id="ade2">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/gokhangokun/dde123bae6f1ecd02eaa83092667fe2e.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="b803"><b>CI/CD Steps</b></p><p id="f187">Our .gitlab-ci.yml file is shown below with descriptions. You can re-arrange your steps to read our descriptions.</p>
    <figure id="1f39">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/gokhangokun/83264ccd17cf4d9eeddd92c5614b8dec.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="8c15">After configuring the GitLab CI/CD file (.gitlab-ci.yml), We are ready to build our customized kubectl images.</p><p id="7f68"><b>The Pipeline</b></p><p id="88e0">We have 7 steps but in this article only dev and stage steps are mentioned. Other steps are the same as dev and stage. Nothing is different.</p><p id="809c">Our pipeline is shown below in Figure 5,</p><figure id="3ee3"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*JHYjZ06vjOcrhcPs"><figcaption>Figure 5. Pipeline</figcaption></figure><p id="9baa"><b>The Container Registry</b></p><p id="9dcc">We are using GitLabs’ own docker container registry, These built images are locate

Options

d in the docker container registry for ready to use when we need it.</p><p id="29a9">Container registry in GitlLab is shown below in Figure 6,</p><figure id="c222"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*Cp3hIKlXqVKclk9w"><figcaption>Figure 6. Container Registry</figcaption></figure><p id="47f6"><b>Use-Case</b></p><p id="416c">Our environment based kubectl images are ready for use. Go to your project and open your .gitlab-ci.yml file. Add a new stage as Deployment.</p> <figure id="c49d"> <div> <div>

            <iframe class="gist-iframe" src="/gist/gokhangokun/09e596d00bf0e55b1337e119072b12fa.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="033a">After, add new deployment jobs for each environment.</p>
    <figure id="5b43">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/gokhangokun/dcdc52f5536e3e327b5b977b999744f1.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="dcc1">As you can see, our deployment jobs have different base images. Images are <b>docker-kubectl:{ENVIRONMENT}</b>. After giving a base image, we should execute some shell script with some pipeline values ({ENVIRONMENT}, $CI_PROJECT_NAME, $CI_COMMIT_SHORT_SHA). These values are respectively environment, project name and commit short SHA. After passing these values to a shell script which is “deploy.sh” in this example, complete the deployment process.</p><p id="a8ca"><b>Deployment Script</b></p><p id="c281">In our project, we have a Kubernetes folder for store deployment script and Kubernetes YAML files. Such as deployment and service YAML files.</p><p id="518d">We give environment prefix for these YAML files. And deploy.sh script uses this environment prefix.</p><figure id="ccdd"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/0*XyBegQQpeQFr-wtK"><figcaption>Figure 7. Deployment templates and scripts</figcaption></figure><p id="eefd">In deploy.sh, we have passed values from the pipeline to YAML files, we replace template values with these. After we generate final Deployment and Service YAML files to push Kubernetes.</p><p id="9948">Kubectl applies command sends generated YAML files to Kubernetes and complete deployment process. After deployment removes generated files and completes the clean-up process.</p><p id="61c7">Deploy.sh file as shown below,</p>
    <figure id="ab11">
        <div>
          <div>
            
            <iframe class="gist-iframe" src="/gist/gokhangokun/c5fdde9702f1e9283f66e65cccf47e28.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
          </div>
        </div>
    </figure></iframe></div></div></figure><p id="02a5"><b>Conclusion</b></p><p id="0f0f">We use this structure in our Kubernetes deployment processes and we find it easy to apply after creation base kubectl images.</p><p id="82e9"><b>What did we do?</b></p><ul><li>Create a kubectl base image from alpine Linux with environment kubeconfigs.</li><li>Create a CI/CD pipeline to build and push kubectl base images.</li><li>Add the deployment step on our existing project pipelines (.gitlab-ci.yml). And gives that kubectl base images as a job base image.</li><li>Create a deployment script.</li><li>Generate Kubernetes service and deployment YAML files with pipeline values.</li><li>Apply YAML files to the Kubernetes cluster with kubectl command.</li><li>Clean-up generated files.</li></ul><p id="6566">If you are using GitLab EE with a Core license. You can integrate multiple Kubernetes cluster with this way.</p></article></body>

Multiple Kubernetes Cluster Integration with Gitlab Core features

In this article, I will describe how you can integrate your Kubernetes clusters to Gitlab CI/CD progress with the Gitlab Core license. In our case, we are using this method for Kubernetes integration. Let’s check gitlab official support and our solution.

Gitlab Official Kubernetes Features

Gitlab provides Kubernetes support for three levels. These are instance-level, group-level, and project-level. Let’s see these configuration levels.

The first one is the instance level. Instance level Kubernetes clusters allow you to connect a Kubernetes cluster to the GitLab instance. It’s enabling you to use the same cluster configuration across multiple projects.

Another configuration level is group-level clusters. Group level Kubernetes clusters allow you to connect a Kubernetes cluster to your group. It’s enabling you to use the same cluster configuration across multiple projects.

The last configuration is the project level. Project-level configuration allows integrating projects with your Kubernetes cluster. But you can do that for all projects.

These configurations are free for only one Kubernetes cluster. For multiple Kubernetes cluster integration, you can pay and get a premium or silver GitLab license. These licenses will use with GitLab Enterprise Edition. Because of that, we will suggest you use Enterprise Edition Core as a minimum for upgradeable GitLab.

We have Multiple Kubernetes cluster. Should I pay money for Kubernetes Integration?

The answer is no. If you have “.kubeconfig” files, you should not pay money for Kubernetes Integration.

How can we do that?

We are using customized kubectl base images for deployments that have their own kubeconfigs.

Let’s start with CI/CD enabling

Let’s give kubeconfigs to base kubectl images, First of all, you can create an empty project on your GitLab. After the creation of the project, you can add a .gitlab-ci.yml file to the project root. This file enables your CI/CD pipeline for this project.

How we store kubeconfig files

To use kubeconfig files in CI/CD pipeline, create a folder like “kube-configs” and put your kubeconfig files inside that as an environment subgroups like dev, stage, prod, etc.

Figure 1. kube-configs folder
Figure 2. config file in environment folder

Example config file,

Project structure

Our project structure is shown in Figure 3,

Figure 3. Project structure

We have a kube-configs folder, .gitognore, .gitlab-ci.yml and Dockerfile in our project.

  • Kube-configs: for store config files
  • .gitignore: for ignore some files on a repository.
  • .gitlab-ci.yml: to creating and manage CI/CD progresses.
  • Dockerfile: for build docker images.

Build with Dockerfile

To create a docker image, you must use Dockerfile. Our Dockerfile is like that,

In our Dockerfile, we use alpine Linux distro.

Why we select Alpine as a base Docker image?

The reason is in that sentence, Small. Simple. Secure. Alpine Linux is a security-oriented, lightweight Linux distribution based on musl libc and busybox. Also, alpines’ size is very minimal. It is only 3.98MB.

Comparing Linux OS images

This chart shows us alpine Linux is very small.

Figure 4. Comparing Linux OS images

After the base image selection, we have a docker argument for the passing environment to Dockerfile. And we have some environments like KUBECTL_VERSION and KUBE_ENVIRONMENT on our Dockerfile. KUBECTL_VERSION is used for deciding the kubectl package version while installation. Also, we use KUBE_ENVIRONMENT for selecting the kubeconfig environment.

Our base image Alpine is very small and simple. For that reason, we should add some missed packages for our build process. These packages are git, gettext, and curl.

Let’s continue with kubectl, kubectl is the most important command for Kubernetes deployment. Kubectl is a command-line interface for running commands against Kubernetes clusters. We use this command on the CI/CD pipeline, to deploy our projects to Kubernetes.

Our build steps are almost ready, only one thing is left; to give kubeconfig files to kubectl. After these steps, we are ready to build a Docker base image. Let’s see these steps below.

CI/CD Steps

Our .gitlab-ci.yml file is shown below with descriptions. You can re-arrange your steps to read our descriptions.

After configuring the GitLab CI/CD file (.gitlab-ci.yml), We are ready to build our customized kubectl images.

The Pipeline

We have 7 steps but in this article only dev and stage steps are mentioned. Other steps are the same as dev and stage. Nothing is different.

Our pipeline is shown below in Figure 5,

Figure 5. Pipeline

The Container Registry

We are using GitLabs’ own docker container registry, These built images are located in the docker container registry for ready to use when we need it.

Container registry in GitlLab is shown below in Figure 6,

Figure 6. Container Registry

Use-Case

Our environment based kubectl images are ready for use. Go to your project and open your .gitlab-ci.yml file. Add a new stage as Deployment.

After, add new deployment jobs for each environment.

As you can see, our deployment jobs have different base images. Images are docker-kubectl:{ENVIRONMENT}. After giving a base image, we should execute some shell script with some pipeline values ({ENVIRONMENT}, $CI_PROJECT_NAME, $CI_COMMIT_SHORT_SHA). These values are respectively environment, project name and commit short SHA. After passing these values to a shell script which is “deploy.sh” in this example, complete the deployment process.

Deployment Script

In our project, we have a Kubernetes folder for store deployment script and Kubernetes YAML files. Such as deployment and service YAML files.

We give environment prefix for these YAML files. And deploy.sh script uses this environment prefix.

Figure 7. Deployment templates and scripts

In deploy.sh, we have passed values from the pipeline to YAML files, we replace template values with these. After we generate final Deployment and Service YAML files to push Kubernetes.

Kubectl applies command sends generated YAML files to Kubernetes and complete deployment process. After deployment removes generated files and completes the clean-up process.

Deploy.sh file as shown below,

Conclusion

We use this structure in our Kubernetes deployment processes and we find it easy to apply after creation base kubectl images.

What did we do?

  • Create a kubectl base image from alpine Linux with environment kubeconfigs.
  • Create a CI/CD pipeline to build and push kubectl base images.
  • Add the deployment step on our existing project pipelines (.gitlab-ci.yml). And gives that kubectl base images as a job base image.
  • Create a deployment script.
  • Generate Kubernetes service and deployment YAML files with pipeline values.
  • Apply YAML files to the Kubernetes cluster with kubectl command.
  • Clean-up generated files.

If you are using GitLab EE with a Core license. You can integrate multiple Kubernetes cluster with this way.

Kubernetes
Gitlab
Gitlab Ci
Cicd Pipeline
Pipeline
Recommended from ReadMedium