Kubernetes aws-为Nginx设置docroot内容的最佳实践是什么



我在aws上使用ingress实现了一个使用Nginx docker容器的web应用程序。目前,我正在创建docker图像与根内容,并使用Nginx来服务它。如果我的do根内容很大,我想把它放在图像之外,我该怎么做?请分享一些例子。

docker文件内容

FROM nginx

ADD . /usr/share/nginx/html/
COPY env.conf /etc/nginx/conf.d/
COPY nginx.conf /etc/nginx/

env.conf中的内容

server {
listen 80 ;
server_name localhost;
root   /usr/share/nginx/html;
index  index.html;
large_client_header_buffers 4 32k;
#default_type text/html;
# CSS
location / {
add_header Cache-Control public;
# Equivalent to above:
expires     15m; # Indicate that the resource can be cached for 86400 seconds (24 hours)
etag on; # Add an ETag header with an identifier that can be stored by the client
rewrite ^/.*$ /index.html;
}
location ~ /[^/]*.[^/]*$ { }
location /ABC {
rewrite ^/abc(/|)$ /abc/index.html break;
rewrite ^/abc/.*$ /abc/index.html break;
}
}

这是kubernetes的yaml文件。

apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-dev4
namespace: abc
spec:
strategy:
type: Recreate
selector:
matchLabels:
app: nginx-dev4
replicas: 3 # tells deployment to run 1 pods matching the template
template: # create pods using pod definition in this template
metadata:
labels:
app: nginx-dev4
spec:
containers:
- name: nginx-dev4
image: XXXX.amazonaws.com/pcl-pv/dev4
ports:
- containerPort: 80
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: "task-pv-volume"
volumes:
- name: task-pv-volume
persistentVolumeClaim:
claimName: task-pv-claim
---
apiVersion: v1
kind: Service
metadata:
name: nginx-dev4
namespace: pv
labels:
app: nginx-dev4
spec:
ports:
- name: http
port: 9091
targetPort: 80
type: ClusterIP
selector:
app: nginx-dev4
---
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
ingress.kubernetes.io/ssl-passthrough: "true"
ingress.kubernetes.io/ssl-redirect: "true"
name: nginx-dev4
namespace: pv
spec:
rules:
- host: XXXX.aws.com
http:
paths:
- path: /
backend:
serviceName: nginx-dev4
servicePort: 9091

添加持久性卷,持久性卷声明实现此。

apiVersion: v1
kind: PersistentVolume
metadata:
name: task-pv-volume
namespace: pv
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 10Gi
accessModes:
- ReadWriteOnce
hostPath:
path: "/home/ec2-user/nginx"

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: task-pv-claim
namespace: pv
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 3Gi

您可以挂载NFS或云存储,并将POD作为选项来服务器大型内容。

下面是一个很好的例子:https://medium.com/grensesnittet/mounting-a-gcp-bucket-as-nfs-in-kubernetes-8f6d3faf4da3

您还可以根据需要使用EFS、NFS或云存储桶。

相关内容

最新更新