Docker&k8s - 創建pod

 

一 創建簡單pod

vi test.yaml

輸入內容:

apiVersion: v1
kind: Pod
metadata:
  name: command-demo
  labels:
    purpose: demonstrate-command
spec:
  containers:
  - name: command-demo-container
    image: debian
    command: ["printenv"]
    args: ["HOSTNAME", "KUBERNETES_PORT"]

保存文件後,shell中執行創建命令:

kubectl create -f test.yaml 

查看pods列表:

kubectl get pods

查看pod的日誌:

kubectl logs command-demo

可以看到執行內容:

command-demo
tcp://10.96.0.1:443

二 給容器分配CPU和RAM資源

當創建一個Pod的時候,你可以爲運行在Pod中的容器請求CPU和RAM資源。你還可以設置CPU和RAM資源的限制。請求CPU和RAM資源,在配置文件裏面包含resources:rquests字段。設置CPU和RAM限制包含resource:limits字段。如果節點上具有足夠的CPU和RAM資源可用於所有容器要求的CPU和RAM總和,Kubernetes將把Pod調度在上面。同樣當容器運行在節點上時,Kubernetes不允許容器消耗的CPU和RAM資源超出指定的容器的限制。如果容器超出RAM限制,將會結束。如果CPU超出限制,將成爲CPU節流的候選者。

pod配置文件內容(測試時保存的文件名爲testWithCpuAndMem.yaml,可以自行調整):

apiVersion: v1
kind: Pod
metadata:
  name: cpu-ram-demo
spec:
  containers:
  - name: cpu-ram-demo-container
    image: gcr.io/google-samples/node-hello:1.0
    resources:
      requests:
        memory: "64Mi"
        cpu: "250m"
      limits:
        memory: "128Mi"
        cpu: "1"

執行創建:

kubectl create -f testWithCpuAndMem.yaml 

顯示pod信息:

kubectl describe pod cpu-ram-demo

我本地的展示內容:

Name:               cpu-ram-demo
Namespace:          default
Priority:           0
PriorityClassName:  <none>
Node:               docker-desktop/192.168.65.3
Start Time:         Sun, 07 Jun 2020 10:06:38 +0800
Labels:             <none>
Annotations:        <none>
Status:             Pending
IP:                 10.1.0.11
Containers:
  cpu-ram-demo-container:
    Container ID:   
    Image:          gcr.io/google-samples/node-hello:1.0
    Image ID:       
    Port:           <none>
    Host Port:      <none>
    State:          Waiting
      Reason:       ImagePullBackOff
    Ready:          False
    Restart Count:  0
    Limits:
      cpu:     1
      memory:  128Mi
    Requests:
      cpu:        250m
      memory:     64Mi
    Environment:  <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-nglmp (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             False 
  ContainersReady   False 
  PodScheduled      True 
Volumes:
  default-token-nglmp:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-nglmp
    Optional:    false
QoS Class:       Burstable
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:
  Type     Reason          Age                   From                     Message
  ----     ------          ----                  ----                     -------
  Normal   Scheduled       28m                   default-scheduler        Successfully assigned default/cpu-ram-demo to docker-desktop
  Normal   SandboxChanged  28m                   kubelet, docker-desktop  Pod sandbox changed, it will be killed and re-created.
  Warning  Failed          26m (x6 over 28m)     kubelet, docker-desktop  Error: ImagePullBackOff
  Normal   Pulling         26m (x4 over 28m)     kubelet, docker-desktop  Pulling image "gcr.io/google-samples/node-hello:1.0"
  Warning  Failed          26m (x4 over 28m)     kubelet, docker-desktop  Failed to pull image "gcr.io/google-samples/node-hello:1.0": rpc error: code = Unknown desc = Error response from daemon: Get https://gcr.io/v2/: net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
  Warning  Failed          26m (x4 over 28m)     kubelet, docker-desktop  Error: ErrImagePull
  Normal   BackOff         3m33s (x99 over 28m)  kubelet, docker-desktop  Back-off pulling image "gcr.io/google-samples/node-hello:1.0"

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章