Monday, October 4, 2021

Running Pod on Kubernetes

Introduction

I want to test the docker-desktop as K8 local cluster. This is not production deployment.

Using Kubectl

$ kubectl run nginx --image=nginx --restart=Never
pod/nginx created

This will create a pod  nginx pod. To make this pod accessible from browser, you can use  kubectl port-forward :

$ kubectl port-forward nginx 8888:80
Forwarding from 127.0.0.1:8888 -> 80
Forwarding from [::1]:8888 -> 80

Open the browser  http://localhost:8888/ :

You can log into the pod and check the logs as well.
$ kubectl logs nginx
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2021/10/05 05:42:13 [notice] 1#1: using the "epoll" event method
2021/10/05 05:42:13 [notice] 1#1: nginx/1.21.3
2021/10/05 05:42:13 [notice] 1#1: built by gcc 8.3.0 (Debian 8.3.0-6)
2021/10/05 05:42:13 [notice] 1#1: OS: Linux 5.10.47-linuxkit
2021/10/05 05:42:13 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2021/10/05 05:42:13 [notice] 1#1: start worker processes
2021/10/05 05:42:13 [notice] 1#1: start worker process 32
2021/10/05 05:42:13 [notice] 1#1: start worker process 33
2021/10/05 05:42:13 [notice] 1#1: start worker process 34
2021/10/05 05:42:13 [notice] 1#1: start worker process 35
2021/10/05 05:42:13 [notice] 1#1: start worker process 36
2021/10/05 05:42:13 [notice] 1#1: start worker process 37
127.0.0.1 - - [05/Oct/2021:05:43:38 +0000] "GET / HTTP/1.1" 200 615 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36" "-"
127.0.0.1 - - [05/Oct/2021:05:43:38 +0000] "GET /favicon.ico HTTP/1.1" 404 555 "http://localhost:8888/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36" "-"
2021/10/05 05:43:38 [error] 34#34: *2 open() "/usr/share/nginx/html/favicon.ico" failed (2: No such file or directory), client: 127.0.0.1, server: localhost, request: "GET /favicon.ico HTTP/1.1", host: "localhost:8888", referrer: "http://localhost:8888/"
127.0.0.1 - - [05/Oct/2021:05:43:40 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36" "-"
127.0.0.1 - - [05/Oct/2021:05:43:41 +0000] "GET / HTTP/1.1" 304 0 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.71 Safari/537.36" "-"

Using YAML

You can create a pod using yaml file as well.
apiVersion: v1
kind: Pod
metadata:
  name: demo
spec:
  containers:
  - name: testpod
    image: alpine:3.5
    command: ["ping", "8.8.8.8"]
Save the above manifest to pod.yaml

$ kubectl apply -f pod.yaml
pod/testpod created
Once the pod is created you can check the pod status kubectl get po :
$ kubectl get po
NAME      READY   STATUS    RESTARTS   AGE
testpod   1/1     Running   0          4s
As this pod makes a ping to 8.8.8.8, you can check the pod running by seeing the log messages.
$ kubectl logs testpod
PING 8.8.8.8 (8.8.8.8): 56 data bytes
64 bytes from 8.8.8.8: seq=0 ttl=37 time=22.417 ms
64 bytes from 8.8.8.8: seq=1 ttl=37 time=14.821 ms
64 bytes from 8.8.8.8: seq=2 ttl=37 time=13.588 ms

We have seen we can run the pod using above 2 methods. 

Happy Coding!!

No comments:

Post a Comment