【k8s-9】--深入pod對象

虛擬機級別的相關(guān)屬性都是pod級別的:描述機器整體。

pod幾個重要字段的含義和用法:

#NodeSelector:是一個供用戶將pod與Node進行綁定的字段
apiVersion: v1
kind: Pod
...
spec:
 nodeSelector:
   disktype: ssd
#這樣的配置意味pod永遠自能運行在攜帶ssd標(biāo)簽的節(jié)點上。

NodeName:一旦Pod的這個字段被賦值,k8s會以為Pod已經(jīng)經(jīng)過調(diào)度了用于測試。

HostAliases:定義Pod的host文件。/etc/hosts里面的內(nèi)容。

apiVersion: v1
kind: Pod
...
spec:
  hostAliases:
  - ip: "10.1.2.3"
    hostnames:
    - "foo.remote"
    - "bar.remote"
...
#最后的效果
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

例子: shareProcessNamespace: true

# 在這個文件中,定義了兩個容器:一個是nginx,一個開啟了tty和stdin的的容器。其實等同于docker run中的 -it(-i:stdin -t:tty)

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  shareProcessNamespace: true
  containers:
  - name: nginx
    image: nginx
  - name: shell
    image: busybox
    stdin: true
    tty: true

#創(chuàng)建
$ kubectl create -f nginx.yaml
#鏈接shell容器的tty
$ kubectl attach -it nginx -c shell
#查看進程。也就是在喲個pod上可以看到里面所有容器的進程。因為他們呢共享一個PID Namespace。
$ kubectl attach -it nginx -c shell
/ # ps ax
PID   USER     TIME  COMMAND
    1 root      0:00 /pause
    8 root      0:00 nginx: master process nginx -g daemon off;
   14 101       0:00 nginx: worker process
   15 root      0:00 sh
   21 root      0:00 ps ax

k8s項目對container的定義:image鏡像,Command(啟動命令),workingDir(容器的工作目錄),Port(容器開發(fā)端口),volumeMounts(容器掛載的Volume)。

1.ImagePullPolicy字段:Always 鏡像拉去策略。默認(rèn)是每創(chuàng)建就拉去一次。
2.Lifecycle字段:容器狀態(tài)發(fā)生變化時觸發(fā)一系列”鉤子“

apiVersion: v1
kind: Pod
metadata:
  name: lifecycle-demo
spec:
  containers:
  - name: lifecycle-demo-container
    image: nginx
    lifecycle:
      #容器啟動后立即執(zhí)行
      postStart:
        exec:
          command: ["/bin/sh", "-c", "echo Hello from the postStart handler > /usr/share/message"]
      #容器被殺死前
      preStop:
        exec:
          command: ["/usr/sbin/nginx","-s","quit"]

總結(jié):關(guān)于pod的狀態(tài)以及含義

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容