PodPreset 用來給指定標籤的 Pod 注入額外的信息,如環境變量、存儲卷等。這樣,Pod 模板就不需要爲每個 Pod 都顯式設置重複的信息。
當然,你也可以給 Pod 增加註解 podpreset.admission.kubernetes.io/exclude: "true"
來避免它們被 PodPreset 修改。
API 版本對照表
開啓 PodPreset
開啓 API kube-apiserver --runtime-config=settings.k8s.io/v1alpha1=true
開啓准入控制 --enable-admission-plugins=..,PodPreset
PodPreset 示例
增加環境變量和存儲卷的 PodPreset
kind: PodPreset
apiVersion: settings.k8s.io/v1alpha1
metadata:
name: allow-database
namespace: myns
spec:
selector:
matchLabels:
role: frontend
env:
- name: DB_PORT
value: "6379"
volumeMounts:
- mountPath: /cache
name: cache-volume
volumes:
- name: cache-volume
emptyDir: {}
用戶提交 Pod
apiVersion: v1
kind: Pod
metadata:
name: website
labels:
app: website
role: frontend
spec:
containers:
- name: website
image: ecorp/website
ports:
- containerPort: 80
經過准入控制 PodPreset
後,Pod 會自動增加環境變量和存儲卷
apiVersion: v1
kind: Pod
metadata:
name: website
labels:
app: website
role: frontend
annotations:
podpreset.admission.kubernetes.io/allow-database: "resource version"
spec:
containers:
- name: website
image: ecorp/website
volumeMounts:
- mountPath: /cache
name: cache-volume
ports:
- containerPort: 80
env:
- name: DB_PORT
value: "6379"
volumes:
- name: cache-volume
emptyDir: {}
ConfigMap 示例
ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: etcd-env-config
data:
number_of_members: "1"
initial_cluster_state: new
initial_cluster_token: DUMMY_ETCD_INITIAL_CLUSTER_TOKEN
discovery_token: DUMMY_ETCD_DISCOVERY_TOKEN
discovery_url: http://etcd_discovery:2379
etcdctl_peers: http://etcd:2379
duplicate_key: FROM_CONFIG_MAP
REPLACE_ME: "a value"
PodPreset
kind: PodPreset
apiVersion: settings.k8s.io/v1alpha1
metadata:
name: allow-database
namespace: myns
spec:
selector:
matchLabels:
role: frontend
env:
- name: DB_PORT
value: 6379
- name: duplicate_key
value: FROM_ENV
- name: expansion
value: $(REPLACE_ME)
envFrom:
- configMapRef:
name: etcd-env-config
volumeMounts:
- mountPath: /cache
name: cache-volume
- mountPath: /etc/app/config.json
readOnly: true
name: secret-volume
volumes:
- name: cache-volume
emptyDir: {}
- name: secret-volume
secretName: config-details
用戶提交的 Pod
apiVersion: v1
kind: Pod
metadata:
name: website
labels:
app: website
role: frontend
spec:
containers:
- name: website
image: ecorp/website
ports:
- containerPort: 80
經過准入控制 PodPreset
後,Pod 會自動增加 ConfigMap 環境變量
apiVersion: v1
kind: Pod
metadata:
name: website
labels:
app: website
role: frontend
annotations:
podpreset.admission.kubernetes.io/allow-database: "resource version"
spec:
containers:
- name: website
image: ecorp/website
volumeMounts:
- mountPath: /cache
name: cache-volume
- mountPath: /etc/app/config.json
readOnly: true
name: secret-volume
ports:
- containerPort: 80
env:
- name: DB_PORT
value: "6379"
- name: duplicate_key
value: FROM_ENV
- name: expansion
value: $(REPLACE_ME)
envFrom:
- configMapRef:
name: etcd-env-config
volumes:
- name: cache-volume
emptyDir: {}
- name: secret-volume
secretName: config-details
修改 Pod 時區示例
下面的示例會把帶有標籤 tz: shanghai
的所有Pod都自動改成上海時區:
kind: PodPreset
apiVersion: settings.k8s.io/v1alpha1
metadata:
name: tz-shanghai
namespace: default
spec:
selector:
matchLabels:
tz: shanghai
volumeMounts:
- mountPath: /etc/localtime
name: tz-config
volumes:
- name: tz-config
hostPath:
path: /usr/share/zoneinfo/Asia/Shanghai