Config file for running service in Kubernetes doesn't work well

7/23/2018

I have a config file named "pod.yaml" for making a pod like bellow:

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

and a config file named "service.yaml" for running a service in that "myapp" pod.

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

When I run

 kubectl apply -f pod.yaml
 kubectl apply -f service.yaml

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

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

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.

 kubectl expose pod myapp  --type=LoadBalancer --port=80 --target-port=5000

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

-- Quoc Lap
kubernetes
kubernetes-pod
kubernetes-service
yaml

1 Answer

7/23/2018

The problem with service.yaml is that the selector is wrong. How it works is that a service by default routes traffic to pods with a certain label. Your pod has the label app: myapp whereas in the service your selector is run: myapp. So, changing service.yaml to the following should solve the issue:

apiVersion: v1
kind: Service
metadata:
  name: myapp
spec:
  type: LoadBalancer
  ports:
  - protocol: TCP
    port: 80
    targetPort: 5000
  selector:
    app: myapp
-- Michael Hausenblas
Source: StackOverflow