Posted on Leave a comment

kubectl commands cheat sheet for daily kubernetes administration

This is a cheat sheet for basic k8s commands that one k8s administrator can use daily.

Apply a YAML definition on k8s-cluster:

kubectl apply -f pod.yaml

Apply a definition on specific namespace:

Kubectl apply -f kati.yml -n namespacename

Create a new namespace:

kubectl create ns namespacename

Set k8s cluster context:

kubectl config use-context geralexgr-aks

Get k8s cluster context:

kubectl config view

Create a deployment:

kubectl create deployment new-app --image=hello-world:latest

Delete a deployment:

kubectl delete deployment nginx -n nginx

Describe a service:

Kubectl describe service/myservice

Get logs for pod:

Kubectl logs pod-123

Get cluster namespaces:

kubectl get namespace

Set active namespace:

kubectl config set-context --current --namespace nginx

Delete a pod:

kubectl delete pod nginx-pod

Scale a deployment:

kubectl scale --replicas=5 deployment/deploymentname

Get pods with labels:

kubectl get pods --show-labels

Get cluster scoped resources:

kubectl api-resources
Posted on Leave a comment

Install and configure kubernetes dashboard for Docker Desktop local cluster

Kubernetes dashboard is a helpful UI application that presents all your resources inside your k8s cluster. As most people prefer GUI instead of single commands, this tool can make your k8s administration experience better.

When you install docker desktop on your local or development machines, you can select to also include a k8s installation with it. You can locate all your Kubernetes settings using the Docker Desktop UI.

The local cluster is composed of only one node, the computer itself.

In order to install dashboard first run the below kubectl apply command:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.5.0/aio/deploy/recommended.yaml

Then you will need to run kubectl proxy

Then open GUI Dashboard.

Kubernetes Dashboard

The below dialog will appear.

We will examine the Token example.

Create and save the below definition as s.yml. Then apply this configuration with kubectl apply -f s.yml

apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin-user
  namespace: kubernetes-dashboard

Create and save the below definition as r.yml. Then apply this configuration with kubectl apply -f r.yml

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: admin-user
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: admin-user
  namespace: kubernetes-dashboard

Then run the below command:

kubectl -n kubernetes-dashboard get secret $(kubectl -n kubernetes-dashboard get sa/admin-user -o jsonpath="{.secrets[0].name}") -o go-template="{{.data.token | base64decode}}"

The output will be your Token.

Paste the Token on the previous link, and then you will have a working dashboard for your local cluster.

You can also skip the Token procedure. Simply run the below command:

kubectl patch deployment kubernetes-dashboard -n kubernetes-dashboard --type 'json' -p '[{"op": "add", "path": "/spec/template/spec/containers/0/args/-", "value": "--enable-skip-login"}]'

Then you will see a skip button near the sign in

Kubernetes dashboard:

Deploy and Access the Kubernetes Dashboard | Kubernetes

Token procedure:

dashboard/creating-sample-user.md at master · kubernetes/dashboard (github.com)