默认的kubernetes服务在哪里获取端点列表?

时间:2019-04-12 13:33:09

标签: kubernetes

我有一个带有3个控制平面的集群。与任何群集一样,我的群集也具有默认的kubernetes服务。作为任何服务,它都有一个端点列表:

apiVersion: v1
items:
- apiVersion: v1
  kind: Endpoints
  metadata:
    creationTimestamp: 2017-12-12T17:08:34Z
    name: kubernetes
    namespace: default
    resourceVersion: "6242123"
    selfLink: /api/v1/namespaces/default/endpoints/kubernetes
    uid: 161edaa7-df5f-11e7-a311-d09466092927
  subsets:
  - addresses:
    - ip: 10.9.22.25
    - ip: 10.9.22.26
    - ip: 10.9.22.27
    ports:
    - name: https
      port: 8443
      protocol: TCP
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""

一切正常,但我完全不明白这些端点来自何处?从Service标签选择器开始进行假设是合乎逻辑的,但是没有任何标签选择器:

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: 2017-12-12T17:08:34Z
  labels:
    component: apiserver
    provider: kubernetes
  name: kubernetes
  namespace: default
  resourceVersion: "6"
  selfLink: /api/v1/namespaces/default/services/kubernetes
  uid: 161e4f00-df5f-11e7-a311-d09466092927
spec:
  clusterIP: 10.100.0.1
  ports:
  - name: https
    port: 443
    protocol: TCP
    targetPort: 8443
  sessionAffinity: ClientIP
  sessionAffinityConfig:
    clientIP:
      timeoutSeconds: 10800
  type: ClusterIP
status:
  loadBalancer: {}

那么,在内置默认kubernetes服务的情况下,有人可以解释k8s服务和端点如何工作吗?

1 个答案:

答案 0 :(得分:0)

目前尚不清楚如何创建多节点集群,但是这里有一些针对您的研究:

Set up High-Availability Kubernetes Masters描述HA k8的创建。他们有notes关于默认的kubernetes服务。

  

不是试图保留Kubernetes apiserver的最新列表   在Kubernetes服务中,系统会将所有流量定向到   外部IP:

     

在一个主群集中,IP指向单个主群集,

     

在多主群集中,IP指向前端的负载均衡器   大师们。

     

类似地,kubelet将使用外部IP进行通信   与主人

所以我宁愿拥有LB ip而不是3个主机。

服务创建:https://github.com/kubernetes/kubernetes/blob/master/pkg/master/controller.go#L46-L83

const kubernetesServiceName = "kubernetes"

// Controller is the controller manager for the core bootstrap Kubernetes
// controller loops, which manage creating the "kubernetes" service, the
// "default", "kube-system" and "kube-public" namespaces, and provide the IP
// repair check on service IPs
type Controller struct {
    ServiceClient   corev1client.ServicesGetter
    NamespaceClient corev1client.NamespacesGetter
    EventClient     corev1client.EventsGetter
    healthClient    rest.Interface

    ServiceClusterIPRegistry rangeallocation.RangeRegistry
    ServiceClusterIPInterval time.Duration
    ServiceClusterIPRange    net.IPNet

    ServiceNodePortRegistry rangeallocation.RangeRegistry
    ServiceNodePortInterval time.Duration
    ServiceNodePortRange    utilnet.PortRange

    EndpointReconciler reconcilers.EndpointReconciler
    EndpointInterval   time.Duration

    SystemNamespaces         []string
    SystemNamespacesInterval time.Duration

    PublicIP net.IP

    // ServiceIP indicates where the kubernetes service will live.  It may not be nil.
    ServiceIP                 net.IP
    ServicePort               int
    ExtraServicePorts         []corev1.ServicePort
    ExtraEndpointPorts        []corev1.EndpointPort
    PublicServicePort         int
    KubernetesServiceNodePort int

    runner *async.Runner
}

服务定期更新:https://github.com/kubernetes/kubernetes/blob/master/pkg/master/controller.go#L204-L242

// RunKubernetesService periodically updates the kubernetes service
func (c *Controller) RunKubernetesService(ch chan struct{}) {
    // wait until process is ready
    wait.PollImmediateUntil(100*time.Millisecond, func() (bool, error) {
        var code int
        c.healthClient.Get().AbsPath("/healthz").Do().StatusCode(&code)
        return code == http.StatusOK, nil
    }, ch)

    wait.NonSlidingUntil(func() {
        // Service definition is not reconciled after first
        // run, ports and type will be corrected only during
        // start.
        if err := c.UpdateKubernetesService(false); err != nil {
            runtime.HandleError(fmt.Errorf("unable to sync kubernetes service: %v", err))
        }
    }, c.EndpointInterval, ch)
}

// UpdateKubernetesService attempts to update the default Kube service.
func (c *Controller) UpdateKubernetesService(reconcile bool) error {
    // Update service & endpoint records.
    // TODO: when it becomes possible to change this stuff,
    // stop polling and start watching.
    // TODO: add endpoints of all replicas, not just the elected master.
    if err := createNamespaceIfNeeded(c.NamespaceClient, metav1.NamespaceDefault); err != nil {
        return err
    }

    servicePorts, serviceType := createPortAndServiceSpec(c.ServicePort, c.PublicServicePort, c.KubernetesServiceNodePort, "https", c.ExtraServicePorts)
    if err := c.CreateOrUpdateMasterServiceIfNeeded(kubernetesServiceName, c.ServiceIP, servicePorts, serviceType, reconcile); err != nil {
        return err
    }
    endpointPorts := createEndpointPortSpec(c.PublicServicePort, "https", c.ExtraEndpointPorts)
    if err := c.EndpointReconciler.ReconcileEndpoints(kubernetesServiceName, c.PublicIP, endpointPorts, reconcile); err != nil {
        return err
    }
    return nil
}

端点更新位置:https://github.com/kubernetes/kubernetes/blob/72f69546142a84590550e37d70260639f8fa3e88/pkg/master/reconcilers/lease.go#L163

也可以手动创建端点。访问Services without selectors了解更多信息。