Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

There are a few steps involved in persisting files and their names from an SFTP server in a k8s volume:

  1. Create a Kubernetes secret containing the SFTP credentials.

  2. Create a Kubernetes Deployment or StatefulSet to deploy a Pod that uses an SFTP client to connect to the server and download the files.

  3. Mount a volume in the Pod to store the downloaded files.

  4. Use a ConfigMap to store the file names.

Here is an example YAML file that demonstrates the process:

apiVersion: v1
kind: Secret
metadata:
  name: sftp-creds
type: Opaque
data:
  username: base64-encoded-username
  password: base64-encoded-password

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: sftp-downloader
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sftp-downloader
  template:
    metadata:
      labels:
        app: sftp-downloader
    spec:
      containers:
      - name: sftp-client
        image: sftp-client-image
        command: ["/bin/sh"]
        args: ["-c", "sftp -oIdentityFile=/etc/secret-volume/ssh-key -b /etc/scripts/get-files-sftp.sh $SFTP_USER@$SFTP_HOST"]
        env:
        - name: SFTP_USER
          valueFrom:
            secretKeyRef:
              name: sftp-creds
              key: username
        - name: SFTP_PASSWORD
          valueFrom:
            secretKeyRef:
              name: sftp-creds
              key: password
        volumeMounts:
        - name: secrets
          mountPath: "/etc/secret-volume"
        - name: config
          mountPath: "/etc/config-volume"
        - name: data
          mountPath: "/data"
      volumes:
      - name: secrets
        secret:
          secretName: sftp-creds
          defaultMode: 256
      - name: config
        configMap:
          name: sftp-files
          defaultMode: 256
      - name: data
        persistentVolumeClaim:
          claimName: sftp-data

---

apiVersion: v1
kind: ConfigMap
metadata:
  name: sftp-files
data:
  file1.txt: "file1-downloaded.txt"
  file2.txt: "file2-downloaded.txt"

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: sftp-data
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

The first section creates a Kubernetes secret containing the SFTP username and password (encoded with base64). The second section creates a Kubernetes Deployment that deploys a Pod running an SFTP client image. The command and args fields in the container spec use an SFTP client command to download files from the server. The env fields reference the values stored in the secret. The volumeMounts field mounts the secret, config map and data volumes in the container. The third section creates a ConfigMap with the file names as keys and the downloaded file names as values. Finally, the fourth section creates a PersistentVolumeClaim to store the downloaded files.

With this setup, the Pod will connect to the SFTP server, download the files, and store them in the persistent volume. The ConfigMap will be used to store the original file names, so you can easily map the downloaded files to their original names.