NOTES
Kubernetes Commands
Author by Unnati Gupta
Pods
i) To List all pods in the current namespace
kubectl get pods
ii) Create a Pod
kubectl run <pod-name> --image=<image-name>
iii) To List all pods in all namespace
kubectl get pods -A
iv) Describe a Pod
kubectl describe pod <pod-name>
vi) Delete a pod
kubectl delete pod <pod-name>
vii) View Logs of Pod
kubectl logs <pod-name>
viii) View logs of specific containers within a pod
kubectl logs <pod-name> -c <container-name>
ix) Port-Forwarding to a Pod
kubectl port-forward pod/mypod 8080:80
Unnati Gupta
Deployments
i) To List all deployments in the current namespace
kubectl get deployments
ii) Create a deployments
kubectl create deployment <deployment-name> --
image=<image-name>
iii) To List all deployments in all namespace
kubectl get deployments -A
iv) Describe a deployment
kubectl describe deployment <deployment-name>
vi) Delete a deployments
kubectl delete deployment <deployment-name>
vii) View logs of deployments
kubectl logs <deployment-name>
viii) Scale a deployment
kubectl scale deployment <deployment-name> --
replicas=<replica-count>
ix) Edit a deployment
kubectl edit deployment <deployment-name>
Unnati Gupta
ReplicaSets
i) To List all replicasets in the current namespace
kubectl get replicasets
ii) Create Replicasets
kubectl create replicaset <replicaset-name> --replicas=
<replica-count> --image=<image-name>
iii) To List all replicasets in all namespace
kubectl get replicasets -A
iv) Describe a replicaset
kubectl describe replicaset <replicaset-name>
vi) Delete a replicaset
kubectl delete replicaset <replicaset-name>
vii) Edit a replicaset
kubectl edit replicset <replicaset-name>
viii) Scale a replicasets
kubectl scale rs <replicaset-name> --replicas=
<number-of-replicas>
ix) Watch replicaset events
kubectl get rs --watch
Unnati Gupta
NameSpaces
i) To Create a Namespace
kubectl create ns <namespace-name>
ii) List all Namespaces
kubectl get namespace
iii) To Describe a Namespace
kubectl describe namespace <namespace-name>
iv) To Delete a Namespace
kubectl delete namespace <namespace-name>
vi) Get Resources in a Namespace
kubectl get pods -n <namespace-name>
vii) Export a namespace configuration
kubectl get namespace <namespace-name> -o yaml >
namespace.yaml
viii) Watching Namespace Events
kubectl get events -n <namespace-name> --watch
Unnati Gupta
Services
i) To list all the services in the current namespace
kubectl get service
ii) Expose pods or deployment as a new service
kubectl expose deployment <deployment-name> --
type=LoadBalancer --name=<service-name> --port=
<port> --target-port=<target-port>
iii) To Describe a Service
kubectl describe service <service-name>
iv) To Delete a Service
kubectl delete service <service-name>
vi) Edit a Service
kubectl edit service <service-name>
vii) Get Service details in yaml
kubectl get svc <service-name> -o yaml
viii) Port Forward to a Service
kubectl port-forward svc/<service-name> <local-port>:
<service-port>
Unnati Gupta
Label & Selectors
i) Label Resources
kubectl label pods <pod-name> <key>=<value>
ii) Update labels on Existing Resources
kubectl label pods <pod-name> <key>=<value> --
overwrite
iii) Listing resources by labels
kubectl get pods -l <key>=<value>
kubectl get deployment --selector <key>=<value>
iv) Describe resources with Labels
kubectl describe pods -l <key>=<value>
vi) Delete resources based on Labels
kubectl delete pods -l <key>=<value>
viii) List the pods using multiple selctors
kubectl get pods -l '<key1>=<value1>,<key2>=
<value2>'
ix) Removing Labels from the resources
kubectl label pods <pod-name> <label-key>-
Unnati Gupta
Taints & Tolerations
i) Applying Taints to a Nodes
kubectl taint nodes <node-name> key=value:taint-
effect
Note:
taint-effect: The effect of the taint, which can be one of
the following:
a) NoSchedule: Pods will not be scheduled on the node
unless they tolerate the taint.
b) PreferNoSchedule: Kubernetes will try to avoid
placing a pod on the node but it's not guaranteed.
c) NoExecute: New pods will not be scheduled on the
node and existing pods on the node if they do not
tolerate the taint will be evicted if they do not tolerate
the taint.
ii) Removing Taints From a Node
kubectl taint nodes <node-name> key:effect-
Taints are always applied over the nodes and
Tolerations apply over the pods.
Unnati Gupta
Daemonsets
i) To list all daemonsets in the current
namespace
kubectl get daemonsets
ii) To Edit a daemonsets
kubectl edit daemonsets <daemonsets-name>
iii) View details of a daemonsets
kubectl describe ds <daemonset-name>
iv) Delete daemonsets
kubectl delete daemonset <daemonset-name>
Unnati Gupta
Logging & Monitoring
i) To view the logs of a Pod
kubectl logs <pod-name>
ii) To View the logs of specific containers within
a pod
kubectl logs <pod-name> -c <container-name>
iii) To follow the logs in real-time
kubectl logs -f <pod-name> -c <container-name>
iv) Monitoring the resources with Kubectl
kubectl top nodes
kubectl top pods
Unnati Gupta
Rolling Updates & RollBacks
i) Perform a Rolling Update
kubectl set image deployment/<deployment-name>
<container-name>=<new-image>:<tag>
ii) Checking the status of rolling-update
kubectl rollout status deployment/<deployment-name>
iii) Pausing Rolling Updates
kubectl rollout pause deployment/<deployment-name>
iv) Resume Rolling Updates
kubectl rollout resume deployment/<deployment-name>
v) Rollback to a previous version
kubectl rollout undo deployment/<deployment-name>
vi) Rollback to a specific version
kubectl rollout undo deployment/<deployment-name> --
to-revision=<revision-number>
vii) View the history of the deployment
kubectl rollout history deployment/<deployment-name>
Unnati Gupta
Envirnoment Variables
i) List the envirnoment variables define in all pods
kubectl set env pods --all-list
ii) List envirnoment variables define on a
deployment
kubectl set env deployment/<deployment-name> --list
iii) Update deployment with new environment
variable
kubectl set env deployment/<deployment-name>
env=dev
iv) Remove environment variable from particular
container
kubectl set env deployments --all --containers="
<container-name>" ENV-
Unnati Gupta
ConfigMap
i) Create a configmap based on the file
kubectl create configmap <configmap-name> --from-
file=<file-path>
ii) Create a configmap with the specified key
kubectl create configmap <configmap-name> --from-
literal=<key-name>=<value-name>
iii) Create a new config map named my-config from
the key=value pairs in the file
kubectl create configmap my-config --from-
file=path/to/bar
iv) Delete a configmap
kubectl delete configmap <configmap-name>
Unnati Gupta
Secrets
i) Create a secret with specified keys
kubectl create secret generic <secret-name> --from-
literal=<key-name>=<value-name>
ii) Create a secret from an env file
kubectl create secret generic <secret-name> --from-
env-file=path/to/bar.env
iii) Create a new TLS Secret
kubectl create secret tls <secret-name> --
cert=path/to/tls.cert --key=path/to/tls.key
iv) Create Docker Registry Secret without using the
docker config file
kubectl create secret docker-registry <secret-name> --
docker-server=DOCKER_REGISTRY_SERVER --
docker-username=DOCKER_USER --docker-
password=DOCKER_PASSWORD --docker-
email=DOCKER_EMAIL
v) Create a secret using docker config.json file
kubectl create secret docker-registry <secret-name> --
from-file=.dockerconfigjson=path/to/.docker/config.json
vi) Delete a secret
kubectl delete secret <secret-name>
Unnati Gupta
Role & RoleBinding
i) Create a role
kubectl create role <role_name> --verb=<verb> --
resource=<resource> --namespace=<namespace>
ii) Create a rolebinding
kubectl create rolebinding <binding_name> --role=
<role_name> --user=<user> --namespace=<namespace>
iii) Listing Roles & Rolebindings
kubectl get roles,rolebindings -n <namespace>
v) View Role Details
kubectl describe role <role_name> -n <namespace>
vi) View RoleBinding Details
kubectl describe rolebinding <binding_name> -n
<namespace>
vi) Delete Role
kubectl delete role <role_name> -n <namespace>
vi) Delete RoleBinding
kubectl delete rolebinding <binding_name> -n
<namespace>
Unnati Gupta
ClusterRole
i) Create a clusterrole
kubectl create clusterrole <clusterrole_name> --verb=
<verb> --resource=<resource>
ii) Listing ClusterRole
kubectl get clusterroles
iii) View Clusterole Details
kubectl describe clusterrole <clusterrole_name>
v) Delete a clusterrole
kubectl delete clusterrole <clusterrole_name>
Unnati Gupta
ClusterRoleBindings
i) Create a clusterrolebindings
kubectl create clusterrolebinding <binding_name> --
clusterrole=<role_name> --user=<user>
ii) Listing ClusterRolebindings
kubectl get clusterrolebinding
iii) View Clusterole Details
kubectl describe clusterrolebinding
<clusterrolebinding_name>
v) Delete a clusterrolebinding
kubectl delete clusterrolebinding
<clusterrolebinding_name>
Unnati Gupta
ServiceAccount
i) Create an serviceaccount
kubectl create serviceaccount <serviceaccount-name>
ii) Describe ServiceAccount
kubectl describe serviceaccount <serviceaccount-name>
iii) Delete ServiceAccount
kubectl delete serviceaccount <serviceaccount-name>
v) Create a ServiceAccount in a particular
namespace
kubectl create serviceaccount <serviceaccount-name> -
n <namespace>
Unnati Gupta
Ingress
i) Create an Ingress Resource
kubectl create ingress NAME --
rule=HOST/PATH=SERVICE:PORT [--
namespace=NAMESPACE]
ii) List the Ingress Resource
kubectl get ingress
iii) Describe Ingress Resource
kubectl describe ingress <ingress-name>
v) Edit Ingress Resource
kubectl edit ingress <ingress-name>
v) Delete Ingress Resource
kubectl delete ingress <ingress-name>
Unnati Gupta
Cluster Configuration Command
i) Display the current context
kubectl config current-context
ii) Delete the context
kubectl config delete-context CONTEXT_NAME
iii) View Cluster Configuration
kubectl config view
iv) Set Cluster Context
kubectl config use-context CONTEXT_NAME
v) List Context Name
kubectl config get-contexts
vi) Switch Cluster
kubectl config use-context CLUSTER_NAME
vii) Add Cluster in kubeconfig file
kubectl config set-cluster CLUSTER_NAME --
server=SERVER_URL [--certificate-
authority=CERT_FILE] [--embed-certs=true]
Unnati Gupta
Cluster Configuration Command
viii) Add user in kubeconfig file
kubectl config set-credentials USER_NAME [--
token=TOKEN] [--client-certificate=CERT_FILE] [--client-
key=KEY_FILE]
ix) Delete Context from the kubeconfig file
kubectl config delete-context CONTEXT_NAME
x) Delete Cluster from the kubeconfig file
kubectl config delete-cluster CLUSTER_NAME
xi) Delete the user from the kubeconfig file
kubectl config delete-user USER_NAME
Unnati Gupta
ThankYou