简体   繁体   English

使用 nginx-ingress 公开服务

[英]Expose service using nginx-ingress

All my pod work in the server.我所有的 pod 都在服务器上工作。

NAMESPACE       NAME                                              READY   STATUS    RESTARTS   AGE
default         user-api-66fc4fc9d-igqnj                          3/3     Running   0          25s
ingress-nginx   nginx-ingress-controller-5556bd698f-qgw8r         1/1     Running   0          12h
kube-system     coredns-6955765f44-4xnhh                          1/1     Running   1          40h
kube-system     coredns-6955765f44-6tb8p                          1/1     Running   1          40h
kube-system     etcd-izbp1dyjigsfwmw0dtl85gz                      1/1     Running   1          40h
kube-system     kube-apiserver-izbp1dyjigsfwmw0dtl85gz            1/1     Running   1          40h
kube-system     kube-controller-manager-izbp1dyjigsfwmw0dtl85gz   1/1     Running   1          40h
kube-system     kube-flannel-ds-amd64-8b5pc                       1/1     Running   0          40h
kube-system     kube-flannel-ds-amd64-jq4kl                       1/1     Running   1          40h
kube-system     kube-proxy-9zx7c                                  1/1     Running   1          40h
kube-system     kube-proxy-lh55j                                  1/1     Running   0          40h
kube-system     kube-scheduler-izbp1dyjigsfwmw0dtl85gz            1/1     Running   1          40h

The ingress I create.我创建的入口。

NAME          HOSTS                ADDRESS   PORTS   AGE
app-ingress   example.com                    80      5h16m

I create ClusterIP service for my single deployment, and using loadBalance ingress-nginx controller with resource file to expose internal service.我为我的单一部署创建了 ClusterIP 服务,并使用带有资源文件的 loadBalance ingress-nginx 控制器来公开内部服务。 The relative code show under below.相关代码如下所示。

apiVersion: apps/v1
kind: Deployment
metadata:
  name: user-api
spec:
  replicas: 1
  selector:
    matchLabels:
      app: user-api
  strategy: {}
  template:
    metadata:
      labels:
        app: user-api
    spec:
      dnsPolicy: ClusterFirstWithHostNet
      hostNetwork: true
      containers:
      - name: user-api
        image: doumeyi/user-api-amd64:1.0
        ports:
        - name: user-api
          containerPort: 3000
        resources: {}
---
apiVersion: v1
kind: Service
metadata:
  name: user-api
spec:
  selector:
    app: user-api
  ports:
  - name: user-api
    port: 3000
    targetPort: 3000
---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: app-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /
spec:
  rules:
    - host: example.com
      http:
        paths:
        - path: /user-api
          backend:
            serviceName: user-api
            servicePort: 3000

It seems any problem with the ingress-nginx service, the external ip is always pending. ingress-nginx 服务似乎有任何问题,外部 ip 总是挂起。

NAMESPACE       NAME            TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                                        AGE
ingress-nginx   ingress-nginx   LoadBalancer   10.104.176.152   <pending>     80:31612/TCP,443:30097/TCP                     13h

Please refer: Access kubernetes service externally请参考: 外部访问kubernetes服务

You can expose using:您可以使用以下方法公开:

  • NodePort Service type节点端口服务类型

  • LoadBalancer Service type (if you are hosted on cloud) LoadBalancer 服务类型(如果您托管在云上)

The port 80 that you specify in ingress is kubernetes service port.您在 ingress 中指定的端口 80 是 kubernetes 服务端口。 There will be no process listening on that port.不会有进程在该端口上侦听。 There should be a process listening on container port specified in pod spec.应该有一个进程监听 pod 规范中指定的容器端口。 That would be in the node where the pod got deployed.那将在部署 pod 的节点中。 Also for the port 80 and 443 that you specify in the deployment of nginx ingress controller itself there should be nginx process listening but you need to check that in the node where nginx controller pod got deployed.此外,对于您在部署 nginx 入口控制器本身时指定的端口 80 和 443,应该有 nginx 进程侦听,但您需要在部署 nginx 控制器 pod 的节点中进行检查。

Please change service definition and specify proper port :请更改服务定义并指定正确的端口:

apiVersion: v1
kind: Service
metadata:
  name: user-api
spec:
  selector:
    app: user-api
  ports:
    - name: user-api
    - port: 3000
      targetPort: 80
  type: LoadBalancer

Traffic from the external load balancer is directed at the backend Pods.来自外部负载均衡器的流量被定向到后端 Pod。 The cloud provider decides how it is load balanced.云提供商决定如何进行负载平衡。

For LoadBalancer type of Services, when there is more than one port defined, all ports must have the same protocol and the protocol must be one of TCP, UDP, and SCTP.对于 LoadBalancer 类型的服务,当定义了多个端口时,所有端口必须具有相同的协议,并且协议必须是 TCP、UDP 和 SCTP 之一。

Some cloud providers allow you to specify the loadBalancerIP.一些云提供商允许您指定 loadBalancerIP。 In those cases, the load-balancer is created with the user-specified loadBalancerIP.在这些情况下,负载平衡器是使用用户指定的 loadBalancerIP 创建的。 If the loadBalancerIP field is not specified, the loadBalancer is set up with an ephemeral IP address.如果未指定 loadBalancerIP 字段,则使用临时 IP 地址设置 loadBalancer。 If you specify a loadBalancerIP but your cloud provider does not support the feature, the loadbalancerIP field that you set is ignored.如果您指定 loadBalancerIP 但您的云提供商不支持该功能,则您设置的 loadbalancerIP 字段将被忽略。

Also if you are using nginx ingress controller please remove dnspolicy and hostnetwork from Deployment configuration file and then apply changes ( $ kubectl apply -f your-deployment-conf-file.yaml ).此外,如果您使用 nginx 入口控制器,请从部署配置文件中删除 dnspolicy 和 hostnetwork,然后应用更改( $ kubectl apply -f your-deployment-conf-file.yaml )。

More infromation you can find here: nginx-troubleshooting , nginx-ingress .您可以在此处找到更多信息: nginx-troubleshootingnginx-ingress

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM