此筆記主要分享如何操作pod,因為筆者我常常把dockerdocker-composeCICD 混再一起,所以我覺得操作指令都不難,但建置 K8s 處處碰壁,建議如果要了解如何用 k8s ,就先拿已經包好的Image 來練習。

再來就是覺得困難的點在於包裝容器,因為每個應用程式,就是你寫的那個程式(我是比較常用laravel),每個Image 包裝的流程會因為你的程式有一些不同,所以覺得處處碰壁,建議可以依照簡單的範例試試看,但未來自己包Image 時如果遇到問題,至少先想辦法包出一個可以用的app Image。

TL;NR

  • 調度的最小單位
  • 一個或多個的容器(container)的一個群組稱為pod
  • 共享相同資源 volume ,都是同一個ip位置

yml 範例

使用 yml 定義 pod,筆者目前是把它想像成一個 docker-compose 檔案的概念,可以依照設定操作多個container,只是寫法略為不同,這裡就是撰寫如何去建立這個 pod

apiVersion: v1
kind: Pod
metadata:
  name: nginx-busybox
spec:
  containers:
  - name: nginx
    image: nginx
    ports:
    - containerPort: 80
  - name: busybox
    image: busybox
    command: ["/bin/sh"]
    args: ["-c", "while true; do echo hello; sleep 10;done"]

Pod基本操作

確認自己的設定沒有問題以後,針對Pod 新增、查看所有pod、pod的資料、刪除pod、進入pod 指令做介紹。

create pod

使用 yml 檔案建立 pod

$ kubectl create -f nginx_busybox.yml
pod "nginx-busybox" created

get pod list

$ kubectl get pods
NAME            READY     STATUS    RESTARTS   AGE
nginx-busybox   2/2       Running   0          57s

get pod detail

使用 describe 來查看 pod 資訊,如下指令是查看 nginx-busybox 這個pod 的資訊

$ kubectl describe pod nginx-busybox

或者使用 get pod 並且加上 -o 顯示更多 pod 的資訊,可以搭配wide, json, yaml ...

$ kubectl get pods nginx-busybox -o wide
NAME            READY     STATUS    RESTARTS   AGE       IP           NODE
nginx-busybox   2/2       Running   0          3m        172.17.0.4   minikube

get into pod(container)

如果要進入 pod(有時候想直接在測試的環境修改container),可以用以下指令進入 pod Image run 起來 container 中。 因為我們pod run 兩個container 有以下提示

$ kubectl exec nginx-busybox -it sh
Defaulting container name to nginx.
Use 'kubectl describe pod/nginx-busybox -n default' to see all of the containers in this pod.
#

delete pod

執行以下指令即可刪除pod

$ kubectl delete -f nginx_busybox.yml
pod "nginx-busybox" deleted
Victor
Victor

哈囉!

文章: 221

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *