<?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: Ahmed Nader Mohamed</title>
    <description>The latest articles on DEV Community by Ahmed Nader Mohamed (@ahmednader10).</description>
    <link>https://dev.to/ahmednader10</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%2F628006%2F5796e1da-7a9a-419a-ae3f-35899b1b79e1.png</url>
      <title>DEV Community: Ahmed Nader Mohamed</title>
      <link>https://dev.to/ahmednader10</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ahmednader10"/>
    <language>en</language>
    <item>
      <title>Zero to Kubernetes in 5 Mins</title>
      <dc:creator>Ahmed Nader Mohamed</dc:creator>
      <pubDate>Sat, 08 May 2021 19:43:44 +0000</pubDate>
      <link>https://dev.to/ahmednader10/zero-to-kubernetes-in-5-mins-2hbp</link>
      <guid>https://dev.to/ahmednader10/zero-to-kubernetes-in-5-mins-2hbp</guid>
      <description>&lt;p&gt;Kubernetes(k8s) has become the de facto standard for not only container orchestration but also for cloud-native development. Being the flagship product for CNCF (Cloud Native Computing Foundation), Kubernetes has managed to establish an ecosystem of opensource projects around it, from service mesh to monitoring, observability, storage, policy driven controls and many more. It is remarkable how fast k8s matured and became enterprise ready with impressive &lt;a href="https://kubernetes.io/case-studies/" rel="noopener noreferrer"&gt;success stories&lt;/a&gt;.&lt;br&gt;
VMware &lt;a href="https://medium.com/better-programming/kubernetes-in-layman-terms-d9c307d2ef1d" rel="noopener noreferrer"&gt;calls&lt;/a&gt; Kubernetes “the new Java”, after the programming language invented in 1996 and used in just about everything today. That is how big and pervasive they think this technology will become.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F89z8zk5gny7w7gza2cel.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F89z8zk5gny7w7gza2cel.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Everyone is talking about Kubernetes, but what the heck it really is?
&lt;/h2&gt;

&lt;p&gt;Kubernetes is a containers deployment and orchestration platform. It helps you deploy, monitor, scale, upgrade, rollback containers and many more seamlessly.&lt;/p&gt;

&lt;h4&gt;
  
  
  But why would I need an extra software to manage my containers?
&lt;/h4&gt;

&lt;p&gt;With the rise of microservices architecture, more and more organizations move to adopt it heavily, ending in many cases with hundreds and in some cases thousands of microservices in a single project. Each microservice of these would most probably be wrapped in a container on its own. Imagine having to manage hundred containers manually. Imagine having to deploy, upgrade, monitor and scale all of these containers. You probably got a sense by now how difficult/impossible it is to handle these operations using only a containerization platform like docker. A layer on top of docker is indeed needed to orchestrate and manage the containers, this layer is Kubernetes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://enterprisersproject.com/article/2017/10/how-explain-kubernetes-plain-english" rel="noopener noreferrer"&gt;Here&lt;/a&gt; is an analogy to help you have a better understanding: You can think of a container orchestrator (like Kubernetes ) as a conductor for an orchestra, says Dave Egts, chief technologist, North America Public Sector, &lt;a href="https://www.redhat.com/en?intcmp=701f2000000tjyaAAA" rel="noopener noreferrer"&gt;Red Hat&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“In the same way a conductor would say how many trumpets are needed, which ones play first trumpet, and how loud each should play, a container orchestrator would say how many web server front end containers are needed, what they serve, and how many resources are to be dedicated to each one.” Egts explains&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzjlm9jwe4e6ul0et6ayd.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzjlm9jwe4e6ul0et6ayd.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  K8s in practice
&lt;/h2&gt;

&lt;p&gt;Now that we have a good understanding of what Kubernetes is, let’s see it in practice.&lt;/p&gt;

