How to Transfer Files from a Kubernetes Pod Using WinSCP

Kubernetes provides powerful orchestration for containerized applications, but sometimes you need to interact directly with the files inside a pod. One way to do this is by using WinSCP, a popular file transfer client. In this blog post, we’ll walk you through the steps to configure file transfer from a Kubernetes pod using WinSCP.

Step 1: Install an SSH Server in the Pod

The first step is to ensure your pod has an SSH server installed. If it doesn’t, you can create a new pod with an SSH server. Here’s an example pod specification with OpenSSH installed:

YAML File:

apiVersion: v1
kind: Pod
metadata:
  name: ssh-pod
  labels:
    app: ssh
spec:
  containers:
  - name: ssh-container
    image: rastasheep/ubuntu-sshd:18.04
    ports:
    - containerPort: 22
      name: ssh
    volumeMounts:
    - name: ssh-key
      mountPath: /root/.ssh
  volumes:
  - name: ssh-key
    secret:
      secretName: ssh-key-secret

This example uses the rastasheep/ubuntu-sshd image, which has OpenSSH pre-installed.

Step 2: Create SSH Keys and Kubernetes Secrets

Generate SSH keys on your local machine:

ssh-keygen -t rsa -b 2048 -f id_rsa_ssh_pod

Next, create a Kubernetes secret with the public key:

kubectl create secret generic ssh-key-secret --from-file=id_rsa.pub=id_rsa_ssh_pod.pub

Step 3: Apply the Pod Configuration

Apply the pod configuration to your Kubernetes cluster:

kubectl apply -f ssh-pod.yaml

Step 4: Port Forwarding

Forward the SSH port of the pod to your local machine:

kubectl port-forward pod/ssh-pod 2222:22

Now, the pod’s SSH service is accessible on your local machine via port 2222.

Step 5: Configure WinSCP

  1. Open WinSCP and create a new site.
  2. Host name: localhost
  3. Port number: 2222
  4. User name: root (or the user configured in the pod)
  5. Password: Leave blank if using key-based authentication.
  6. Private key file: Point to the private key file (id_rsa_ssh_pod).

Step 6: Connect and Transfer Files

You should now be able to connect to the pod using WinSCP and transfer files as needed.

Example Command Summary

  1. Generate SSH keys:
ssh-keygen -t rsa -b 2048 -f id_rsa_ssh_pod

2. Create a Kubernetes secret:

kubectl create secret generic ssh-key-secret --from-file=id_rsa.pub=id_rsa_ssh_pod.pub

3. Apply the pod configuration:

kubectl apply -f ssh-pod.yaml

4. Port forwarding:

kubectl port-forward pod/ssh-pod 2222:22

5. Configure and connect using WinSCP with localhost as the host and 2222 as the port.

    By following these steps, you can seamlessly use WinSCP to transfer files between your local machine and a Kubernetes pod, making it easier to manage and interact with your containerized applications.

    Leave a Reply