Yandex Cloud
Search
Contact UsGet started
  • Blog
  • Pricing
  • Documentation
  • All Services
  • System Status
    • Featured
    • Infrastructure & Network
    • Data Platform
    • Containers
    • Developer tools
    • Serverless
    • Security
    • Monitoring & Resources
    • ML & AI
    • Business tools
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Customer Stories
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Education and Science
    • Yandex Cloud Partner program
  • Blog
  • Pricing
  • Documentation
© 2025 Direct Cursus Technology L.L.C.
Tutorials
    • All tutorials
    • Setting up a Managed Service for PostgreSQL connection from a container in Serverless Containers
    • Creating a VM from a Container Optimized Image
    • Creating a VM from a Container Optimized Image with an additional volume for a Docker container
    • Creating an instance group from a Container Optimized Image with multiple Docker containers
    • Creating an instance group from a Container Optimized Image
    • Creating a VM from a Container Optimized Image with multiple Docker containers
    • Updating a Container Optimized Image VM
    • Configuring data output from a Docker container to a serial port
      • Creating a new Kubernetes project
      • Creating a Kubernetes cluster with no internet access
      • Running workloads with GPUs
      • Using node groups with GPUs and no pre-installed drivers
      • Setting up Time-Slicing GPUs
      • Migrating resources to a different availability zone
      • Encrypting secrets
        • Scanning Container Registry for vulnerabilities during continuous deployment of applications using GitLab

In this article:

  • Required paid resources
  • Getting started
  • Set up your infrastructure
  • Install additional dependencies
  • Create a GitLab instance
  • Configure GitLab
  • Create a test application
  • Create a GitLab Runner
  • Set up Kubernetes authentication in GitLab
  • Configure the CI script
  • Check the result
  • Delete the resources you created
  1. Container infrastructure
  2. Managed Service for Kubernetes
  3. Continuous integration with GitLab
  4. Scanning Container Registry for vulnerabilities during continuous deployment of applications using GitLab

Scanning vulnerabilities during continuous deployment of Managed Service for Kubernetes applications using GitLab

Written by
Yandex Cloud
Updated at May 5, 2025
  • Required paid resources
  • Getting started
    • Set up your infrastructure
    • Install additional dependencies
  • Create a GitLab instance
  • Configure GitLab
  • Create a test application
  • Create a GitLab Runner
  • Set up Kubernetes authentication in GitLab
  • Configure the CI script
  • Check the result
  • Delete the resources you created

You can scan Docker images for vulnerabilities in Yandex Container Registry when continuously deploying Yandex Managed Service for Kubernetes applications via GitLab.

To do this, use Continuous Integration (CI) to create a special script in GitLab that will run after each commit:

  1. Building an application into a Docker image and pushing the image to Container Registry.
  2. Scanning Docker images in Container Registry for vulnerabilities.
  3. Deploying an application from a Docker image in a Managed Service for Kubernetes cluster using the Yandex Cloud tools.

To set up the vulnerability scanner:

  1. Create a GitLab instance.
  2. Configure GitLab.
  3. Create a test application.
  4. Create a GitLab Runner.
  5. Configure the CI script.
  6. Check the result.

If you no longer need the resources you created, delete them.

Required paid resourcesRequired paid resources

The infrastructure support cost includes:

  • Fee for disks and continuously running VMs (see Yandex Compute Cloud pricing).
  • Fee for storing the created Docker images and for the vulnerability scanner (see Container Registry pricing).
  • Fee for using the Managed Service for Kubernetes master (see Managed Service for Kubernetes pricing).
  • Fee for using public IP addresses (see Yandex Virtual Private Cloud pricing).

Getting startedGetting started

Set up your infrastructureSet up your infrastructure

Manually
Terraform

If you do not have the Yandex Cloud CLI yet, install and initialize it.

