【k8s系列】(202207) 利用k8s的nginx搭建文件下载器

需求

想通过k8s的nginx容器下载主机上的某个文件

方法:通过容器挂载文件挂载的方式,把主机文件下载目录挂载到nginx容器内,并且挂载主机上的nginx.conf文件到nginx容器内。当访问nginx时,会列出相应的文件进行下载即可

操作流程

主机文件结构:nginx.conf文件 download文件夹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
[root@master nginx]# pwd
/root/nginx
[root@master nginx]# tree
.
├── download
│   └── test.txt
└── nginx.conf

1 directory, 2 files
[root@master nginx]# cat nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
default_type application/octet-stream;
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
root /usr/share/nginx/html/download;
autoindex on; #开启索引功能
autoindex_exact_size off; #关闭计算文件确切大小(单位bytes),只显示大概大小(单位kb、mb、gb)
autoindex_localtime on; #显示本机时间而非 GMT 时间
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}

[root@master nginx]# cat download/test.txt
Hello, anan
[root@master nginx]#

image-20220719115853392

k8s nginx yaml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
[root@master nginx]# cat nginx4.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.20.1
ports:
- containerPort: 80
volumeMounts:
- name: conf
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
- name: download
mountPath: /usr/share/nginx/html/download
volumes:
- name: conf
hostPath:
path: /root/nginx/
- name: download
hostPath:
path: /root/nginx/download/
---
apiVersion: v1
kind: Service
metadata:
name: ngx-service
labels:
app: nginx
spec:
type: NodePort
selector:
app: nginx
ports:
- port: 80
targetPort: 80
nodePort: 8899


image-20220719120057356

1
kubectl apply -f nginx.conf

pod成功启动后,访问nodeport端口,可以看到下载列表,进行下载即可!

image-20220719120130869

参考

使用docker搭建nginx文件下载服务器


【k8s系列】(202207) 利用k8s的nginx搭建文件下载器
http://example.com/2022/07/19/k8s/【k8s系列】(202207) 利用k8s的nginx搭建文件下载器/
作者
ningan123
发布于
2022年7月19日
许可协议