list the nodes in the k8s cluster [ run it on the master node ]

root@master:~# alias k=kubectl
root@master:~# k get nodes
NAME                 STATUS   ROLES           AGE    VERSION
master.ckalab4.com   Ready    control-plane   64m    v1.27.1
node1.ckalab4.com    Ready    <none>          103s   v1.27.1
node2.ckalab4.com    Ready    <none>          52s    v1.27.1
root@master:~#

display default pods in the cluster

root@master:~# k get pods -A
NAMESPACE     NAME                                         READY   STATUS    RESTARTS   AGE
kube-system   coredns-5d78c9869d-2hkjb                     1/1     Running   0          159m
kube-system   coredns-5d78c9869d-6qbvj                     1/1     Running   0          159m
kube-system   etcd-master.ckalab4.com                      1/1     Running   0          159m
kube-system   kube-apiserver-master.ckalab4.com            1/1     Running   0          159m
kube-system   kube-controller-manager-master.ckalab4.com   1/1     Running   0          159m
kube-system   kube-proxy-76xqr                             1/1     Running   0          96m
kube-system   kube-proxy-89frm                             1/1     Running   0          97m
kube-system   kube-proxy-j2rmn                             1/1     Running   0          159m
kube-system   kube-scheduler-master.ckalab4.com            1/1     Running   0          159m
kube-system   weave-net-6474h                              2/2     Running   0          97m
kube-system   weave-net-7vtqq                              2/2     Running   0          124m
kube-system   weave-net-hhzts                              2/2     Running   0          96m
root@master:~#

To get more [ wider ] details of the pods

root@master:~# k get pods -o wide
No resources found in default namespace.
root@master:~# k get pods -o wide -A
NAMESPACE     NAME                                         READY   STATUS    RESTARTS   AGE    IP               NODE                 NOMINATED NODE   READINESS GATES
kube-system   coredns-5d78c9869d-2hkjb                     1/1     Running   0          161m   10.32.0.2        master.ckalab4.com   <none>           <none>
kube-system   coredns-5d78c9869d-6qbvj                     1/1     Running   0          161m   10.32.0.3        master.ckalab4.com   <none>           <none>
kube-system   etcd-master.ckalab4.com                      1/1     Running   0          161m   101.53.148.104   master.ckalab4.com   <none>           <none>
kube-system   kube-apiserver-master.ckalab4.com            1/1     Running   0          161m   101.53.148.104   master.ckalab4.com   <none>           <none>
kube-system   kube-controller-manager-master.ckalab4.com   1/1     Running   0          161m   101.53.148.104   master.ckalab4.com   <none>           <none>
kube-system   kube-proxy-76xqr                             1/1     Running   0          98m    101.53.148.93    node2.ckalab4.com    <none>           <none>
kube-system   kube-proxy-89frm                             1/1     Running   0          99m    101.53.148.77    node1.ckalab4.com    <none>           <none>
kube-system   kube-proxy-j2rmn                             1/1     Running   0          161m   101.53.148.104   master.ckalab4.com   <none>           <none>
kube-system   kube-scheduler-master.ckalab4.com            1/1     Running   0          161m   101.53.148.104   master.ckalab4.com   <none>           <none>
kube-system   weave-net-6474h                              2/2     Running   0          99m    101.53.148.77    node1.ckalab4.com    <none>           <none>
kube-system   weave-net-7vtqq                              2/2     Running   0          126m   101.53.148.104   master.ckalab4.com   <none>           <none>
kube-system   weave-net-hhzts                              2/2     Running   0          98m    101.53.148.93    node2.ckalab4.com    <none>           <none>
root@master:~#

Create sample pod

root@master:~# kubectl run nginx-test --image=nginix
pod/nginx-test created
root@master:~#
root@master:~# k get pods
NAME         READY   STATUS         RESTARTS   AGE
nginx-test   0/1     ErrImagePull   0          7s
root@master:~# k get pods -o wide
NAME         READY   STATUS             RESTARTS   AGE   IP            NODE                NOMINATED NODE   READINESS GATES
nginx-test   0/1     ImagePullBackOff   0          15s   10.244.64.1   node2.ckalab4.com   <none>           <none>
root@master:~#

get full details using describe

root@master:~# k get pods -o wide
NAME         READY   STATUS             RESTARTS   AGE   IP            NODE                NOMINATED NODE   READINESS GATES
nginx-test   0/1     ImagePullBackOff   0          15s   10.244.64.1   node2.ckalab4.com   <none>           <none>
root@master:~# k describe pod nginx-test
Name:             nginx-test
Namespace:        default
Priority:         0
Service Account:  default
Node:             node2.ckalab4.com/101.53.148.93
Start Time:       Tue, 18 Apr 2023 06:31:45 +0000
Labels:           run=nginx-test
Annotations:      <none>
Status:           Pending
IP:               10.244.64.1
IPs:
  IP:  10.244.64.1
