Thursday 9 July 2020

Kubernetes: Hello World Application

This is a simple hello world application. Do not worry about the technical terms that come in this tutorial, I will explain all these things in detail later.

 

Step 1: Start the Virtualbox.

 

Step 2: Execute the command ‘minikube start’ to start minikube service.


Once minikube service started, execute the command ‘virtualbox’ to confirm that minikube is up and running.

$minikube start
😄  minikube v1.9.2 on Darwin 10.14.6
✨  Using the virtualbox driver based on existing profile
👍  Starting control plane node m01 in cluster minikube
🔄  Restarting existing virtualbox VM for "minikube" ...
🐳  Preparing Kubernetes v1.18.0 on Docker 19.03.8 ...
🌟  Enabling addons: default-storageclass, storage-provisioner
🏄  Done! kubectl is now configured to use "minikube"

Execute the command ‘virtualbox’ to confirm that minikube is up and running.

How to confirm minikube and VirtualBox are talking to each other?

Execute the command ‘kubectl get nodes’.

$kubectl get nodes
NAME       STATUS   ROLES    AGE     VERSION
minikube   Ready    master   2m36s   v1.18.0

Now everything is set up, lets run Hello World Application.

 

Create a deployment

Use the command 'kubectl create deployment' to create a deployment that manages a pod.

 

Example

kubectl create deployment hello-node --image=jboss/wildfly

 

Above statement pulls the image ‘jboss/wildfly’ from an image registry like Dokcerhub and creates a deployment.

$kubectl create deployment hello-node --image=jboss/wildfly
deployment.apps/hello-node created

View the deployment by executing the below command.

kubectl get deployments

$kubectl get deployments
NAME         READY   UP-TO-DATE   AVAILABLE   AGE
hello-node   1/1     1            1           17s

View the pods by executing the below command.

$kubectl get pods
NAME                         READY   STATUS    RESTARTS   AGE
hello-node-59fddbb58-rv8wt   1/1     Running   0          35s

Create a Service

By default, pod is accessible by its internal IP address within Kubernetes cluster. To make the pod accessible outside of the Kubernetes network, we need to expose pod as a Kubernetes service.

$kubectl expose deployment hello-node --type=LoadBalancer --port=8080
service/hello-node exposed

View the created service.

$kubectl get services
NAME         TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
hello-node   LoadBalancer   10.98.157.76   <pending>     8080:31007/TCP   22s
kubernetes   ClusterIP      10.96.0.1      <none>        443/TCP          76m

As you see hello-node service is available on port 31007.

 

Execute the command ‘minikube service hello-node’ to open hello-node service in the browser.

$minikube service hello-node
|-----------|------------|-------------|-----------------------------|
| NAMESPACE |    NAME    | TARGET PORT |             URL             |
|-----------|------------|-------------|-----------------------------|
| default   | hello-node |        8080 | http://192.168.99.100:31007 |
|-----------|------------|-------------|-----------------------------|
🎉  Opening service default/hello-node in default browser...

How to see everything that is deployed?

Execute the command ‘kubectl get all’ to see all that is deployed.

$kubectl get all
NAME                             READY   STATUS    RESTARTS   AGE
pod/hello-node-59fddbb58-rv8wt   1/1     Running   0          9m57s

NAME                 TYPE           CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
service/hello-node   LoadBalancer   10.98.157.76   <pending>     8080:31007/TCP   8m23s
service/kubernetes   ClusterIP      10.96.0.1      <none>        443/TCP          84m

NAME                         READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/hello-node   1/1     1            1           9m57s

NAME                                   DESIRED   CURRENT   READY   AGE
replicaset.apps/hello-node-59fddbb58   1         1         1       9m57s

From the output, you can confirm that pods, services, deployments, and replicaset are created.

 

Get yaml of the deployment

Execute the below command to see the yaml of the deployment.

 

kubectl get deploy hello-node -o yaml

$kubectl get deploy hello-node -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    deployment.kubernetes.io/revision: "1"
  creationTimestamp: "2020-05-06T11:10:02Z"
  generation: 1
  labels:
    app: hello-node
  managedFields:
  - apiVersion: apps/v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:labels:
          .: {}
          f:app: {}
      f:spec:
        f:progressDeadlineSeconds: {}
        f:replicas: {}
        f:revisionHistoryLimit: {}
        f:selector:
          f:matchLabels:
            .: {}
            f:app: {}
        f:strategy:
          f:rollingUpdate:
            .: {}
            f:maxSurge: {}
            f:maxUnavailable: {}
          f:type: {}
        f:template:
          f:metadata:
            f:labels:
              .: {}
              f:app: {}
          f:spec:
            f:containers:
              k:{"name":"wildfly"}:
                .: {}
                f:image: {}
                f:imagePullPolicy: {}
                f:name: {}
                f:resources: {}
                f:terminationMessagePath: {}
                f:terminationMessagePolicy: {}
            f:dnsPolicy: {}
            f:restartPolicy: {}
            f:schedulerName: {}
            f:securityContext: {}
            f:terminationGracePeriodSeconds: {}
    manager: kubectl
    operation: Update
    time: "2020-05-06T11:10:02Z"
  - apiVersion: apps/v1
    fieldsType: FieldsV1
    fieldsV1:
      f:metadata:
        f:annotations:
          .: {}
          f:deployment.kubernetes.io/revision: {}
      f:status:
        f:availableReplicas: {}
        f:conditions:
          .: {}
          k:{"type":"Available"}:
            .: {}
            f:lastTransitionTime: {}
            f:lastUpdateTime: {}
            f:message: {}
            f:reason: {}
            f:status: {}
            f:type: {}
          k:{"type":"Progressing"}:
            .: {}
            f:lastTransitionTime: {}
            f:lastUpdateTime: {}
            f:message: {}
            f:reason: {}
            f:status: {}
            f:type: {}
        f:observedGeneration: {}
        f:readyReplicas: {}
        f:replicas: {}
        f:updatedReplicas: {}
    manager: kube-controller-manager
    operation: Update
    time: "2020-05-06T11:10:08Z"
  name: hello-node
  namespace: default
  resourceVersion: "7725"
  selfLink: /apis/apps/v1/namespaces/default/deployments/hello-node
  uid: 65df08ee-dfaa-4595-866b-73e31d10b876
spec:
  progressDeadlineSeconds: 600
  replicas: 1
  revisionHistoryLimit: 10
  selector:
    matchLabels:
      app: hello-node
  strategy:
    rollingUpdate:
      maxSurge: 25%
      maxUnavailable: 25%
    type: RollingUpdate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: hello-node
    spec:
      containers:
      - image: jboss/wildfly
        imagePullPolicy: Always
        name: wildfly
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  availableReplicas: 1
  conditions:
  - lastTransitionTime: "2020-05-06T11:10:08Z"
    lastUpdateTime: "2020-05-06T11:10:08Z"
    message: Deployment has minimum availability.
    reason: MinimumReplicasAvailable
    status: "True"
    type: Available
  - lastTransitionTime: "2020-05-06T11:10:02Z"
    lastUpdateTime: "2020-05-06T11:10:08Z"
    message: ReplicaSet "hello-node-59fddbb58" has successfully progressed.
    reason: NewReplicaSetAvailable
    status: "True"
    type: Progressing
  observedGeneration: 1
  readyReplicas: 1
  replicas: 1
  updatedReplicas: 1
$

Get json of the deployment

Execute the below command to see the JSON of deployment.

kubectl get deploy hello-node -o json

