Saturday 2 July 2022

Basic Commands of Kubernetes


#Creating the Deployment

kubectl create deployment myapp2 --image=docker.io/openshift/hello-openshift

deployment.apps/myapp2 created


#Verify the Deployment and Pods’ states 

kubectl get po

NAME                      READY   STATUS    RESTARTS       AGE

myapp2-757cc77ff9-lnzwc   1/1     Running   0              8s


#describe Pod content

kubectl describe po  myapp2-757cc77ff9-lnzwc

Name:         myapp2-757cc77ff9-lnzwc

Namespace:    default

Priority:     0

Node:         node-2.example.com/172.31.55.63

Start Time:   Thu, 30 Jun 2022 22:52:42 +0000

Labels:       app=myapp2

              pod-template-hash=757cc77ff9

Annotations:  <none>

Status:       Running

IP:           10.36.0.5

IPs:

  IP:           10.36.0.5

Controlled By:  ReplicaSet/myapp2-757cc77ff9

Containers:

  hello-openshift:

    Container ID:   docker://f39e3bfb9b6b4cdf127dfd8368ec76610078d64c79516e70848b884d2e6e4be7

    Image:          docker.io/openshift/hello-openshift

    Image ID:       docker-pullable://openshift/hello-openshift@sha256:aaea76ff622d2f8bcb32e538e7b3cd0ef6d291953f3e7c9f556c1ba5baf47e2e

    Port:           <none>

    Host Port:      <none>

    State:          Running

      Started:      Thu, 30 Jun 2022 22:52:44 +0000

    Ready:          True

    Restart Count:  0

    Environment:    <none>

    Mounts:

      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-x5skk (ro)

Conditions:

  Type              Status

  Initialized       True 

  Ready             True 

  ContainersReady   True 

  PodScheduled      True 

Volumes:

  kube-api-access-x5skk:

    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  98s   default-scheduler  Successfully assigned default/myapp2-757cc77ff9-lnzwc to node-2.example.com

  Normal  Pulling    98s   kubelet            Pulling image "docker.io/openshift/hello-openshift"

  Normal  Pulled     97s   kubelet            Successfully pulled image "docker.io/openshift/hello-openshift" in 827.570703ms

  Normal  Created    97s   kubelet            Created container hello-openshift

  Normal  Started    97s   kubelet            Started container hello-openshift


#to get running Deployment 

kubectl get deployment

NAME     READY   UP-TO-DATE   AVAILABLE   AGE

myapp2   1/1     1            1           4m14s


#to describe content

kubectl describe deployment myapp2

Name:                   myapp2

Namespace:              default

CreationTimestamp:      Thu, 30 Jun 2022 22:52:42 +0000

Labels:                 app=myapp2

Annotations:            deployment.kubernetes.io/revision: 1

Selector:               app=myapp2

Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable

StrategyType:           RollingUpdate

MinReadySeconds:        0

RollingUpdateStrategy:  25% max unavailable, 25% max surge

Pod Template:

  Labels:  app=myapp2

  Containers:

   hello-openshift:

    Image:        docker.io/openshift/hello-openshift

    Port:         <none>

    Host Port:    <none>

    Environment:  <none>

    Mounts:       <none>

  Volumes:        <none>

Conditions:

  Type           Status  Reason

  ----           ------  ------

  Available      True    MinimumReplicasAvailable

  Progressing    True    NewReplicaSetAvailable

OldReplicaSets:  <none>

NewReplicaSet:   myapp2-757cc77ff9 (1/1 replicas created)

Events:

  Type    Reason             Age   From                   Message

  ----    ------             ----  ----                   -------

  Normal  ScalingReplicaSet  10m   deployment-controller  Scaled up replica set myapp2-757cc77ff9 to 1


#create a yaml file 

kubectl create deployment myhttpd --image=docker.io/httpd --dry-run=client -o yaml > mydep.yaml


cat mydep.yaml 

apiVersion: apps/v1

kind: Deployment

metadata:

  creationTimestamp: null

  labels:

    app: myhttpd

  name: myhttpd

spec:

  replicas: 1

  selector:

    matchLabels:

      app: myhttpd

  strategy: {}

  template:

    metadata:

      creationTimestamp: null

      labels:

        app: myhttpd

    spec:

      containers:

      - image: docker.io/httpd

        name: httpd

        resources: {}

status: {}


#Create the Deployment 

kubectl apply -f mydep.yaml 

deployment.apps/myhttpd created


kubectl get deployment

NAME                  READY   UP-TO-DATE   AVAILABLE   AGE

myhttpd               1/1     1            1           11s


#Expose the Deployment to create a service

kubectl expose deployment myhttpd --port=8080

service/myhttpd exposed


#Verify the created services

kubectl get svc

NAME                     TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE

