KubernetesUpAndRunning-Services

来源:互联网 发布:债市与股市的关系 知乎 编辑:程序博客网 时间:2024/06/06 12:18

Service discovery is a mechanism that enables clients of a service to locate service endpoints without manual intervention. DNS is the most common implementation of service discovery in use today.

Kubernetes provides service discovery out of the box in the form of an Endpoints API, DNS, and environment variables which can be exposed to Pods. Each service discovery mechanism is backed by Kubernetes Services and Endpoints.

Endpoints

Endpoints hold a collection of IP addresses and ports that back a Kubernetes Service.The following Kubernetes configuration represents an Endpoint for the “my-service” Service with a single service endpoint at 10.52.0.2 on port 80:

apiVersion: v1kind: Endpointsmetadata:  name: my-service  namespace: defaultsubsets:  - addresses:      - ip: 10.52.0.2    ports:      - port: 80

Virtual IPs and Service Proxies

When Services are created via the Kubernetes API, a virtual IP address is allocated to the Service from the Kubernetes Service address range. One thing to keep in mind Virtual IP addresses are not assigned to any network interfaces on the hosts or Pods. Virtual IP addresses must be routed in order to work.

Creating Services

apiVersion: v1kind: Servicemetadata:  labels:    app: helloworld  name: helloworldspec:  ports:    - port: 80      protocol: TCP      targetPort: 80  selector:    app: helloworld

Submit the helloworld service to the Kubernetes API using the kubectl create command:

$ kubectl create -f helloworld.yamlservice "helloworld" created

Managing Services

$ kubectl get servicesNAME         CLUSTER_IP       EXTERNAL_IP   PORT(S)   SELECTOR         AGEhelloworld   10.223.244.100   <none>        80/TCP    app=helloworld   20skubernetes   10.223.240.1     <none>        443/TCP   <none>           9m$ kubectl describe services helloworldName:             helloworldNamespace:        defaultLabels:           app=helloworldSelector:         app=helloworldType:             ClusterIPIP:               10.223.244.100Port:             <unnamed>       80/TCPEndpoints:        <none>Session Affinity: NoneNo events.

Use the spec.clusterIP key to assign a fixed cluster IP to the helloworld service.

apiVersion: v1kind: Servicemetadata:  labels:    app: helloworld  name: helloworldspec:  clusterIP: 10.223.244.110  ports:    - port: 80      protocol: TCP      targetPort: 80  selector:    app: helloworld
$ kubectl delete services helloworld$ kubectl create -f helloworld.yamlName:             helloworldNamespace:        defaultLabels:           app=helloworldSelector:         app=helloworldType:             ClusterIPIP:               10.223.244.110Port:             <unnamed>      80/TCPEndpoints:        <none>Session Affinity: NoneNo events.

Service Discovery

Environment variables

Environment variables are created for every service defined. These variables are made available to every pod in the namespace.

Environment variables are not dynamically updated.

DNS

Due to these shortcomings, DNS is the preferred way to communicate information about Services.

In most Kubernetes deployments Services will be mapped to DNS names using the skyDNS cluster add-on.

Exposing Services

  • NodePort
  • LoadBalancer
  • External IPs
0 0
原创粉丝点击