关于kubernetes:Kubernetes-endpoints资源类型——映射外部服务
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服务: