본문 바로가기
Programming/Kubernetes

[Kubernetes] kubectl cp (Kubernetes container files copy - pod 파일 복사)

by guru_k 2021. 11. 30.
728x90
반응형

kubernetes 를 사용하면서 Volume을 통해서 local directories & files를 공유할 수 있지만 docker cp 처럼 local files 을 pod으로 직접 복사하거나 가져올 수 있습니다.

kubectl cp 명령어를 사용해서 파일을 복사하거나 가져 올 수 있으며 아래처럼 사용하면 됩니다.

Pod 에서 Local 환경으로 파일 복사

$ kubectl cp Pod이름:경로/파일 로컬경로/파일

Local 환경에서 Pod으로 파일 복사

$ kubectl cp 로컬경로/파일 Pod이름:경로/파일

예제 (nginx 생성 후 nginx.conf 복사하기)

Pod 생성을 위한 nginx deploy 배포

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80
$ kubectl apply -f nginx.yaml

$ kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-66b6c48dd5-7kzj8   1/1     Running   0          39s
nginx-deployment-66b6c48dd5-krmnf   1/1     Running   0          39s
nginx-deployment-66b6c48dd5-wnlhs   1/1     Running   0          39s

Pod 내부에서 nginx.conf 생성 후 로컬 환경으로 복사

container 내부에 nginx.conf 생성

$ kubectl exec -it nginx-deployment-66b6c48dd5-7kzj8 bash

# mkdir -p /usr/local/nginx/conf

# cat << EOF > /usr/local/nginx/conf/nginx.conf
 worker_processes  1;
 events {
     worker_connections  1024;
 }
 http {
     include       mime.types;
     server {
         listen       80;
         location / {
             root   html;
             index  index.html index.htm;
         }
     }
 }
 EOF
 
# ls /usr/local/nginx/conf/
nginx.conf

Pod에서 로컬 환경으로 nginx.conf 복사

$ kubectl cp nginx-deployment-66b6c48dd5-7kzj8:/usr/local/nginx/conf/nginx.conf ./nginx.conf

$ ls
nginx.conf

$ cat nginx.conf
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    server {
        listen       80;
        location / {
            root   html;
            index  index.html index.htm;
        }
    }
}
728x90
반응형

댓글