tilt-dev / tilt

Define your dev environment as code. For microservice apps on Kubernetes.
https://tilt.dev/
Apache License 2.0
7.68k stars 303 forks source link

Mapped port-forwards don't seem to work #3638

Open bobjackman opened 4 years ago

bobjackman commented 4 years ago
k8s_resource('blah', port_forwards=[80])      # works 🥳
k8s_resource('blah', port_forwards=['80'])    # works 🎉
k8s_resource('blah', port_forwards=80)        # works 🌈
k8s_resource('blah', port_forwards='80')      # works ✨
k8s_resource('blah', port_forwards=['80:80']) # doesn't work 😢
k8s_resource('blah', port_forwards='80:80')   # doesn't work 😢

deployed k8s service:

...snip...
spec:
  type: LoadBalancer
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 8080
      nodePort: 32123
    - name: https
      protocol: TCP
      port: 443
      targetPort: 8443
      nodePort: 30050
bobjackman commented 4 years ago

This sort of port forwarding seems to work fine for other services in my stack, but not this one. Does it maybe have something to do with the nodePort declaration? (my other services don't have this)

nicks commented 4 years ago

I think I basically understand what's happening here.

Under the hood, port-forwarding only acts on pods. It never acts on deployments, or on services, or on anything else.

This bites people all the time because lots of tools (kubectl, tilt) offer a UX of port-forwarding on arbitrarily high-level resources, but under the hood, they're just resolving the resource to a pod and port-forwarding to that.

What's happening in the first few examples is that you didn't provide a target port, so tilt looks through the pod spec and just port-forwards to the first port on the pod -- which in your case, is probably 8080.

When you specify target port 80, Tilt tries to port-forward to port 80 on the pod -- not the service

I still think there's a bug here -- e.g., Tilt should be able to tell you when you made this kind of mistake.

bobjackman commented 4 years ago

Ohh, tilt maps to the container port -- that's really good to know!

When a target port isn't defined, is there a way to see what Tilt auto-resolved it to?

bobjackman commented 4 years ago

So, maybe I'm not understanding your explanation like I thought I did. Based on what you said, I tried targeting the containerPort of the pod (bypassing the service entirely)...

k8s_resource('blah', port_forwards=['9999:8080'])  # doesn't work 😢

So then I tried removing my targetPort, to let Tilt resolve it automatically...

k8s_resource('blah', port_forwards=['9999'])  # doesn't work 😢
bobjackman commented 4 years ago

@nicks actually, scratch that. Now that I'm targeting the container port (not the service port), it's actually working as expected!

I only thought it wasn't working because, as it turns out, I was making my test request on the right host/port, but the rest of the request was bad. I was getting an empty response from the server that made it just look like the port forwarding was busted.