Why won't curl connect to node port when Kubernetes service created as yaml?

110 Views Asked by At

Hi I have the following service definition...

apiVersion: v1
kind: Service
metadata:
  name: my-app-dev
  labels:
    run: my-app-dev
spec:
  type: NodePort
  selector:
    app: my-app-dev
  ports:
    - port: 18080
      targetPort: 18080
      name: http
    - port: 47100
      targetPort: 47100
      name: ignite-com
    - port: 47500
      targetPort: 47500
      name: ignite-disc

Then I run kubctl get svc which returns...

my-app-dev     NodePort    10.xxx.xxx.xxx   <none>        18080:32058/TCP,47100:30524/TCP,47500:31437/TCP   12m

When I try curl http://ip to kube agent:32058 I get curl: (7) Failed to connect to xxxxxx port 32058: Connection refused

But then I run, kubectl expose deployment my-app-dev --name=my-app-dev-2 --type=NodePort --port=18080 --target-port=18080

Which then kubctl get svc returns...

my-app-dev     NodePort    10.xxx.xxx.xxx   <none>        18080:32058/TCP,47100:30524/TCP,47500:31437/TCP   12m
my-app-dev-2   NodePort    10.xxx.xxx.xxx    <none>        18080:30571/TCP                                   11s

And curl http://ip to kube agent:30571 works perfectly fine. My minimum config seems right to me, but i could be wrong...

Update adding my deployment yaml in case I missed something...

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    run: my-app-dev
  name: my-app-dev
spec:
  replicas: 1
  selector:
    matchLabels:
      run: my-app-dev
  template:
    metadata:
      labels:
        run: my-app-dev
    spec:
      containers:
        - name: private
          image: foo/bar
          volumeMounts:
            - name: config-data
              mountPath: /config
          ports:
            - containerPort: 18080
              protocol: TCP
              name: http
            - containerPort: 47100
              protocol: TCP
              name: ignite-com
            - containerPort: 47500
              protocol: TCP
              name: ignite-disc
          env:
            - name: JAVA_OPTS
              value: "-Xms512m -Xmx512m -XX:+UseG1GC -XX:MaxInlineLevel=18 -Djava.net.preferIPv4Stack=true"
            - name: HOST
              valueFrom:
                fieldRef:
                  fieldPath: status.hostIP
      initContainers:
        - name: config-data
          image: curlimages/curl
          command: ["sh", "-c", "TOKEN=`cat /var/run/secrets/kubernetes.io/serviceaccount/token`; curl -k -H \"Authorization: Bearer $TOKEN\" https://kubernetes.default:443/api/v1/namespaces/default/services/my-app-dev >> /config/service.json"]
          volumeMounts:
            - name: config-data
              mountPath: /config
      volumes:
        - name: config-data
          emptyDir: {}

0

There are 0 best solutions below