Monday, August 11, 2025

kubernetes part2

Kubernetes part2

Class 86th Kubernetes Part1 August 11th 

1.Self managed cluster  (everything managed by self master server,worker node ,data..etc)

  example of using tools 
    1.minikube (single node cluster) remain all them multi node cluster 
    2.kops
    3.kubeadm
    4.k3d

For Minikube setup below are the requirements
-->2CPUs  or more 
-->2GB of free memory 
-->20 GB of free disk space 
-->Internet connection
-->Container or virtual machine manager, such as: Docker 


Practical :
Step1: Create one Ec2 instance amazon linux , t3.small 2 VCPU,2 memory 20 GB lunched ,we will try mini 2 gb required.

https://minikube.sigs.k8s.io/docs/start/?arch=%2Fwindows%2Fx86-64%2Fstable%2F.exe+download

Step2:
[ec2-user@ip-10-0-2-53 ~]$ sudo -i
[root@ip-10-0-2-53 ~]# curl -LO https://github.com/kubernetes/minikube/releases/latest/download/minikube-linux-amd64
sudo install minikube-linux-amd64 /usr/local/bin/minikube && rm minikube-linux-amd64
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  126M  100  126M    0     0  96.7M      0  0:00:01  0:00:01 --:--:--  127M
rm: remove regular file 'minikube-linux-amd64'? yes
[root@ip-10-0-2-53 ~]# minikube version
minikube version: v1.36.0
commit: f8f52f5de11fc6ad8244afac475e1d0f96841df1-dirty

Step3: need to install docker 
[root@ip-10-0-2-53 ~]# yum install docker -y[root@ip-10-0-2-53 ~]# systemctl start docker
● docker.service - Docker Application Container Engine
     Loaded: loaded (/usr/lib/systemd/system/docker.service; disabled; preset: disabled)
     Active: active (running) since Wed 2025-08-13 15:20:40 UTC; 19s ago
TriggeredBy: ● docker.socket
Step4: docker --force with out my permission it will start

[root@ip-10-0-2-53 ~]# minikube start --driver=docker --force * minikube v1.36.0 on Amazon 2023.8.20250808 (xen/amd64) ! minikube skips various validations when --force is supplied; this may lead to unexpected behavior * Using the docker driver based on user configuration
* kubectl not found. If you need it, try: 'minikube kubectl -- get pods -A' * Done! kubectl is now configured to use "minikube" cluster and "default" namespace by
default
Step5: minikube status 
[root@ip-10-0-2-53 ~]# minikube status
minikube
type: Control Plane
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
Step6: git CLI tool git ,docker CLI docker only ,Kubernetes CLI kubectl (add/delete)
For Kubernete clusture to communicate, kubectl CLI tool will help 
Installation kubectl 
https://kubernetes.io/docs/tasks/tools/install-kubectl-linux/
[root@ip-10-0-2-53 ~]#    curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   138  100   138    0     0   1197      0 --:--:-- --:--:-- --:--:--  1200
100 57.3M  100 57.3M    0     0   127M      0 --:--:-- --:--:-- --:--:--  127M
[root@ip-10-0-2-53 ~]#

Step7: We got one kubectl file ,for give execute permission to the file 
[root@ip-10-0-2-53 ~]# ls -lrt
total 58724
-rw-r--r--. 1 root root 60129464 Aug 13 19:34 kubectl

[root@ip-10-0-2-53 ~]# chmod -x kubectl
[root@ip-10-0-2-53 ~]# mv kubectl /usr/local/bin/
[root@ip-10-0-2-53 ~]# sudo chmod 777 /usr/local/bin/kubectl
[root@ip-10-0-2-53 ~]# kubectl version
Client Version: v1.33.3
Kustomize Version: v5.6.0
Server Version: v1.33.1

Pod (inside container, inside container application),pod is smallest portion 
Note:
Kubernetes will not deploy the containers directly on worker nodes.
Kubernetes has a object called POD which contains containers . Lets learn about PODS 


if you want see all the resource in Kubernetes give this command , these are Kubernetes object 
smallest object is in Kubernetes pod , one pod has one container or multiple container
We always work with pods only 

[root@ip-10-0-2-53 ~]# kubectl api-resources

Step1: list of pods to see the command 

[root@ip-10-0-2-53 ~]# kubectl get pods
No resources found in default namespace.

or 

[root@ip-10-0-2-53 ~]# kubectl get po
No resources found in default namespace.

if required you can put aliase in bash_profile 

Imperative Way :

Create the pod in imperative way ,below one 1 pod has 1 container is running
[root@ip-10-0-2-53 ~]# kubectl run pod-1 --image=nginx
pod/pod-1 created
[root@ip-10-0-2-53 ~]# kubectl get po
NAME    READY   STATUS    RESTARTS   AGE
pod-1   1/1     Running   0          17s

