Skip to content

Kubernetes for beginners

Deekshith SN edited this page Apr 23, 2021 · 9 revisions

kubernetes

basic_objects

Pod

pod is a object which encapsulates or groups one or more containers (such as Docker containers), with shared storage/network, and a specification for how to run the containers.

below is the basic structure of pod

  apiVersion: v1
  kind: Pod
  metadata:
    name: my-first-app
    labels:
      type: front-end
  spec:
    containers:
      - name: nginx
        image: nginx
  • command to list the pods

    kubectl get pods
    
  • command to create a pod with image busybox ( it actually creates deployment )

    kubectl run my-pod --image=busybox
    
  • command to delete a particular pod

    kubectl delete pod pod_name 
    
  • if you have created pod using kubectl run command but you want see configrations of pod, so to get yml out of pod use below command

    kubectl get pod pod_name -o yaml > my_pod_file.yaml
    
  • if you want describe your pod

    kubectl describe pod pod_name 
    
  • if you want to login for running pod

    kubectl exec -it pod_name /bin/sh
    
  • if you want access logs of any container inside pod

    kubectl logs pod_name -c conatiner_name_inside_pod   ( container name inside pod you will get it by kubectl describe pod command ) 
    
  • let's say you have created pod using kubectl run command and now you want to change small configuration of pod then you can use below command

      kubectl edit pod pod_name ( it will open a vi editor , so you can change and save it back then you no need apply 
      it back kubernetes will automatically updates the pod )
    
  • let's you have created pods in any other namespaces ( other than default ) , to list them use below command

      kubectl get pods --namespace=dev   ( assuming namespace is dev ) 
    

ReplicaSet

replicaset is an kubernetes object which is used make your pod highly available and any point time it will make sure sepcified replicas are up and running

basic structure replicaset


         apiVersion: apps/v1
         kind: ReplicaSet
         metadata:
           name: my-rs
           labels:
             type: front-end
         spec:
           template:
             metadata:
               name: my-pod
               labels:
                 type: front-end
             spec:
              containers:
                - name: nginx
                  image: nginx
           replicas: 6
           selector:
             matchLabels:
               type: front-end

when you create replica set , it automatically creates pods

  • to list replicaset

      kubectl get rs or kubectl get replicasets 
    
  • to list replicasets in other name space

      kubectl get rs --namespace=dev ( assuming namespace is dev ) 
    
  • to delete replicaset

      kubectl delete rs rs_name
    
  • lets say replica set has been created via kubectl run command , you want to see configuration file of it

      kubectl get rs rs_name -o yaml > rs_sample.yaml
    
  • lets say you want to edit rs

      kubectl edit rs rs_name ( it opens vi editor and do the changes , save it back it automatically applies it back )
      
     but one thing to keep in mind is lets you have created rs with image busybox777 and replicas 3
     it creates 3 pods with status failed , you will change rs and apply it back
     but even then pods will with old configuration, so here you have 2 options 
     1. delete all old pods and now while creating new pods it will take updated configurations
     2. delete rs and re-create it
    
  • to increase or decrease replica count without changing rs yaml config file use below command

      kubectl scale rs rs_name --replicas=6
    

Deployment

A Deployment provides declarative updates for Pods and ReplicaSets.

You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate. You can define Deployments to create new ReplicaSets, or to remove existing Deployments and adopt all their resources with new Deployments.

basic structure Deployment

         apiVersion: apps/v1
         kind: Deployment
         metadata:
           name: nginx-deployment
           labels:
             app: nginx
         spec:
           replicas: 3
           selector:
             matchLabels:
               app: nginx
           template:
             metadata:
               labels:
                 app: nginx
             spec:
               containers:
               - name: nginx
                 image: nginx:1.14.2
                 ports:
                 - containerPort: 80

when you create replica set , it automatically creates pods

  • to list deployment

      kubectl get deploy or kubectl get deployment
    
  • to list deployment in other name space

      kubectl get deploy --namespace=dev ( assuming namespace is dev ) 
    
  • to delete deployment

      kubectl delete deploy deployment_name
    
  • lets say deployment has been created via kubectl run command , you want to see configuration file of it

      kubectl get deploy deployment -o yaml > deploy_sample.yaml
    
  • lets say you want to edit deployment

      kubectl edit deploy deployment_name ( it opens vi editor and do the changes , save it back it automatically applies it back )
    
  • lets say you want to check status of new deployment then use below command

     kubectl rollout status deployment_name
    
  • lets say you want to see history of deployment then use below command

     kubectl rollout history deployment deployment_name 
    
  • if you want play with undo option use below commands

     kubectl --record deployment.apps/deployment_name set image deployment.v1.apps/deployemnt_name conatiner_name=deekshithsn/ui
     kubectl --record deployment.apps/deployment_name set image deployment.v1.apps/deployemnt_name conatiner_name=nginx
     kubectl rollout undo deployment deployment_name 
     kubectl rollout undo deployment deployment_name --to-revision=1
    
Clone this wiki locally