0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[備忘録] kubectl でcp コマンド使ってみる

Last updated at Posted at 2025-05-17

今日はコマンドの備忘録記事になります。kubernetes 使いたくなって、勉強し始めました。ただ、コマンドが全然わからないなーーってなったので、command の使い方の備忘録になります。

それで、今回はkubectl コマンドでホストから、pod の中にファイルが転送できるらしいので、それをやってみました。

適当にyaml 作るよ

nginx のpod を作成するmanifest ファイルです。

apiVersion: v1
kind: Pod
metadata:
  name: test
  namespace: default
  labels:
    env: test
spec:
  containers:
    - name: test
      image: nginx:latest
kubectl apply -f test.yml

pod/test created

test created ならオッケーです。

kubectl get pod -o wide

NAME          READY   STATUS      RESTARTS   AGE    IP           NODE       NOMINATED NODE   READINESS GATES
test          1/1     Running     0          73s    10.244.0.7   minikube   <none>           <none>

これで立ち上がりましたね。

ファイルを転送してみる

わかりやすいように、/var/tmp 配下にファイルを送ってみます。

touch test.txt

kubectl cp ./test.txt test:/var/tmp/test.txt

 kubectl exec -it test -- ls -la /var/tmp
total 12
drwxrwxrwt 1 root root 4096 May 17 08:12 .
drwxr-xr-x 1 root root 4096 Apr 28 00:00 ..
-rw-r--r-- 1 1000 1000    0 May 17 08:12 test.txt

ちゃんとありますね!!!

じゃあ、つぎはホスト側にファイルを転送します!

# ファイル作成
kubectl exec -it test -- touch /host.txt

# 存在確認
kubectl exec -it test -- ls -la /host.txt
-rw-r--r-- 1 root root 0 May 17 08:16 /host.txt

# host 側に移動
kubectl cp test:/host.txt ./host.txt
tar: Removing leading `/' from member names

host側にファイルが出てきたら、転送成功です!!簡単で、めっちゃ便利なので、なんか転送するの忘れたとかコマンドベースで運用できるのいいですね!!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?