|
| 1 | +# Kubernetes Setup |
| 2 | + |
| 3 | +The following section assumes that you have at least some knowledge about deploying an application to Kubernetes. |
| 4 | +For more details refer to the [official documentation](https://kubernetes.io/docs/home/) |
| 5 | + |
| 6 | +Kubernetes (or more precisely most of kubernetes DNS provider's) expose enough information for grpc-java to resolve |
| 7 | +the addresses of services that run inside the cluter. Should also work for OKD/OpenShift. |
| 8 | + |
| 9 | +There are a few things you should keep in mind here though. |
| 10 | + |
| 11 | +1. Inside your (target's) deployment, make sure that you expose the port specified by `grpc.server.port` |
| 12 | + (defaults to `9090`) |
| 13 | + |
| 14 | +````yaml |
| 15 | +[...] |
| 16 | + spec: |
| 17 | + containers: |
| 18 | + - name: my-grpc-server-app |
| 19 | + image: ... |
| 20 | + ports: |
| 21 | + - name: grpc # Use whatever name you want |
| 22 | + containerPort: 9090 # Use the same as `grpc.server.port` (prefer 80, 443 or 9090) |
| 23 | +[...] |
| 24 | +```` |
| 25 | + |
| 26 | +> **Note:** Container ports can be re-used by other deployments/pods, unless you use `hostPort`s. |
| 27 | +> So there is no reason not to use a default one. |
| 28 | +
|
| 29 | +2. Inside your (target's) service definition, you should map that port to your preferred port. |
| 30 | + |
| 31 | +````yaml |
| 32 | +apiVersion: v1 |
| 33 | +kind: Service |
| 34 | +metadata: |
| 35 | + name: my-grpc-server-app # This name is important |
| 36 | + namespace: example # This name might be important |
| 37 | +spec: |
| 38 | + selector: |
| 39 | + app: my-grpc-server-app |
| 40 | + ports: |
| 41 | + - name: grpclb # The name is important, if you wish to use DNS-SVC-Lookups (must be grpclb) |
| 42 | + port: 1234 # Remember this port number, unless you use DNS-SVC-Lookups (prefer 80, 443 or 9090) |
| 43 | + targetPort: grpc # Use the port name from the deployment (or just the port number) |
| 44 | +```` |
| 45 | + |
| 46 | +> **Note:** Service ports can be re-used by other services, unless you use `hostPort`s. |
| 47 | +> So there is no reason not to use a default one. |
| 48 | + |
| 49 | +3. Inside your client application config, configure the channel address to refer to the service name: |
| 50 | + |
| 51 | +````properties |
| 52 | +## Choose your matching variant |
| 53 | +# Same namespace (target port=80 or derived by DNS-SVC) |
| 54 | +grpc.clients.my-grpc-server-app.address=dns:///my-grpc-server-app |
| 55 | +# Same namespace (different port) |
| 56 | +grpc.clients.my-grpc-server-app.address=dns:///my-grpc-server-app:1234 |
| 57 | +# Different namespace |
| 58 | +grpc.clients.my-grpc-server-app.address=dns:///my-grpc-server-app.example:1234 |
| 59 | +# Different cluster |
| 60 | +grpc.clients.my-grpc-server-app.address=dns:///my-grpc-server-app.example.svc.cluster.local:1234 |
| 61 | +# Format |
| 62 | +grpc.clients.my-grpc-server-app.address=dns:///<serviceName>[.<namespace>[.<clusterAddress>]][:<service-port>] |
| 63 | +```` |
| 64 | + |
| 65 | +> **Note:** DNS-SVC lookups require the `grpclb` dependency to be present and the service's port name to be `grpclb`. |
| 66 | +> Refer to grpc's official docs for more details. |
| 67 | +
|
| 68 | +---------- |
| 69 | + |
| 70 | +[<- Back to Index](index.md) |
0 commit comments