question1
sidebar_position: 1 title: "Questão 1 - Gerenciamento de Contextos kubectl" sidebar_label: "Questão 1" description: "Questão prática 1 do exame CKA: gerenciamento de contextos kubectl, listagem de contextos disponíveis e obtenção do contexto atual com e sem kubectl." keywords:
- questão cka
- exame prático
- kubernetes hands-on
- resolução passo a passo
- kubectl contexts
- gerenciamento contextos
- current-context
- configuração kubectl
- kubeconfig
- multiple clusters slug: /kubernetes/cka/solved-questions/question1 image: /img/social-cards/kubernetes.png
Question 1 | Contexts
You have access to multiple clusters from your main terminal through kubectl contexts. Write all those context names into /opt/course/1/contexts.
Next write a command to display the current context into /opt/course/1/context_default_kubectl.sh, the command should use kubectl.
Finally write a second command doing the same thing into /opt/course/1/context_default_no_kubectl.sh, but without the use of kubectl.
k config get-contexts -o name > /opt/course/1/contexts
#Usando o json path
# Só para fazer um check
k config view -o yaml
# Irá trazer os resultados todos em uma linha separados por espaços
k config view -o jsonpath="{.contexts[*].name}"
# Vamos mudar espaços por nova linha e colocar escrever no arquivo
k config view -o jsonpath="{.contexts[*].name}" | tr " " "\n" > /opt/course/1/contexts
# Pegando o current-context e colocando no arquivo
echo 'kubectl config current-context' > /opt/course/1/context_default_kubectl.sh
sh /opt/course/1/context_default_kubectl.sh
# O segundo método sem usar o kubectl
# Poderíamos filtrar por current-context e imprimir somente a segunda parte
cat ~/.kube/config | grep current | awk '{print $2}'
# ou poderíamos deletar a primeira
cat ~/.kube/config | grep current | sed -e "s/current-context: //"
#Escreva o comando em /opt/course/1/context_default_no_kubectl.sh
sh /opt/course/1/context_default_no_kubectl.sh