The folder specified when creating the CLI profile is used by default. To change the default folder, use the yc config set folder-id <folder_ID> command. You can specify a different folder using the --folder-name or --folder-id parameter.

  1. If you do not have a network yet, create one.

  2. If you do not have any subnets yet, create them in the availability zones where your Yandex Managed Service for Kubernetes cluster and node group will be created.

  3. Create service accounts:

    • For the resources with the k8s.clusters.agent and vpc.publicAdmin roles for the folder the Managed Service for Kubernetes cluster is created in. This service account will be used to create the resources required for the Managed Service for Kubernetes cluster.
    • For nodes with the container-registry.images.puller and container-registry.images.pusher roles for the folder with the Docker image registry. This service account will be used by the Managed Service for Kubernetes nodes to push the Docker images built in GitLab to the registry and pull them to run pods.

    Tip

    You can use the same service account for both operations.

  4. Create security groups for the Managed Service for Kubernetes cluster and its node groups.

    Warning

    The configuration of security groups determines the performance and availability of the cluster and the services and applications running in it.

  5. Create a security group for the Managed Service for GitLab instance.

  6. Create a Managed Service for Kubernetes cluster and a node group. When creating a Managed Service for Kubernetes cluster, specify the previously created service accounts for resources and nodes and the security group.

  7. Create a registry in Yandex Container Registry.

  8. Create an authorized key for the service account with the container-registry.images.pusher role and save it to the key.json file:

    yc iam key create \
      --service-account-name <service_account_name> \
      --output key.json
    

    This key is required to access the registry from GitLab.

  1. If you do not have Terraform yet, install it.

  2. Get the authentication credentials. You can add them to environment variables or specify them later in the provider configuration file.

  3. Configure and initialize a provider. There is no need to create a provider configuration file manually, you can download it.

  4. Place the configuration file in a separate working directory and specify the parameter values. If you did not add the authentication credentials to environment variables, specify them in the configuration file.

  5. Download the k8s-and-registry-for-gitlab.tf configuration file to the same working directory.

    This file describes:

    • Network.

    • Subnet.

    • Managed Service for Kubernetes cluster.

    • Service account required for the Managed Service for Kubernetes cluster and node group.

    • Security groups which contain rules required for the Managed Service for Kubernetes cluster and its node groups.

      Warning

      The configuration of security groups determines the performance and availability of the cluster and the services and applications running in it.

    • Default security group and rules needed to run the Managed Service for GitLab instance.

    • Registry in Yandex Container Registry.

    • Authorized key for the service account. This key is required to access the registry from GitLab.

    • Local key.json file with authorized key data.

  6. Specify the following in the k8s-and-registry-for-gitlab.tf file:

    • Folder ID.
    • Kubernetes version for the Managed Service for Kubernetes cluster and node groups.
    • Name of the Managed Service for Kubernetes cluster service account.
    • Name of the Container Registry registry.
  7. Make sure the Terraform configuration files are correct using this command:

    terraform validate
    

    If there are any errors in the configuration files, Terraform will point them out.

  8. Create the required infrastructure:

    1. Run this command to view the planned changes:

      terraform plan
      

      If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.

    2. If everything looks correct, apply the changes:

      1. Run this command:

        terraform apply
        
      2. Confirm updating the resources.

      3. Wait for the operation to complete.

    All the required resources will be created in the specified folder. You can check resource availability and their settings in the management console.

Warning

For applications running in production environments, make sure to restrict access of Managed Service for Kubernetes cluster service accounts to pushing Docker images to a registry. This is required for security reasons. In that case, create a separate service account with the container-registry.images.pusher role and specify it for deploying applications.

Additionally, assign the container-registry.images.scanner role to the service account for the nodes. This role allows scanning Docker images for vulnerabilities.

Install additional dependenciesInstall additional dependencies

Install the following items in the local environment:

  • jq JSON stream processor
  • kubectl command-line tool. Configure it to work with the created Managed Service for Kubernetes cluster.

Create a GitLab instanceCreate a GitLab instance

Create either a Managed Service for GitLab instance or a VM with a GitLab image in the same cloud network as the Managed Service for Kubernetes cluster.

Managed Service for GitLab instance
VM with a GitLab image

Create a Managed Service for GitLab instance by following this guide.

Launch GitLab on a VM with a public IP.

  1. On the folder page in the management console, click Create resource and select Virtual machine instance.

  2. Under Boot disk image, in the Product search field, enter Gitlab and select a public GitLab image.

  3. Under Location, select an availability zone to place your VM in. If you do not know which availability zone you need, leave the default one.

  4. Under Computing resources, navigate to the Custom tab and specify the required platform, number of vCPUs, and the amount of RAM:

    • Platform: Intel Ice Lake.
    • vCPU: 4.
    • Guaranteed vCPU performance: 100%.
    • RAM: 8 GB.
  5. Under Network settings:

    • In the Subnet field, select the network and subnet to connect your VM to. If the required network or subnet is not listed, create it.
    • Under Public IP address, keep Auto to assign your VM a random external IP address from the Yandex Cloud pool or select a static address from the list if you reserved one in advance.
  6. Under Access, select SSH key and specify the VM access data:

    • Under Login, enter the username. Do not use root or other names reserved by the OS. To perform operations requiring superuser permissions, use the sudo command.
    • In the SSH key field, select the SSH key saved in your organization user profile.

      If there are no saved SSH keys in your profile, or you want to add a new key:

      • Click Add key.
      • Enter a name for the SSH key.
      • Upload or paste the contents of the public key file. You need to create a key pair for the SSH connection to a VM yourself.
      • Click Add.

      The SSH key will be added to your organization user profile.

      If users cannot add SSH keys to their profiles in the organization, the added public SSH key will only be saved to the user profile of the VM being created.

  7. Under General information, specify the VM name: ci-tutorial-gitlab.

  8. Click Create VM.

It may take a few minutes to create the VM. When the VM status changes to RUNNING and GitLab starts, configure its settings.

Configure GitLabConfigure GitLab

To configure GitLab and enable Continuous Integration (CI), create a new project and enter the CI authorization parameters:

Managed Service for GitLab instance
VM with a GitLab image
  1. Log in to the Managed Service for GitLab instance web interface.

  2. Click Create a project.

  3. Click Create blank project.

  4. Fill in the fields as follows:

    • Project name: gitlab-test.
    • Project URL: Select the administrator user in the field next to the Managed Service for GitLab instance FQDN.

    Leave the other fields unchanged.

  5. Click Create project.

  1. On the Yandex Compute Cloud page, select the created VM and copy its public IP.

  2. Connect to the VM over SSH.

  3. Get the GitLab administrator password with the following VM command:

    sudo cat /etc/gitlab/initial_root_password
    
  4. Copy the password without spaces from the Password line to the clipboard or a separate file.

  5. In your browser, open http://<VM_public_IP_address>. This will take you to the GitLab web interface.

  6. Log in as the administrator:

    • Username or email: root.
    • Password: Password you copied in the previous step.

    If you are unable to log in, reset the administrator password.

  7. Change the administrator password.

  8. Log in as the administrator with the new password.

  9. Select Create a project.

  10. Specify the project name: gitlab-test.

  11. Click Create project.

Create a test applicationCreate a test application

Create a test application that can be deployed in a Yandex Managed Service for Kubernetes cluster:

  1. Add a Dockerfile to the project:
    1. Log in to GitLab.

    2. Open the GitLab project.

    3. Click in the repository navigation bar and select New file from the drop-down menu.

    4. Name the file as Dockerfile and add the following code to it:

      FROM alpine:3.10
      CMD echo "Hello"
      
    5. Add a comment to the commit in the Commit message field: Dockerfile for test application.

    6. Click Commit changes.

  2. Add the manifest for creating Managed Service for Kubernetes cluster resources to the project:
    1. Open the GitLab project.

    2. Click in the repository navigation bar and select New file from the drop-down menu.

    3. Name the file as k8s.yaml:

      k8s.yaml
      apiVersion: v1
      kind: Namespace
      metadata:
        name: hello-world
      ---
      apiVersion: apps/v1
      kind: Deployment
      metadata:
        name: hello-world-deployment
        namespace: hello-world
      spec:
        replicas: 1
        selector:
          matchLabels:
            app: hello
        template:
          metadata:
            namespace: hello-world
            labels:
              app: hello
          spec:
            containers:
              - name: hello-world
                image: __VERSION__
                imagePullPolicy: Always
      
    4. Add a comment to the commit in the Commit message field: Docker image deployment config.

    5. Click Commit changes.

