Kubectl Command Reference Sheet For Kubernetes

in STEMGeeks2 months ago (edited)

Over the past few weeks, we been putting together a group of articles to introduce readers to Kubernetes. We've covered a lot of the basics and thought we would put together a command reference guide to list all the kubectl commands that you'll probably need to at least get started when using Kubernetes. We have also liked our previous articles as well, so if you need a little more information, you can refer back to the original article.

Basic Cluster Commands


kubectl version         # Check your kubeclt version
kubectl cluster-info        # Verify your cluster is running                              
kubectl get nodes           # Check your nodes are running
kubectl get nodes -o wide   # Get more details of your nodes 



kubectl get namespaces                  # List all your namesapces in your cluster
kubectl create namespace my_namespace       # Create a namespace

# So you can always use a specific namespace by default with kubectl, you simply use the "--namespace" option or "-n" option. You can also run the following command to set the context of your cluster.
kubectl config set-context ${kubectl config current-context} --namespace dev



kubectl run my_pod --image=image_name       # Create a new pod with name my_pod
kubectl get pods                            # List all the pods in your namesapce
kubectl describe pods my_pods               # Get further details on your pod, or all pods
kubectl get pods --all-namespaces           # All pods on all namespaces 
kubectl delete pods my_pod                  # Delete your pod

#Generate Pod Manifest YAML file (-o yaml). Don't create it(--dry-run)
kubectl run <pod_name> --image=<image_name> --dry-run=client -o yaml

# Note: you can put the output into a file and then edit and use the create command to create your new pod like the following:
kubect create -f yaml_file_name

# Connect to the pod to help troubleshoot any issues and test
kubectl exec -it my_pod /bin/bash

Note: In the above command we are connecting to the bash shell. You need to have a shell running on the pod and connect specific to that location.



kubectl create deployment --image=nginx my_dep              # Create a deployment
kubectl create deployment my_dep --image=nginx --replicas=4     # Create with 4 replicas
kubectl get deployments                                 # List your deployments
kubectl describe deployments my_dep                     # Describe your deployment
kubectl edit deployment my_dep                              # Edit deployment
kubectl scale deployment my_dep --replicas=4                    # Scale deployment
kubectl delete deployment my_dep                            # Delete deployment
kubectl autoscale deployment my_dep --min=2 --max=10            # Set autoscale up

# Generate Deployment YAML file (-o yaml). Don't create it(--dry-run)
kubectl create deployment --image=nginx my_dep --dry-run=client -o yaml



kubectl get daemonset                           # Get all daemonsets in a namespace
kubectl describe daemonset my_ds                # Extra details of daemonsets
kubectl edit daemonsets my_ds                   # Edit daemonsets details
kubectl delete daemonsets my_ds             # Delete a daemonsets



kubectl get services                            # Get all services in a namespace
kubectl get services --sort-by=.metadata.name   # Get services and order
kubectl describe service my_svc                 # Extra details of service
kubectl edit service my_svc                     # Edit service details
kubectl delete service my_svc                   # Delete a service
kubectl delete pod, service my_pod my_svc       # Delete pods and service

# Create a Service named redis-service of type ClusterIP to expose pod redis on port 6379
kubectl expose pod redis --port=6379 --name redis-service --dry-run=client -o yaml

# Create a Service named nginx of type NodePort to expose pod nginx's port 80 on port 30080 on the nodes:
kubectl expose pod nginx --type=NodePort --port=80 --name=nginx-service --dry-run=client -o yaml
# This will automatically use the pod's labels as selectors, but you cannot specify the node port. You have to generate a definition file and then add the node port in manually before creating the service with the pod.
# Or
kubectl create service nodeport nginx --tcp=80:80 --node-port=30080 --dry-run=client -o yaml

ReplicaSet And ReplicationControllers


kubectl get replicationcontroller                       # Get all your rc's
kubectl describe replicationcontroller my_rc            # Describe your rc
kubectl scale --replicas=4 replicationcontroller my_rc  # Scale up your pods
kubectl delete replicationcontroller my_rc              # Delete the rc
kubectl get replicaset                              # Get your replicasets
kubectl describe replicaset my_rs                   # Describe your replicaset
kubectl scale --replicas=3 replicaset my_rs         # Scale your pods
kubectl delete replicaset my_rs                     # Delete your replicaset

Remember, if you are using a deployment, it will automatically create a replicaset as well.

About The Author
I am a DevOps Engineer, Endurance Athlete and Author. As a DevOps Engineer I specialize in Linux and Open Source Applications. Particularly interested in Search Marketing and Analytic’s, and is currently developing my skills in devops, continuous integration, security, and development(Python).

Posted with STEMGeeks