Kubernetes 101 - CLI kubectl

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Although I'm not a DevOps guy, I wanted to learn more about this engineering. It's an area I always semi-understood but never took the time to explore beyond that. Kubernetes was available to me as an engineer, and it worked without knowing why half ...
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Yesterday we set up our Kubernetes dashboard and used something called kubectl. This is a command line interface for communicating with the Kubernetes control plane.
In the article, we didn't go into detail on how to install and use it, so that this article will help you with that.
We can run either of the following commands to install the tool.
# MacOS (Homebrew)
brew install kubectl
# MacOs (Curl - Intel)
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/amd64/kubectl"=
# MacOs (Curl - Silicon)
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/darwin/arm64/kubectl"
# Windows
curl.exe -LO "https://dl.k8s.io/release/v1.25.0/bin/windows/amd64/kubectl.exe"
To verify your installation is correct, run the following command.
kubectl cluster-info
It outputs if the cluster is running and you're connected to it.
Before diving into the commands, let's look at the basic command syntax for kubectl.
A typical command is structured like this:
kubectl [command] [type] [name] [flags]
Where the following mean:
command: Describes the operation we are executing. Examples are create, get, and delete.type: Describes the type of resource we are trying to interact with, examples being pods, namespaces, and deployments.name: Describes a particular name of a resource. If you omit this, your command will execute on all target resource types.flags: Describes unique options for the command. These vary per command.kubectl can do many amazing things, but let's look at some of the most valuable commands you want to know.
The first ones are around resources, as often we want to see which resources exist.
# List all services in the namespace
kubectl get services
# List all pods in all namespaces
kubectl get pods --all-namespaces
# List all pods in the current namespace with more details
kubectl get pods -o wide
# List all pods in the namespace
kubectl get pods
# Get a pod's YAML
kubectl get pod my-pod -o yaml
Then in some cases, you might want to delete resources.
# Delete a pod using the type and name specified in pod.json
kubectl delete -f ./pod.json
# Delete a pod with no grace period
kubectl delete pod unwanted --now
# Delete pods and services with the same names, "baz" and "foo"
kubectl delete pod, service baz foo
# Delete all pods and services in namespace my-ns
kubectl -n my-ns delete pod,svc --all
It's often also super handy to get some logs for a running pod. This can help you debug if it's stuck in some loop, for instance.
# dump pod logs (stdout)
kubectl logs my-pod
# dump pod logs, with label name=myLabel (stdout)
kubectl logs -l name=myLabel
# stream pod logs (stdout)
kubectl logs -f my-pod
Another helpful element is being able to set up new configurations and resources. Kubernetes uses the apply command for this, and we can apply specific yaml files like this.
# create resource(s)
kubectl apply -f ./my-manifest.yaml
# create from multiple files
kubectl apply -f ./my1.yaml -f ./my2.yaml
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter