Skip to content

Instantly share code, notes, and snippets.

@bashizip
Forked from wesleyegberto/Kubernetes Utils
Created April 29, 2023 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bashizip/fa9ef92105c17d1d8731afe861a7a4e7 to your computer and use it in GitHub Desktop.
Save bashizip/fa9ef92105c17d1d8731afe861a7a4e7 to your computer and use it in GitHub Desktop.
## Cluster
### Cluster info
kubectl cluster-info
kubectl get nodes
kubectl describe node
### Listar os eventos
kubectl get events
### Finalizar corretamente os pods no node e tornar-lo não-agendável
kubectl drain <node_name>
### Tornar o node não-agendável
kubectl cordon <node_name>
### Tornar o node agendável
kubectl uncordon <node_name>
### Cotas
gcloud compute project-info describe --project myproject
gcloud compute regions describe example-region
### Criar cluster
gcloud container clusters create <name> --num-nodes <n> --machine-type <type> --zone <zone_name>
### Listar as operações
gcloud container operations list
### Mudar o Projeto default
gcloud config set project <project_name>
### Auth Kubectl
gcloud container clusters get-credentials <name>
### Listar os backend-services
gcloud compute backend-services list
### Reservar IP estatico
gcloud compute addresses create <nome> --global
### Alterar Cluster que sera usado
gcloud config set container/cluster <cluster-name>
## Utils
* Show resources usage by nodes
`kubectl get nodes --no-headers | awk '{print $1}' | xargs -I {} sh -c 'echo {}; kubectl describe node {} | grep Allocated -A 5 | grep -ve Event -ve Allocated -ve percent -ve -- ; echo'`
* List pods filtering by label
`kubectl get pod -l app=my-app -o jsonpath="{.items[0].metadata.name}"`
* Delete all evicted pods
`kubectl get pods --show-all=true --all-namespaces | grep Evicted`
`kubectl get pods --show-all=true | grep Evicted | awk '{print $1}' | xargs kubectl delete pod`
* Show resources usage by namespaces and pods
`kubectl get po --all-namespaces -o=jsonpath="{range .items[*]}{.metadata.namespace}:{.metadata.name}{'\n'}{range .spec.containers[*]} {.name} - {.resources.requests.cpu} - {.resources.requests.memory}{'\n'}{end}{'\n'}{end}"`
* See last status from last pod execution
`kubectl get pod -o go-template='{{range.status.containerStatuses}}{{"Container Name: "}}{{.name}}{{"\r\nLastState: "}}{{.lastState}}{{end}}' mypod`
* See pods IPs
`kubectl get pods --selector=app=service_test_pod -o jsonpath='{.items[*].status.podIP}'`
* Show image by pod
`kubectl get pods --all-namespaces -o=jsonpath='{range .items[*]}{"\n"}{.metadata.name}{":\t"}{range .spec.containers[*]}{.image}{", "}{end}{end}' | sort`
### Atualizar a imagem de um Deployment
kubectl set image deployments/<deployment_name> <image_tag>
### Ver o status de atualização de deployment com estratégia RollingUpdate
kubectl rollout status deployments/<deployment_name>
### Desfazer uma atualização de deployment com estrategia RollingUpdate
kubectl rollout undo deployments/<deployment_name>
### Detalhar um recurso por tipo
kubectl describe [pods | services] [-o yaml] <name>
### Setar um novo Label num Pod
kubectl label pod <pod_name> <label_name>=<label_value>
### Listar os Services
kubectl get services [-l <label_key>=<label_value>
### Deletar Services/Pods filtrando por Label
kubectl delete [services | pods] [-l <label_key>=<label_value>
### Listar os pods
kubectl get pods [-o wide] [-l <label_key>=<label_value>
* Opções:
* -o wide: mais detalhes
### Acessar Services fechados
* Executar proxy: `kubectl proxy`
* Formato da URL: http://127.0.0.1:8001/api/v1/namespaces/<NOME_NAMESPACE>/services/<NOME_SERVICO>:<PORTA_OU_APELIDO>/proxy
### Print os logs de um pod
kubectl logs <pod_name>
### Executa um comando em um container
kubectl exec [-ti] <pod_name> <comando>
Ex:
* kubectl exec <pod_name> env
* kubectl exec -ti <pod_name> bash
### Criar um Service e expor
kubectl expose deployment/<deployment_name> --type="<type>" --port <port>
* Types: ClusterIP, NodePort, LoadBalance e ExternalName
* Load balancer interno ao GCP:
`annotations:
cloud.google.com/load-balancer-type: "Internal"`
### Formato da TAG para GCloud
docker tag [IMAGE] [HOSTNAME]/[YOUR-PROJECT-ID]/[IMAGE]
### Criar um Kubernetes Deployment/Pod (responsável por rodar e atualizar instancias dos apps)
kubectl run <deployment_name> --image=<image_tag> --port=<port> [--env="<chave>=<valor>"]
### Scaling
kubectl scale deployments/<deployments_name> --replicas=<num replicas>
### Atualizar um Pod (Replication Controller)
kubectl rolling-update NAME [<NEW_NAME>] --image=IMAGE:TAG
### Desfazer atualização (Replication Controller)
kubectl rolling-back NAME --rollback
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment