- Kubernetes for Developers
- Joseph Heck
- 388字
- 2021-08-27 19:04:19
Attaching to a running Pod
If your pod is up and running, and you want to run some commands from within the context of that container image, you can attach an interactive session to it. You do this via the kubectl attach command. A Pod must be active for this command to work, so if you’re trying to figure out why a Pod didn’t start properly, this command probably won’t help.
Attaching to a Pod will connect stdin into your process, and take anything from stdout and stderr and present it on the screen, so it’s more like an interactive version of the kubectl logs -f command. Whatever you specified for the container will need to take stdin in order for this to be useful. You will also need to explicitly enable the TTY in order to connect to it. If you do not, you will frequently see the following as a first line of the output:
Unable to use a TTY - container flask did not allocate one
If you had created a deployment from the nodejs example earlier using the following command:
kubectl run nodejs --image=quay.io/kubernetes-for-developers/nodejs:latest —-port=3000
You could attach to this Pod using the following command:
kubectl attach deployment/express -i -t
This will return a warning message:
Unable to use a TTY - container flask did not allocate one
If you don't see a command prompt, try pressing enter.
And thereafter, when you interact with the service, you will see stdout streamed in the Terminal window.
This is most effective if your application prints its logs to stdout and you want to watch those logs while you interact with your code, for example by using a web browser. To use a web browser to interact with your running Pod, remember to use either the kubectl proxy or kubectl port-forward commands, typically from another Terminal window, to route access from your laptop to your Pod within the cluster.
In many cases, you will be better served by using the kubectl logs command that we described earlier with the -f option. The primary difference is if you have enabled your application to react to input from stdin and you ran it with stdin and a TTY defined, then you can interact with it directly by using the kubectl attach command.
- LabVIEW虛擬儀器從入門到測控應(yīng)用130例
- Practical Ansible 2
- 人工智能超越人類
- 腦動力:C語言函數(shù)速查效率手冊
- Seven NoSQL Databases in a Week
- 圖形圖像處理(Photoshop)
- UTM(統(tǒng)一威脅管理)技術(shù)概論
- 精通Excel VBA
- 群體智能與數(shù)據(jù)挖掘
- 運動控制器與交流伺服系統(tǒng)的調(diào)試和應(yīng)用
- INSTANT Autodesk Revit 2013 Customization with .NET How-to
- 學(xué)練一本通:51單片機(jī)應(yīng)用技術(shù)
- 計算機(jī)應(yīng)用基礎(chǔ)實訓(xùn)(職業(yè)模塊)
- 電腦故障排除與維護(hù)終極技巧金典
- 工業(yè)機(jī)器人應(yīng)用系統(tǒng)三維建模