NodeSelector
将 Pod 与 Node绑定,例如只能运行在携带了“disktype: ssd”标签(Label)的节点上,否则将调度失败
apiVersion: v1kind: Pod...spec:nodeSelector:disktype: ssd
NodeName
一旦 Pod 的这个字段被赋值,Kubernetes 项目就会被认为这个 Pod 已经被调用,可以在测试调试时设置。
HostAliases
定义了 Pod 的 hosts 文件(比如 /etc/hosts)
apiVersion: v1
kind: Pod
...
spec:
hostAliases:
- ip: "10.1.2.3"
hostnames:
- "foo.remote"
- "bar.remote"
...
这个 Pod 启动后,/etc/hosts 文件的内容将如下
cat /etc/hosts
# Kubernetes-managed hosts file.
127.0.0.1 localhost
...
10.244.135.10 hostaliases-pod
10.1.2.3 foo.remote
10.1.2.3 bar.remote
如果不通过hostAliases设置hosts文件内容,pod被删除重建后,kubectl会自动覆盖掉hosts中被直接修改的内容
容器 Linux Namespace 相关的属性
Pod 的设计,就是要让它里面的容器尽可能多地共享 Linux Namespace,仅保留必要的隔离和限制能力。
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
shareProcessNamespace: true #共享PID Namespace,整个Pod里每个容器的进程,对于所有容器可见
hostNetwork: true #pod中容器直接使用宿主机的网络
hostIPC: true #pod中容器直接使用宿主机的IPC
hostPID: true #pod中容器看到宿主机正在运行的所有进程
containers:
- name: nginx
image: nginx
- name: shell
image: busybox
stdin: true
tty: true
Init Containers和Containers
ImagePullPolicy
定义镜像拉取策略,默认是 Always,即每次创建 Pod 都重新拉取一次镜像
可选Never 或者 IfNotPresent
Lifecycle
apiVersion: v1
kind: Pod
metadata:
name: lifecycle-demo
spec:
containers:
- name: lifecycle-demo-container
image: nginx
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
preStop:
exec:
command: ["/usr/sbin/nginx","-s","quit"]
