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"

 

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