0. 查看帮助
bash
kubectl explain svc
kubectl explain svc.spec
kubectl explain svc
kubectl explain svc.spec
1. 查看
shell
[root@kube-master ~]# kubectl get svc|service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 192.168.0.1 <none> 443/TCP 24h
nginx NodePort 192.168.253.94 <none> 8080:32498/TCP 109m
NodePort ---> 让节点对外可以访问
# 查看所有service信息
kubectl get svc
kubectl get svc -o wide -n prod
# 查看指定service的详细信息
kubectl get svc nginx -o yaml -n prod
kubectl describe svc nginx -n prod
# 查看Service的endpoint信息
kubectl get endpoints <service-name>
[root@kube-master ~]# kubectl get svc|service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 192.168.0.1 <none> 443/TCP 24h
nginx NodePort 192.168.253.94 <none> 8080:32498/TCP 109m
NodePort ---> 让节点对外可以访问
# 查看所有service信息
kubectl get svc
kubectl get svc -o wide -n prod
# 查看指定service的详细信息
kubectl get svc nginx -o yaml -n prod
kubectl describe svc nginx -n prod
# 查看Service的endpoint信息
kubectl get endpoints <service-name>
2. 创建
shell
# 命令行创建,通过暴露deployment创建Service
kubectl expose deployment <deployment-name> --type=<service-type> --port=<port> --target-port=<target-port>
kubectl expose deploy nginx --port=80 --target-port=80 --type=NodePort
# 创建
kubectl apply -f service.yaml
#创建负载均衡
kubectl expose pod pod_name --port=9999 --tart-port=8080 --type=NodePort
--port --->负载均衡port
# 命令行创建,通过暴露deployment创建Service
kubectl expose deployment <deployment-name> --type=<service-type> --port=<port> --target-port=<target-port>
kubectl expose deploy nginx --port=80 --target-port=80 --type=NodePort
# 创建
kubectl apply -f service.yaml
#创建负载均衡
kubectl expose pod pod_name --port=9999 --tart-port=8080 --type=NodePort
--port --->负载均衡port
3. 编辑
shell
kubectl edit service <service-name>
kubectl edit svc nginx -n prod
# 更新端口
kubectl patch service my-service -p '{"spec":{"ports":[{"port":80,"targetPort":8081}]}}'
kubectl edit service <service-name>
kubectl edit svc nginx -n prod
# 更新端口
kubectl patch service my-service -p '{"spec":{"ports":[{"port":80,"targetPort":8081}]}}'
4. 访问
5. svc查看Endpoints
shell
[root@kube-master yaml]# kubectl describe svc demo-service|grep Endpoints
Endpoints: 172.17.74.126:80,172.23.127.105:80,172.23.127.109:80 + 2 more...
[root@kube-master yaml]# kubectl describe svc demo-service|grep Endpoints
Endpoints: 172.17.74.126:80,172.23.127.105:80,172.23.127.109:80 + 2 more...
- 查看完整ip
shell
[root@kube-master yaml]# kubectl describe endpoints <svc-name>
Name: demo-service
Namespace: default
Labels: <none>
Annotations: endpoints.kubernetes.io/last-change-trigger-time: 2024-05-14T01:50:10Z
Subsets:
Addresses: 172.17.74.126,172.23.127.105,172.23.127.109,172.30.0.170,172.30.0.171
NotReadyAddresses: <none>
Ports:
Name Port Protocol
---- ---- --------
http 80 TCP
Events: <none>
[root@kube-master yaml]# kubectl describe endpoints <svc-name>
Name: demo-service
Namespace: default
Labels: <none>
Annotations: endpoints.kubernetes.io/last-change-trigger-time: 2024-05-14T01:50:10Z
Subsets:
Addresses: 172.17.74.126,172.23.127.105,172.23.127.109,172.30.0.170,172.30.0.171
NotReadyAddresses: <none>
Ports:
Name Port Protocol
---- ---- --------
http 80 TCP
Events: <none>
6. 删除svc
bash
# 命令行删除
kubectl delete svc <service-name>
kubectl delete svc nginx
# 基于yaml文件删除
kubectl delete -f service.yaml
# 命令行删除
kubectl delete svc <service-name>
kubectl delete svc nginx
# 基于yaml文件删除
kubectl delete -f service.yaml
7. svc工作原理
7.1 Pod 的 IP 和生命周期管理
每个 Pod 都有一个自己的 IP 地址,当 Pod 被删除或重新调度时,IP 地址会发生变化。Kubernetes 会监控这些变化,并确保流量能够正确地转发到新的 Pod 上。
7.2 Service 的创建与标签选择器
Service 通过标签选择器来匹配需要暴露的 Pod。当 Service 创建时,Kubernetes 会根据该标签选择器找到所有匹配的 Pod,并通过 Endpoints 记录它们的 IP 地址
7.3 DNS 解析
Kubernetes 会为每个 Service 分配一个 DNS 名称,Pod 可以通过 DNS 名称访问 Service,而不需要关心具体的 IP 地址。
DNS命名规则:
<ServiceName>.<Namespace>.svc.<ClusterDomain>
<ServiceName>.<Namespace>.svc.<ClusterDomain>