Skip to content

Instantly share code, notes, and snippets.

@akshayithape-devops
Created November 2, 2021 02:18
Show Gist options
  • Save akshayithape-devops/0e9271c6c7c6dc914d91ad664484be14 to your computer and use it in GitHub Desktop.
Save akshayithape-devops/0e9271c6c7c6dc914d91ad664484be14 to your computer and use it in GitHub Desktop.
Kubernetes Imperative Commands

Kubernetes Imperative Commands

POD Object

Create POD

kubectl run pod-name --image=nginx

Generate POD Manifest YAML file (-o yaml). Don’t create it(–dry-run)

kubectl run pod-name --image=nginx --dry-run=client -o yaml > file-name.yml

Get POD Details

# To get basic details
kubectl get pod

# To get complete details
kubectl describe pod pod-name

To edit/update that POD

  1. Way One(Permanent)
# Create the YAML file from POD Details
kubectl get pod pod-name -o yaml > file-name.yml

# Next with VI/Nano editor you can update that file

# After that first delete the older POD
kubectl delete pod pod-name

# Then create new POD with edited file
kubectl apply -f file-name.yml
  1. Way Two(Temporary)
kubectl edit pod pod-name

To delete POD

kubectl delete pod pod-name

Deployment Object

Create Deployment

kubectl create deployment --image=nginx --replicas=4 deployment-name

Generate Deployment Manifest YAML file (-o yaml). Don’t create it(–dry-run)

kubectl create deployment --image=nginx deployment-name --replicas=4 --dry-run=client -o yaml > file-name.yml

Get Deployment Details

# To get basic details
kubectl get deployment

# To get complete details
kubectl describe deployment deployment-name

To edit/update that Deployment

  1. Way One(Permanent)
# Create the YAML file from POD Details
kubectl get deployment deployment-name -o yaml > file-name.yml

# Next with VI/Nano editor you can update that file

# Then just apply changes
kubectl apply -f file-name.yml
  1. Way Two(Temporary)
kubectl edit deployment deployment-name
  1. Third Way(Only to scale replicas)
kubectl scale deployment deployment-name --replicas=4

To delete Deployment

kubectl delete deployment deployment-name

Service Object

Create Service

# Command Pattern : kubectl expose pod pod-name --type=ClusterIP --port=80 --name service-name

# For Cluster IP 
kubectl expose pod pod-name --type=ClusterIP --port=80 --name service-name

# For NodePort
kubectl expose pod pod-name --type=NodePort --port=80 --name service-name

# Note : With expose command we can't mention the NodePort.
# Note : We can't directly bind replicaset and deployment to service with imperative commands because selector option is not available.

Generate Deployment Manifest YAML file (-o yaml). Don’t create it(–dry-run)

# Command Pattern : kubectl create service service-type service-name --tcp=80:80 --dry-run=client -o yaml > file-name.yml

# For Cluster IP
kubectl create service clusterip service-name --tcp=80:80 --dry-run=client -o yaml > file-name.yml

# For NodePort
kubectl create service nodeport service-name --tcp=80:80 --node-port=30080 --dry-run=client -o yaml > file-name.yml

# For Load Balancer
kubectl create service loadbalancer service-name --tcp=80:80 --dry-run=client -o yaml > file-name.yml

# Note : We can't mention the selector in imperative commands. So first we have to create definition file and then we can edit that file and define selector.

# To create object
kubectl create -f file-name.yml

Get Service Details

# To get basic details
kubectl get service

# To get complete details
kubectl describe service service-name

To edit/update that Service

  1. Way One(Permanent)
# Create the YAML file from POD Details
kubectl get service service-name -o yaml > file-name.yml

# Next with VI/Nano editor you can update that file

# Then just apply changes
kubectl apply -f file-name.yml
  1. Way Two(Temporary)
kubectl edit service service-name

To delete Service

kubectl delete service service-name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment