k8s tips and tricks

Kubernetes tips & tricks, how to change namespace for a cluster

k8s tips and tricks

Introduction

In this article, we will explore various tips and tricks for managing Kubernetes (k8s) clusters using kubectl. These commands will help you efficiently handle namespaces, pods, deployments, and custom resource definitions (CRDs).

Change the kubectl namespace while keeping the current context intact

# change namespace of kubectl while keeping current context
kubectl config set-context --current --namespace=default

List pods from default namespace

kubectl get pods

List pods from all namespaces

kubectl get pods -A # list pods from all namespaces

List pods from a particular namespace

kubectl get pods -n mynamespace

How to check logs of a pod

kubectl logs nginx-pod -n nginix-ns

How to check logs of a pod as it gets generated or watch k8s pod logs

## use -f to follow the log as it generated
kubectl logs <pod_name> -n <namespace> -f

Deleting a Kubernetes deployment using kubectl

If you don’t have proper permissions on the cluster, you will encounter an error like this. Check your permissions and try again.

kubectl --context p-cluster-fr-1 delete deployment poller-receiving -n test
Error from server (Forbidden): deployments.apps "poller-receiving" is forbidden: User "sam" cannot delete resource "deployments" in API group "apps" in the namespace "test"

Creating multiple k8s resources using kubectl from a folder with config files

# Now we make sure a deployment, ingress, and service defined in config files.
ls ./ingress/example/
# Output 
deployment.yaml  ingress.yaml  README.md  service.yaml
# Create using kubectl create command from a folder with config files
kubectl create -f ./ingress/example/
# Output 
deployment.apps/nginx created
ingress.networking.k8s.io/nginx created
service/nginx created
# delete all resources from a folder
kubectl delete -f ./ingress/example/

deployment.apps "nginx" deleted
ingress.networking.k8s.io "nginx" deleted
service "nginx" deleted

Listing custom resource definitions using kubectl

# on minikube
kubectl get crd 
NAME                                       CREATED AT
authorizationpolicies.security.istio.io    2024-06-09T08:19:05Z
destinationrules.networking.istio.io       2024-06-09T08:19:05Z
envoyfilters.networking.istio.io           2024-06-09T08:19:05Z
gateways.networking.istio.io               2024-06-09T08:19:05Z
peerauthentications.security.istio.io      2024-06-09T08:19:05Z
proxyconfigs.networking.istio.io           2024-06-09T08:19:05Z
requestauthentications.security.istio.io   2024-06-09T08:19:05Z
serviceentries.networking.istio.io         2024-06-09T08:19:05Z
sidecars.networking.istio.io               2024-06-09T08:19:05Z
telemetries.telemetry.istio.io             2024-06-09T08:19:05Z
virtualservices.networking.istio.io        2024-06-09T08:19:05Z
wasmplugins.extensions.istio.io            2024-06-09T08:19:05Z
workloadentries.networking.istio.io        2024-06-09T08:19:05Z
workloadgroups.networking.istio.io         2024-06-09T08:19:05Z

Conclusion

These commands and tips should help you manage your Kubernetes clusters more effectively. Whether you are changing namespaces, checking logs, or managing deployments, these kubectl commands are essential tools for any Kubernetes administrator.