K8s Hands-On: Create a pod
Tags: kubernetes, beginners, handson, tutorial
Published: 2023-10-02 14:42:01

**Introduction**
Hi, I am Akshay Rao. i am starting a Kubernetes hands-on series. This will help you to pass the CKAD exam.

Note:- Kubernetes will be addressed as **_K8s_**, bcoz i am lazy to type.

**Pre-requisite**

1. Need to be familiar with k8s jargon like pod, namespace and 
   also need basic understanding of containers.
2. Need to install Minikube and kubectl in the local machine.
   https://minikube.sigs.k8s.io/docs/start/
   https://kubernetes.io/docs/tasks/tools/

**Agenda** 
In this we will create a simple pod in a custom created namespace(hands-on), understand different parts and see the other kubectl commands related to pod.

**Let's start**
- We are brought to the default namespace, there is no namespace named "hands-on".
```
[k8s-ckad (⎈|minikube:default)]$ kubectl get namespaces
NAME              STATUS   AGE
default           Active   29m
kube-node-lease   Active   29m
kube-public       Active   29m
kube-system       Active   29m
```
- need to create a namespace name "hands-on"
  ``` 
[k8s-ckad (⎈|minikube:default)]$ kubectl create namespace hands-on
namespace/hands-on created
```
- now need to switch to this namespace
```
[k8s-ckad (⎈|minikube:default)]$ kubectl config set-context --current --namespace=hands-on
Context "minikube" modified.
```

```
CURRENT   NAME         CLUSTER            AUTHINFO                NAMESPACE
*         minikube     minikube           minikube                hands-on
```

-  now to create a pod create a file ``` vi pod1.yaml``` and write the following snippet.
```
apiVersion: v1 # version of K8s Api using
kind: pod # what we are trying to create
metadata:
  name:  pod1 # pod name
spec:
  containers:
  - name: nginx # container name
    image: nginx:latest # container image
    ports:
    - containerPort: 80 #configure the pod to expose port 80
```
- Create a pod and verify
```
[k8s-ckad (⎈|minikube:default)]$ kubectl create -f pod1.yaml
pod/pod1 created
```
```
[k8s-ckad (⎈|minikube:hands-on)]$ k get pods -o wide
NAME   READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
pod1   1/1     Running   0          15s   172.17.0.3   minikube   <none>           <none>
```
- now lets see other kubectl commands
```
[k8s-ckad (⎈|minikube:default)]$ kubectl describe pods pod1
Name:             pod1
Namespace:        hands-on
Priority:         0
Service Account:  default
Node:             minikube/192.168.49.2
Start Time:       Mon, 02 Oct 2023 23:14:45 +0900
Labels:           <none>
Annotations:      <none>
Status:           Running
IP:               172.17.0.3
IPs:
  IP:  172.17.0.3
Containers:
  nginx:
    Container ID:   docker://7c55270c7615a5e8ad533af9e82990bfcad855625b0440ab97b57fb5319e1d62
    Image:          nginx:latest
    Image ID:       docker-pullable://nginx@sha256:32da30332506740a2f7c34d5dc70467b7f14ec67d912703568daff790ab3f755
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Mon, 02 Oct 2023 23:15:02 +0900
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-vvqt8 (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-vvqt8:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   BestEffort
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                             node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  118s  default-scheduler  Successfully assigned default/pod1 to minikube
  Normal  Pulling    117s  kubelet            Pulling image "nginx:latest"
  Normal  Pulled     101s  kubelet            Successfully pulled image "nginx:latest" in 15.837068995s
  Normal  Created    101s  kubelet            Created container nginx
  Normal  Started    101s  kubelet            Started container nginx
```
describe command is used for troubleshooting, in the events section all the event will be given. In this all the information related to pod will be given. 

Seeing Logs
```
[k8s-ckad (⎈|minikube:default)]$ kubectl logs  pod1
/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: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
/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
2023/10/02 14:15:02 [notice] 1#1: using the "epoll" event method
2023/10/02 14:15:02 [notice] 1#1: nginx/1.25.2
2023/10/02 14:15:02 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14) 
2023/10/02 14:15:02 [notice] 1#1: OS: Linux 5.15.49-linuxkit
2023/10/02 14:15:02 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/10/02 14:15:02 [notice] 1#1: start worker processes
2023/10/02 14:15:02 [notice] 1#1: start worker process 30
2023/10/02 14:15:02 [notice] 1#1: start worker process 31
2023/10/02 14:15:02 [notice] 1#1: start worker process 32
2023/10/02 14:15:02 [notice] 1#1: start worker process 33
```
now delete the pod
```
[k8s-ckad (⎈|minikube:default)]$ kubectl delete pod pod1
pod "pod1" deleted
```