k8s 无头service 是指 clusterIP 为 None 的service
案例,假定有一个 deployment,containerPort 端口80,同时还被打上 python=myweb 标签。
deployment内容如下
apiVersion: apps/v1
kind: Deployment
metadata:
name: mywebdeployment
spec:
selector:
matchLabels:
python: myweb
replicas: 6
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 0
maxUnavailable: 1
template:
metadata:
labels:
python: myweb
spec:
containers:
- name: mywebcontainer
image: python:3.7
command: ['sh', '-c']
args: ['echo "<p>The host is $(hostname) </p>" > index.html;python -m http.server 80']
ports:
- name: mywebport
containerPort: 80
无头service如下
apiVersion: v1
kind: Service
metadata:
name: myweb-service
spec:
selector:
python: myweb
type: ClusterIP
clusterIP: None
ports:
- port: 81
targetPort: 80
创建工具型pod
apiVersion: v1
kind: Pod
metadata:
name: mytestpod
labels:
pod: test
spec:
containers:
- name: mytestpod
image: appropriate/curl
command: ['sh','-c']
args: ['echo "good";sleep 3600;']
进入工具型Pod,并且在pod中查询service的解析地址 {ServiceName}.{Namespace}.svc.{ClusterDomain}