kubernetes               ClusterIP   10.96.0.1        <none>        443/TCP        7d9h

myhttpd                  ClusterIP   10.110.234.236   <none>        8080/TCP       9s


#Describe the service 

kubectl describe svc myhttpd

Name:              myhttpd

Namespace:         default

Labels:            app=myhttpd

Annotations:       <none>

Selector:          app=myhttpd

Type:              ClusterIP

IP Family Policy:  SingleStack

IP Families:       IPv4

IP:                10.110.234.236

IPs:               10.110.234.236

Port:              <unset>  8080/TCP

TargetPort:        8080/TCP

Endpoints:         10.36.0.5:8080

Session Affinity:  None

Events:            <none>


Creating the namespaces


#create specific namespaces

kubectl create namespace mynamespace

namespace/mynamespace created


#Verify the namespace

kubectl get namespace

NAME              STATUS   AGE

default           Active   7d9h

kube-node-lease   Active   7d9h

kube-public       Active   7d9h

kube-system       Active   7d9h

mynamespace       Active   8s


Scaling and deleting the Deployment


#create a Deployment in a particular namespace

kubectl create deployment myhttpd --image=docker.io/httpd -n mynamespace

deployment.apps/myhttpd created


kubectl get deployment -n mynamespace

NAME      READY   UP-TO-DATE   AVAILABLE   AGE

myhttpd   1/1     1            1           2m39s


kubectl get pods -n mynamespace

NAME                       READY   STATUS    RESTARTS   AGE

myhttpd-78f86b45d5-pd2ph   1/1     Running   0          3m5s


#Scale the Deployment 

kubectl scale --replicas=3 deployment myhttpd -n  mynamespace

deployment.apps/myhttpd scaled


kubectl get deployment -n mynamespace

NAME      READY   UP-TO-DATE   AVAILABLE   AGE

myhttpd   3/3     3            3           4m49s


#get the endpoints

kubectl get endpoints

NAME                     ENDPOINTS                       AGE

kubernetes               172.31.54.141:6443              7d10h

myhttpd                  10.36.0.5:8080                  39m


kubectl describe endpoints

Name:         kubernetes

Namespace:    default

Labels:       endpointslice.kubernetes.io/skip-mirror=true

Annotations:  <none>

Subsets:

  Addresses:          172.31.54.141

  NotReadyAddresses:  <none>

  Ports:

    Name   Port  Protocol

    ----   ----  --------

    https  6443  TCP


Events:  <none>


Name:         myhttpd

Namespace:    default

Labels:       app=myhttpd

Annotations:  endpoints.kubernetes.io/last-change-trigger-time: 2022-07-03T01:32:13Z


Delete the created Pods, Deployment, service, and namespace 


kubectl delete deployment myhttpd

deployment.apps "myhttpd" deleted


kubectl delete svc myhttpd

service "myhttpd" deleted


kubectl get svc

NAME                     TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE

kubernetes               ClusterIP   10.96.0.1        <none>        443/TCP        7d10h


kubectl delete namespace mynamespace

namespace "mynamespace" deleted


#Verify the events 

kubectl get events

LAST SEEN   TYPE      REASON                    OBJECT                                          MESSAGE

57m         Normal    Starting                  node/main.example.com                         Starting kubelet.

57m         Normal    NodeHasSufficientMemory   node/main.example.com                         Node main.example.com status is now: NodeHasSufficientMemory

57m         Normal    NodeHasNoDiskPressure     node/main.example.com                         Node main.example.com status is now: NodeHasNoDiskPressure

57m         Normal    NodeHasSufficientPID      node/main.example.com                         Node main.example.com status is now: NodeHasSufficientPID

57m         Normal    NodeAllocatableEnforced   node/main.example.com                         Updated Node Allocatable limit across pods

<...>


#Verify the Nodes’ state 

kubectl get nodes

NAME                        STATUS   ROLES                  AGE     VERSION

node.example.com          Ready    control-plane,main   7d10h   v1.23.4

node-1.example.com   Ready    <none>                 7d10h   v1.23.4

node-2.example.com   Ready    <none>                 7d10h   v1.23.4


#describe Nodes’ 

kubectl describe node node-1.example.com

Name:               node-1.example.com

Roles:              <none>

Labels:             beta.kubernetes.io/arch=amd64

                    beta.kubernetes.io/os=linux

                    kubernetes.io/arch=amd64

                    kubernetes.io/hostname=worker-node-1.example.com

                    kubernetes.io/os=linux

Annotations:        kubeadm.alpha.kubernetes.io/cri-socket: /var/run/dockershim.sock

                    node.alpha.kubernetes.io/ttl: 0

                    volumes.kubernetes.io/controller-managed-attach-detach: true

<...>


No comments:

Post a Comment