There are a few steps involved in persisting files and their names from an SFTP server in a k8s volume:
Create a Kubernetes secret containing the SFTP credentials.
Create a Kubernetes Deployment or StatefulSet to deploy a Pod that uses an SFTP client to connect to the server and download the files.
Mount a volume in the Pod to store the downloaded files.
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.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-05-18 06:55:28 +0000
Seen: 8 times
Last updated: May 18 '23