要通過 nginx
和 sshd
實現文件的上傳和下載,通常的做法是結合 SSH 協議和 HTTP 協議,使用 nginx
提供 Web 服務器功能,同時使用 sshd
(即 SSH 服務)來處理通過 SSH 協議進行的文件傳輸。
-
SSH 實現文件的上傳和下載: 通過
sshd
實現文件上傳和下載通常使用 SCP 或 SFTP 協議。你可以通過 SSH 客戶端將文件上傳到服務器,或從服務器下載文件。這個過程不依賴于nginx
,但你可以通過nginx
提供 Web 界面來管理文件傳輸。 -
nginx 提供 Web 界面進行文件上傳和下載:
nginx
本身并不直接處理文件上傳功能,但你可以配合一些后端服務(如 PHP、Python、Node.js 等)來實現文件上傳和下載的 Web 界面。
一、準備工作
思路
在同個pod部署nginx和sshd服務,然后共享一個存儲卷即可
準備nginx和ssd的鏡像
docker pull nginx:stable-alpine
docker pull circleci/sshd:0.1
共享目錄
/usr/share/nginx/html
示意圖
二、配置共享存儲
創建一個 PVC 來請求共享存儲
[root@node1.local ~]# nginx-ssh-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: shared-pvc
spec:accessModes:- ReadWriteMany # 允許多個容器讀寫同一存儲resources:requests:storage: 5Gi # 存儲大小可以根據需要調整
部署 PVC
kubectl apply -f nginx-ssh-pvc.yaml
三、sshd打docker鏡像
#查看目錄
[root@node1.local sshd]# ll
total 20
drwxr-xr-x 2 root root 4096 Dec 24 13:50 ./
drwx------ 33 root root 4096 Dec 30 16:52 ../
-rw-r--r-- 1 root root 174 Dec 24 12:00 Dockerfile
-rw-r--r-- 1 root root 591 Dec 24 11:48 shadow
-rw-r--r-- 1 root root 140 Dec 24 13:32 sshd_config#生成加密密碼
[root@node1.local sshd]# openssl passwd -6
Password:
Verifying - Password:
$6$YiALKQwJcDubTbBn$OEKLYvJfA8vkXAbgCGqTonP.hz5v4/gDcdvDJx0xHGiHlU.Obqpgji0m5tt1vHcTsUlqnFaMSzNiBlnn0USQZ0#設置root密碼
[root@node1.local sshd]# cat shadow
root:$6$YiALKQwJcDubTbBn$OEKLYvJfA8vkXAbgCGqTonP.hz5v4/gDcdvDJx0xHGiHlU.Obqpgji0m5tt1vHcTsUlqnFaMSzNiBlnn0USQZ0:20081:0:::::
bin:!::0:::::
...#將配置文件添加到容器
[root@node1.local sshd]# cat sshd_config
UsePAM yes
PasswordAuthentication yes
PermitEmptyPasswords no
ChallengeResponseAuthentication no
PermitRootLogin yes
AllowTcpForwarding yes
編寫dockerfile
[root@node1.local sshd]# cat Dockerfile
FROM harbor.cherry.com/sshd/sshd:0.1COPY shadow /etc/shadow
COPY sshd_config /etc/ssh/sshd_configENV TZ=Asia/ShanghaiRUN chmod 640 /etc/shadow
打鏡像
docker build -t . sshd:v2
推送harbor倉庫
docker tag sshd:v2 harbor.cherry.com/sshd/sshd:2
docker push harbor.cherry.com/sshd/sshd:2
四、部署 Nginx 和 SSH
在同個pod中來運行 Nginx 和 SSH 服務,并使用共享的 PVC 掛載文件存儲
[root@node1.local ~]# nginx-ssh-pod.yaml
apiVersion: v1
kind: Pod
metadata:name: nginx-ssh-pod
spec:containers:- name: nginximage: nginx:stable-alpine # 使用官方 Nginx 鏡像ports:- containerPort: 80volumeMounts:- name: shared-storagemountPath: /usr/share/nginx/html # 共享目錄,用于提供文件下載- name: sshimage: harbor.cherry.com/sshd/sshd:2 # 使用自定義的 SSH 鏡像ports:- containerPort: 22volumeMounts:- name: shared-storagemountPath: /usr/share/nginx/html # 共享目錄,用于文件上傳volumes:- name: shared-storagepersistentVolumeClaim:claimName: shared-pvc # 使用上面創建的 PVC
此配置文件定義了一個包含兩個容器的 Pod:
- Nginx 容器:它提供文件下載服務,將
/usr/share/nginx/html
目錄掛載到共享存儲。 - SSH 容器:它提供文件上傳服務,將
/usr/share/nginx/html
目錄掛載到共享存儲
部署pod
kubectl apply -f nginx-ssh-pod.yaml
五、暴露 Nginx 和 SSH 服務
創建 Nginx Service
[root@node1.local ~]# nginx-service.yaml
apiVersion: v1
kind: Service
metadata:name: nginx-service
spec:selector:app: nginx-ssh-podports:- protocol: TCPport: 80targetPort: 80type: LoadBalancer
創建 SSH Service
[root@node1.local ~]# ssh-service.yaml
apiVersion: v1
kind: Service
metadata:name: ssh-service
spec:selector:app: nginx-ssh-podports:- protocol: TCPport: 22targetPort: 22type: LoadBalancer
六、訪問使用
- 文件下載:可以通過直接訪問web界面 http:///files/來下載文件。
- 文件上傳:可以通過winscp來實現上傳文件