Skip to content

Service Without Selector

A Service without a selector is a Kubernetes configuration that allows a Service to abstract an external endpoint or a manually managed set of IP addresses, rather than automatically load balancing traffic across Pods selected via labels.^[k8s中引入外部服务.md]

This configuration typically involves creating a Service resource that explicitly omits the selector field and defines the desired ports, alongside a separate Endpoints resource that specifies the external IP addresses to which traffic should be forwarded.^[k8s中引入外部服务.md]

Configuration Example

The following example demonstrates a Service named mysql-svc without a selector, paired with an Endpoints object mysql-ep that directs traffic to an external IP (114.32.146.154).^[k8s中引入外部服务.md]

apiVersion: v1
kind: Service
metadata:
  name: mysql-svc
  namespace: default
spec:
  clusterIP: None
  ports:
    - name: default-ep
      port: 3306
      protocol: TCP
      targetPort: 3306
  type: ClusterIP
---
apiVersion: v1
kind: Endpoints
metadata:
  name: mysql-ep
  namespace: default
subsets:
  - addresses:
      - ip: 114.32.146.154
    ports:
      - name: mysql
        port: 3306
        protocol: TCP

Verification

Once configured, internal Pods can access the external service using the standard Kubernetes DNS naming conventions (e.g., mysql-svc or mysql-svc.default.svc.cluster.local).^[k8s中引入外部服务.md] For instance, commands run from within the cluster can successfully retrieve content from the external server as if it were a local service.^[k8s中引入外部服务.md]

Sources

  • k8s中引入外部服务.md