如何从正在运行的 pod 执行 Kubectl cp 到本地,说没有这样的文件或目录

Mar*_*eal 7 kubernetes kubectl

如何从正在运行的 pod 执行 Kubectl cp 到本地,说没有这样的文件或目录

我的 Ubuntu 容器中的内容如下

vagrant@ubuntu-xenial:~/k8s/pods$ kubectl exec command-demo-67m2b -c 
ubuntu 
-- sh -c "ls /tmp"
docker-sock
Run Code Online (Sandbox Code Playgroud)

现在我想使用下面的 kubectl cp 命令复制上面的 /tmp 内容

kubectl cp command-demo-67m2b/ubuntu:/tmp /home
Run Code Online (Sandbox Code Playgroud)

我有一个命令输出如下

vagrant@ubuntu-xenial:~/k8s/pods$ kubectl cp command-demo-67m2b/ubuntu:/tmp 
/home
error: tmp no such file or directory
Run Code Online (Sandbox Code Playgroud)

现在我想做的就是将上面的 /tmp 文件夹复制到本地主机,不幸的是 kubectl 说没有这样的文件或目录。当 Ubuntu 容器中存在 /tmp 文件夹时,我很困惑为什么 kubectl cp 说找不到文件夹

我的 Pod 是 command-demo-67m2b,容器名称是 ubuntu

但 Pod 已启动并正在运行,如下所示

vagrant@ubuntu-xenial:~/k8s/pods$ kubectl describe pods  command-demo-67m2b
Name:           command-demo-67m2b
Namespace:      default
Node:           ip-172-31-8-145/172.31.8.145
Start Time:     Wed, 16 Jan 2019 00:57:05 +0000
Labels:         controller-uid=a4ac12c1-1929-11e9-b787-02d8b37d95a0
            job-name=command-demo
Annotations:    kubernetes.io/limit-ranger: LimitRanger plugin set: memory 
request for container ubuntu; memory limit for container ubuntu
Status:         Running
IP:             10.1.40.75
Controlled By:  Job/command-demo
Containers:
command-demo-container:
Container ID:   
docker://c680fb336242f456d90433a9aa89cf3e1cb1d45d73447769fcf86ce329176437
Image:          tarunkumard/fromscratch6.0
Image ID:       docker- ullable://tarunkumard/fromscratch6.0@sha256:709b588aa4edcc9bc2b39bee60f248bb02347a605da09fb389c448e41e2f543a
Port:           <none>
Host Port:      <none>
State:          Terminated
  Reason:       Completed
  Exit Code:    0
  Started:      Wed, 16 Jan 2019 00:57:07 +0000
  Finished:     Wed, 16 Jan 2019 00:58:36 +0000
Ready:          False
Restart Count:  0
Limits:
  memory:  1Gi
Requests:
  memory:     900Mi
Environment:  <none>
Mounts:
  /opt/gatling-fundamentals/build/reports/gatling/ from docker-sock (rw)
  /var/run/secrets/kubernetes.io/serviceaccount from default-token-w6jt6 
(ro)
ubuntu:
Container ID:  
docker://7da9d43816253048fb4137fadc6c2994aac93fd272391b73f2fab3b02487941a
Image:         ubuntu:16.04
Image ID:      docker- 
Port:          <none>
Host Port:     <none>
Command:
  /bin/bash
  -c
  --
Args:
  while true; do sleep 10; done;
State:          Running
  Started:      Wed, 16 Jan 2019 00:57:07 +0000
Ready:          True
Restart Count:  0
Limits:
  memory:  1Gi
Requests:
  memory:  1Gi
Environment:
  JVM_OPTS:  -Xms900M -Xmx1G
Mounts:
  /docker-sock from docker-sock (rw)
  /var/run/secrets/kubernetes.io/serviceaccount from default-token-w6jt6 
(ro)
Conditions:
Type              Status
Initialized       True
Ready             False
ContainersReady   False
PodScheduled      True
Volumes:
docker-sock:
Type:    EmptyDir (a temporary directory that shares a pod's lifetime)
Medium:
default-token-w6jt6:
Type:        Secret (a volume populated by a Secret)
SecretName:  default-token-w6jt6
Optional:    false
QoS Class:       Burstable
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
             node.kubernetes.io/unreachable:NoExecute for 300s
Run Code Online (Sandbox Code Playgroud)

活动:

这是我的 yaml 文件,以防您需要参考:-

apiVersion: batch/v1
kind: Job
metadata:
name: command-demo
spec:
ttlSecondsAfterFinished: 100
template:
spec:
  volumes:
    - name: docker-sock                   # Name of the AWS EBS Volume
      emptyDir: {}
  restartPolicy: Never
  containers:
    - name: command-demo-container
      image: tarunkumard/fromscratch6.0
      volumeMounts:
        - mountPath: /opt/gatling-fundamentals/build/reports/gatling/    
   # 
   Mount path within the container
          name: docker-sock                       # Name must match the 
  AWS 
  EBS volume name defined in spec.Volumes
      imagePullPolicy: Never
      resources:
        requests:
          memory: "900Mi"
        limits:
          memory: "1Gi"
    - name: ubuntu
      image: ubuntu:16.04
      command: [ "/bin/bash", "-c", "--" ]
      args: [ "while true; do sleep 10; done;" ]
      volumeMounts:
        - mountPath: /docker-sock    # Mount path within the container
          name: docker-sock                       # Name must match the 
   AWS 
   EBS volume name defined in spec.Volumes
      imagePullPolicy: Never
      env:
      - name: JVM_OPTS
        value: "-Xms900M -Xmx1G"
Run Code Online (Sandbox Code Playgroud)

我希望 kubectl cp 命令将内容从 pod 容器复制到本地

jax*_*orm 4

在执行容器的原始命令中,您传递了该-c ubuntu命令,这意味着您正在从 Pod 中选择 Ubuntu 容器:

kubectl exec command-demo-67m2b -c 
ubuntu 
-- sh -c "ls /tmp"
Run Code Online (Sandbox Code Playgroud)

但是,在您的kubectl cp命令中,您没有指定相同的容器:

kubectl cp command-demo-67m2b/ubuntu:/tmp /home
Run Code Online (Sandbox Code Playgroud)

尝试这个:

kubectl cp command-demo-67m2b:/tmp /home -c ubuntu
Run Code Online (Sandbox Code Playgroud)