Nginx ingress controller logs keeps telling me that i have wrong pod information

1/16/2020

I am running two nodes in kubernetes cluster. I am able to deploy my microservices with 3 replicas, and its service. Now I am trying to have nginx ingress controller to expose my service but i am getting this error from the logs:

unexpected error obtaining pod information: unable to get POD information (missing POD_NAME or POD_NAMESPACE environment variable)

I have set a namespace of development in my cluster, that is where my microservice is deploy and also nginx controller. I do not understand how nginx picks up my pods or how i am passing pods name or pod_namespace.

here is my nginx controller:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-controller
spec:
  replicas: 1
  selector:
    matchLabels:
      name: nginx-ingress
  template:
    metadata:
      labels:
        name: nginx-ingress
    spec:
      containers:
        - name: nginx-ingress-controller
          image: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.27.0
          args:
            - /nginx-ingress-controller
            - --configmap=$(POD_NAMESPACE)/nginx-configuration
          env:
            - name: mycha-deploy
              valueFrom: 
                fieldRef:
                  fieldPath: metadata.name
          ports:
          - name: http
            containerPort: 80
          - name: https
            containerPort: 443

and here my deployment:

#dDeployment
apiVersion: apps/v1
kind:  Deployment
metadata:
  name: mycha-deploy
  labels:
    app: mycha-app
spec:
  replicas: 3
  selector:
    matchLabels:
      app: mycha-app
  template:
    metadata:
      labels:
        app: mycha-app
    spec:
      containers:
        - name: mycha-container
          image: us.gcr.io/##########/mycha-frontend_kubernetes_rrk8s
          ports:
          - containerPort: 80

thank you

-- Roberto Rios
docker
kubernetes
nginx
nginx-ingress

2 Answers

1/16/2020

Your nginx ingress controller deployment yaml looks incomplete and does not have below among many other items.

           env:
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: POD_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace

Follow the installation docs and use yamls from here

-- Arghya Sadhu
Source: StackOverflow

1/17/2020

To expose your service using a Nginx Ingress, you need to configure it before.

Follow the installation guide for you kubernetes installation.

You also need a service to 'group' the containers of your application.

In Kubernetes, a Service is an abstraction which defines a logical set of Pods and a policy by which to access them (sometimes this pattern is called a micro-service). The set of Pods targeted by a Service is usually determined by a selector ... For example, consider a stateless image-processing backend which is running with 3 replicas. Those replicas are fungible—frontends do not care which backend they use. While the actual Pods that compose the backend set may change, the frontend clients should not need to be aware of that, nor should they need to keep track of the set of backends themselves. The Service abstraction enables this decoupling.

As you can see, the service will discover your containers based on the label selector configured in your deployment.

To check the container's label selector: kubectl get pods -owide -l app=mycha-app

Service yaml

Apply the follow yaml to create a service for your deployment:

apiVersion: v1
kind: Service
metadata:
  name: mycha-service
spec:
  selector:
    app: mycha-app <= This is the selector
  ports:
    - protocol: TCP
      port: 8080
      targetPort: 80

Check if the service is created with kubectl get svc.

Test the app using port-forwarding from your desktop at http://localhost:8080:

kubectl port-forward svc/mycha-service 8080:8080

nginx-ingress yaml

The last part is the nginx-ingress. Supposing your app has the url mycha-service.com and only the root '/' path:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: ingress-mycha-service
  annotations:
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: mycha-service.com <= app url
    http:
      paths:
      - path: /
        backend:
          serviceName: mycha-service <= Here you define what is the service that your ingress will use to send the requests.
          servicePort: 80

Check the ingress: kubectl get ingress

NAME                    HOSTS                           ADDRESS         PORTS     AGE
ingress-mycha-service   mycha-service.com               XX.X.X.X   80        63s

Now you are able to reach your application using the url mycha-service.com and ADDRESS displayed by command above.

I hope it helps =)

-- KoopaKiller
Source: StackOverflow