kubernetes-handbook/appendix/tricks.md

32 lines
657 B
Markdown
Raw Normal View History

2017-08-01 21:58:22 +08:00
# 1. 在容器中获取 Pod 的IP
通过环境变量来实现,该环境变量直接引用 resource 的状态字段,示例如下:
```Yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: world-v2
spec:
replicas: 3
selector:
app: world-v2
template:
metadata:
labels:
app: world-v2
spec:
containers:
- name: service
image: test
env:
- name: POD_IP
valueFrom:
fieldRef:
fieldPath: status.podIP
ports:
- name: service
containerPort: 777
```
容器中可以直接使用 `POD_IP` 环境变量获取容器的 IP。