Resumo Kubernetes

Configurar cluster Configurar kubectl para acessar um Azure AKS az aks get-credentials -g ${GROUP} -n ${CLUSTER} Ver configurações do kubectl kubectl config view Adicionando um novo cluster ao kubectl # Add a user/principal that will be used when connecting to the cluster kubectl config set-credentials kubeuser/foo.com --username=kubeuser --password=kubepassword # Point to a cluster kubectl config set-cluster foo.com --insecure-skip-tls-verify=true --server=https://foo.com # This context points to the cluster with a specific user kubectl config set-context default/foo.com/kubeuser --user=kubeuser/foo.com --namespace=default --cluster=foo.com # Use this specific context kubectl config use-context default/foo.com/kubeuser Trocando de cluster # Mostrando os clusters configurados no ~/.kube kubectl config get-contexts # Mostrando o contexto atual kubectl config current-context # Trocando de contexto kubectl config use-context CONTEXT_NAME Namespaces Criando um namespace kubectl create namespace ${NAMESPACE} Listando os namespaces existentes: kubectl get namespaces Trocando o namespace corrente kubectl config set-context --current --namespace=${NAMESPACE} Pods Procurar (por label) kubectl get pods -l app=MY-APP POD_NAME=$(kubectl get pods -o=jsonpath='{.items[?(@.metadata.labels.app=="MY-APP")].metadata.name}') Describe (ver eventos) kubectl describe pod $POD_NAME Logs (da aplicação) kubectl logs $POD_NAME Mostrar os nodes de execução dos pods kubectl get pods -o=wide # Filtrar pelo nome do node kubectl get pods --field-selector spec.nodeName=$NODE_NAME Terminal Abrir terminal num pod kubectl exec --stdin --tty $POD_NAME -- /bin/bash Se estiver usando um terminal Git-bash ou MinGW, colocar a variável MSYS_NO_PATHCONV: MSYS_NO_PATHCONV=1 kubectl exec --stdin --tty $POD_NAME -- /bin/bash Service Acessando um serviço kubectl get svc ${SERVICE_NAME} EXTERNAL_IP=$(kubectl get svc ${SERVICE_NAME} -o=jsonpath='{.status.loadBalancer.ingress[0].ip}') curl http: ${EXTERNAL_IP}/api/v1/hello Expondo um endereço: kubectl port-forward service/${SERVICE_NAME} 9200:9200 Logs kubectl logs -p $POD --all-containers --previous=false Restart pods Sem downtime: kubectl rollout restart deployment Parando todos pods e depois reiniciando todos: kubectl scale deployment - replicas=0 kubectl scale deployment - replicas=1 Alterando uma variável associada ao pod: kubectl set env deployment LAST_START_DATE="$(date)" Selecionando um pod específico: kubectl delete pod Selecionando todos pods com label: kubectl delete pod -l "app:myapp" Performance Mostra consumo de CPU e MEMORY de pods kubectl top pod kubectl top pod POD_ID

Mar 25, 2025 - 14:13
 0
Resumo Kubernetes

Configurar cluster

Configurar kubectl para acessar um Azure AKS

az aks get-credentials -g ${GROUP} -n ${CLUSTER}

Ver configurações do kubectl

kubectl config view

Adicionando um novo cluster ao kubectl

# Add a user/principal that will be used when connecting to the cluster
kubectl config set-credentials kubeuser/foo.com --username=kubeuser --password=kubepassword

# Point to a cluster
kubectl config set-cluster foo.com --insecure-skip-tls-verify=true --server=https://foo.com

# This context points to the cluster with a specific user
kubectl config set-context default/foo.com/kubeuser --user=kubeuser/foo.com --namespace=default --cluster=foo.com

# Use this specific context
kubectl config use-context default/foo.com/kubeuser

Trocando de cluster

#  Mostrando os clusters configurados no ~/.kube
kubectl config get-contexts

# Mostrando o contexto atual
kubectl config current-context

# Trocando de contexto
kubectl config use-context CONTEXT_NAME

Namespaces

Criando um namespace

kubectl create namespace ${NAMESPACE}

Listando os namespaces existentes:

kubectl get namespaces

Trocando o namespace corrente

kubectl config set-context --current --namespace=${NAMESPACE}

Pods

Procurar (por label)

kubectl get pods -l app=MY-APP

POD_NAME=$(kubectl get pods -o=jsonpath='{.items[?(@.metadata.labels.app=="MY-APP")].metadata.name}')

Describe (ver eventos)

kubectl describe pod $POD_NAME

Logs (da aplicação)

kubectl logs $POD_NAME

Mostrar os nodes de execução dos pods

kubectl get pods -o=wide

# Filtrar pelo nome do node
kubectl get pods --field-selector spec.nodeName=$NODE_NAME

Terminal

Abrir terminal num pod

kubectl exec --stdin --tty $POD_NAME -- /bin/bash

Se estiver usando um terminal Git-bash ou MinGW, colocar a variável MSYS_NO_PATHCONV:

MSYS_NO_PATHCONV=1 kubectl exec --stdin --tty $POD_NAME -- /bin/bash

Service

Acessando um serviço

kubectl get svc ${SERVICE_NAME}

EXTERNAL_IP=$(kubectl get svc ${SERVICE_NAME} -o=jsonpath='{.status.loadBalancer.ingress[0].ip}')

curl http: ${EXTERNAL_IP}/api/v1/hello

Expondo um endereço:

kubectl port-forward service/${SERVICE_NAME} 9200:9200

Logs

kubectl logs -p $POD --all-containers --previous=false

Restart pods

Sem downtime:

kubectl rollout restart deployment 

Parando todos pods e depois reiniciando todos:

kubectl scale deployment  - replicas=0
kubectl scale deployment  - replicas=1

Alterando uma variável associada ao pod:

kubectl set env deployment  LAST_START_DATE="$(date)"

Selecionando um pod específico:

kubectl delete pod 

Selecionando todos pods com label:

kubectl delete pod -l "app:myapp"

Performance

Mostra consumo de CPU e MEMORY de pods

kubectl top pod
kubectl top pod POD_ID