ClusterIP Service
ClusterIP Service
apiVersion: v1
kind: Service
metadata:
name: web
namespace: prod
spec:
type: ClusterIP # 默认值,可省略
selector:
app: web
ports:
- name: http
port: 80 # Service 暴露的端口
targetPort: 8080 # 容器实际监听端口
protocol: TCP查看与验证
查看与验证
kubectl get svc web -o wide
kubectl get endpoints web
kubectl run curl-test --rm -it --image=curlimages/curl:8.6.0 --restart=Never -- curl -s http://web.prod.svc.cluster.local/ClusterIP 是虚拟地址,没有实体网卡;同命名空间可直接用服务名访问,跨命名空间用 <svc>.<ns>。