简体   繁体   中英

Expose service using nginx-ingress

All my pod work in the server.

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. 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.

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

You can expose using:

  • NodePort Service type

  • LoadBalancer Service type (if you are hosted on cloud)

The port 80 that you specify in ingress is kubernetes service port. There will be no process listening on that port. There should be a process listening on container port specified in pod spec. That would be in the node where the pod got deployed. 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.

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. 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.

Some cloud providers allow you to specify the loadBalancerIP. In those cases, the load-balancer is created with the user-specified loadBalancerIP. If the loadBalancerIP field is not specified, the loadBalancer is set up with an ephemeral IP address. If you specify a loadBalancerIP but your cloud provider does not support the feature, the loadbalancerIP field that you set is ignored.

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 ).

More infromation you can find here: nginx-troubleshooting , nginx-ingress .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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