$kubectl get deploy hello-node -o json
{
    "apiVersion": "apps/v1",
    "kind": "Deployment",
    "metadata": {
        "annotations": {
            "deployment.kubernetes.io/revision": "1"
        },
        "creationTimestamp": "2020-05-06T11:10:02Z",
        "generation": 1,
        "labels": {
            "app": "hello-node"
        },
        "managedFields": [
            {
                "apiVersion": "apps/v1",
                "fieldsType": "FieldsV1",
                "fieldsV1": {
                    "f:metadata": {
                        "f:labels": {
                            ".": {},
                            "f:app": {}
                        }
                    },
                    "f:spec": {
                        "f:progressDeadlineSeconds": {},
                        "f:replicas": {},
                        "f:revisionHistoryLimit": {},
                        "f:selector": {
                            "f:matchLabels": {
                                ".": {},
                                "f:app": {}
                            }
                        },
                        "f:strategy": {
                            "f:rollingUpdate": {
                                ".": {},
                                "f:maxSurge": {},
                                "f:maxUnavailable": {}
                            },
                            "f:type": {}
                        },
                        "f:template": {
                            "f:metadata": {
                                "f:labels": {
                                    ".": {},
                                    "f:app": {}
                                }
                            },
                            "f:spec": {
                                "f:containers": {
                                    "k:{\"name\":\"wildfly\"}": {
                                        ".": {},
                                        "f:image": {},
                                        "f:imagePullPolicy": {},
                                        "f:name": {},
                                        "f:resources": {},
                                        "f:terminationMessagePath": {},
                                        "f:terminationMessagePolicy": {}
                                    }
                                },
                                "f:dnsPolicy": {},
                                "f:restartPolicy": {},
                                "f:schedulerName": {},
                                "f:securityContext": {},
                                "f:terminationGracePeriodSeconds": {}
                            }
                        }
                    }
                },
                "manager": "kubectl",
                "operation": "Update",
                "time": "2020-05-06T11:10:02Z"
            },
            {
                "apiVersion": "apps/v1",
                "fieldsType": "FieldsV1",
                "fieldsV1": {
                    "f:metadata": {
                        "f:annotations": {
                            ".": {},
                            "f:deployment.kubernetes.io/revision": {}
                        }
                    },
                    "f:status": {
                        "f:availableReplicas": {},
                        "f:conditions": {
                            ".": {},
                            "k:{\"type\":\"Available\"}": {
                                ".": {},
                                "f:lastTransitionTime": {},
                                "f:lastUpdateTime": {},
                                "f:message": {},
                                "f:reason": {},
                                "f:status": {},
                                "f:type": {}
                            },
                            "k:{\"type\":\"Progressing\"}": {
                                ".": {},
                                "f:lastTransitionTime": {},
                                "f:lastUpdateTime": {},
                                "f:message": {},
                                "f:reason": {},
                                "f:status": {},
                                "f:type": {}
                            }
                        },
                        "f:observedGeneration": {},
                        "f:readyReplicas": {},
                        "f:replicas": {},
                        "f:updatedReplicas": {}
                    }
                },
                "manager": "kube-controller-manager",
                "operation": "Update",
                "time": "2020-05-06T11:10:08Z"
            }
        ],
        "name": "hello-node",
        "namespace": "default",
        "resourceVersion": "7725",
        "selfLink": "/apis/apps/v1/namespaces/default/deployments/hello-node",
        "uid": "65df08ee-dfaa-4595-866b-73e31d10b876"
    },
    "spec": {
        "progressDeadlineSeconds": 600,
        "replicas": 1,
        "revisionHistoryLimit": 10,
        "selector": {
            "matchLabels": {
                "app": "hello-node"
            }
        },
        "strategy": {
            "rollingUpdate": {
                "maxSurge": "25%",
                "maxUnavailable": "25%"
            },
            "type": "RollingUpdate"
        },
        "template": {
            "metadata": {
                "creationTimestamp": null,
                "labels": {
                    "app": "hello-node"
                }
            },
            "spec": {
                "containers": [
                    {
                        "image": "jboss/wildfly",
                        "imagePullPolicy": "Always",
                        "name": "wildfly",
                        "resources": {},
                        "terminationMessagePath": "/dev/termination-log",
                        "terminationMessagePolicy": "File"
                    }
                ],
                "dnsPolicy": "ClusterFirst",
                "restartPolicy": "Always",
                "schedulerName": "default-scheduler",
                "securityContext": {},
                "terminationGracePeriodSeconds": 30
            }
        }
    },
    "status": {
        "availableReplicas": 1,
        "conditions": [
            {
                "lastTransitionTime": "2020-05-06T11:10:08Z",
                "lastUpdateTime": "2020-05-06T11:10:08Z",
                "message": "Deployment has minimum availability.",
                "reason": "MinimumReplicasAvailable",
                "status": "True",
                "type": "Available"
            },
            {
                "lastTransitionTime": "2020-05-06T11:10:02Z",
                "lastUpdateTime": "2020-05-06T11:10:08Z",
                "message": "ReplicaSet \"hello-node-59fddbb58\" has successfully progressed.",
                "reason": "NewReplicaSetAvailable",
                "status": "True",
                "type": "Progressing"
            }
        ],
        "observedGeneration": 1,
        "readyReplicas": 1,
        "replicas": 1,
        "updatedReplicas": 1
    }
}




Previous                                                    Next                                                    Home

No comments:

Post a Comment