I have a gRPC server running on port 9000 with gRPC-gateway running on port 9080.
I can make request to my service with postman using the following link: ```http://cluster1.example.com/api/v1/namespaces/default/services/my-service:9080/proxy
How can I connect to my service from gRPC client (on my local machine, which is outside of the cluster) using grpc.Dial()?
Example:
conn, err := grpc.Dial(...?, grpc.WithInsecure())
if err != nil {
panic(err)
}
Short answer:
This is mostly not a Golang question, it is a Kubernetes question. You have to set up the Kubernetes part and use it like ever before in Golang.
You can refer to @blackgreen's answer for a simple and temporary way.
Kubernetes uses an overlay network, Flannel in most cases, the communication inside the cluster is set up by default, and it is isolated from outside.
Of cause there are some projects like Calico can connect the inside and outside network, but it another story.
There are several solutions if we want to access the pods from outside.
https://kubernetes.io/docs/tasks/access-application-cluster/port-forward-access-application-cluster/
Kubectl uses socat to create a tunnel and forward one or more local ports to a pod.
The port forward will end when you stop the command, but it is a good choice if you want to temporarily access the pod for debugging.
kubectl port-forward redis-master-765d459796-258hz 7000:6379
https://kubernetes.io/docs/concepts/services-networking/service/#publishing-services-service-types
Service is an abstract way to expose an application running on a set of Pods as a network service.
when accessing from outside, there are kinds of Service to use, NodePort may be a good choice in most case.
It uses iptables or ipvs to create a Port Forward in all Nodes forwarding network to the target port.
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
type: NodePort
selector:
app: MyApp
ports:
# By default and for convenience, the `targetPort` is set to the same value as the `port` field.
- port: 80
targetPort: 80
# Optional field
# By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
nodePort: 30007
https://kubernetes.io/docs/concepts/services-networking/ingress/
Ingress is a layer 7 proxy managing external network access to Service, as gRPC is also built on top of HTTP/2, Ingress work perfectly.
Ingress should be the choice if you are exposing a Production Application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With