K8s-ops-book
跳转到导航
跳转到搜索
chapter5
5.4 endpoints对象
➜ chapter5 git:(master) ✗ kubectl get ep
Warning: v1 Endpoints is deprecated in v1.33+; use discovery.k8s.io/v1 EndpointSlice
NAME ENDPOINTS AGE
hello-web-svc <none> 106d
kubernetes 192.168.10.105:6443 142d
ngint-1 <none> 69d
nginx 10.42.2.47:80,10.42.4.22:80,10.42.4.23:80 67d
nginx-2 <none> 69d
nginx-3 <none> 69d
nginx-alb-style-svc <none> 105d
nginx-demo-svc <none> 106d
nginx-lb 10.42.2.47:80,10.42.4.22:80,10.42.4.23:80 106d
nginx-test 10.42.2.47:80,10.42.4.22:80,10.42.4.23:80 140d
pod-readiness <none> 66d
portal 10.42.2.47:8080,10.42.4.22:8080,10.42.4.23:8080 67d
➜ chapter5 git:(master) ✗ kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-web-svc ClusterIP 10.43.179.173 <none> 80/TCP 106d
kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 142d
ngint-1 LoadBalancer 10.43.62.220 <pending> 80:30771/TCP 69d
nginx ClusterIP 10.43.57.74 <none> 80/TCP 67d
nginx-2 NodePort 10.43.6.192 <none> 80:30388/TCP 69d
nginx-3 NodePort 10.43.171.2 <none> 80:31393/TCP 69d
nginx-alb-style-svc ClusterIP 10.43.134.238 <none> 80/TCP 105d
nginx-demo-svc ClusterIP 10.43.54.218 <none> 80/TCP 106d
nginx-lb LoadBalancer 10.43.27.174 <pending> 80:32693/TCP 106d
nginx-test NodePort 10.43.162.71 <none> 80:30080/TCP 140d
pod-readiness ClusterIP 10.43.233.102 <none> 80/TCP 66d
portal NodePort 10.43.87.94 <none> 80:30003/TCP 67d
➜ chapter5 git:(master) ✗ kubectl get svc,ep mysql-proxy
Warning: v1 Endpoints is deprecated in v1.33+; use discovery.k8s.io/v1 EndpointSlice
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/mysql-proxy ClusterIP 10.43.255.171 <none> 3306/TCP 97s
NAME ENDPOINTS AGE
endpoints/mysql-proxy 192.168.10.105:3306 97s
➜ chapter5 git:(master) ✗ cat service-mysql-prox.yaml
apiVersion: v1
kind: Endpoints
metadata:
name: mysql-proxy
subsets:
- addresses:
- ip: 192.168.10.105
ports:
- port: 3306
protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
name: mysql-proxy
spec:
ports:
- port: 3306
targetPort: 3306
5.5
➜ chapter5 git:(master) ✗ kubectl exec -it nginx-deployment-689b8f77c-ckqfq -- /bin/sh / # env | grep _SERVICE KUBERNETES_SERVICE_PORT=443 POD_READINESS_SERVICE_PORT=80 NGINX_3_SERVICE_PORT_HTTP=80 NGINT_1_SERVICE_HOST=10.43.62.220 NGINX_DEMO_SVC_SERVICE_PORT=80 NGINX_TEST_SERVICE_HOST=10.43.162.71 NGINX_2_SERVICE_HOST=10.43.6.192 NGINX_3_SERVICE_HOST=10.43.171.2 HELLO_WEB_SVC_SERVICE_HOST=10.43.179.173 NGINT_1_SERVICE_PORT=80 NGINX_ALB_STYLE_SVC_SERVICE_PORT_HTTP=80 NGINX_TEST_SERVICE_PORT=80 PORTAL_SERVICE_HOST=10.43.87.94 NGINX_2_SERVICE_PORT=80 NGINX_3_SERVICE_PORT=80 HELLO_WEB_SVC_SERVICE_PORT=80 NGINX_ALB_STYLE_SVC_SERVICE_HOST=10.43.134.238 PORTAL_SERVICE_PORT=80 NGINX_SERVICE_HOST=10.43.57.74 NGINX_LB_SERVICE_HOST=10.43.27.174 NGINX_2_SERVICE_PORT_80_80=80 NGINT_1_SERVICE_PORT_TCP_80_80_XPKQP=80 NGINX_ALB_STYLE_SVC_SERVICE_PORT=80 NGINX_SERVICE_PORT=80 KUBERNETES_SERVICE_PORT_HTTPS=443 NGINX_DEMO_SVC_SERVICE_PORT_HTTP=80 NGINX_LB_SERVICE_PORT=80 POD_READINESS_SERVICE_HOST=10.43.233.102 KUBERNETES_SERVICE_HOST=10.43.0.1 NGINX_DEMO_SVC_SERVICE_HOST=10.43.54.218 NGINX_SERVICE_HOST=10.43.57.74 kubectl get svc nginx NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE nginx ClusterIP 10.43.57.74 <none> 80/TCP 68d
5.5.2
/etc/nginx/conf.d # nslookup nginx.default.svc.cluster.local
nslookup: can't resolve '(null)': Name does not resolve
Name: nginx.default.svc.cluster.local
Address 1: 10.43.57.74 nginx.default.svc.cluster.local
nginx.default.svc.cluster.local 成功解析到了 10.43.57.74 —— 这就是 nginx 这个 Service 的 ClusterIP。CoreDNS 工作正常,Endpoints 有值,服务发现在这一层是通的。
(null) 到底是什么
这是 busybox nslookup 的已知瑕疵,不是 K8s 的问题。
/etc/nginx/conf.d # cat /etc/resolv.conf
search default.svc.cluster.local svc.cluster.local cluster.local local
nameserver 10.43.0.10
options ndots:5
➜ chapter5 git:(master) ✗ kubectl get pod,svc -l k8s-app=kube-dns -n kube-system
NAME READY STATUS RESTARTS AGE
pod/coredns-5f5694d56b-t7b95 1/1 Running 1 (119m ago) 24h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kube-dns ClusterIP 10.43.0.10 <none> 53/UDP,53/TCP,9153/TCP 142d
➜ chapter5 git:(master) ✗
kubectl edit configmap coredns -n kube-system
kubectl get configmap coredns -n kube-system -o yaml
apiVersion: v1
data:
Corefile: |
.:53 {
errors
health
ready
kubernetes cluster.local in-addr.arpa ip6.arpa {
pods insecure
fallthrough in-addr.arpa ip6.arpa
}
hosts /etc/coredns/NodeHosts {
ttl 60
reload 15s
fallthrough
}
prometheus :9153
cache 30
loop
reload
loadbalance
import /etc/coredns/custom/*.override
forward . /etc/resolv.conf
}
import /etc/coredns/custom/*.server
NodeHosts: |
192.168.10.105 myxps
192.168.10.201 k3s-agent-01
192.168.10.202 k3s-agent-02
2408:8256:7b1:7091::2c0 k3s-agent-02
2408:8256:7b1:7091::e1a k3s-agent-01
kind: ConfigMap
metadata:
annotations:
objectset.rio.cattle.io/applied: H4sIAAAAAAAA/4yQwWrzMBCEX0Xs2fEf20nsX9BDybH02lMva2kdq1Z2g6SkBJN3L8IUCiVtbyNGOzvfzoAn90IhOmHQcKmgAIsJQc+wl0CD8wQaSr1t1PzKSilFIUiIix4JfRoXHQjtdZHTuafAlCgq488xUSi9wK2AybEFDXvhwR2e8QQFHCnh50ZkloTJCcf8lP6NTIqUyuCkNJiSp9LJP5czoLjryztTWB0uE2iYmvjFuVSFenJsHx6tFf41gvGY6Y0Eshz/9D2e0OSZfIJVvMZExwzusSf/I9SIcQQNvaG6a+r/XVdV7abBddPtsN9W66Eedi0N7aberM22zaHf6t0tcPsIAAD//8Ix+PfoAQAA
objectset.rio.cattle.io/id: ""
objectset.rio.cattle.io/owner-gvk: k3s.cattle.io/v1, Kind=Addon
objectset.rio.cattle.io/owner-name: coredns
objectset.rio.cattle.io/owner-namespace: kube-system
creationTimestamp: "2026-07-01T17:17:46Z"
labels:
objectset.rio.cattle.io/hash: bce283298811743a0386ab510f2f67ef74240c57
name: coredns
namespace: kube-system
resourceVersion: "1800318"
uid: dc0c730a-6691-4c2c-a33a-d4db5f6551a5