关于kubernetes:Kubernetes-endpoints资源类型

7次阅读

共计 1954 个字符,预计需要花费 5 分钟才能阅读完成。

endpoints 是一种 kubernetes 资源类型,它是 namespace scoped,简写为 ep:

kubectl api-resources|grep endpoints
endpoints                         ep                                          true         Endpoints

endpoints 有两种应用办法:

  • 随 service 被创立:

    • 创立 service 对象的同时,kubernetes 会创立同名的 endpoints 的对象;
    • service 的 podSelector 会将指标 pod 的 endpoint 信息(ip/port),增加到 endpoints 对象中;
  • 独立创立 endpoints:

    • enpdoints 作为一种资源类型,反对用户创立;
    • 将内部服务的 endpoint 增加到 endpoints 对象,能够应用 kubernetes service 拜访内部服务;

service 与 endpoints

以一个简略的例子,来演示 endpoints 随 service 被主动创立进去。
创立 nginx deploy,含 2 个 pod:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: nginx
  name: nginx
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:latest
        imagePullPolicy: IfNotPresent
        command:
        - nginx
        - -g
        - "daemon off;"
        workingDir: /usr/share/nginx/html
        ports:
        - name: http
          containerPort: 80
          protocol: TCP

创立 nginx service:

apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: nginx

查看其 service 和 endpoints 资源:有一个 同名的 endpoints 被创立进去

# kubectl get svc,endpoints
NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
service/nginx-svc    ClusterIP   10.233.26.39   <none>        80/TCP    3m2s

NAME                   ENDPOINTS                                                       AGE
endpoints/nginx-svc    10.233.96.113:80,10.233.97.95:80                3m2s

其 endpoints 的内容,理论是 nginx pod 的 ip+port:

# kubectl get pod -owide
NAME                    READY   STATUS    RESTARTS   AGE    IP              NODE      NOMINATED NODE   READINESS GATES
nginx-6dcdfd5b8-8q7l5   1/1     Running   0          4m3s   10.233.97.95    master1   <none>           <none>
nginx-6dcdfd5b8-xsl96   1/1     Running   0          4m3s   10.233.96.113   master3   <none>           <none>

内部服务与 endpoints

手动创立 endpoints,增加内部服务的 endpoint,能够将内部服务作为 kubernetes 的 service 应用。
etcd 有 3 节点部署在内部,为其创立 service: etcd-k8s,不指定 podSelector:

apiVersion: v1
kind: Service
metadata:
  name: etcd-k8s
  namespace: kube-system
spec:
  type: ClusterIP
  ports:
  - name: port
    port: 2379
    protocol: TCP

创立同名的 endpoints 资源: etcd-k8s,将 etcd 节点的 ip+port 填进去:

apiVersion: v1
kind: Endpoints
metadata:
  name: etcd-k8s
  namespace: kube-system
subsets:
- addresses:
  - ip: 178.104.163.38
    nodeName: etc-master1
  - ip: 178.104.163.187
    nodeName: etc-master2
  - ip: 178.104.163.243
    nodeName: etc-master3
  ports:
  - name: port
    port: 2379
    protocol: TCP

这样,通过 serviceName.namespace:port 就能够在 kubernetes 集群内拜访内部的 etcd 服务:

etcd-k8s.kube-system:2379
正文完
 0