k8S通过Nginx挂载静态资源
1、上传静态资源到服务器
我们将准备好的静态资源上传到服务器 /home/rke/nginx/html 该路径下
2、编写Nginx的Deployment
注意:Nginx通过K8S部署之后所使用的nginx.conf和静态文件位置的内容都需要通过挂载的方式去配置使用
apiVersion: apps/v1
kind: Deployment
metadata:
name: photo-nginx
spec:
replicas: 3
selector:
matchLabels:
app: photo-nginx
template:
metadata:
labels:
app: photo-nginx
spec:
containers:
- name: photo-nginx
image: nginx
imagePullPolicy: Always
ports:
- containerPort: 83
volumeMounts:
- mountPath: /etc/nginx/nginx.conf
name: photo-conf
subPath: nginx.conf
- mountPath: /usr/share/nginx/html
name: nfs #/usr/share/nginx/html 挂载到volumes中server+path
volumes:
- name: photo-conf
configMap:
name: photo-conf
- name: nfs
nfs:
server: "172.31.0.110" #挂载的IP地址
path: "/home/rke/nginx/html" #挂载的位置
---
apiVersion: v1
kind: ConfigMap
metadata:
name: photo-conf
data:
nginx.conf: |
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
client_max_body_size 50m;
sendfile on;
keepalive_timeout 65;
server {
listen 83;
server_name localhost;
root /usr/share/nginx/html;
location / {
index index.html index.htm;
}
location ~* ^.+\.(js|ico|gif|jpg|jpeg|png|html|htm)$ {
log_not_found off;
access_log off;
expires 7d;
}
}
}
---
apiVersion: v1
kind: Service
metadata:
name: photo-nginx
labels:
name: photo-nginx
spec:
type: NodePort
ports:
- port: 83
targetPort: 83
nodePort: 83
selector:
app: photo-nginx