&lt;h4&gt;
  
  
  In this tutorial, we will:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Create a simple http server with &lt;a href="https://golang.org/" rel="noopener noreferrer"&gt;golang&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Deploy a k8s cluster, we will use &lt;a href="https://k3s.io/" rel="noopener noreferrer"&gt;K3s&lt;/a&gt; (a lightweight Kubernetes distribution)&lt;/li&gt;
&lt;li&gt;Deploy the golang server on top of K8s&lt;/li&gt;
&lt;li&gt;Expose the server to the outer world to be able to access it over the internet.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;ul&gt;
&lt;li&gt;Linux based machine (Ubuntu or Centos)- For Windows you can use &lt;a href="https://minikube.sigs.k8s.io/docs/start/" rel="noopener noreferrer"&gt;MiniKube&lt;/a&gt; or &lt;a href="https://kind.sigs.k8s.io/docs/user/quick-start/" rel="noopener noreferrer"&gt;Kind&lt;/a&gt; instead of K3s and almost everything else should remain the same.&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.docker.com/" rel="noopener noreferrer"&gt;Docker&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Golang (this tutorial assumes version 1.15.3 but older versions should work as well)&lt;/li&gt;
&lt;li&gt;A docker registry account (you can create an account for free on &lt;a href="https://hub.docker.com/" rel="noopener noreferrer"&gt;dockerhub&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let’s get our hands dirty!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Install golang following the steps mentioned &lt;a href="https://golang.org/doc/install" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Then create a folder in any location of your choice. In this tutorial I will be using the following path as the root folder to my project &lt;code&gt;/root/tutorial/&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Create a simple http go server. Use the following code block and save it in &lt;code&gt;/root/tutorial&lt;/code&gt; under any name. I will refer to it as &lt;code&gt;server.go&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;package main

import (
"fmt"
"log"
"net/http"
)

func helloHandler(w http.ResponseWriter, r *http.Request) {
    if r.URL.Path != "/hello" {
        http.Error(w, "404 not found.", http.StatusNotFound)
        return
    }
    if r.Method != "GET" {
        http.Error(w, "Method is not supported.", http.StatusNotFound)
        return
    }
    fmt.Fprintf(w, "Hello!")
}

func main() {
    http.HandleFunc("/hello", helloHandler) // Update this line of code
    fmt.Printf("Starting server at port 8080\n")
    if err := http.ListenAndServe(":8080", nil); err != nil {
        log.Fatal(err)
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Make sure everything is setup correctly and the files runs correctly by running the following command &lt;code&gt;$ go run server.go&lt;/code&gt; You should be able to see &lt;strong&gt;“Starting server at port 8080”&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Next step is to build our go project into an executable. Run the following command at the root directory of the project(where server.go occurs)
&lt;code&gt;$ GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build .&lt;/code&gt; You should be able to see a newly created file with no extension.&lt;/li&gt;
&lt;li&gt;To complete the next steps we’ll need to create a &lt;a href="http://dockerhub.com/" rel="noopener noreferrer"&gt;dockerhub&lt;/a&gt; account. You can signup on it for free. We will need an account on dockerhub on any registry to push the docker image that we will create.&lt;/li&gt;
&lt;li&gt;Then we need to containerize our server to run it inside a container. First we will need to install docker. After installing docker successfully, we can then build a docker image for our server. To do that, copy the following code block into a file named &lt;code&gt;Dockerfile&lt;/code&gt; inside &lt;code&gt;/root/tutorial&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FROM alpine

WORKDIR /home/

ADD tutorial /home/

CMD ./tutorial
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Afterwards run the following command&lt;br&gt;
&lt;code&gt;$ docker build . -t &amp;lt;dockerhub-username&amp;gt;/tutorial&lt;/code&gt; where you should replace &lt;code&gt;&amp;lt;dockerhub-username&amp;gt;&lt;/code&gt; with your username on dockerhub.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Login using docker cli command &lt;code&gt;$ docker login -u &amp;lt;dockerhub-username&amp;gt;&lt;/code&gt; Then push the created image to dockerhub &lt;code&gt;$ docker push &amp;lt;dockerhub-username&amp;gt;/tutorial&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Now that our docker image is ready, we can use K8s to deploy and manage for us container(s) from that image. To do that, let’s install &lt;a href="https://k3s.io/" rel="noopener noreferrer"&gt;K3s&lt;/a&gt; first. I am using K3s for this tutorial as I find it a great reliable and lightweight distribution for K8s, perfect for development and edge (environments with limited resources) deployments. To install it you just need to run &lt;code&gt;$ curl -sfL https://get.k3s.io | sh -&lt;/code&gt; That’s it! That is all what you need to do in order to have a functional Kubernetes cluster.&lt;/li&gt;
&lt;li&gt;Next we want to create a k8s deployment file for our image. Copy and paste the following code block on your machine.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;apiVersion: apps/v1
kind: Deployment
metadata:
  name: go-server-deployment
  labels:
    app: go-server
spec:
  replicas: 1
  selector:
    matchLabels:
      app: go-server
  template:
    metadata:
      labels:
        app: go-server
    spec:
      containers: 
      - name: go-server
        image: &amp;lt;dockerhub-username&amp;gt;/tutorial #replace the variable with your username on dockerhub
        ports:
        - containerPort: 8080 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Now we want to instruct k8s to create that deployment for us. To do that we need to run &lt;code&gt;$ kubectl create -f tutorial-deployment.yaml&lt;/code&gt; To validate the deployment was created successfully, we should run &lt;code&gt;$ kubectl get pods&lt;/code&gt; and we should be able to see something similar to
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NAME                        READY   STATUS    RESTARTS   AGE
go-server-deployment-xxxx   1/1     Running   0          65m
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;We are almost there, we have everything set by now and K8s is currently managing our container for us. The only missing step is to expose our pod to the outer world in order to be able to access it over the internet. To achieve that we just need to run the following command
&lt;code&gt;$ kubectl expose deployment go-server-deployment --type=NodePort&lt;/code&gt;
This command will expose our deployment to the outer world on a random port. To identify that random port, we need to run &lt;code&gt;$ kubectl get svc&lt;/code&gt; we should be able to see a service that got created for us
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)     AGE
kubernetes  ClusterIP     x.x.x.x       &amp;lt;none&amp;gt;      443/TCP      87m
go-xxx       NodePort     x.x.x.x       &amp;lt;none&amp;gt;    8080:31628/TCP 37m
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Under PORT(s) column we should be able to find &lt;code&gt;8080:&amp;lt;external-random-port&amp;gt;&lt;/code&gt; We can then use our machine IP and that external port that k8s assigned for our service in order to access our server. This can be tested from any browser by visiting &lt;code&gt;http://&amp;lt;machine-ip&amp;gt;:&amp;lt;external-port&amp;gt;/hello&lt;/code&gt; We should be able to see &lt;code&gt;Hello!&lt;/code&gt; in the browser.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F515dbi4qufk5lyc8z5pz.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F515dbi4qufk5lyc8z5pz.jpeg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That’s all folks 🎉&lt;/p&gt;

&lt;p&gt;A quick recap of what we have accomplished here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Implemented a simple http server using golang&lt;/li&gt;
&lt;li&gt;Dockerized our server and pushed the image on Dockerhub&lt;/li&gt;
&lt;li&gt;Installed and Deployed a functional single node k8s cluster&lt;/li&gt;
&lt;li&gt;Used k8s to deploy and manage the http server container for us&lt;/li&gt;
&lt;li&gt;Exposed our server to the outer world through k8s&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;Disclaimer: all code files used are not meant for production use, they just show the functionality. You will need to introduce some changes to make them production ready&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>kubernetes</category>
      <category>cloudnative</category>
      <category>go</category>
      <category>docker</category>
    </item>
  </channel>
</rss>
