Kubernetes Learning Phase 1 - Basic Commands

This post will only includes scenario based Kubernetes problem and the commands/debugging steps.It will be updated whenever I learn something new.

Installation of Kubernetes refer herehttps://kubernetes.io/docs/tasks/tools/

Case 1: Get all pods in a namespace

kubectl get pods -n <namespace_name>

Case 2: Get all namespaces

kubectl get namespaces 
OR
kubectl get ns

Case 3: Create pods with a name and image in a particular namespace

kubectl run <pod_name> --image=<image_name> -n <namespace>

e.g.
kubectl run redis --image=redis -n my-namespace

Case 4: Get all pods in all the namespaces

kubectl get pods --all-namespaces

Case 5: Get all replicasets

kubectl get replicaset

Case 6: Create pods using yaml file

kubectl apply -f abc.yml -n namespace

Content of abc.yaml or abc.yml file:

apiVersionv1
kindPod
metadata
  namenew-pod
  label:
    typelinux
spec:
  containers:
    -nameredis
     imageredis

Case 7: Create deployment using yaml file

kubectl apply -f deployment.yml

apiVersionapps/v1
kindDeployment
metadata:
  namenew-deployment
spec:
  replicas3
  selector:
    matchLabels:
      nameabc-pod
  template:
    metadata:
      labels:
        nameabc-pod
    spec:
      containers:
      - nameabc-container
        imagenginx
        command:
        - sh
        - "-c"
        - echo Hello Kubernetes! && sleep 3600

Here important points are: 
  • apiVersion should follow the way apps/v1 or apps/v
  • kind is case sensitive
  • matchlabels.name should be same as metadata.labels.name
  • You can specify the replica number and that many pods will be cretaed.
  • When this deployment will be created, a replicaset, 3 pods, one deployment will be created.

Case 8: Get all details of the pod

kubectl describe pod <pod_name> -n <namespace>

Case 9: Get all pods, replicaset, deployment and services present

kubectl get all

Case 10: Editing an existing replicaset

kubectl edit replicaset <replicaset_name>


Comments