14

我正在尝试将 nginx 入口控制器添加到我的 kubernetes 集群。我当前的集群有 3 个节点,它们之间都有开放的防火墙规则。(注意:这是一个用于教育目的的裸机集群)

我已经使用以下配置创建了一个部署

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nodejs-test
  labels:
    app: nodejs
spec:
  replicas: 5
  selector:
    matchLabels:
      app: nodejs
  template:
    metadata:
      labels:
        app: nodejs
    spec:
      containers:
      - name: nodejs-container
        image: gcr.io/infrastructure/test-nodejs-server
        ports:
        - containerPort: 3000
---
apiVersion: v1
kind: Service
metadata:
  name: test-deployment-service
spec:
  selector:
    app: nodejs
  ports:
  - protocol: TCP
    port: 80
    targetPort: 3000

运行 kubectl create -f file.yaml 后

我看到所有 pod 和服务都在运行。在集群中,我可以卷曲该服务 ip 并从 pod 获得响应。

现在我去创建一个入口。

我首先按照https://github.com/kubernetes/ingress-nginx/tree/master/deploy上的安装步骤进行操作

我可以看到我的入口控制器正在运行。

    NAMESPACE       NAME                                         READY     STATUS    RESTARTS   AGE
default         my-test-6445d9bb7b-blm7t                     1/1       Running   0          3h
default         nodejs-test-5657f7bb74-7hwqk                 1/1       Running   0          23h
default         nodejs-test-5657f7bb74-bh44x                 1/1       Running   0          23h
default         nodejs-test-5657f7bb74-dw4g4                 1/1       Running   0          23h
default         nodejs-test-5657f7bb74-k7bf4                 1/1       Running   0          23h
default         nodejs-test-5657f7bb74-xdthl                 1/1       Running   0          23h
ingress-nginx   default-http-backend-55c6c69b88-4d5bf        1/1       Running   0          26m
ingress-nginx   nginx-ingress-controller-5c6698dfbf-gs9zd    1/1       Running   0          25m
kube-system     etcd-yoda.me.com                      1/1       Running   1          1d
kube-system     kube-apiserver-yoda.me.com            1/1       Running   1          1d
kube-system     kube-controller-manager-yoda.me.com   1/1       Running   1          1d
kube-system     kube-dns-6f4fd4bdf-62bxg                     3/3       Running   3          1d
kube-system     kube-flannel-ds-jbmwg                        1/1       Running   1          1d
kube-system     kube-flannel-ds-r8nfb                        1/1       Running   2          1d
kube-system     kube-flannel-ds-rczbx                        1/1       Running   0          1d
kube-system     kube-proxy-brm2g                             1/1       Running   0          1d
kube-system     kube-proxy-qjgcv                             1/1       Running   1          1d
kube-system     kube-proxy-sz6rw                             1/1       Running   1          1d
kube-system     kube-scheduler-yoda.me.com            1/1       Running   1          1d
kube-system     kubernetes-dashboard-5bd6f767c7-stscl        1/1       Running   1          23h
kube-system     tiller-deploy-59d854595c-tngg6               1/1       Running   0          23h

然后我使用以下文件创建我的入口

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  annotations:
    ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - http:
      paths:
      - path: /whoop
        backend:
          serviceName: test-deployment-service
          servicePort: 80

然后我运行 kubectl get services --all-namespaces 来查找我的入口控制器端口。

NAMESPACE       NAME                      TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                      AGE
default         kubernetes                ClusterIP   10.96.0.1        <none>        443/TCP                      1d
default         my-test                   ClusterIP   10.110.102.5     <none>        80/TCP                       4h
default         test-deployment-service   ClusterIP   10.104.239.37    <none>        80/TCP                       23h
ingress-nginx   default-http-backend      ClusterIP   10.105.218.87    <none>        80/TCP                       29m
ingress-nginx   ingress-nginx             NodePort    10.110.39.104    <none>        80:30652/TCP,443:31624/TCP   27m
kube-system     kube-dns                  ClusterIP   10.96.0.10       <none>        53/UDP,53/TCP                1d
kube-system     kubernetes-dashboard      ClusterIP   10.100.219.110   <none>        443/TCP                      23h
kube-system     tiller-deploy             ClusterIP   10.101.179.202   <none>        44134/TCP                    23h

现在我去 curl 我的服务器:

curl -v myserver:30652/whoop/

得到的响应是:

*   Trying myip...
* TCP_NODELAY set
* Connected to myserver (myip) port 30652 (#0)
> GET /whoop/ HTTP/1.1
> Host: myserver:30652
> User-Agent: curl/7.54.0
> Accept: */*
> 
< HTTP/1.1 308 Permanent Redirect
< Server: nginx/1.13.9
< Date: Fri, 02 Mar 2018 14:38:21 GMT
< Content-Type: text/html
< Content-Length: 187
< Connection: keep-alive
< Location: https://myserver:30652/whoop/
< Strict-Transport-Security: max-age=15724800; includeSubDomains;
< 
<html>
<head><title>308 Permanent Redirect</title></head>
<body bgcolor="white">
<center><h1>308 Permanent Redirect</h1></center>
<hr><center>nginx/1.13.9</center>
</body>
</html>
* Connection #0 to host myserver left intact

我希望能从我的 pod 中得到响应。但是我总是得到308。

如果我卷曲一条随机路径

卷曲我的服务器:30652/随机

我得到的回应是

default backend - 404

如何连接我的入口和入口控制器以使路由正常工作?

4

3 回答 3

15

我遇到了同样的问题,但我不想添加host值。为了防止像David提到的默认服务器将 HTTP 重定向到 HTTPS ,相反,我将此注释添加到入口并为我修复了它:nginx.ingress.kubernetes.io/ssl-redirect: "false".

我的工作入口现在看起来像这样:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  annotations:
    kubernetes.io/ingress.class: "internal-nginx"
    nginx.ingress.kubernetes.io/rewrite-target: /
    nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
  - http:
      paths:
      - path: /*
        backend:
          serviceName: app1
          servicePort: 80
      - path: /app2/*
        backend:
          serviceName: app2
          servicePort: 80
于 2018-06-27T20:49:14.743 回答
9

这里的关键是命名主机,否则入口规则适用于默认服务器,默认服务器配置为将 HTTP 重定向到 HTTPS。

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: test-ingress
  annotations:
    ingress.kubernetes.io/rewrite-target: /
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: foo.bar.com
    http:
      paths:
      - path: /whoop
        backend:
          serviceName: test-deployment-service
          servicePort: 80

您应该能够禁用默认服务器的重定向行为,我还没有尝试过。

于 2018-03-23T17:57:54.807 回答
4

我遇到了将 HTTP 调用重定向到 HTTPS 的问题(使用提琴手日志验证了它)。

将此添加到我的 yml 文件中使其对我有用。

注释:

nginx.ingress.kubernetes.io/ssl-redirect: "false"
于 2021-01-08T09:33:18.423 回答