<?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>Forem: ustundagsemih</title>
    <description>The latest articles on Forem by ustundagsemih (@ustundagsemih).</description>
    <link>https://forem.com/ustundagsemih</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%2F421551%2F7ec77559-8f4d-4f27-bf51-be517baa41b3.png</url>
      <title>Forem: ustundagsemih</title>
      <link>https://forem.com/ustundagsemih</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/ustundagsemih"/>
    <language>en</language>
    <item>
      <title>Kubernetes Deployment &amp; ReplicaSet</title>
      <dc:creator>ustundagsemih</dc:creator>
      <pubDate>Tue, 15 Sep 2020 12:14:12 +0000</pubDate>
      <link>https://forem.com/ustundagsemih/kubernetes-deployment-replicaset-33ge</link>
      <guid>https://forem.com/ustundagsemih/kubernetes-deployment-replicaset-33ge</guid>
      <description>&lt;h2&gt;
  
  
  What is a ReplicaSet?
&lt;/h2&gt;

&lt;p&gt;A ReplicaSet's purpose is to maintain a stable set of replica Pods running at any given time. A ReplicaSet ensures that a specified number of pod replicas are running at any given time. We often don't need to create a &lt;strong&gt;ReplicaSet&lt;/strong&gt; directly. Instead we use &lt;strong&gt;Deployment&lt;/strong&gt; to manage a ReplicaSet. Let's first take a look at how should we create a ReplicaSet.&lt;/p&gt;

&lt;p&gt;We will create a ReplicaSet to ensure there is always 3 instances of a nginx container.&lt;/p&gt;

&lt;p&gt;Here &lt;strong&gt;.spec.selector&lt;/strong&gt; is a label selector and it is used to identify potential Pods to acquire by the ReplicaSet. In our case it will look for the Pods which have the label &lt;strong&gt;tier: frontend&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx-replicaset
  labels:
    tier: frontend
spec:
  replicas: 2
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
      - name: my-nginx
        image: nginx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Save this manifest into replicaset.yaml and apply it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kubectl apply -f replicaset.yaml
replicaset.apps/nginx-replicaset created

$ kubectl get replicaset
NAME               DESIRED   CURRENT   READY   AGE
nginx-replicaset   2         2         2       19s

$ kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
nginx-replicaset-7db6b   1/1     Running   0          19s
nginx-replicaset-ncz56   1/1     Running   0          19s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can see that our ReplicaSet has been deployed and there are 2 PODs just as we specified.&lt;/p&gt;

&lt;h2&gt;
  
  
  To the Deployments!
&lt;/h2&gt;

&lt;p&gt;Now we have seen what is a ReplicaSet and how to create it, it is time to integrate it into a Deployment.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A Deployment provides declarative updates for Pods ReplicaSets.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's create a Deployment manifest. Instead of creating the manifest from scratch, we can use &lt;strong&gt;kubectl&lt;/strong&gt; to create a template file for us. With the &lt;strong&gt;--dry-run&lt;/strong&gt; flag, we tell kubectl to not create the resource. This way, we can easily modify the attributes later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kubectl create deployment nginx-deployment --image=nginx --dry-run=client -o yaml &amp;gt; deployment.yaml
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt; $ kubectl get deploy
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   1/1     1            1           4m9s

$ kubectl get po
NAME                                READY   STATUS        RESTARTS   AGE
nginx-deployment-5969c7f455-j6685   1/1     Running       0          3m39s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After creting the deployment, we can see that only 1 Pod is created. This is because we didn't specify a value for &lt;strong&gt;replicas&lt;/strong&gt; option. We can scale our deployment by editing the yaml file or we can use the imperative way.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kubectl scale deploy nginx-deployment --replicas=2
deployment.apps/nginx-deployment scaled

$ kubectl get deploy
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   2/2     2            2           12m

$ kubectl get po
NAME                                READY   STATUS        RESTARTS   AGE
nginx-deployment-5969c7f455-9nrs7   1/1     Running       0          71s
nginx-deployment-5969c7f455-j6685   1/1     Running       0          6m20ss
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
    </item>
    <item>
      <title>How to pass variables to a JSON file in Terraform</title>
      <dc:creator>ustundagsemih</dc:creator>
      <pubDate>Fri, 04 Sep 2020 07:53:38 +0000</pubDate>
      <link>https://forem.com/ustundagsemih/how-to-pass-variables-to-a-json-file-in-terraform-57k1</link>
      <guid>https://forem.com/ustundagsemih/how-to-pass-variables-to-a-json-file-in-terraform-57k1</guid>
      <description>&lt;p&gt;Let's say you are creating an IAM policy with Terraform by using a seperate json file which includes the statement for the policy.&lt;/p&gt;

&lt;p&gt;In a typical statement we have 4 values to satisfy. These are Sid, Effect, Action and Resource.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Policy Name",
            "Effect": "Allow",
            "Action": [
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::mytestbucket"
            ]
        }
    ]
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;How should we dynamically change Resource and Sid values in this statement?&lt;/p&gt;

&lt;p&gt;Well, Terraform has a data resource called &lt;a href="https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file"&gt;template_file.&lt;/a&gt; With this, we can easily pass parameters dynamically to our statement file.&lt;/p&gt;

&lt;h2&gt;
  
  
  Demo Time!
&lt;/h2&gt;

&lt;p&gt;First, we need to create a user, policy and bucket. We will then use the bucket ARN in our statement. I am using variables for most of the things in order to have a clean code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resource "aws_iam_user" "iam-user" {
    name = var.iam_user_name
}

resource "aws_s3_bucket" "s3-bucket" {
    bucket = "mytestbucket"
}

resource "aws_iam_user_policy" "iam-policy" {
    name = var.iam_policy_name
    user = aws_iam_user.iam-user.name
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Normally we can attach the json policy with the following;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;br&gt;
policy = file("/path/to/policy.json")&lt;br&gt;
&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Since we want to pass parameters to the json file, we will use a data resource called "template_file".&lt;/p&gt;

&lt;p&gt;In order to use it, we must rename our json extension to &lt;strong&gt;tpl&lt;/strong&gt;. With this we are stating that we want to use this file as a template.&lt;/p&gt;

&lt;p&gt;Now we will modify our template file using interpolation syntax. We will pass parameters for both Sid and Resource.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"Statement": [
    {
        "Sid": "${sid-name}",
        "Effect": "Allow",
        "Action": [
            "s3:ListBucket"
        ],
        "Resource": [
            "${resource-name}"
        ]
    }
]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Back to our main file we will add template_file resource.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;data "template_file" "iam-policy-template" {
    template = file("/path/to/policy.tpl")

    vars = {
        sid-name = "Policy for S3 Access"
        resource-name = aws_s3_bucket.sample_bucket.arn
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here we specified the &lt;strong&gt;vars&lt;/strong&gt; block and inside we use the variables from the template file. Now we can use this data in our IAM policy resource.&lt;/p&gt;

&lt;p&gt;Notice that we are accessing our template's rendered version.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;resource "aws_iam_user_policy" "iam-policy" {
    name = var.iam_policy_name
    user = aws_iam_user.iam-policy.name

    policy = data.template_file.iam-policy-template.rendered
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
      <category>terraform</category>
      <category>aws</category>
      <category>devops</category>
    </item>
    <item>
      <title>Kubernetes, Beginning with PODs</title>
      <dc:creator>ustundagsemih</dc:creator>
      <pubDate>Wed, 01 Jul 2020 14:10:14 +0000</pubDate>
      <link>https://forem.com/ustundagsemih/kubernetes-beginning-with-pods-24ck</link>
      <guid>https://forem.com/ustundagsemih/kubernetes-beginning-with-pods-24ck</guid>
      <description>&lt;p&gt;I am planning to prepare for the &lt;a href="https://www.cncf.io/certification/cka/"&gt;CKA&lt;/a&gt; exam. Since one of the best methods to learn for me is to write down what I have learned, I decided to write them as blog posts. So let's get started!&lt;/p&gt;

&lt;p&gt;post-full-content pre { margin: 0;}&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a POD?
&lt;/h2&gt;

&lt;p&gt;A Pod is the smallest deployable unit that can be created and managed in Kubernetes. In the &lt;a href="https://kubernetes.io/docs/concepts/workloads/pods/pod/"&gt;official documentation&lt;/a&gt; you can find much more theoretical information about Pods. In this post I try to go over practical usage of Pods.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to create and run a POD?
&lt;/h3&gt;

&lt;p&gt;There are mainly 2 ways of creating Pods in Kubernetes. One of them is using &lt;strong&gt;kubectl&lt;/strong&gt; command. &lt;strong&gt;kubectl&lt;/strong&gt; is a command line tool which lets you control Kubernetes clusters. For more information, please check &lt;a href="https://kubernetes.io/docs/reference/kubectl/overview/"&gt;the documentation&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With the command below, we are creating a Pod labeled as &lt;strong&gt;nginx&lt;/strong&gt; and  using an image &lt;strong&gt;nginx.&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kubectl run nginx --image nginx
pod/nginx created
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If we check what Pods are running in our cluster, we can see that the Pod is running.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kubectl get pods
NAME    READY   STATUS    RESTARTS   AGE
nginx   1/1     Running   0          2m45s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can also check the details of the Pod by running &lt;strong&gt;describe&lt;/strong&gt; argument.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kubectl describe pod nginx
Name:         nginx
Namespace:    default
Priority:     0
Node:         node01/172.17.0.18
Start Time:   Sun, 28 Jun 2020 17:05:41 +0000
Labels:       run=nginx
Annotations:  &amp;lt;none&amp;gt;
Status:       Running
IP:           10.244.1.3
...
...
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the output, we can see which node is our Pod is running at, which image has been used etc. It also shows events related to the Pod. If there is an error, we could easily see it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  15m   default-scheduler  Successfully assigned default/nginx to node01
  Normal  Pulling    15m   kubelet, node01    Pulling image "nginx"
  Normal  Pulled     15m   kubelet, node01    Successfully pulled image "nginx"
  Normal  Created    15m   kubelet, node01    Created container nginx
  Normal  Started    15m   kubelet, node01    Started container nginx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's delete this Pod and create another one with the second method, by writing &lt;strong&gt;yaml&lt;/strong&gt; files.&lt;br&gt;
To delete a Pod we can simply run the command below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kubectl delete pods nginx
pod "nginx" deleted
$ kubectl get pods
No resources found in default namespace.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now create a file named as &lt;strong&gt;pod.yml&lt;/strong&gt; and write some yaml.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: v1
kind: Pod
metadata:
 name: my-nginx
 labels:
  app: my-first-app
  type: frontend
spec:
 containers:
  - name: nginx-container
    image: nginx123
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here we basically specify what kind of resource we want to create, which is Pod.&lt;br&gt;
With &lt;strong&gt;metadata&lt;/strong&gt; we specify a name for our Pod as well as apply some labels to it. Labels are handy in an environment where you have hundreds of Pods.&lt;br&gt;
With &lt;strong&gt;spec&lt;/strong&gt; section, we specify a name for our container and which image to use.&lt;br&gt;
Now let's create a Pod with our definition file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kubectl apply -f pod.yml
pod/my-nginx created
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It successfully created the Pod. Let's check the status of it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kubectl get pods
NAME       READY   STATUS         RESTARTS   AGE
my-nginx   0/1     ErrImagePull   0          58s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In &lt;strong&gt;STATUS&lt;/strong&gt; column we can see that there is an error. Let's describe the Pod to get more information in the &lt;strong&gt;events&lt;/strong&gt; section.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kubectl describe pods my-nginx
...
...
Events:
  Type     Reason     Age                  From               Message
  ----     ------     ----                 ----               -------
  Normal   Scheduled  2m47s                default-scheduler  Successfully assigned default/my-nginx to node01
  Normal   Pulling    49s (x4 over 2m22s)  kubelet, node01    Pulling image "nginx123"
  Warning  Failed     48s (x4 over 2m21s)  kubelet, node01    Failed to pull image "nginx123": rpc error: code = Unknown desc = Error response from daemon: pull access denied for nginx123, repository does not exist or may require 'docker login': denied: requested access to the resource is denied
  Warning  Failed     48s (x4 over 2m21s)  kubelet, node01    Error: ErrImagePull
  Normal   BackOff    34s (x6 over 2m20s)  kubelet, node01    Back-off pulling image "nginx123"
  Warning  Failed     23s (x7 over 2m20s)  kubelet, node01    Error: ImagePullBackOff
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here we can see that there is no image named as &lt;strong&gt;nginx123.&lt;/strong&gt; Now let's try to resolve this problem. To solve it, we simply need to edit the definition file with the correct image name and apply it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spec:
 containers:
  - name: nginx-container
    image: nginx
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;





&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kubectl apply -f pod.yml
pod/my-nginx configured
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now if we check the status, we can see there is no error.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ kubectl get pods
NAME       READY   STATUS    RESTARTS   AGE
my-nginx   1/1     Running   0          6m47s
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



</description>
    </item>
  </channel>
</rss>
