<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Rizwan Khan</title>
    <description>The latest articles on DEV Community by Rizwan Khan (@rizwankh_24).</description>
    <link>https://dev.to/rizwankh_24</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F169944%2Fe7c18511-5582-42b2-a86f-bcf143369bf2.jpeg</url>
      <title>DEV Community: Rizwan Khan</title>
      <link>https://dev.to/rizwankh_24</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rizwankh_24"/>
    <language>en</language>
    <item>
      <title>Gitlab CI &amp; Kaniko to build Docker Images</title>
      <dc:creator>Rizwan Khan</dc:creator>
      <pubDate>Thu, 10 Sep 2020 11:44:16 +0000</pubDate>
      <link>https://dev.to/rizwankh_24/gitlab-ci-kaniko-to-build-docker-images-54ch</link>
      <guid>https://dev.to/rizwankh_24/gitlab-ci-kaniko-to-build-docker-images-54ch</guid>
      <description>&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;You can build container images from a Dockerfile inside a container or a Kubernetes cluster, though &lt;em&gt;Jérôme Petazzoni&lt;/em&gt; strongly discourages from doing so. He wrote a detailed blog that can be read &lt;a href="http://jpetazzo.github.io/2015/09/03/do-not-use-docker-in-docker-for-ci/"&gt;here&lt;/a&gt; on why not to build container images using Dockerfile inside a container or a Kubernetes cluster.&lt;/p&gt;




&lt;h4&gt;
  
  
  Context
&lt;/h4&gt;

&lt;p&gt;You will get N number of blogs on how to use the CI/CD of GitLab; here we will see an easy reference point to extend a file and create CI/CD for Docker Image to be built and stored in the same GitLab registry using &lt;a href="https://cloud.google.com/blog/products/gcp/introducing-kaniko-build-container-images-in-kubernetes-and-google-container-builder-even-without-root-access"&gt;kaniko&lt;/a&gt;. This file needs to be created in the individual project in the GitLab using the template available with the name .gitlab-ci.yml.&lt;/p&gt;




&lt;h4&gt;
  
  
  What is Kaniko?
&lt;/h4&gt;



&lt;p&gt;&lt;code&gt;Note: Kaniko is not an officially supported Google product&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;It is a tool to build container images from a Dockerfile inside a container or a Kubernetes cluster. It doesn't depend on the Docker daemon to run each Dockerfile command.&lt;/p&gt;

&lt;p&gt;It comes with its own limitations, but we don't run the risk of using Docker-in-Docker&lt;/p&gt;




&lt;h4&gt;
  
  
  Prerequisites
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Access to GitLab (either private self-hosted or managed)&lt;/li&gt;
&lt;li&gt;GitLab project with a Dockerfile&lt;/li&gt;
&lt;/ul&gt;




&lt;h4&gt;
  
  
  CI YAML for auto-devops
&lt;/h4&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# .gitlab-ci.yml
variables:
    GIT_SSL_NO_VERIFY: "true"

before_script:
  - echo "Random image creation, user = $GITLABUSER"

stages:
  - build

build_image:
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
  stage: build
  script:
    - ls
    - pwd
    - export CI_REGISTRY_IMAGE=mygitlab.com/base-project/subproject/project
    - echo "{\"auths\":{\"mygitlab.com\":{\"username\":\"gitlab-ci-token\",\"password\":\"$CI_BUILD_TOKEN\"},\"repository.xyz-company.io\":{\"username\":\"user\",\"password\":\"123random\"}}}" &amp;gt; /kaniko/.docker/config.json
    - wget https://letsencrypt.org/certs/lets-encrypt-x3-cross-signed.pem | xargs cat lets-encrypt-x3-cross-signed.pem &amp;gt;&amp;gt; /kaniko/ssl/certs/ca-certificates.crt
    - /kaniko/executor --skip-tls-verify --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE:$CI_BUILD_REF_NAME

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;






&lt;p&gt;&lt;strong&gt;variables&lt;/strong&gt;: These are static values which aren't going to change and is used at multiple locations in the gitlab-ci.yml file&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;before_script&lt;/strong&gt;: Set(s) of commands or echo statement we want to print&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;stages&lt;/strong&gt;: Stages are the block of code for an identical job or set of jobs viz. build, test, clean up, delete, deploy, etc. This executes in the order it's defined in the YAML. A dot(.) in front of any job(block of code) disables it and it won't be executed or available neither as an automatic or manual job.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Jobs (Each block of code)&lt;/strong&gt;: Each block of individual stage contains key-value pair or set of commands to it. We can define each block of code to point to a particular stage and all the set of commands it requires to perform that function in the script block. It can be made to run automatically and also manual (start the job manually by clicking a button). The variables like password, access/secret key can be defined in the CI/CD settings under the secret variables section so it's not available in plain text format.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: If you want to use the GitLab docker registry and store docker images in the GitLab project; this by default is disabled and needs to be enabled in the General setting section.&lt;/p&gt;

</description>
      <category>docker</category>
    </item>
    <item>
      <title>How to use service accounts for Kubernetes imagePullSecrets</title>
      <dc:creator>Rizwan Khan</dc:creator>
      <pubDate>Wed, 12 Aug 2020 13:16:38 +0000</pubDate>
      <link>https://dev.to/rizwankh_24/how-to-use-service-accounts-for-kubernetes-imagepullsecrets-3b57</link>
      <guid>https://dev.to/rizwankh_24/how-to-use-service-accounts-for-kubernetes-imagepullsecrets-3b57</guid>
      <description>&lt;h2&gt;
  
  
  What are Service Accounts in Kubernetes?
&lt;/h2&gt;

&lt;p&gt;As per &lt;a href="https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/"&gt;Kubernetes.io&lt;/a&gt; - A service account provides an identity for processes that run in a Pod.&lt;/p&gt;

&lt;p&gt;One can think of service accounts as service users for pods. They help pods authenticate with the api-server and interact with it.&lt;/p&gt;

&lt;p&gt;Many times, we come across a situation where our organization uses a private Docker registry to store the Docker images and to make this available we need to create a &lt;code&gt;docker-registry&lt;/code&gt; kubernetes secret and pass as &lt;code&gt;imagePullSecrets&lt;/code&gt; in the deployment manifest.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl create secret docker-registry registry-cred \
 --docker-server=my.private-registry.com \
 --docker-username=my_username \
 --docker-password="my_superr_strong_password" \
 --docker-email=my.email@mycompany.com -n my-namespace
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then, we pass this secret in the deployment manifest as below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
      imagePullSecrets:
      - name: registry-cred
...

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  The problem with this approach?
&lt;/h2&gt;

&lt;p&gt;Not many that I can think of, except below ones:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The deployment yaml are generally developed by Developers who doesn't need to know about this credentials&lt;/li&gt;
&lt;li&gt;If there are a large number of pods in the namespace, then each manifest needs to be updated, whenever the password is rotated&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The solution
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;serviceAccounts&lt;/code&gt; - your Kubernetes administrator can just patch serviceAccounts with the registry credential secret and you don't need to worry about replacing or adding it in your manifest yaml each time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;kubectl patch serviceaccount default -p '{"imagePullSecrets": [{"name": "registry-cred"}]}' -n my-namespace
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>kubernetes</category>
      <category>registry</category>
    </item>
  </channel>
</rss>
