# 场景描述
DeamonSet的部署形式会在每个节点部署一个相应的pod。
对于提供agent形式的pod,当前节点上的pod访问该agent最有效的方式就是直接访问当前节点上的agent pod。
# 基于HostPort实现
使用hostPort暴露端口到所在node,则可以使用hostIP:hostPort访问agent服务。
以下YAML仅为示例:
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: consul-agent
spec:
template:
metadata:
labels:
name: consul
spec:
containers:
- name: consul-agent
image: consul
ports:
- containerPort: 8500
hostPort: 8500
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
向客户端pod中以环境变量形式注入hostIP,则可以在该pod中使用HOST_IP:8500访问consul agent。
env:
- name: HOST_IP
valueFrom:
fieldRef:
fieldPath: status.hostIP
1
2
3
4
5
2
3
4
5