首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >来自/nextcloud的Kubernetes访问服务nextcloud

来自/nextcloud的Kubernetes访问服务nextcloud
EN

Stack Overflow用户
提问于 2021-10-04 11:02:16
回答 1查看 228关注 0票数 1

我正在尝试使用Kubernetes托管我自己的Nextcloud服务器。

我想要从http://localhost:32738/nextcloud访问我的Nextcloud服务器,但每次我访问这个URL时,它都会被重定向到http://localhost:32738/login,并为我提供404 Not Found

如果我将路径替换为:

代码语言:javascript
复制
path: /

然后,它在http://localhost:32738/login上运行没有问题,但正如我所说的,这不是我正在寻找的解决方案。应从http://localhost:32738/nextcloud/login访问登录页面。

转到http://127.0.0.1:32738/nextcloud/确实适用于初始设置,但在此之后它将变得不可访问,因为它总是重定向到:

代码语言:javascript
复制
http://127.0.0.1:32738/apps/dashboard/

而不是去:

代码语言:javascript
复制
http://127.0.0.1:32738/nextcloud/apps/dashboard/

这是我的yaml:

代码语言:javascript
复制
#Nextcloud-Dep
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nextcloud-server
  labels:
    app: nextcloud
spec:
  replicas: 1
  selector:
    matchLabels:
      pod-label: nextcloud-server-pod
  template:
    metadata:
      labels:
        pod-label: nextcloud-server-pod
    spec:
      containers:
      - name: nextcloud
        image: nextcloud:22.2.0-apache
        env:
        - name: POSTGRES_DB
          valueFrom:
            secretKeyRef:
              name: nextcloud
              key: db-name
        - name: POSTGRES_USER
          valueFrom:
            secretKeyRef:
              name: nextcloud
              key: db-username
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: nextcloud
              key: db-password
        - name: POSTGRES_HOST
          value: nextcloud-database:5432
        volumeMounts:
        - name: server-storage
          mountPath: /var/www/html
          subPath: server-data
      volumes:
      - name: server-storage
        persistentVolumeClaim:
          claimName: nextcloud
---
#Nextcloud-Serv
apiVersion: v1
kind: Service
metadata:
  name: nextcloud-server
  labels:
    app: nextcloud
spec:
  selector:
    pod-label: nextcloud-server-pod
  ports:
  - port: 80
    protocol: TCP
    name: nextcloud-server
---
#Database-Dep
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nextcloud-database
  labels:
    app: nextcloud
spec:
  replicas: 1
  selector:
    matchLabels:
      pod-label: nextcloud-database-pod
  template:
    metadata:
      labels:
        pod-label: nextcloud-database-pod
    spec:
      containers:
      - name: postgresql
        image: postgres:13.4
        env:
        - name: POSTGRES_DATABASE
          valueFrom:
            secretKeyRef:
              name: nextcloud
              key: db-name
        - name: POSTGRES_USER
          valueFrom:
            secretKeyRef:
              name: nextcloud
              key: db-username
        - name: POSTGRES_PASSWORD
          valueFrom:
            secretKeyRef:
              name: nextcloud
              key: db-password
        - name: POSTGRES_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: nextcloud
              key: db-rootpassword
        - name: PGDATA
          value: /var/lib/postgresql/data/
        volumeMounts:
        - name: database-storage
          mountPath: /var/lib/postgresql/data/
          subPath: data
      volumes:
      - name: database-storage
        persistentVolumeClaim:
          claimName: nextcloud
---
#Database-Serv
apiVersion: v1
kind: Service
metadata:
  name: nextcloud-database
  labels:
    app: nextcloud
spec:
  selector:
    pod-label: nextcloud-database-pod
  ports:
  - port: 5432
    protocol: TCP
    name: nextcloud-database
---
#PV
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nextcloud-pv
  labels:
    type: local
spec:
  capacity:
    storage: 8Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/tmp"
---
#PVC
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nextcloud
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 8Gi
---
#Ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nextcloud-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: "true"
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
  - http:
      paths:
      - backend:
          service:
            name: nextcloud-server
            port:
              number: 80
        pathType: Prefix
        path: /nextcloud(/.*)
---
#Secret
apiVersion: v1
kind: Secret
metadata:
  name: nextcloud
  labels:
    app: nextcloud
immutable: true
stringData:
  db-name: nextcloud
  db-username: nextcloud
  db-password: changeme
  db-rootpassword: longpassword
  username: admin
  password: changeme

ingress-nginx安装时带有:

代码语言:javascript
复制
helm install nginx ingress-nginx/ingress-nginx

如果你想让我提供更多信息,请告诉我。

EN

回答 1

Stack Overflow用户

发布于 2021-10-04 16:56:06

在您的示例中,后端服务中公开的URL与Ingress规则中指定的路径之间存在差异。这就是为什么你会得到一个错误。

为了避免这种情况,您可以使用重写规则。

使用该方法,您的入口路径将被重写为您提供的值。在将请求发送到后端服务之前,此注释ingress.kubernetes.io/rewrite-target: /login会将URL /nextcloud/login重写为/login

但是:

从0.22.0版本开始,使用注释nginx.ingress.kubernetes.io/

-的入口定义不能向后兼容以前的版本。

this documentation上,您可以找到以下示例:

代码语言:javascript
复制
$ echo '
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$2
  name: rewrite
  namespace: default
spec:
  rules:
  - host: rewrite.bar.com
    http:
      paths:
      - backend:
          serviceName: http-svc
          servicePort: 80
        path: /something(/|$)(.*)
' | kubectl create -f -

在此入口定义中,(.*)捕获的任何字符都将分配给占位符$2,然后将其用作rewrite-target注释中的参数。

所以在你的网址中你可以看到想要的/nextcloud/login,但是重写将导致在入口规则中更改/login的路径并找到你的后端。我建议使用以下选项之一:

代码语言:javascript
复制
path: /nextcloud(/.*)
代码语言:javascript
复制
nginx.ingress.kubernetes.io/rewrite-target: /$1

代码语言:javascript
复制
path: /nextcloud/login
代码语言:javascript
复制
nginx.ingress.kubernetes.io/rewrite-target: /login

另请参见this article

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69434884

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档