avatarAli Zeynalli

Summary

The website content provides a comprehensive list of essential Kubernetes commands for Cloud Developers, emphasizing the use of the kubectl command-line tool for managing Kubernetes clusters.

Abstract

The article titled "The Ultimate List of Must-Know Kubernetes Commands" serves as a practical guide for Cloud Developers who prefer using command-line interfaces over graphical dashboards. It introduces kubectl, the primary command-line tool for interacting with Kubernetes, and outlines its command structure. The author categorizes commands into various operations such as getting resources, interacting with configurations, creating resources, and scaling, among others. These commands are crucial for daily operations involving Kubernetes clusters, including resource creation, scaling, logging, and deletion. The article also touches on advanced interactions with nodes and clusters, and it concludes with a personal invitation from the author to explore more software engineering topics through their other articles.

Opinions

  • The author believes that every Cloud Developer should be familiar with kubectl commands for effective Kubernetes management.
  • There is an emphasis on the importance of understanding the structure of kubectl commands for precise and functional operations.
  • The author suggests that the listed commands are indispensable for developers in their everyday work with Kubernetes.
  • By providing a range of commands from basic to advanced, the author implies that the commands listed are sufficient for most tasks a developer would encounter.
  • The author encourages further engagement with their content by inviting readers to connect on social media platforms like Twitter and LinkedIn.

The Ultimate List of Must-Know Kubernetes Commands

Photo by Growtika on Unsplash

Kubernetes is an open-source container orchestration tool that was initially developed by Google and now maintained by Cloud Native Computing Foundation. kubectl is the command line tool to manage kubernetes clusters. Whoever likes writing commands rather than using Kubernetes Dashboard will end up using kubectl. In that sense you will find quite a few commands in this article that for my point of view every Cloud Developer should know.

The structure of kubectl commands is built in following way:

kubectl [command] [Type] [Name] [flags]

[command] is basically the imperative way associated with specific verb like create/apply/get/deletethat instructs to do the special activity. [Type] refers to specific kubernetes resource pods, services, replicasets, statefulsets etc. [Name] points to the exact name of resource which is by the way case-sensitive. [flags] are extra options that give special functionalities.

Without further due, here is a whole list of commands, that I think are pretty important ones in every Cloud Developers daily work.

# get resource(s)
$ kubectl get services               
$ kubectl get pods --all-namespaces  
$ kubectl get pods -o wide           
$ kubectl get rc <rc-name>           
$ kubectl get secrets

# interaction with config
kubectl config get-contexts
kubectl config current-context
kubectl config use-context my-cluster-name           # set the default context to my-cluster-name                          
kubectl config set-cluster my-cluster-name           # set a cluster entry in the kubeconfig

# create resource(s)
$ kubectl create -f ./file.yml    
$ kubectl create -f ./dir

# interact with resources
$ kubectl label pods <pod-name> new-label=awesome                  # Add a Label
$ kubectl annotate pods <pod-name> icon-url=http://goo.gl/XXBTWq   # Add an annotation
kubectl run -i --tty busybox --image=busybox:1.28 -- sh            # Run pod as interactive shell
kubectl run nginx --image=nginx -n mynamespace                     # Start a single instance of nginx pod in the namespace of mynamespace
kubectl run nginx --image=nginx --dry-run=client -o yaml > pod.yaml
                                                                   # Generate spec for running pod nginx and write it into a file called pod.yaml
kubectl attach my-pod -i                                           # Attach to Running Container
kubectl port-forward my-pod 5000:6000                              # Listen on port 5000 on the local machine and forward to port 6000 on my-pod
kubectl exec my-pod -- ls /                                        # Run command in existing pod (1 container case)

# log resources
kubectl logs my-pod                                 # dump pod logs (stdout)
kubectl logs -l name=myLabel                        # dump pod logs, with label name=myLabel (stdout)
kubectl logs my-pod --previous                      # dump pod logs (stdout) for a previous instantiation of a container
kubectl logs my-pod -c my-container                 # dump pod container logs (stdout, multi-container case)
kubectl logs -l name=myLabel -c my-container        # dump pod logs, with label name=myLabel (stdout)
kubectl logs my-pod -c my-container --previous      # dump pod container logs (stdout, multi-container case) for a previous instantiation of a container

# apply changes to the resources
kubectl apply -f ./my-manifest.yaml            # create resource(s)
kubectl apply -f ./my1.yaml -f ./my2.yaml      # create from multiple files
kubectl apply -f ./dir                         # create resource(s) in all manifest files in dir
kubectl apply -f https://git.io/vPieo          # create resource(s) from url
kubectl create deployment nginx --image=nginx  # start a single instance of nginx

# scaling resources
kubectl scale --replicas=3 rs/foo                                 # Scale a replicaset named 'foo' to 3
kubectl scale --replicas=3 -f foo.yaml                            # Scale a resource specified in "foo.yaml" to 3
kubectl scale --current-replicas=2 --replicas=3 deployment/mysql  # If the deployment named mysql's current size is 2, scale mysql to 3
kubectl scale --replicas=5 rc/foo rc/bar rc/baz                   # Scale multiple replication controllers 

# deleting resources
kubectl delete -f ./pod.json                                      # Delete a pod using the type and name specified in pod.json
kubectl delete pod unwanted --now                                 # Delete a pod with no grace period
kubectl delete pod,service baz foo                                # Delete pods and services with same names "baz" and "foo"
kubectl delete pods,services -l name=myLabel                      # Delete pods and services with label name=myLabkubectl -n my-ns delete pod,svc --all                             # Delete all pods and services in namespace my-ns,

# interact with the nodes and clusters
kubectl cordon my-node                                            # Mark my-node as unschedulable
kubectl drain my-node                                             # Drain my-node in preparation for maintenance
kubectl uncordon my-node                                          # Mark my-node as schedulable
kubectl top node my-node                                          # Show metrics for a given node
kubectl cluster-info                                              # Display addresses of the master and services

If you are interested in more Software Engineering topics take a look at my other articles.

Relevant Articles:

  1. Software Architecture Patterns for Front-End Development
  2. Message Queueing and Event Streaming Providers in Comparison
  3. Documentation as Code Approach in Software Development
  4. How to apply SOLID Software Design Principles to Spring Boot Application

P.S. You can connect with me on twitter or linkedin.

Software Engineering
Software Development
DevOps
Software Architecture
Cloud Computing
Recommended from ReadMedium