Creating a Service for an Endpoint without a selector

First, we are going to create an Endpoint directing the external service. A Kubernetes Endpoint is an abstraction, making components beyond Kubernetes (for instance, a database in other system) become a part of Kubernetes resources. It provides a feasible use case for a hybrid environment. To create an endpoint, an IP address, along with a port, is required. Please take a look at the following template:

$ cat k8s-endpoint.yaml
apiVersion: v1
kind: Endpoints
metadata:
name: k8s-ep
subsets:
- addresses:
- hostname: kubernetes-io
ip: 45.54.44.100
ports:
- port: 80

The template defines an Endpoint named k8s-ep, which points to the IP of the host of the official Kubernetes website (https://kubernetes.io). Never mind that this Endpoint forwards to a plain HTML; we just take this Endpoint as an example. As mentioned, Endpoint is not a resource supported by the Kubernetes API for exposing:

// Give it a try!
$ kubectl expose -f k8s-endpoint.yaml
error: cannot expose a { Endpoints}

In Kubernetes, an Endpoint not only represents an external service; an internal Kubernetes Service is also a Kubernetes Endpoint. You can check Endpoint resources with the command kubectl get endpoints. You will find that there is not a single endpoint k8s-ep (which you just created), but many endpoints named the same as the Services in previous pages. When a Service is created with a selector and exposes certain resources (such as a Pod, Deployment, or other Service), a corresponding Endpoint with the same name is created at the same time.

Therefore, we still can create a Service associated with the Endpoint using an identical name, as in the following template:

$ cat endpoint-service.yaml
apiVersion: v1
kind: Service
metadata:
name: k8s-ep
spec:
ports:
- protocol: TCP
port: 8080
targetPort: 80

The relationship between the Endpoints and the Service is built up with the resource name. For the Service k8s-ep, we didn't indicate the selector, since it did not actually take any Pod in responsibility:

// go create the Service and the endpoint
$ kubectl create -f endpoint-service.yaml && kubectl create -f k8s-endpoint.yaml
service "k8s-ep" created
endpoints "k8s-ep" created
// verify the Service k8s-ep
$ kubectl describe svc k8s-ep
Name: k8s-ep
Namespace: default
Labels: <none>
Annotations: <none>
Selector: <none>
Type: ClusterIP
IP: 10.105.232.226
Port: <unset> 8080/TCP
TargetPort: 80/TCP
Endpoints: 45.54.44.100:80
Session Affinity: None
Events: <none>

Now you can see that the endpoint of the Service is just the one defined in k8s-endpoint.yaml. It is good for us to access the outside world through the Kubernetes Service! In the case earlier, we can verify the result with the following command:

$ curl 10.105.232.226:8080
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.170.134