【问题标题】:Kubernetes aws- what's the best practice to setup docroot contents for NginxKubernetes aws-为 Nginx 设置 docroot 内容的最佳实践是什么
【发布时间】:2021-05-07 09:19:43
【问题描述】:

我已经使用 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

【问题讨论】:

  • 如果您可以共享示例 docker 文件或获取的上下文不多。您可以启动 webapp pod 并使用 ingress 将流量转发到其中。
  • @HarshManvar:我已经更新了我的问题,请看一下。
  • 也许您可以使用 NFS 来存储到 doc 根目录的内容,并且 pod 将安装在 NFS 或云存储桶上。
  • 在 AWS 上,您可以将内容保存在 S3 中。您可以不使用容器直接从 S3 提供服务,也可以设置 nginx 以代理对 S3 的请求。它看起来不像本地文件,但对于这个用例,您不一定需要它。
  • 您决定解决方案了吗? NFS 似乎是个好方法。

标签: nginx kubernetes amazon-eks


【解决方案1】:

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

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

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

【讨论】:

  • 感谢 Harsh,我能够使用持久性卷和持久性卷声明来实现这一点。
  • @NikhilBharati 如果答案有帮助,请更新问题的状态,如果解决了问题,请点赞,您可以将其标记为已接受。
猜你喜欢
  • 2015-09-22
  • 2021-08-11
  • 2014-04-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-04
  • 2023-03-03
  • 2021-07-02
相关资源
最近更新 更多