Kubernetes- Service is up but pods is not getting created

Hi all , i am facing issue when i am creating service by yaml file.Service is getting created but pods is not getting created. Can you please help in this ?
deployment.yaml -

apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp-deployment
  labels:
    app: nginx
    tier: front-end
spec:
  replicas: 3 
  selector:
    matchLabels:
      env: production # this sould match with labels env
  template:
    metadata:
      name: bhaskar-pod
      labels:
        app: my-app # pls see this and place in other place
        tier: front-end
        env: production # this sould match with matchlabels env
    spec:
      containers:
         - name: nginx-container
           image: nginx	    


service.yaml

apiVersion: v1
kind: Service
metadata:
  name: myapp-service
spec:
  selector:
    app:  my-app
    type: front-end
    env: production
  type: NodePort
  ports:
    - targetPort: 80
      port: 80
      nodePort: 30004

I tried your YAML, and the pods actually do get created. So does the service, but it does not bind any pods; it has no end points.

The problem is your selector in the service. You specific

  selector:
    app:  my-app
    type: front-end
    env: production

but for the service to work correctly, that needs to be

  selector:
    app:  my-app
    tier: front-end
    env: production

I made changes but still getting same issue. Pls find error -

windows 10@BHASKAR-PC MINGW64 /h/kubernetes
$ kubectl create -f service.yaml
service/myapp-service created

windows 10@BHASKAR-PC MINGW64 /h/kubernetes
$ kubectl get pods
No resources found in default namespace.

windows 10@BHASKAR-PC MINGW64 /h/kubernetes
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 95s
myapp-service NodePort 10.99.68.139 80:30004/TCP 23s

windows 10@BHASKAR-PC MINGW64 /h/kubernetes
$ minikube service myapp-service --url
W0514 08:08:01.862222 9964 main.go:291] Unable to resolve the current Docker CLI context “default”: context “default”: context not found: open C:\Users\windows 10.docker\contexts\meta\37a8eec1ce19687d132fe29051dca629d164e2c4958ba141d5f4133a33f0688f\meta.json: The system cannot find the path specified.
http://192.168.59.113:30004

X Exiting due to SVC_UNREACHABLE: service not available: no running pod for service myapp-service found

Let’s see the actual YAML for the service, after you’ve edited it. It sounds like the service still does not link to your deployment’s pods.

Here’s my version of the service:

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    app: nginx
    tier: front-end
  name: myapp-deployment
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    nodePort: 30004
  selector:
    env: production
    tier: front-end
    app: my-app
  type: NodePort
status:
  loadBalancer: {}

Hi Rob,
I have to make changes here in my service.yaml ?

metadata:
  creationTimestamp: null
  labels:
    app: nginx
    tier: front-end
  name: myapp-deployment

No; you need to make changes under selector. The error you’re getting indicates that you are not matching your pods, so the service points to nothing. Here’s what I see when I check that your service is valid, i.e., that it really does have endpoints:

$ k get ep myapp-deployment
NAME               ENDPOINTS                                         AGE
myapp-deployment   10.244.0.195:80,10.244.0.196:80,10.244.0.197:80   29m

If your service is valid, you should see something similar. I’m betting that you don’t see that, judging by the error

X Exiting due to SVC_UNREACHABLE: service not available: no running pod for service myapp-service found

Hi rob, I am seeing the same error , earlier i have posted .

X Exiting due to SVC_UNREACHABLE: service not available: no running pod for service myapp-service found.

This is my selector block for service.yaml file-

spec:
  selector:
    app:  my-app
    tier: front-end
    env: production
  type: NodePort
  ports:
    - targetPort: 80
      port: 80
      nodePort: 30004

This is deployment.yaml file -

template:
metadata:
name: bhaskar-pod
labels:
app: my-app # pls see this and place in other place
tier: front-end
env: production # this sould match with matchlabels env
spec:
containers:
- name: nginx-container
image: nginx

Sorry for bothering you so much i am unable to figure it out from where the error is coming.

OK, I need to see both your deployment file and your service file, as they are currently written. This does not make sense. Also, there are issues with your deployment; you look to have written it by hand; it may be better to start by doing this:

# 1. delete both the deployment and service
k delete svc myapp-service
k delete deploy myapp-deployment

Also: what specification are you using for this deployment and this service? It looks like you are sort of picking and choosing field values, and in a somewhat random way. Sometimes when you do this, a deployment will not work predictably.

Thanks Rob, by deleting all deployment and writing freshly it is working now.
Thanks again.