2
2
title : 调试运行中的 Pod
3
3
content_type : task
4
4
---
5
-
6
5
<!--
7
6
reviewers:
8
7
- verb
@@ -429,16 +428,6 @@ status:
429
428
## Examining pod logs {#examine-pod-logs}
430
429
431
430
First, look at the logs of the affected container:
432
-
433
- ` ` ` shell
434
- kubectl logs ${POD_NAME} ${CONTAINER_NAME}
435
- ```
436
-
437
- If your container has previously crashed, you can access the previous container's crash log with:
438
-
439
- ``` shell
440
- kubectl logs --previous ${POD_NAME} ${CONTAINER_NAME}
441
- ```
442
431
-->
443
432
## 检查 Pod 的日志 {#examine-pod-logs}
444
433
@@ -448,6 +437,9 @@ kubectl logs --previous ${POD_NAME} ${CONTAINER_NAME}
448
437
kubectl logs ${POD_NAME} ${CONTAINER_NAME}
449
438
```
450
439
440
+ <!--
441
+ If your container has previously crashed, you can access the previous container's crash log with:
442
+ -->
451
443
如果你的容器之前崩溃过,你可以通过下面命令访问之前容器的崩溃日志:
452
444
453
445
``` shell
@@ -461,28 +453,6 @@ If the {{< glossary_tooltip text="container image" term_id="image" >}} includes
461
453
debugging utilities, as is the case with images built from Linux and Windows OS
462
454
base images, you can run commands inside a specific container with
463
455
`kubectl exec`:
464
-
465
- ```shell
466
- kubectl exec ${POD_NAME} -c ${CONTAINER_NAME} -- ${CMD} ${ARG1} ${ARG2} ... ${ARGN}
467
- ```
468
-
469
- `-c ${CONTAINER_NAME}` is optional. You can omit it for Pods that only contain a single container.
470
-
471
- As an example, to look at the logs from a running Cassandra pod, you might run
472
-
473
- ```shell
474
- kubectl exec cassandra -- cat /var/log/cassandra/system.log
475
- ```
476
-
477
- You can run a shell that's connected to your terminal using the `-i` and `-t`
478
- arguments to `kubectl exec`, for example:
479
-
480
- ```shell
481
- kubectl exec -it cassandra -- sh
482
- ```
483
-
484
- For more details, see [Get a Shell to a Running Container](
485
- /docs/tasks/debug/debug-application/get-shell-running-container/).
486
456
-->
487
457
## 使用容器 exec 进行调试 {#container-exec}
488
458
@@ -495,21 +465,35 @@ kubectl exec ${POD_NAME} -c ${CONTAINER_NAME} -- ${CMD} ${ARG1} ${ARG2} ... ${AR
495
465
```
496
466
497
467
{{< note >}}
468
+ <!--
469
+ `-c ${CONTAINER_NAME}` is optional. You can omit it for Pods that only contain a single container.
470
+ -->
498
471
` -c ${CONTAINER_NAME} ` 是可选择的。如果 Pod 中仅包含一个容器,就可以忽略它。
499
472
{{< /note >}}
500
473
474
+ <!--
475
+ As an example, to look at the logs from a running Cassandra pod, you might run
476
+ -->
501
477
例如,要查看正在运行的 Cassandra Pod 中的日志,可以运行:
502
478
503
479
``` shell
504
480
kubectl exec cassandra -- cat /var/log/cassandra/system.log
505
481
```
506
482
483
+ <!--
484
+ You can run a shell that's connected to your terminal using the `-i` and `-t`
485
+ arguments to `kubectl exec`, for example:
486
+ -->
507
487
你可以在 ` kubectl exec ` 命令后面加上 ` -i ` 和 ` -t ` 来运行一个连接到你的终端的 Shell,比如:
508
488
509
489
``` shell
510
490
kubectl exec -it cassandra -- sh
511
491
```
512
492
493
+ <!--
494
+ For more details, see [Get a Shell to a Running Container](
495
+ /docs/tasks/debug/debug-application/get-shell-running-container/).
496
+ -->
513
497
若要了解更多内容,可查看[ 获取正在运行容器的 Shell] ( /zh-cn/docs/tasks/debug/debug-application/get-shell-running-container/ ) 。
514
498
515
499
<!--
@@ -589,25 +573,27 @@ parameter targets the process namespace of another container. It's necessary
589
573
here because `kubectl run` does not enable [process namespace sharing](
590
574
/docs/tasks/configure-pod-container/share-process-namespace/) in the pod it
591
575
creates.
576
+ -->
577
+ 此命令添加一个新的 busybox 容器并将其挂接到该容器。` --target ` 参数指定另一个容器的进程命名空间。
578
+ 这个指定进程命名空间的操作是必需的,因为 ` kubectl run ` 不能在它创建的 Pod
579
+ 中启用[ 共享进程命名空间] ( /zh-cn/docs/tasks/configure-pod-container/share-process-namespace/ ) 。
592
580
581
+ {{< note >}}
582
+ <!--
593
583
The `--target` parameter must be supported by the {{< glossary_tooltip
594
584
text="Container Runtime" term_id="container-runtime" >}}. When not supported,
595
585
the Ephemeral Container may not be started, or it may be started with an
596
586
isolated process namespace so that `ps` does not reveal processes in other
597
587
containers.
598
-
599
- You can view the state of the newly created ephemeral container using `kubectl describe`:
600
588
-->
601
- 此命令添加一个新的 busybox 容器并将其挂接到该容器。` --target ` 参数指定另一个容器的进程命名空间。
602
- 这个指定进程命名空间的操作是必需的,因为 ` kubectl run ` 不能在它创建的 Pod
603
- 中启用[ 共享进程命名空间] ( /zh-cn/docs/tasks/configure-pod-container/share-process-namespace/ ) 。
604
-
605
- {{< note >}}
606
589
{{< glossary_tooltip text="容器运行时" term_id="container-runtime" >}}必须支持 ` --target ` 参数。
607
590
如果不支持,则临时容器可能不会启动,或者可能使用隔离的进程命名空间启动,
608
591
导致 ` ps ` 不显示其他容器内的进程。
609
592
{{< /note >}}
610
593
594
+ <!--
595
+ You can view the state of the newly created ephemeral container using `kubectl describe`:
596
+ -->
611
597
你可以使用 ` kubectl describe ` 查看新创建的临时容器的状态:
612
598
613
599
``` shell
@@ -701,6 +687,7 @@ If you don't see a command prompt, try pressing enter.
701
687
root@myapp-debug:/#
702
688
```
703
689
690
+ {{< note >}}
704
691
<!--
705
692
* `kubectl debug` automatically generates a container name if you don't choose
706
693
one using the `--container` flag.
@@ -712,7 +699,6 @@ root@myapp-debug:/#
712
699
works, see [Share Process Namespace between Containers in a Pod](
713
700
/docs/tasks/configure-pod-container/share-process-namespace/).
714
701
-->
715
- {{< note >}}
716
702
* 如果你没有使用 ` --container ` 指定新的容器名,` kubectl debug ` 会自动生成的。
717
703
* 默认情况下,` -i ` 标志使 ` kubectl debug ` 附加到新容器上。
718
704
你可以通过指定 ` --attach=false ` 来防止这种情况。
@@ -791,8 +777,9 @@ If you don't see a command prompt, try pressing enter.
791
777
Now you have an interactive shell that you can use to perform tasks like
792
778
checking filesystem paths or running the container command manually.
793
779
-->
794
- 现在你有了一个可以执行类似检查文件系统路径或者手动运行容器命令的交互式 shell。
780
+ 现在你有了一个可以执行类似检查文件系统路径或者手动运行容器命令的交互式 shell。
795
781
782
+ {{< note >}}
796
783
<!--
797
784
* To change the command of a specific container you must
798
785
specify its name using `--container` or `kubectl debug` will instead
@@ -801,7 +788,6 @@ checking filesystem paths or running the container command manually.
801
788
You can prevent this by specifying `--attach=false`. If your session becomes
802
789
disconnected you can reattach using `kubectl attach`.
803
790
-->
804
- {{< note >}}
805
791
* 要更改指定容器的命令,你必须用 ` --container ` 命令指定容器的名字,
806
792
否则 ` kubectl debug ` 将建立一个新的容器运行你指定的命令。
807
793
* 默认情况下,标志 ` -i ` 使 ` kubectl debug ` 附加到容器。
@@ -833,7 +819,7 @@ As an example, create a Pod using `kubectl run`:
833
819
834
820
下面的例子,用 ` kubectl run ` 创建一个 Pod:
835
821
836
- ```
822
+ ``` shell
837
823
kubectl run myapp --image=busybox:1.28 --restart=Never -- sleep 1d
838
824
```
839
825
@@ -843,7 +829,7 @@ to `ubuntu`:
843
829
-->
844
830
现在可以使用 ` kubectl debug ` 创建一个拷贝并将其容器镜像更改为 ` ubuntu ` :
845
831
846
- ```
832
+ ``` shell
847
833
kubectl debug myapp --copy-to=myapp-debug --set-image=* =ubuntu
848
834
```
849
835
@@ -892,7 +878,7 @@ When creating a debugging session on a node, keep in mind that:
892
878
* The container runs in the host IPC, Network, and PID namespaces, although
893
879
the pod isn't privileged, so reading some process information may fail,
894
880
and `chroot /host` may fail.
895
- * If you need a privileged pod, create it manually.
881
+ * If you need a privileged pod, create it manually or use the `--profile=sysadmin` flag .
896
882
897
883
Don't forget to clean up the debugging Pod when you're finished with it:
898
884
-->
@@ -901,10 +887,131 @@ Don't forget to clean up the debugging Pod when you're finished with it:
901
887
* 节点的根文件系统会被挂载在 ` /host ` 。
902
888
* 新的调试容器运行在主机 IPC 名字空间、主机网络名字空间以及主机 PID 名字空间内,
903
889
Pod 没有特权,因此读取某些进程信息可能会失败,并且 ` chroot /host ` 也可能会失败。
904
- * 如果你需要一个特权 Pod,需要手动创建 。
890
+ * 如果你需要一个特权 Pod,需要手动创建或使用 ` --profile=sysadmin ` 标志 。
905
891
906
892
当你完成节点调试时,不要忘记清理调试 Pod:
907
893
908
894
``` shell
909
895
kubectl delete pod node-debugger-mynode-pdx84
910
896
```
897
+
898
+ <!--
899
+ ## Debugging Profiles {#debugging-profiles}
900
+
901
+ When using `kubectl debug` to debug a node via a debugging Pod, a Pod via an ephemeral container,
902
+ or a copied Pod, you can apply a debugging profile to them using the `--profile` flag.
903
+ By applying a profile, specific properties such as [securityContext](/docs/tasks/configure-pod-container/security-context/)
904
+ are set, allowing for adaptation to various scenarios.
905
+
906
+ The available profiles are as follows:
907
+ -->
908
+ ## 调试配置 {#debugging-profiles}
909
+
910
+ 使用 ` kubectl debug ` 通过调试 Pod 来调试节点、通过临时容器来调试 Pod 或者调试复制的 Pod 时,
911
+ 你可以使用 ` --profile ` 标志为其应用调试配置(Debugging Profile)。通过应用配置,可以设置特定的属性(如
912
+ [ securityContext] ( /zh-cn/docs/tasks/configure-pod-container/security-context/ ) ),
913
+ 以适应各种场景。
914
+
915
+ 可用的配置如下:
916
+
917
+ <!--
918
+ | Profile | Description |
919
+ | ------------ | --------------------------------------------------------------- |
920
+ | legacy | A set of properties backwards compatibility with 1.22 behavior |
921
+ | general | A reasonable set of generic properties for each debugging journey |
922
+ | baseline | A set of properties compatible with [PodSecurityStandard baseline policy](/docs/concepts/security/pod-security-standards/#baseline) |
923
+ | restricted | A set of properties compatible with [PodSecurityStandard restricted policy](/docs/concepts/security/pod-security-standards/#restricted) |
924
+ | netadmin | A set of properties including Network Administrator privileges |
925
+ | sysadmin | A set of properties including System Administrator (root) privileges |
926
+ -->
927
+ | 配置 | 描述 |
928
+ | ----------- | --------------------------------------------------------- |
929
+ | legacy | 一组与 1.22 行为向后兼容的属性 |
930
+ | general | 一组对大多数调试过程而言均合理的通用属性 |
931
+ | baseline | 一组与 [ PodSecurityStandard Baseline 策略] ( /zh-cn/docs/concepts/security/pod-security-standards/#baseline ) 兼容的属性 |
932
+ | restricted | 一组与 [ PodSecurityStandard Restricted 策略] ( /zh-cn/docs/concepts/security/pod-security-standards/#restricted ) 兼容的属性 |
933
+ | netadmin | 一组包含网络管理员特权的属性 |
934
+ | sysadmin | 一组包含系统管理员(root)特权的属性 |
935
+
936
+
937
+ {{< note >}}
938
+ <!--
939
+ If you don't specify `--profile`, the `legacy` profile is used by default, but it is planned to be deprecated in the near future.
940
+ So it is recommended to use other profiles such as `general`.
941
+ -->
942
+ 如果你不指定 ` --profile ` ,` legacy ` 配置被默认使用,但此配置计划在不久的将来弃用。
943
+ 因此建议使用 ` general ` 等其他配置。
944
+ {{< /note >}}
945
+
946
+ <!--
947
+ Assume that you create a Pod and debug it.
948
+ First, create a Pod named `myapp` as an example:
949
+ -->
950
+ 假设你要创建一个 Pod 并进行调试。
951
+ 先创建一个名为 ` myapp ` 的 Pod 作为示例:
952
+
953
+ ``` shell
954
+ kubectl run myapp --image=busybox:1.28 --restart=Never -- sleep 1d
955
+ ```
956
+
957
+ <!--
958
+ Then, debug the Pod using an ephemeral container.
959
+ If the ephemeral container needs to have privilege, you can use the `sysadmin` profile:
960
+ -->
961
+ 然后,使用临时容器调试 Pod。
962
+ 如果临时容器需要具有特权,你可以使用 ` sysadmin ` 配置:
963
+
964
+ ``` shell
965
+ kubectl debug -it myapp --image=busybox:1.28 --target=myapp --profile=sysadmin
966
+ ```
967
+
968
+ ```
969
+ Targeting container "myapp". If you don't see processes from this container it may be because the container runtime doesn't support this feature.
970
+ Defaulting debug container name to debugger-6kg4x.
971
+ If you don't see a command prompt, try pressing enter.
972
+ / #
973
+ ```
974
+
975
+ <!--
976
+ Check the capabilities of the ephemeral container process by running the following command inside the container:
977
+ -->
978
+ 通过在容器内运行以下命令检查临时容器进程的权能:
979
+
980
+ ``` shell
981
+ / # grep Cap /proc/$$/status
982
+ ```
983
+
984
+ ```
985
+ ...
986
+ CapPrm: 000001ffffffffff
987
+ CapEff: 000001ffffffffff
988
+ ...
989
+ ```
990
+
991
+ <!--
992
+ This means the container process is granted full capabilities as a privileged container by applying `sysadmin` profile.
993
+ See more details about [capabilities](/docs/tasks/configure-pod-container/security-context/#set-capabilities-for-a-container).
994
+
995
+ You can also check that the ephemeral container was created as a privileged container:
996
+ -->
997
+ 这意味着通过应用 ` sysadmin ` 配置,容器进程被授予了作为特权容器的全部权能。
998
+ 更多细节参见[ 权能] ( /zh-cn/docs/tasks/configure-pod-container/security-context/#set-capabilities-for-a-container ) 。
999
+
1000
+ 你还可以检查临时容器是否被创建为特权容器:
1001
+
1002
+ ``` shell
1003
+ kubectl get pod myapp -o jsonpath=' {.spec.ephemeralContainers[0].securityContext}'
1004
+ ```
1005
+
1006
+ ```
1007
+ {"privileged":true}
1008
+ ```
1009
+
1010
+ <!--
1011
+ Clean up the Pod when you're finished with it:
1012
+ -->
1013
+ 你在完成上述操作后,可运行以下命令清理 Pod:
1014
+
1015
+ ``` shell
1016
+ kubectl delete pod myapp
1017
+ ```
0 commit comments