<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>当执行kubectl exec 命令时，发生了什么？ :: Ay Docs</title>
    <link>https://ops.docs.72602.space/using/k8s/pod_exec/index.html</link>
    <description>kubectl exec 的实现原理涉及多个组件协同工作，以下是详细原理分析：&#xA;1. 整体架构流程 用户 -&gt; kubectl -&gt; API Server -&gt; Kubelet -&gt; 容器运行时 -&gt; 目标容器 2. 详细执行步骤 步骤1：kubectl 客户端处理 kubectl exec -it &lt;pod-name&gt; -- /bin/bash kubectl 解析命令参数 构造 Exec API 请求 建立与 API Server 的长连接 步骤2：API Server 处理 // API 路径示例 POST /api/v1/namespaces/{namespace}/pods/{name}/exec 认证和授权检查 验证用户是否有 exec 权限 查找目标 Pod 所在节点 将请求代理到对应节点的 Kubelet 步骤3：Kubelet 处理 // Kubelet 的 exec 处理逻辑 func (h *ExecHandler) serveExec(w http.ResponseWriter, req *http.Request) { // 获取容器信息 // 调用容器运行时接口 // 建立数据流传输 } 通过 CRI（Container Runtime Interface）调用容器运行时 创建到容器的连接 管理标准输入、输出、错误流 步骤4：容器运行时执行 // CRI 接口定义 service RuntimeService { rpc Exec(ExecRequest) returns (ExecResponse) {} } Docker: 使用 docker exec 底层机制 Containerd: 通过 task 执行命令 CRI-O: 通过 conmon 管理执行会话 3. 关键技术机制 3.1 流式传输协议 // 使用 SPDY 或 WebSocket 协议 // 支持多路复用的数据流 type StreamProtocol interface { Stream(stdin io.Reader, stdout, stderr io.Writer) error } 3.2 终端处理（TTY） // 伪终端配置 type ExecOptions struct { Stdin io.Reader Stdout io.Writer Stderr io.Writer TTY bool ptyMaster *os.File } 3.3 会话管理 // ExecSession 管理执行会话 type ExecSession struct { id string stdinPipe io.WriteCloser stdoutPipe io.ReadCloser stderrPipe io.ReadCloser done chan struct{} } 4. 网络通信流程 客户端 (kubectl) ↓ HTTPS with SPDY/WebSocket API Server ↓ 代理连接 Kubelet (节点) ↓ CRI gRPC 容器运行时 ↓ 容器命名空间 目标容器进程 5. 安全机制 5.1 认证授权 # RBAC 配置示例 apiVersion: rbac.authorization.k8s.io/v1 kind: ClusterRole metadata: name: pod-exec rules: - apiGroups: [&#34;&#34;] resources: [&#34;pods/exec&#34;] verbs: [&#34;create&#34;] 5.2 安全上下文 // 安全配置 securityContext := &amp;v1.SecurityContext{ RunAsUser: &amp;uid, RunAsGroup: &amp;gid, Capabilities: &amp;v1.Capabilities{ Drop: []v1.Capability{&#34;ALL&#34;}, }, } 6. 实际代码示例 kubectl 端实现 func (o *ExecOptions) Run() error { // 建立与 API Server 的连接 executor, err := remotecommand.NewSPDYExecutor( o.Config, &#34;POST&#34;, req.URL()) // 执行命令 return executor.Stream(remotecommand.StreamOptions{ Stdin: o.In, Stdout: o.Out, Stderr: o.ErrOut, Tty: o.TTY, }) } Kubelet 端处理 func (h *ExecHandler) serveExec(w http.ResponseWriter, req *http.Request) { // 获取容器 ID containerID := podContainer.ContainerID // 通过 CRI 执行命令 execRequest := &amp;runtimeapi.ExecRequest{ ContainerId: containerID.ID, Cmd: cmd, Tty: tty, Stdin: stdin, Stdout: stdout, Stderr: stderr, } // 调用容器运行时 runtimeService.Exec(execRequest) } 7. 容器运行时差异 Docker // 使用 Docker Engine API client.ContainerExecCreate() client.ContainerExecAttach() Containerd // 使用 CRI 插件 task.Exec() 8. 故障排查要点 权限问题: 检查 RBAC 配置 网络连通性: API Server ↔ Kubelet 网络 容器状态: 目标容器必须处于 Running 状态 资源限制: 容器资源是否充足 安全策略: Pod Security Policies 限制 这种设计使得 kubectl exec 能够在分布式环境中安全、可靠地执行容器内命令，同时保持了良好的用户体验。</description>
    <generator>Hugo</generator>
    <language>en</language>
    <lastBuildDate></lastBuildDate>
    <atom:link href="https://ops.docs.72602.space/using/k8s/pod_exec/index.xml" rel="self" type="application/rss+xml" />
  </channel>
</rss>