Navigation

"Hello World!" in a local K8s cluster

This includes how you can deploy a simple Docker image into a Minikube cluster which is nothing but a local K8s cluster. A K8s pod will be used as just a single K8s object to print "Hello World!" in this tutorial. Both imperative command and yaml resource definition will be included where "command" and "args" usage in K8s pod will be included. Busybox docker image will be used as the container image. Make sure to meet the mentioned prerequisites below.


Prerequisites.

1. Docker as the container runtime in your machine (see installation).

2. Minikube as the local Kubernetes cluster (see installation).

4. Make sure that your minikube cluster is started and running properly! You may use following commands to make sure this.

    Check the status: `minikube status`
    Start minikube: `minikube start`


Deployment using imperative command.

Imperative command mode provides the super fast deployment capability where we can meet our requirements by just a single command. All you need to do is that execute the following command.

kubectl run hello-world --image=busybox --restart=Never --command -- echo "Hello World!"

Explanation

hello-world     : Name of the pod.
--image=busybox     : Defines the image to be used inside the pod. Since the busybox is given, this will use the latest busybox image available at the deployment time.
--restart=Never      : Defines the restart policy as Never where pod will not be restarted once its task is done.
--command     : Specify that a custom command will be used inside the container instead of the entrypoint defined in the Dockerfile for busybox image.
--      : Double dashes specify that kubectl command has ended and everything after -- should be considered as arguments inside the container (inside the pod).
echo "Hello World!"     : Since --command was specified before --, pod container will interpret echo as the command and "Hello World" as the arguments to the command.

Use the following command to check pod logs once the pod is created. 
kubectl logs -f hello-world


Deployment using YAML resource definition.

Since we are using just a single pod, following yaml definition for the pod is only required.
YAML name: pod.yaml
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: hello-world
  name: hello-world
spec:
  containers:
  - command:
    - echo
    - Hello World!
    image: busybox
    name: hello-world
  restartPolicy: Never

Once the pod.yaml is ready with the above content, use the following command to deploy it in the cluster.

kubectl create -f pod.yaml

Use the following command again to check for pod logs.

kubectl logs -f hello-world

As per the pod.yaml definition above, the command itself has specified both the command and its arguments.  Instead we can use args option to pass required arguments to the command which is specified in the command options. See the following YAML definition which includes the said change.

YAML name: pod-with-args.yaml

apiVersion: v1
kind: Pod
metadata:
labels:
run: hello-world
name: hello-world
spec:
containers:
- command:
   - echo
   args
   - Hello World!
   image: busybox
   name: hello-world
restartPolicy: Never

Once the pod-with-args.yaml is ready with above content, use the following command to deploy the pod.

kubectl create -f pod-with-args.yaml 

Check pod logs using the following command once the pod is created.

kubectl logs -f hello-world 

You can  see Hello World! in your terminal as pod logs!

Share

WitCentre

Post A Comment:

0 comments: