DEV Community

Eduardo Issao Ito
Eduardo Issao Ito

Posted on

Resumo Kubernetes

Configurar cluster

Configurar kubectl para acessar um Azure AKS

az aks get-credentials -g ${GROUP} -n ${CLUSTER} 
Enter fullscreen mode Exit fullscreen mode

Ver configurações do kubectl

kubectl config view 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Namespaces

Criando um namespace

kubectl create namespace ${NAMESPACE} 
Enter fullscreen mode Exit fullscreen mode

Listando os namespaces existentes:

kubectl get namespaces 
Enter fullscreen mode Exit fullscreen mode

Trocando o namespace corrente

kubectl config set-context --current --namespace=${NAMESPACE} 
Enter fullscreen mode Exit fullscreen mode

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}') 
Enter fullscreen mode Exit fullscreen mode

Describe (ver eventos)

kubectl describe pod $POD_NAME 
Enter fullscreen mode Exit fullscreen mode

Logs (da aplicação)

kubectl logs $POD_NAME 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Terminal

Abrir terminal num pod

kubectl exec --stdin --tty $POD_NAME -- /bin/bash 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Expondo um endereço:

kubectl port-forward service/${SERVICE_NAME} 9200:9200 
Enter fullscreen mode Exit fullscreen mode

Logs

kubectl logs -p $POD --all-containers --previous=false 
Enter fullscreen mode Exit fullscreen mode

Restart pods

Sem downtime:

kubectl rollout restart deployment <deployment_name> 
Enter fullscreen mode Exit fullscreen mode

Parando todos pods e depois reiniciando todos:

kubectl scale deployment <deployment name> - replicas=0 kubectl scale deployment <deployment name> - replicas=1 
Enter fullscreen mode Exit fullscreen mode

Alterando uma variável associada ao pod:

kubectl set env deployment <deployment name> LAST_START_DATE="$(date)" 
Enter fullscreen mode Exit fullscreen mode

Selecionando um pod específico:

kubectl delete pod <pod_name> 
Enter fullscreen mode Exit fullscreen mode

Selecionando todos pods com label:

kubectl delete pod -l "app:myapp" 
Enter fullscreen mode Exit fullscreen mode

Performance

Mostra consumo de CPU e MEMORY de pods

kubectl top pod kubectl top pod POD_ID 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)