Create a GitLab RunnerCreate a GitLab Runner

To run build tasks in the Yandex Managed Service for Kubernetes cluster, create a GitLab Runner. To do this, install GitLab Runner by following this guide.

Once it is installed, you can run automated builds inside your Managed Service for Kubernetes cluster.

For more information about installing and running GitLab Runner, see the GitLab documentation.

Set up Kubernetes authentication in GitLabSet up Kubernetes authentication in GitLab

You can set up authentication in GitLab using a Kubernetes service account token or the GitLab Agent application:

Service account token
GitLab Agent

Note

The Kubernetes service account is different from the Yandex Identity and Access Management service account.

To get the Kubernetes service account token:

  1. Create a service account.
  2. Get a service account token.
  3. Save the token: you need it for the next steps.

To connect your Yandex Managed Service for Kubernetes cluster to GitLab, create a GitLab Agent. To do this, install GitLab Agent by following this guide.

Once it is installed, you can connect your Managed Service for Kubernetes cluster to a GitLab instance.

For more information about installing and running GitLab Agent, see the GitLab documentation.

Configure the CI scriptConfigure the CI script

  1. Create the GitLab environment variables:

    1. In GitLab, navigate to Settings in the left-hand panel and select CI/CD from the drop-down list.

    2. Click Expand next to Variables.

    3. Add the following environment variables depending on the Managed Service for Kubernetes authentication method in GitLab:

      Service account token
      GitLab Agent
      • KUBE_URL: Managed Service for Kubernetes master address. You can retrieve it using the following command:

        yc managed-kubernetes cluster get <cluster_name_or_ID> --format=json \
           | jq -r .master.endpoints.external_v4_endpoint
        
      • KUBE_TOKEN: Token that GitLab will use to apply the configuration. Use the token obtained earlier.

      • CI_REGISTRY: Address of the previously created registry in cr.yandex/<registry_ID> format.
      • CI_REGISTRY_KEY: Key that GitLab will use to access the registry. Copy the contents of the previously obtained key.json static key file to access the registry.

      To add a variable:

      • Click Add variable.
      • In the window that opens, specify the variable name in the Key field and its value in the Value field.
      • Click Add variable.
  2. Create the CI script configuration file:

    1. Open the gitlab-test project.

    2. Click in the repository navigation bar and select New file from the drop-down menu.

    3. Name your file .gitlab-ci.yml. Add the steps to build and push a Docker image, scan it for vulnerabilities, and update the application configuration in the Managed Service for Kubernetes cluster. The file structure depends on the Kubernetes authentication method in GitLab:

      Service account token
      GitLab Agent
      .gitlab-ci.yml
      stages:
        - build
        - test
        - deploy
      
      build_docker_image:
        stage: build
        variables:
          DOCKER_CUSTOM_SUBFOLDER: "" # Specify a custom path (if any) to your folder with docker files.
        image:
          name: gcr.io/kaniko-project/executor:debug
          entrypoint: [""]
        script:
          - mkdir -p /kaniko/.docker
          - echo "{\"auths\":{\"${CI_REGISTRY}\":{\"auth\":\"$(echo -n "json_key:${CI_REGISTRY_KEY}" | base64 | tr -d '\n' )\"}}}" > /kaniko/.docker/config.json
          - >-
            /kaniko/executor
            --context "${CI_PROJECT_DIR}"/"${DOCKER_CUSTOM_SUBFOLDER}"
            --dockerfile "${CI_PROJECT_DIR}/"${DOCKER_CUSTOM_SUBFOLDER}"/Dockerfile"
            --destination "${CI_REGISTRY}/${CI_COMMIT_REF_SLUG}:${CI_COMMIT_SHA}"
      
      container_scanning_free_yc:
        image: 
          name: pindar/jq
          entrypoint: [""]
        stage: test
        artifacts:
          when: always
          paths:
            - gl-container-scanning-report-yc.json
        script:
          - export CI_COMMIT_SHA=${CI_COMMIT_SHA}
          # Install YC CLI.
          - curl https://storage.yandexcloud.net/yandexcloud-yc/install.sh | bash -s -- -a && cp /root/yandex-cloud/bin/yc /usr/bin/
          # Start scanning.
          - echo "Scanning image $IMAGE_NAME ${CI_REGISTRY}/${CI_COMMIT_REF_SLUG}:${CI_COMMIT_SHA}..."
          - export IMAGE_ID=$(yc container image list --registry-id (${CI_REGISTRY} | cut -d/ -f2) --format=json | jq -r --arg CI_COMMIT_SHA $CI_COMMIT_SHA '.[] | select(.tags[0]==$CI_COMMIT_SHA) | .id ')
          # Make a report.
          - export SCAN_RESULT=$(yc container image scan $IMAGE_ID --format=json)
          - export CRIT_VULN=$(echo $SCAN_RESULT | jq -r '.vulnerabilities.critical // 0')
          - export HIGH_VULN=$(echo $SCAN_RESULT | jq -r '.vulnerabilities.high // 0')
          - export SCAN_ID=$(echo $SCAN_RESULT | jq -r '.id')
          - echo "Scan results:"
          - yc container image list-vulnerabilities --scan-result-id="${SCAN_ID}" --format json | jq -r '.[] | select(.severity=="CRITICAL", .severity=="HIGH")'
          - yc container image list-vulnerabilities --scan-result-id="${SCAN_ID}" --format json | jq -r '.[] | select(.severity=="CRITICAL", .severity=="HIGH")' > gl-container-scanning-report-yc.json
          # Check the result.
          - (( SUM = $CRIT_VULN + $HIGH_VULN )) && (( RES = (SUM >= 1) )) && echo $RES && echo "image has $CRIT_VULN critical vulns and $HIGH_VULN high vulns" && exit 1 || echo "image has no high or crit vulns" exit 0
      
      deploy:
        image: gcr.io/cloud-builders/kubectl:latest
        stage: deploy
        script:
          - kubectl config set-cluster k8s --server="$KUBE_URL" --insecure-skip-tls-verify=true
          - kubectl config set-credentials admin --token="$KUBE_TOKEN"
          - kubectl config set-context default --cluster=k8s --user=admin
          - kubectl config use-context default
          - sed -i "s,__VERSION__,${CI_REGISTRY}/${CI_COMMIT_REF_SLUG}:${CI_COMMIT_SHA}," k8s.yaml
          - kubectl apply -f k8s.yaml
      
      .gitlab-ci.yml
      stages:
        - build
        - test
        - deploy
      
      build_docker_image:
        stage: build
        variables:
          DOCKER_CUSTOM_SUBFOLDER: "" # Specify a custom path (if any) to your folder with docker files.
        image:
          name: gcr.io/kaniko-project/executor:debug
          entrypoint: [""]
        script:
          - mkdir -p /kaniko/.docker
          - echo "{\"auths\":{\"${CI_REGISTRY}\":{\"auth\":\"$(echo -n "json_key:${CI_REGISTRY_KEY}" | base64 | tr -d '\n' )\"}}}" > /kaniko/.docker/config.json
          - >-
            /kaniko/executor
            --context "${CI_PROJECT_DIR}"/"${DOCKER_CUSTOM_SUBFOLDER}"
            --dockerfile "${CI_PROJECT_DIR}/"${DOCKER_CUSTOM_SUBFOLDER}"/Dockerfile"
            --destination "${CI_REGISTRY}/${CI_COMMIT_REF_SLUG}:${CI_COMMIT_SHA}"
      
      container_scanning_free_yc:
        image: 
          name: pindar/jq
          entrypoint: [""]
        stage: test
        artifacts:
          when: always
          paths:
            - gl-container-scanning-report-yc.json
        script:
          - export CI_COMMIT_SHA=${CI_COMMIT_SHA}
          # Install YC CLI.
          - curl https://storage.yandexcloud.net/yandexcloud-yc/install.sh | bash -s -- -a && cp /root/yandex-cloud/bin/yc /usr/bin/
          # Start scanning.
          - echo "Scanning image $IMAGE_NAME ${CI_REGISTRY}/${CI_COMMIT_REF_SLUG}:${CI_COMMIT_SHA}..."
          - export IMAGE_ID=$(yc container image list --registry-id (${CI_REGISTRY} | cut -d/ -f2) --format=json | jq -r --arg CI_COMMIT_SHA $CI_COMMIT_SHA '.[] | select(.tags[0]==$CI_COMMIT_SHA) | .id ')
          # Make a report.
          - export SCAN_RESULT=$(yc container image scan $IMAGE_ID --format=json)
          - export CRIT_VULN=$(echo $SCAN_RESULT | jq -r '.vulnerabilities.critical // 0')
          - export HIGH_VULN=$(echo $SCAN_RESULT | jq -r '.vulnerabilities.high // 0')
          - export SCAN_ID=$(echo $SCAN_RESULT | jq -r '.id')
          - echo "Scan results:"
          - yc container image list-vulnerabilities --scan-result-id="${SCAN_ID}" --format json | jq -r '.[] | select(.severity=="CRITICAL", .severity=="HIGH")'
          - yc container image list-vulnerabilities --scan-result-id="${SCAN_ID}" --format json | jq -r '.[] | select(.severity=="CRITICAL", .severity=="HIGH")' > gl-container-scanning-report-yc.json
          # Check the result.
          - (( SUM = $CRIT_VULN + $HIGH_VULN )) && (( RES = (SUM >= 1) )) && echo $RES && echo "image has $CRIT_VULN critical vulns and $HIGH_VULN high vulns" && exit 1 || echo "image has no high or crit vulns" exit 0
      
      deploy:
        image: bitnami/kubectl:latest
        stage: deploy
        script:
          - kubectl config use-context ${CI_PROJECT_PATH}:<GitLab_Agent_name>
          - cat k8s.yaml | sed -e "s,__VERSION__,${CI_REGISTRY}/${CI_COMMIT_REF_SLUG}:${CI_COMMIT_SHA}," | kubectl apply -f -
      

      Replace <GitLab_Agent_name> with the name of the agent in Managed Service for GitLab.

    4. Add a comment in the Commit message field: CI scripts.

    5. Click Commit changes.

    .gitlab-ci.yml describes the following CI script steps:

    • Building a Docker image using Dockerfile and pushing the image to Container Registry.
    • Scanning the Docker image for vulnerabilities in Container Registry.
    • Setting up an environment to work with Kubernetes and applying the k8s.yaml configuration to Managed Service for Kubernetes clusters. This way, the application is deployed on the previously created Managed Service for Kubernetes cluster.

Check the resultCheck the result

After you save the .gitlab-ci.yml configuration file, the build script will start. To check its results, select Build on the left-hand panel in the gitlab-test project, and then Pipelines from the drop-down menu. Vulnerability scanning is performed at the second stage (test).

Delete the resources you createdDelete the resources you created

If you no longer need the resources you created, delete them:

  1. Delete the Managed Service for GitLab instance or the created VM with the GitLab image.
  2. Delete all Docker images from the Container Registry registry.

Delete the other resources depending on how they were created:

Manually
Terraform
  1. Delete the Managed Service for Kubernetes cluster.
  2. If you reserved a public static IP address for the Managed Service for Kubernetes cluster, delete it.
  3. Delete the service accounts.
  4. Delete the Container Registry registry.
  5. Delete the subnets and network.
  1. In the terminal window, go to the directory containing the infrastructure plan.

    Warning

    Make sure the directory has no Terraform manifests with the resources you want to keep. Terraform deletes all resources that were created using the manifests in the current directory.

  2. Delete resources:

    1. Run this command:

      terraform destroy
      
    2. Confirm deleting the resources and wait for the operation to complete.

    All the resources described in the Terraform manifests will be deleted.

Was the article helpful?

Previous
Storing Docker images created in Managed Service for GitLab projects
Next
Integrating with a corporate DNS zone
© 2025 Direct Cursus Technology L.L.C.