Step2: to get the pod ip ,we can know how many pod and node cluster ,here we used single node cluster 
[root@ip-10-0-2-53 ~]# kubectl get po -o wide
NAME    READY   STATUS    RESTARTS   AGE    IP           NODE       NOMINATED NODE   READINESS GATES
pod-1   1/1     Running   0          119s   10.244.0.3   minikube   <none>   
If you want see yaml format 
[root@ip-10-0-2-53 ~]# kubectl get po -o yaml
If you want see json format 
[root@ip-10-0-2-53 ~]# kubectl get po -o json
Step3: Creating some more pods 
[root@ip-10-0-2-53 ~]# kubectl run mypod --image=httpd
pod/mypod created
[root@ip-10-0-2-53 ~]# kubectl get po -o wide
NAME    READY   STATUS    RESTARTS   AGE     IP           NODE       NOMINATED NODE   READINESS GATES
mypod   1/1     Running   0          10s     10.244.0.4   minikube   <none>           <none>
pod-1   1/1     Running   0          7m39s   10.244.0.3   minikube   <none>           <none>
Declarative Way : manifest file 
Write file for pod creation apiVersion , V should be capital letter , here multiple thing
multiple version there  (deployment,pod,rba), we can that using the kubectl api-resources command
Step1: Metadata is information about data ,mycontainer container name ,nginx port 80

[root@ip-10-0-2-53 ~]# vi manifest.yaml [root@ip-10-0-2-53 ~]# cat manifest.yaml --- apiVersion: v1 kind: Pod metadata: name: newpod1 spec: containers: - name: mycontainer image: nginx ports: - containerPort: 80 [root@ip-10-0-2-53 ~]# kubectl create -f manifest.yaml pod/newpod1 created

Step4: new pod created
[root@ip-10-0-2-53 ~]# kubectl get po -o wide
NAME      READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
mypod     1/1     Running   0          19m   10.244.0.4   minikube   <none>           <none>
newpod1   1/1     Running   0          64s   10.244.0.5   minikube   <none>           <none>
pod-1     1/1     Running   0          27m   10.244.0.3   minikube   <none>           <none>

Step5: if you want details of the pod give below command , you will get complete information about the detail ,if any pod not working ,we have to check first this command only.
[root@ip-10-0-2-53 ~]# kubectl describe pod newpod1
Name:             newpod1
Namespace:        default
Priority:         0
Service Account:  default
Node:             minikube/192.168.49.2
Start Time:       Wed, 13 Aug 2025 20:23:23 +0000
Labels:           <none>
Annotations:      <none>
Status:           Running
IP:               10.244.0.5
IPs:
  IP:  10.244.0.5
Containers:
  mycontainer:
    Container ID:   docker://299b8337f2320bcfa6abc75abf077fda16e1c192b26c2db69a43c6071375456a
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:3ab4ed065a1437cbbd45e65617b1285bdf6523c6bf56a121e00df41720e09a89
    Port:           80/TCP
    Host Port:      0/TCP
    State:          Running
      Started:      Wed, 13 Aug 2025 20:23:25 +0000
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-pgtgr (ro)
Conditions:
  Type                        Status
  PodReadyToStartContainers   True
  Initialized                 True
  Ready                       True
  ContainersReady             True
  PodScheduled                True
Volumes:
  kube-api-access-pgtgr:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    Optional:                false
    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  3m     default-scheduler  Successfully assigned default/newpod1 to minikube
  Normal  Pulling    2m59s  kubelet            Pulling image "nginx"
  Normal  Pulled     2m58s  kubelet            Successfully pulled image "nginx" in 1.036s (1.036s including waiting). Image size: 192237226 bytes.
  Normal  Created    2m58s  kubelet            Created container: mycontainer
  Normal  Started    2m58s  kubelet            Started container mycontainer

See above all event we can see 
Step6:  delete the pod using below command 
[root@ip-10-0-2-53 ~]# kubectl delete pod mypod
pod "mypod" deleted

[root@ip-10-0-2-53 ~]# kubectl get po
NAME      READY   STATUS    RESTARTS   AGE
newpod1   1/1     Running   0          7m57s
pod-1     1/1     Running   0          33m

To delete all pods 
[root@ip-10-0-2-53 ~]# kubectl delete pod --all
pod "newpod1" deleted
pod "pod-1" deleted
[root@ip-10-0-2-53 ~]# kubectl get po
No resources found in default namespace.

Label :

Step1:

Pod Labelling 
Whenever we create the pod ,we should expose the pod  so the application in the pod container able access internet or out side
For expose the pod ,we use services

[root@ip-10-0-2-53 ~]# vi manifest.yaml
[root@ip-10-0-2-53 ~]# cat manifest.yaml
---
apiVersion: v1
kind: Pod
metadata:
  name: newpod1
  labels:
   app: swiggy
spec:
 containers:
  - name: container-1
    image: nginx
    ports:
      - containerPort: 80

[root@ip-10-0-2-53 ~]# kubectl create -f manifest.yaml
pod/newpod1 created

Pod created succesfully

[root@ip-10-0-2-53 ~]# kubectl get po
NAME      READY   STATUS    RESTARTS   AGE
newpod1   1/1     Running   0          30s


Step2: We can create one po and container
[root@ip-10-0-2-53 ~]# vi manifest.yaml
[root@ip-10-0-2-53 ~]# cat  manifest.yaml
---
apiVersion: v1
kind: Pod
metadata:
  name: newpod2
  labels:
   app: uber
