Accessing Service within the Cluster
Once we have deployed services within the UK8S cluster and configured svc, if the application accessing the service is also within the k8s cluster, the service can be accessed via a domain name.
I. Obtaining the Service Address
When our service access initiator (we call it client, here using api-pod-3 as an example) and the service receiver (we call it server, here using access as an example) are both running in UK8S, k8s domain name is generally used to access server services, and k8s will automatically forward the traffic to the corresponding pod.
The access address is as follows:
[servicename].[namespace].[resourcetype].[clusterdomain]
- servicename: the name of the service, such as the above access
- namespace: the namespace where the service is located, corresponding to access’s namespace prj-foo
- resourcetype: resource type, when the access type is service, the value is uniformly svc
- clusterdomain: cluster domain name, in the console, get details from a specific k8s instance, the specific value can be obtained from ‘Overview’-> ‘Basic Information’ -> ‘Cluster Local Domain Name’, generally ‘cluster.local’
II. Service Access Examples
1. HTTP Service Access
If the service is an HTTP service, we can access it through an HTTP client, and the corresponding port is the port configured by svc
curl http://access.prj-foo.svc.cluster.local:8080/
2. TCP Service Access
Similarly, in tcp services, we use the service address as our host when accessing, and the service port as the port when accessing.
For example, access is a grpc server
func main() {
conn, err := grpc.Dial("access.prj-foo.svc.cluster.local:8080", grpc.WithInsecure())
...
defer conn.Close()
client := pb.NewSearchServiceClient(conn)
resp, err := client.Search(context.Background(), &pb.SearchRequest{
Request: "gRPC",
})
...
}