我无法获得 Ingress 服务工作的基本示例

I can't get a basic example of an Ingress service working

我正在努力处理面向 nginx pod 的 Ingress 服务的一个非常基本的示例。每当我尝试访问我的示例站点时,我都会得到这个简单的文本输出而不是默认的 nginx 页面:

404 page not found

这是我正在使用的部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 4
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: nginx-ingress
  namespace: default
spec:
  rules:
  - host: argo.corbe.net
    http:
      paths:
      - backend:
          serviceName: ningx
          servicePort: 80

k3s kubectl get pods -o wide:

NAME                              READY   STATUS    RESTARTS   AGE   IP           NODE    NOMINATED NODE   READINESS GATES
nginx-deployment-d6dcb985-942cz   1/1     Running   0          8h    10.42.0.17   k3s-1   <none>           <none>
nginx-deployment-d6dcb985-d7v69   1/1     Running   0          8h    10.42.0.18   k3s-1   <none>           <none>
nginx-deployment-d6dcb985-dqbn9   1/1     Running   0          8h    10.42.1.26   k3s-2   <none>           <none>
nginx-deployment-d6dcb985-vpf79   1/1     Running   0          8h    10.42.1.25   k3s-2   <none>           <none>

k3s kubectl -o wide 获取服务:

NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE   SELECTOR
kubernetes      ClusterIP   10.43.0.1       <none>        443/TCP   5d    <none>
nginx-service   ClusterIP   10.43.218.234   <none>        80/TCP    8h    app=nginx

k3s kubectl -o wide get 入口:

NAME            CLASS    HOSTS            ADDRESS          PORTS   AGE
nginx-ingress   <none>   argo.corbe.net   207.148.25.119   80      8h

k3s kubectl describe deployment nginx-deployment:

Name:                   nginx-deployment
Namespace:              default
CreationTimestamp:      Mon, 22 Feb 2021 15:19:07 +0000
Labels:                 app=nginx
Annotations:            deployment.kubernetes.io/revision: 2
Selector:               app=nginx
Replicas:               4 desired | 4 updated | 4 total | 4 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=nginx
  Containers:
   nginx:
    Image:        nginx
    Port:         8080/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   nginx-deployment-7848d4b86f (4/4 replicas created)
Events:
  Type    Reason             Age    From                   Message
  ----    ------             ----   ----                   -------
  Normal  ScalingReplicaSet  2m43s  deployment-controller  Scaled up replica set nginx-deployment-7848d4b86f to 1
  Normal  ScalingReplicaSet  2m43s  deployment-controller  Scaled down replica set nginx-deployment-d6dcb985 to 3
  Normal  ScalingReplicaSet  2m43s  deployment-controller  Scaled up replica set nginx-deployment-7848d4b86f to 2
  Normal  ScalingReplicaSet  2m40s  deployment-controller  Scaled down replica set nginx-deployment-d6dcb985 to 2
  Normal  ScalingReplicaSet  2m40s  deployment-controller  Scaled up replica set nginx-deployment-7848d4b86f to 3
  Normal  ScalingReplicaSet  2m40s  deployment-controller  Scaled down replica set nginx-deployment-d6dcb985 to 1
  Normal  ScalingReplicaSet  2m40s  deployment-controller  Scaled up replica set nginx-deployment-7848d4b86f to 4
  Normal  ScalingReplicaSet  2m38s  deployment-controller  Scaled down replica set nginx-deployment-d6dcb985 to 0

您收到 404,这意味着请求将到达您正在使用的 nginx 或入口

您的入口现在可能有问题

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: nginx-ingress
  namespace: default
spec:
  rules:
  - host: argo.corbe.net
    http:
      paths:
      - backend:
          serviceName: ningx
          servicePort: 80

检查您正在使用的服务名称serviceName: ningx

应该是 nginx-service ingress 应该是

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: nginx-ingress
  namespace: default
spec:
  rules:
  - host: argo.corbe.net
    http:
      paths:
      - backend:
          serviceName: nginx-service
          servicePort: 80

整个流程将是这样的:DNS > ingress > service (nginx-service) > deployment (nginx-deployment) or pod replicas.

nginx 镜像默认在 80 端口监听连接。

$ kubectl run --image nginx
$ kubectl exec -it nginx -- bash
root@nginx:/# apt update
    **output hidden**
root@nginx:/# apt install iproute2
    **output hidden**
root@nginx:/# ss -lunpt
Netid     State      Recv-Q     Send-Q         Local Address:Port         Peer Address:Port                                     
tcp       LISTEN     0          0                    0.0.0.0:80                0.0.0.0:*        users:(("nginx",pid=1,fd=7))    
tcp       LISTEN     0          0                          *:80                      *:*        users:(("nginx",pid=1,fd=8))    

请注意,打开的是端口 80,而不是端口 8080。 这意味着您的服务配置错误,因为它转发到端口 8080。

您应该将目标端口设置为 80,如下所示。:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80    # <- HERE

还要注意服务名称:

kind: Service
metadata:
  name: nginx-service

作为支持,您使用不同名称的服务:

- backend:
    serviceName: ningx

将其更改为服务的实际名称,如下所示:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: nginx-ingress
  namespace: default
spec:
  rules:
  - host: argo.corbe.net
    http:
      paths:
      - backend:
          serviceName: ningx-service
          servicePort: 80

应用更改,它现在应该可以工作了。