Kubernetes Commands (kubectl)

Installation for Azure Kubernetes

1. Azure CLI

AZ_REPO=$(lsb_release -cs)    

echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" | sudo tee /etc/apt/sources.list.d/azure-cli.list    

curl -L https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add -    

sudo apt-get update && sudo apt-get install -y azure-cli

2. Login

az login

3. Subscription for access to Azure Kubernetes NON PROD

az aks get-credentials --resource-group <XXX> --name <XXX> --subscription "XXX"

4. Subscription for access to Azure Kubernetes PROD

az aks get-credentials --resource-group <XXX> --name b2b-gb-aks-prod --subscription "XXX"

5. To check for which environment it is currently set.

kubectl config get-contexts

Search for PODs

6. All DEV environment PODs that contain the string “item-service” in their names

Find pods ids:    

kubectl get pods -n dev | grep <pod-name>

Search pod configuration by idPod:    

kubectl describe pod <POD_ID> -n dev    

See pod logs by idPod:  

kubectl logs -f <POD_ID> -n dev --since=1h

7. All environment Pods

-n < environment >

8. Parameter to inform which environment

-f <POD_ID>

9. Inform the name of the POD

kubectl get pods

10. Specify the time range for log retrieval.

kubectl logs pod-name --since=1h

Commands

11. Retrieve all DEV pods containing the text “item-service” in their name:

kubectl get pods -n dev | grep item-service

12. The AWK command retrieves the first column of the obtained result. In the example below, it uses the first column, “{print $1}”. The $ is used to transform the content inside the parentheses into a variable.

kubectl logs $(kubectl get pods -n dev | grep item-service | awk '{print $1}') -n dev --since=1h

13. To read the logs using “tail” type, use the -f parameter right after the logs: To read the logs of a replicated module/app in a pod:

kubectl logs deployment/item-relay-service --all-containers=true -f -n uat

Development pods log

14. The “watch” command keeps running the command within the quotes.

kubectl logs -f $(kubectl get pods -n dev | grep item-service | awk '{print $1}') -n dev --since=1h

15. -o wide: returns some extra information

watch "kubectl get pods -n dev -o wide | grep item"

16. Changing the number of active PODs for a specific microservice. The replica parameter value represents the final number of active PODs. Entering the value zero forces the termination of all PODs.

kubectl scale deployment item-relay-service -n dev --replicas=2kubectl scale deployment item-service -n dev --replicas=0

17. To edit the Kubernetes file directly, the first KUBE_EDITOR parameter forces the use of the NANO editor.

KUBE_EDITOR="nano" kubectl edit deployment item-service -n dev

18. Run a command inside the pod interactively

kubectl exec -it item-consumer-service-54584f9567-nxs7x -n dev -- /bin/sh

19. To obtain the version of all microservices deployed in the environment you are logged into

kubectl get pods --all-namespaces -o jsonpath="{..image}" |\tr -s '[[:space:]]' '\n' |\sort |\un

20. To obtain the version of the Products-Service deployed in the environment you are logged into.

kubectl get pods -o jsonpath="{..image}" -l app=products-service |tr -s '[[:space:]]' '\n' |sort |uniq -c