|
| 1 | +--- |
| 2 | +title: Obter um Shell em um Contêiner em Execução |
| 3 | +content_type: task |
| 4 | +--- |
| 5 | + |
| 6 | +<!-- overview --> |
| 7 | + |
| 8 | +Esta página mostra como usar `kubectl exec` para obter um shell em um contêiner em execução. |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +## {{% heading "prerequisites" %}} |
| 14 | + |
| 15 | + |
| 16 | +{{< include "task-tutorial-prereqs.md" >}} |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +<!-- steps --> |
| 22 | + |
| 23 | +## Obtendo um Shell em um Contêiner |
| 24 | + |
| 25 | +Neste exercício, você cria um Pod que possui um contêiner. O contêiner |
| 26 | +executa a imagem do nginx. Aqui está o arquivo de configuração para o Pod: |
| 27 | + |
| 28 | +{{% code_sample file="application/shell-demo.yaml" %}} |
| 29 | + |
| 30 | +Crie o Pod: |
| 31 | + |
| 32 | +```shell |
| 33 | +kubectl apply -f https://k8s.io/examples/application/shell-demo.yaml |
| 34 | +``` |
| 35 | + |
| 36 | +Verifique se o contêiner está em execução: |
| 37 | + |
| 38 | +```shell |
| 39 | +kubectl get pod shell-demo |
| 40 | +``` |
| 41 | + |
| 42 | +Obtenha um shell no contêiner em execução: |
| 43 | + |
| 44 | +```shell |
| 45 | +kubectl exec --stdin --tty shell-demo -- /bin/bash |
| 46 | +``` |
| 47 | + |
| 48 | +{{< note >}} |
| 49 | +O duplo traço (`--`) separa os argumentos que você deseja passar para o comando dos argumentos do `kubectl`. |
| 50 | +{{< /note >}} |
| 51 | + |
| 52 | +No seu shell, liste o diretório raiz: |
| 53 | + |
| 54 | +```shell |
| 55 | +# Execute isso dentro do contêiner |
| 56 | +ls / |
| 57 | +``` |
| 58 | + |
| 59 | +No seu shell, experimente outros comandos. Aqui estão alguns exemplos: |
| 60 | + |
| 61 | +```shell |
| 62 | +# Você pode executar esses comandos de exemplo dentro do contêiner |
| 63 | +ls / |
| 64 | +cat /proc/mounts |
| 65 | +cat /proc/1/maps |
| 66 | +apt-get update |
| 67 | +apt-get install -y tcpdump |
| 68 | +tcpdump |
| 69 | +apt-get install -y lsof |
| 70 | +lsof |
| 71 | +apt-get install -y procps |
| 72 | +ps aux |
| 73 | +ps aux | grep nginx |
| 74 | +``` |
| 75 | + |
| 76 | +## Escrevendo a página raiz para o nginx |
| 77 | + |
| 78 | +Veja novamente o arquivo de configuração do seu Pod. O Pod |
| 79 | +possui um volume `emptyDir`, e o contêiner monta esse volume |
| 80 | +em `/usr/share/nginx/html`. |
| 81 | + |
| 82 | +No seu shell, crie um arquivo `index.html` no diretório `/usr/share/nginx/html`: |
| 83 | + |
| 84 | +```shell |
| 85 | +# Execute isso dentro do contêiner |
| 86 | +echo 'Hello shell demo' > /usr/share/nginx/html/index.html |
| 87 | +``` |
| 88 | + |
| 89 | +No seu shell, envie uma solicitação GET para o servidor nginx: |
| 90 | + |
| 91 | +```shell |
| 92 | +# Execute isso no shell dentro do seu contêiner |
| 93 | +apt-get update |
| 94 | +apt-get install curl |
| 95 | +curl http://localhost/ |
| 96 | +``` |
| 97 | + |
| 98 | +A saída exibe o texto que você escreveu no arquivo `index.html`: |
| 99 | + |
| 100 | +``` |
| 101 | +Hello shell demo |
| 102 | +``` |
| 103 | + |
| 104 | +Quando terminar de usar o shell, digite `exit`. |
| 105 | + |
| 106 | +```shell |
| 107 | +exit # Para sair do shell no contêiner |
| 108 | +``` |
| 109 | + |
| 110 | +## Executando comandos individuais em um contêiner |
| 111 | + |
| 112 | +Em uma janela de comando comum, fora do seu shell, liste as variáveis de ambiente no contêiner em execução: |
| 113 | + |
| 114 | +```shell |
| 115 | +kubectl exec shell-demo -- env |
| 116 | +``` |
| 117 | + |
| 118 | +Experimente executar outros comandos. Aqui estão alguns exemplos: |
| 119 | + |
| 120 | +```shell |
| 121 | +kubectl exec shell-demo -- ps aux |
| 122 | +kubectl exec shell-demo -- ls / |
| 123 | +kubectl exec shell-demo -- cat /proc/1/mounts |
| 124 | +``` |
| 125 | + |
| 126 | + |
| 127 | + |
| 128 | +<!-- discussion --> |
| 129 | + |
| 130 | +## Abrindo um shell quando um Pod tem mais de um contêiner |
| 131 | + |
| 132 | +Se um Pod tiver mais de um contêiner, use `--container` ou `-c` para |
| 133 | +especificar um contêiner no comando `kubectl exec`. Por exemplo, |
| 134 | +suponha que você tenha um Pod chamado `my-pod`, e esse Pod tenha dois contêineres |
| 135 | +chamados _main-app_ e _helper-app_. O seguinte comando abriria um |
| 136 | +shell no contêiner _main-app_. |
| 137 | + |
| 138 | +```shell |
| 139 | +kubectl exec -i -t my-pod --container main-app -- /bin/bash |
| 140 | +``` |
| 141 | + |
| 142 | +{{< note >}} |
| 143 | +As opções curtas `-i` e `-t` são equivalentes às opções longas `--stdin` e `--tty` |
| 144 | +{{< /note >}} |
| 145 | + |
| 146 | + |
| 147 | +## {{% heading "whatsnext" %}} |
| 148 | + |
| 149 | + |
| 150 | +* Leia mais sobre [`kubectl exec`](/docs/reference/generated/kubectl/kubectl-commands/#exec) |
0 commit comments