Logo

dev-resources.site

for different kinds of informations.

Resumo Kubernetes

Published at
7/1/2024
Categories
kubernetes
k8s
kubectl
aks
Author
Eduardo Issao Ito
Categories
4 categories in total
kubernetes
open
k8s
open
kubectl
open
aks
open
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}

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 <deployment_name>

Parando todos pods e depois reiniciando todos:

kubectl scale deployment <deployment name> - replicas=0
kubectl scale deployment <deployment name> - replicas=1

Alterando uma variƔvel associada ao pod:

kubectl set env deployment <deployment name> LAST_START_DATE="$(date)"

Selecionando um pod especĆ­fico:

kubectl delete pod <pod_name>

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

Featured ones: