kubernetes-handbook/appendix/tricks.md

42 lines
1.0 KiB
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
```
2017-09-28 17:59:35 +08:00
容器中可以直接使用 `POD_IP` 环境变量获取容器的 IP。
# 2. 指定容器的启动参数
我们可以在 Pod 中为容器使用 command 为容器指定启动参数:
```Bash
command: ["/bin/bash","-c","bootstrap.sh"]
```
看似很简单,使用数组的方式定义,所有命令使用跟 Dockerfile 中的 CMD 配置是一样的,但是有一点不同的是,`bootsttap.sh` 必须具有可执行权限,否则容器启动时会出错。