# Pod (附上 cheetsheet) ###### tags: `Kubernetes` ## 一、Pod簡介 ### 1. 創建Pod ``` # 只能創建Pod kubectl run <pod-name> --image=<image> --restart=Never / --dry-run=client -o yaml > pod-def.yaml # 創建其他物件: kubectl create deploy nginx-deploy --image=nginx / --dry-run=client -o yaml > deploy.yaml # 比如 job 、 cronjob 、 namespace 、 quota、 # configmap 、 service 、 serviceaccount # 執行 kubectl apply -f pod-def.yaml ``` > restart=Never用來識別Pod,若restart=Always則是deployment; > --dry-run=client表示一些default的參數先拿掉,暫時不需要submit出去 (通常在輸出yaml時使用),若要直接創建物件不輸出yaml就無需加此參數 <br/> ``` # 查看 pod label kubectl get pod --show-labels # 新增 pod/node label kubectl label po <pod-name> <key>=<value> kubectl label nodes <pod-name> <key>=<valu # 刪除 pod/node label kubectl label po <pod-name> <key>- kubectl label nodes <node-name> <key>=<value>- # 對 pod 內部下達指令 kubectl exec <pod-name> -- <command> # 查看特定label的Pod kubectl get po --selector <key>=<value> # 將Pod expose出去 (創建一個Service) kubectl expose po <pod-name> --type=NodePort --name=<svc-name> --port=80 # 查看Log kubectl logs <pod-name> kubectl logs <pod-name> > haha.txt # 查看initContainer的log kubectl logs <pod-name> -c <initContainer-name> # 查看Cluster資訊 kubectl config view kubectl cluster-info ``` ### 2. 如何存取Pod內的container[1] ``` # Get a shell to the running container: kubectl exec --stdin --tty shell-demo -- /bin/bash # If pod has more than one container kubectl exec -i -t my-pod --container main-app -- /bin/bash ``` > Note: The short options -i and -t are the same as the long options --stdin and --tty <br> ## 二、Static Pod [2] #### Pod創建過程: Control plane 透過kube-apiserver命令Node中的kubelet,kubelet透過啟動一個名為 `k8s.gcr.io/pause:3.2` 的image來創建此Pod > docker images 命令好像可以看到image,但為甚麼我沒辦法看呢QQ #### Static Pod: * kubelet會定期去某個路徑(/etc/kubernetes/manifest)執行這些static pod,就算把執行的pod刪掉也沒用,還是會重新執行起來。 <br/> 但要設定一個環境變數: (我是沒有重啟就可以啦!) ``` KUBELET_ARGS="--cluster-dns=10.254.0.10 --cluster-domain=kube.local --pod-manifest-path=/etc/kubernetes/manifest" ## 接著重啟kubelet systemctl restart kubele ``` ## 三、 Pod Scheduling * nodeSelector * Taint&Toleration > Taints是允許Node抵制 (repel) 某些Pod,使Pod無法在該節點上運行 > Tolerations則是搭配Taints一同使用,擁有Tolerations的Pod可以被分派到擁有Taints的節點上,但前提是兩者能夠匹配 #### Taints用於Node;Tolerations則用於Pod ``` ## 新增 taint $ kubectl taint nodes <node-name> <key>=<value>:<effect> ## 例如 $ kubectl taint nodes g8node1 size=large:NoSchedule ## 刪除 taint $ kubectl taint nodes <node-name> <key>=<value>:<effect>- ## 例如 $ kubectl taint nodes g8node1 size=large:NoSchedule- ``` effect (當Pod因為此Taint而無法調度到該節點上的時候,該怎麼處理) 有三種: NoSchedule、PreferNoSchedule、NoExcute ## 參考資料: [1] [Get a Shell to a Running Container](https://kubernetes.io/docs/tasks/debug/debug-application/get-shell-running-container/) [2] [【從題目中學習k8s】-【Day6】奇怪的Pod - Static Pod](https://ithelp.ithome.com.tw/articles/10235803) [3] [Cheet Sheet](https://kubernetes.io/docs/reference/kubectl/cheatsheet/)