Problems using Kubernetes Services

I’ve created a Kubernetes cluster using Rancher and used a k8s Deployment to schedule 10 instances of a simple test app (receives HTTP requests and echoes back the time). I can curl the individual pods’ addresses OK. When I add a k8s Service exposing a node port, I can curl using the service IP address and the original container port, but not the node port.

Deployment YAML:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: test-deploy
spec:
  replicas: 10
  template:
    metadata:
      labels:
        app: test
    spec:
      containers:
      - name: test-pod
        image: xxx/test_app
        ports:
        - containerPort: 888

Service YAML:

apiVersion: v1
kind: Service
metadata:
  name: test-svc
  labels:
    app: test
spec:
  type: NodePort
  ports:
  - port: 888
    protocol: TCP
  selector:
    app: test

You are missing the nodePort setting in the ports section. The ports: option is the port that the service is available internally in Kubernetes.

apiVersion: v1
kind: Service
metadata:
  name: test-svc
  labels:
    app: test
spec:
  type: NodePort
  ports:
  - port: 888
    nodePort: 40888    < This is the port that is open to the outside on each host.
    protocol: TCP
  selector:
    app: test

Thanks, but I tried setting that - to 30001 - doesn’t work.

If you leave the nodePort specification out then one is assigned, in my case 30252, which can be obtained with:

kubectl describe service <name>

Still doesn’t work.