spec:
 containers:
  - name: container-1
    image: nginx
    ports:
      - containerPort: 80

[root@ip-10-0-2-53 ~]# kubectl create -f manifest.yaml
pod/newpod2 created


[root@ip-10-0-2-53 ~]# vi manifest.yaml
[root@ip-10-0-2-53 ~]# cat  manifest.yaml
---
apiVersion: v1
kind: Pod
metadata:
  name: newpod3
  labels:
   app: zomato
spec:
 containers:
  - name: container-1
    image: nginx
    ports:
      - containerPort: 80

[root@ip-10-0-2-53 ~]# kubectl create -f manifest.yaml
pod/newpod3 created

[root@ip-10-0-2-53 ~]# cat manifest.yaml
---
apiVersion: v1
kind: Pod
metadata:
  name: newpod4
  labels:
   app: uber
spec:
 containers:
  - name: container-1
    image: nginx
    ports:
      - containerPort: 80

[root@ip-10-0-2-53 ~]# kubectl create -f manifest.yaml
pod/newpod4 created

[root@ip-10-0-2-53 ~]# kubectl get po --show-labels
NAME      READY   STATUS    RESTARTS   AGE     LABELS
newpod1   1/1     Running   0          8m7s    app=swiggy
newpod2   1/1     Running   0          5m      app=uber
newpod3   1/1     Running   0          3m21s   app=zomato
newpod4   1/1     Running   0          95s     app=uber


Step3: if you want get only uber label pods using below command 
[root@ip-10-0-2-53 ~]# kubectl get po -l app=uber
NAME      READY   STATUS    RESTARTS   AGE
newpod2   1/1     Running   0          6m28s
newpod4   1/1     Running   0          3m3s
[root@ip-10-0-2-53 ~]# kubectl get po -l app=zomato
NAME      READY   STATUS    RESTARTS   AGE
newpod3   1/1     Running   0          4m59s

Single base select is called equality based selector  

Step4: multiple label to select 
[root@ip-10-0-2-53 ~]# kubectl get po -l 'app in(zomato,uber)'
NAME      READY   STATUS    RESTARTS   AGE
newpod2   1/1     Running   0          10m
newpod3   1/1     Running   0          9m17s
newpod4   1/1     Running   0          7m31s

We can called set based sector 

Step5: i have create one po newpod5 lable not exist ,we can attache the label 

[root@ip-10-0-2-53 ~]# kubectl run newpod5 --image=nginx
pod/newpod5 created
[root@ip-10-0-2-53 ~]# kubectl get po --show-labels
NAME      READY   STATUS    RESTARTS   AGE   LABELS
newpod1   1/1     Running   0          17m   app=swiggy
newpod2   1/1     Running   0          14m   app=uber
newpod3   1/1     Running   0          12m   app=zomato
newpod4   1/1     Running   0          10m   app=uber
newpod5   1/1     Running   0          7s    run=newpod5

Step6:
[root@ip-10-0-2-53 ~]# kubectl label pod newpod5 app=swiggy
pod/newpod5 labeled
[root@ip-10-0-2-53 ~]# kubectl get po --show-labels
NAME      READY   STATUS    RESTARTS   AGE     LABELS
newpod1   1/1     Running   0          19m     app=swiggy
newpod2   1/1     Running   0          16m     app=uber
newpod3   1/1     Running   0          14m     app=zomato
newpod4   1/1     Running   0          13m     app=uber
newpod5   1/1     Running   0          2m21s   app=swiggy,run=newpod5

[root@ip-10-0-2-53 ~]# kubectl label pod newpod2 env=dev
pod/newpod2 labeled
[root@ip-10-0-2-53 ~]# kubectl get po --show-labels
NAME      READY   STATUS    RESTARTS   AGE     LABELS
newpod1   1/1     Running   0          21m     app=swiggy
newpod2   1/1     Running   0          18m     app=uber,env=dev
newpod3   1/1     Running   0          16m     app=zomato
newpod4   1/1     Running   0          14m     app=uber
newpod5   1/1     Running   0          3m51s   app=swiggy,run=newpod5

[root@ip-10-0-2-53 ~]# kubectl get po -l app=swiggy
NAME      READY   STATUS    RESTARTS   AGE
newpod1   1/1     Running   0          22m
newpod5   1/1     Running   0          5m10s

[root@ip-10-0-2-53 ~]# kubectl get po -l app!=swiggy
NAME      READY   STATUS    RESTARTS   AGE
newpod2   1/1     Running   0          19m
newpod3   1/1     Running   0          17m
newpod4   1/1     Running   0          16m


Node selector -->we need inform prior, which worker node pod going to be created
Worker node we have make it label , and  master node will decided based on label create the pod 


Reference Document:

https://mustafa-k8s.hashnode.dev/essential-kubernetes-pod-concepts-for-beginners-to-master

https://mustafa-k8s.hashnode.dev/labels-selectors-and-node-selectors

--Thanks

No comments:

Post a Comment