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

时间:2021-02-03 06:45:21

标签: nginx kubernetes amazon-eks

我已经使用 Ingress 在 aws eks 上使用 Nginx docker 容器实现了一个 Web 应用程序。目前,我正在使用 docroot 内容创建 docker 映像并使用 Nginx 为其提供服务。如果我的根目录内容很大并且我想将其保留在图像之外,我该怎么做?请分享一些例子。

这里是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

1 个答案:

答案 0 :(得分:1)

您可以使用 POD 挂载 NFS 或云存储作为服务器大型内容的选项。

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

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

相关问题