<p>I have a config file named "pod.yaml" for making a pod like bellow:</p>

<p><code>
apiVersion: v1
kind: Pod
metadata:
  name: myapp
  labels:
    app: myapp
spec:
    containers:
      - name: comet-app
        image: gcr.io/my-project/my-app:v2
        ports:
          - containerPort: 5000
</code></p>

<p>and a config file named "service.yaml" for running a service in that "myapp" pod.</p>

<p><code>
apiVersion: v1
kind: Service
metadata:
  name: myapp
spec:
  type: LoadBalancer
  ports:
  - protocol: TCP
    port: 80
    targetPort: 5000
  selector:
    run: myapp
</code></p>

<p>When I run </p>

<pre><code> kubectl apply -f pod.yaml
 kubectl apply -f service.yaml
</code></pre>

<p>The 'myapp' service is created but I couldn't access my website by the internal ip and it returned ERR_CONNECTION_TIMED_OUT.</p>

<p><code>
NAME         TYPE           CLUSTER-IP      EXTERNAL-IP      PORT(S)        AGE
kubernetes   ClusterIP      10.xx.xxx.1     &lt;none&gt;           443/TCP        11d
myapp        LoadBalancer   10.xx.xxx.133   35.xxx.xx.172    80:30273/TCP   3s
</code></p>

<p>But when I deleted that service and re-run by exposing a service with bellow command, everything worked well and I could access to my website by the external-ip.</p>

<pre><code> kubectl expose pod myapp  --type=LoadBalancer --port=80 --target-port=5000
</code></pre>

<p>Could anyone explain it for me and tell me what is wrong in my service.yaml?</p>

<p>The problem with <code>service.yaml</code> is that the selector is wrong. <a href="https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service" rel="nofollow noreferrer">How it works</a> is that a service by default routes traffic to pods with a certain label. Your pod has the label <code>app: myapp</code> whereas in the service your selector is <code>run: myapp</code>. So, changing <code>service.yaml</code> to the following should solve the issue:</p>

<pre><code>apiVersion: v1
kind: Service
metadata:
  name: myapp
spec:
  type: LoadBalancer
  ports:
  - protocol: TCP
    port: 80
    targetPort: 5000
  selector:
    app: myapp
</code></pre>
