Kubernetes Lab- Hello World !!

Photo by Growtika on Unsplash

Kubernetes Lab- Hello World !!

What is Kubernetes?

  • Kubernetes also known as K8s.

  • K8s [ K - First letter, 8 - count of characters between K & s (ubernete), s -> Last Letter ]

  • It's used to manage containers. It's an open-source system for automating the deployment, scaling, and management of containerized applications.

  • Originally designed by Google, the project is now maintained by the "Cloud Native Computing Foundation (CNCF)".

  • Kubernetes source code is in the "Go language".

  • The design and development of Kubernetes were influenced by Google's Borg cluster manager. Many of its top contributors had previously worked on Borg; they codenamed Kubernetes "Project 7" after the Star Trek ex-Borg character Seven of Nine and gave its logo a "seven-spoked wheel".

No alt text provided for this image

Let's start with Minikube:

Why minikube ?

minikube is local Kubernetes, focusing on making it easy to learn and develop for Kubernetes. Here, AWS Instance is used for the whole Lab setup.

No alt text provided for this image

1) Create an AWS instance with the following configuration:

. Name of the Instance: "minikube" ( can be anything )

No alt text provided for this image

  • Application & OS Image: Select "ubuntu"

No alt text provided for this image

  • Instance Type: Select "t2.medium" ( pre-requisite is 2 vCPU for minikube)

No alt text provided for this image

  • Create a new key pair - minikube.ppk (for Putty connection )

  • minimum 20GB of storage disk space

  • Other Settings: Keep it as default.

  • Click on "Launch Instance"

No alt text provided for this image

The AWS t2.medium Instance is ready for use.

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

2) Connect to the Instance and install docker, kubectl and minikube.

No alt text provided for this image

  1. Run with sudo privileges.

No alt text provided for this image

sudo su

2. Update all the packages.

No alt text provided for this image

apt-get update

3. Install Docker on the ubuntu instance.

No alt text provided for this image

apt-get install docker.io

4. Docker Installation will take a few minutes to complete, verify the docker installation.

No alt text provided for this image

docker --version

5. Now, let's install the Kubernetes command-line tool - kubectl.

kubectl - allows you to run commands against Kubernetes clusters. kubectl can be used to deploy applications, inspect and manage cluster resources, and view logs.

(Follow steps from kubernetes.io/docs/tasks/tools/install-kube..)

  • Download the kubectl

No alt text provided for this image

curl -LO "[https://dl.k8s.io/release/$(curl](dl.k8s.io/release/$(curl) -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl/bin/linux/amd64/kubectl)"

  • Make the file executable via chmod +x.

No alt text provided for this image

chmod +x ./kubectl

  • Move the executable kubectl file under /usr/local/bin

No alt text provided for this image

mv ./kubectl /usr/local/bin/kubectl

6. Kubectl is installed, now let's install minikube, the Debian package.

  • Download the package

No alt text provided for this image

curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube_latest_amd64.deb

  • Unpack the package

No alt text provided for this image

sudo dpkg -i minikube_latest_amd64.deb

  • minikube is installed, verify the version.

No alt text provided for this image

minikube version

  • start the minikube server

No alt text provided for this image

minikube start --force

  • Now, check the kubectl version command, it provides the server version details too.

No alt text provided for this image

kubectl version

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

3) Creating Pod using a Kubernetes manifest file.

  • We have an infrastructure ready with the required applications. Let's create a manifests YAML file, to create Pod on minikube server. A manifest specifies the desired state of an object that Kubernetes will maintain when you apply the manifest.

  • Pods are the smallest deployable units of computing that you can create and manage in Kubernetes. It is a group of one or more containers, with shared storage and network resources, and a specification for how to run the containers.

  • Here, we are creating a single container "c0" within a Pod.

No alt text provided for this image

Details on myfirstpod.yml

manifest myfirstpod.yml file code are below for reference.

kind: Pod                             
apiVersion: v1                     
metadata:                           
  name: myfirstpod                  
spec:                                    
  containers:                      
    - name: c0                     
      image: ubuntu              
      command: ["/bin/bash", "-c", "while true; do echo Hello-World!; sleep 10 ; done"]
  restartPolicy: Never
  • Create the myfirstpod.yml manifest file.

No alt text provided for this image

ls -lrt

  • Apply the manifest file to create the pod

No alt text provided for this image

kubectl apply -f myfirstpod.yml

  • Check the pods created

No alt text provided for this image

kubectl get pods

  • For detailed output, use - kubectl get pods -o wide. [Note: IP is assigned to POD, not to containers. ]
root@ip-172-31-8-148:/home/ubuntu# kubectl get pods -o wide
NAME         READY   STATUS    RESTARTS   AGE     IP           NODE       NOMINATED NODE   READINESS GATES
myfirstpod   1/1     Running   0          4m50s   172.17.0.3   minikube   <none>           <none>
  • To view the detail events while creating POD:- kubectl describe pod/myfirstpod
Events
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  17m   default-scheduler  Successfully assigned default/myfirstpod to minikube
  Normal  Pulling    17m   kubelet            Pulling image "ubuntu"
  Normal  Pulled     17m   kubelet            Successfully pulled image "ubuntu" in 5.358130716s
  Normal  Created    17m   kubelet            Created container c0
  Normal  Started    17m   kubelet            Started container c0:
  • Check the logs of the container "c0" : kubectl logs -f myfirstpod -c c0
root@ip-172-31-8-148:/home/ubuntu# kubectl logs -f myfirstpod -c c0
Hello-World! 
Hello-World!
Hello-World! 
Hello-World!
Hello-World!
Hello-World!
Hello-World!
Hello-World!
Hello-World!
Hello-World!
Hello-World!
Hello-World!

Hope, this article gives you a headstart to learn Kubernetes !!

References:

https://kubernetes.io/

https://en.wikipedia.org/wiki/Kubernetes

https://minikube.sigs.k8s.io/docs/start/

Did you find this article valuable?

Support Nikhil Sureka by becoming a sponsor. Any amount is appreciated!