Containers:
  nginx-test:
    Container ID:
    Image:          nginix
    Image ID:
    Port:           <none>
    Host Port:      <none>
    State:          Waiting
      Reason:       ImagePullBackOff
    Ready:          False
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-9pqls (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             False
  ContainersReady   False
  PodScheduled      True
Volumes:
  kube-api-access-9pqls:
    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  71s                default-scheduler  Successfully assigned default/nginx-test to node2.ckalab4.com
  Normal   Pulling    29s (x3 over 70s)  kubelet            Pulling image "nginix"
  Warning  Failed     27s (x3 over 68s)  kubelet            Failed to pull image "nginix": rpc error: code = Unknown desc = failed to pull and unpack image "docker.io/library/nginix:latest": failed to resolve reference "docker.io/library/nginix:latest": pull access denied, repository does not exist or may require authorization: server message: insufficient_scope: authorization failed
  Warning  Failed     27s (x3 over 68s)  kubelet            Error: ErrImagePull
  Normal   BackOff    1s (x4 over 67s)   kubelet            Back-off pulling image "nginix"
  Warning  Failed     1s (x4 over 67s)   kubelet            Error: ImagePullBackOff
root@master:~#

Above image name is wrong . now edit the pod dynamically apply the change

root@master:~# k edit pod nginx-test


pod/nginx-test edited
root@master:~#

Image: nginix

root@master:~# k get pods
NAME         READY   STATUS    RESTARTS   AGE
nginx-test   1/1     Running   0          3m42s
root@master:~#

showing the pod in yaml using -o yaml option

root@master:~# k get pod -o yaml
apiVersion: v1
items:
- apiVersion: v1
  kind: Pod
  metadata:
    creationTimestamp: "2023-04-18T06:31:45Z"
    labels:
      run: nginx-test
    name: nginx-test
    namespace: default
    resourceVersion: "15271"
    uid: cf6e7300-0aa2-4baa-b69b-6e19ffb1b1e2
  spec:
    containers:
    - image: nginx
      imagePullPolicy: Always
      name: nginx-test
      resources: {}
      terminationMessagePath: /dev/termination-log
      terminationMessagePolicy: File
      volumeMounts:
      - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
        name: kube-api-access-9pqls
        readOnly: true
    dnsPolicy: ClusterFirst
    enableServiceLinks: true
    nodeName: node2.ckalab4.com
    preemptionPolicy: PreemptLowerPriority
    priority: 0
    restartPolicy: Always
    schedulerName: default-scheduler
    securityContext: {}
    serviceAccount: default
    serviceAccountName: default
    terminationGracePeriodSeconds: 30
    tolerations:
    - effect: NoExecute
      key: node.kubernetes.io/not-ready
      operator: Exists
      tolerationSeconds: 300
    - effect: NoExecute
      key: node.kubernetes.io/unreachable
      operator: Exists
      tolerationSeconds: 300
    volumes:
    - name: kube-api-access-9pqls
      projected:
        defaultMode: 420
        sources:
        - serviceAccountToken:
            expirationSeconds: 3607
            path: token
        - configMap:
            items:
            - key: ca.crt
              path: ca.crt
            name: kube-root-ca.crt
        - downwardAPI:
            items:
            - fieldRef:
                apiVersion: v1
                fieldPath: metadata.namespace
              path: namespace
  status:
    conditions:
    - lastProbeTime: null
      lastTransitionTime: "2023-04-18T06:31:45Z"
      status: "True"
      type: Initialized
    - lastProbeTime: null
      lastTransitionTime: "2023-04-18T06:35:22Z"
      status: "True"
      type: Ready
    - lastProbeTime: null
      lastTransitionTime: "2023-04-18T06:35:22Z"
      status: "True"
      type: ContainersReady
    - lastProbeTime: null
      lastTransitionTime: "2023-04-18T06:31:45Z"
      status: "True"
      type: PodScheduled
    containerStatuses:
    - containerID: containerd://c75f7e07eff43fe1f67fbd1ceefd137b1c7515048e516fffaf1a42f3ba10ecc7
      image: docker.io/library/nginx:latest
      imageID: docker.io/library/nginx@sha256:63b44e8ddb83d5dd8020327c1f40436e37a6fffd3ef2498a6204df23be6e7e94
      lastState: {}
      name: nginx-test
      ready: true
      restartCount: 0
      started: true
      state:
        running:
          startedAt: "2023-04-18T06:35:22Z"
    hostIP: 101.53.148.93
    phase: Running
    podIP: 10.244.64.1
    podIPs:
    - ip: 10.244.64.1
    qosClass: BestEffort
    startTime: "2023-04-18T06:31:45Z"
kind: List
metadata:
  resourceVersion: ""
root@master:~#

checking the logs of nginx pod just created

root@master:~# k get pods -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP            NODE                NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          22s   10.244.64.1   node2.ckalab4.com   <none>           <none>
root@master:~# k 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
2023/04/18 06:41:19 [notice] 1#1: using the "epoll" event method
2023/04/18 06:41:19 [notice] 1#1: nginx/1.23.4
2023/04/18 06:41:19 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2023/04/18 06:41:19 [notice] 1#1: OS: Linux 5.4.0-146-generic
2023/04/18 06:41:19 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/04/18 06:41:19 [notice] 1#1: start worker processes
2023/04/18 06:41:19 [notice] 1#1: start worker process 28
root@master:~#
root@master:~# k run nginx-a --image=nginx
pod/nginx-a created
root@master:~# k get pods -o wide
NAME      READY   STATUS              RESTARTS   AGE     IP            NODE                NOMINATED NODE   READINESS GATES
nginx     1/1     Running             0          3m52s   10.244.64.1   node2.ckalab4.com   <none>           <none>
nginx-a   0/1     ContainerCreating   0          4s      <none>        node1.ckalab4.com   <none>           <none>
root@master:~# k get pods -o wide
NAME      READY   STATUS              RESTARTS   AGE     IP            NODE                NOMINATED NODE   READINESS GATES
nginx     1/1     Running             0          3m57s   10.244.64.1   node2.ckalab4.com   <none>           <none>
nginx-a   0/1     ContainerCreating   0          9s      <none>        node1.ckalab4.com   <none>           <none>
root@master:~# k get pods -o wide -w
NAME      READY   STATUS    RESTARTS   AGE    IP             NODE                NOMINATED NODE   READINESS GATES
nginx     1/1     Running   0          4m1s   10.244.64.1    node2.ckalab4.com   <none>           <none>
nginx-a   1/1     Running   0          13s    10.244.192.1   node1.ckalab4.com   <none>           <none>
^Croot@master:~#
root@master:~#
root@master:~# k logs nginx-a
/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
2023/04/18 06:45:15 [notice] 1#1: using the "epoll" event method
2023/04/18 06:45:15 [notice] 1#1: nginx/1.23.4
2023/04/18 06:45:15 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2023/04/18 06:45:15 [notice] 1#1: OS: Linux 5.4.0-146-generic
2023/04/18 06:45:15 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2023/04/18 06:45:15 [notice] 1#1: start worker processes
2023/04/18 06:45:15 [notice] 1#1: start worker process 29
root@master:~#

either edit online pod LIVE or update using yaml file

root@master:~# k get pod nginx-a -o yaml > /tmp/nginx-a.yaml
root@master:~# vi /tmp/nginx-a.yaml
root@master:~# k replace -f /tmp/nginx-a.yaml

Create new pod with exising running pod

root@master:~# vi /tmp/nginx-a.yaml
root@master:~# k replace -f /tmp/nginx-a.yaml
Error from server (Conflict): error when replacing "/tmp/nginx-a.yaml": Operation cannot be fulfilled on pods "nginx-b": StorageError: invalid object, Code: 4, Key: /registry/pods/default/nginx-b, ResourceVersion: 0, AdditionalErrorMsg: Precondition failed: UID in precondition: 2762b5aa-1c14-47e4-94e2-cf5d4bba86a2, UID in object meta:
root@master:~# k create -f /tmp/nginx-a.yaml
pod/nginx-b created
root@master:~#
root@master:~#
root@master:~# k get pods
NAME      READY   STATUS    RESTARTS   AGE
nginx     1/1     Running   0          15m
nginx-a   1/1     Running   0          11m
nginx-b   1/1     Running   0          27s
root@master:~# k get pods -o wide
NAME      READY   STATUS    RESTARTS   AGE   IP             NODE                NOMINATED NODE   READINESS GATES
nginx     1/1     Running   0          15m   10.244.64.1    node2.ckalab4.com   <none>           <none>
nginx-a   1/1     Running   0          11m   10.244.192.1   node1.ckalab4.com   <none>           <none>
nginx-b   1/1     Running   0          32s   10.244.192.2   node1.ckalab4.com   <none>           <none>
root@master:~#

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *