Skip to content

K8s Pod

On the right hand are reference infomations for create kubernetes resources. resources manifast / official document.


Create Pod using yaml file

pod.yaml
apiVersion: v1 # api 文档版本
kind: Pod #资源对象类型,其他的:Deployment、StatefulSet 
metadata: #Pod的相关元数据,用于描述pod的数据
  name: my-nginx  #pod的名称
  labels: #定义pod标签
    type: app
    version: 1.0.x #自定义
  namespace: 'default' #配置命名空间,默认的是default
spec:  #期望pod 按照这里的描述进行创建
  containers:
  - name: nginx
    image: nginx:1.26.0
    imagePullPolicy: IfNotPresent #指定镜像拉取策略,如果本地没有就拉取远程
    command:
    - nginx
    - -g
    - 'daemon off;' #意为:nginx -g 'daemon off;'
    workingDir: /usr/share/nginx/html #指定容器启动后的工作目录
    ports:
    - name: http #端口名称
      containerPort: 80 #描述容器内要暴露的端口
      protocol: TCP #描述该端口是基于那种协议通信
    env: # 环境变量
      - name: JVM-OPTS #环境名称
        value: '-Xms128m -Xmx128m' #环境变量的值
    resources:
      requests:
        cpu: 100m # cpu一个核心为1000m,这里的100m 为最少0.1个核心
        memory: "128Mi" #最少128兆
      limits:
        cpu: 200m # cpu一个核心为1000m,这里的200m 为最多0.2个核心
        memory: "256Mi" #最多256兆
    #command: ["stress"]
    #args: ["--vm", "1", "--vm-bytes", "150M", "--vm-hang", "1"]
  restartPolicy: OnFailure #重启策略:只用失败的情况才会重启

check the pod

kubectl get po
kubectl get po -o wide
kubectl descrbe po my-nginx
Operation_result
[root@k8s-master pods]# kubectl get pod
NAME            READY   STATUS    RESTARTS   AGE
memory-demo-3   1/1     Running   0          4m50s
my-nginx        1/1     Running   0          3m31s
[root@k8s-master pods]# kubectl get pod -o wide
NAME            READY   STATUS    RESTARTS   AGE     IP              NODE        NOMINATED NODE   READINESS GATES
memory-demo-3   1/1     Running   0          4m55s   192.168.36.72   k8s-node1   <none>           <none>
my-nginx        1/1     Running   0          3m36s   192.168.36.73   k8s-node1   <none>           <none>
[root@k8s-master pods]# kubectl describe pod my-nginx
Name:         my-nginx
Namespace:    default
Priority:     0
Node:         k8s-node1/10.0.0.101
Start Time:   Wed, 24 Apr 2024 06:49:02 -0400
Labels:       type=app
              version=1.0.x
Annotations:  cni.projectcalico.org/containerID: bb27fe44a58187928bced7bb6a76234c1717ea8a000d35e8948796a7970b548a
              cni.projectcalico.org/podIP: 192.168.36.73/32
              cni.projectcalico.org/podIPs: 192.168.36.73/32
Status:       Running
IP:           192.168.36.73
IPs:
  IP:  192.168.36.73
Containers:
  nginx:
    Container ID:  docker://71f60f7711b116cf0a6dfa8041381f83c51d40b35ef15f4c0c8756e12ce45c33
    Image:         nginx:1.26.0
    Image ID:      docker-pullable://nginx@sha256:3f28ecedbd787b3ff0a6d56cd4a1b13901acda532c717e3b7351fcc786973d79
    Port:          80/TCP
    Host Port:     0/TCP
    Command:
      nginx
      -g
      daemon off;
    State:          Running
      Started:      Wed, 24 Apr 2024 06:49:03 -0400
    Ready:          True
    Restart Count:  0
    Limits:
      cpu:     200m
      memory:  256Mi
    Requests:
      cpu:     100m
      memory:  128Mi
    Environment:
      JVM-OPTS:  -Xms128m -Xmx128m
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-mkwwb (ro)
Conditions:
  Type              Status
  Initialized       True 
  Ready             True 
  ContainersReady   True 
  PodScheduled      True 
Volumes:
  kube-api-access-mkwwb:
    Type:                    Projected (a volume that contains injected data from multiple sources)
    TokenExpirationSeconds:  3607
    ConfigMapName:           kube-root-ca.crt
    ConfigMapOptional:       <nil>
    DownwardAPI:             true
QoS Class:                   Burstable
Node-Selectors:              <none>
Tolerations:                 node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
                            node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
  Type    Reason     Age    From               Message
  ----    ------     ----   ----               -------
  Normal  Scheduled  3m55s  default-scheduler  Successfully assigned default/my-nginx to k8s-node1
  Normal  Pulled     3m55s  kubelet            Container image "nginx:1.26.0" already present on machine
  Normal  Created    3m55s  kubelet            Created container nginx
  Normal  Started    3m55s  kubelet            Started container nginx
[root@k8s-master pods]#