kubernetes集群核心概念---pod

1.pod介绍

参考链接: https://kubernetes.io/zh/docs/concepts/workloads/pods/

1.1.查看pod

[root@centos ~]# kubectl get pod # pod或pods都可以,不指定namespace,默认是名为default的namespace
	
[root@centos ~]# kubectl get pod -n kube-system

1.2.pod的yaml资源清单格式

apiVersion: v1
kind: Pod
metadata:
  name:
  labels:
spec:
  hostNetwork: true|false
  nodeName: "指定的节点名称必须在etcd中有记录"
  volumes:
  - name: data01
    emptyDir:{}
  - name: data02
    hostPath:
      path: 
  - name: data03
    nfs:
      server:
      path:
  - name: data04
    configMap:
      name: 
      items:	  
  contaners:
  - name:
    image:
	env:
	- name:
	  value:
	- name:
	  value:
	command: []
	args: []
	ports:
	- containerPort:
	  hostIP:
	  hostPort:
	  name:
	  protocol:
	imagePullPolicy: Alwasy|Never|IfNotPresent
	resources:
	   request:
	     cpu:
		 memory:
	   limits:
	     cpu:
		 memory:
	volumeMounts:
	- name:
	  mountPath:
status:

1.3.yaml格式查找帮助方法

[root@centos ~]# kubectl explain namespace

[root@centos ~]# kubectl explain pod
[root@centos ~]# kubectl explain pod.spec
[root@centos ~]# kubectl explain pod.spec.containers

2.pod创建

2.1.创建一个名为pod-nginx的pod

[root@centos ~]# kubectl run nginx1 --image=nginx:1.15-alpine
pod/nginx1 created

[root@centos ~]# kubectl get pods
NAME             READY   STATUS    RESTARTS   AGE
nginx1           1/1     Running   0          41s

2.2.YAML创建pod

[root@centos ~]# vim pod1.yml
apiVersion: v1		# api版本
kind: Pod		# 资源类型为Pod
metadata:
  name: pod-test-stress		# 自定义pod的名称
spec:
  containers:		# 定义pod里包含的容器
  - name: c1		# 自定义pod中的容器名
    image: polinux/test-stress		# 启动容器的镜像名
    command: ["test-stress"]			# 自定义启动容器时要执行的命令(类似dockerfile里的CMD)
    args: ["--vm", "1", "--vm-bytes", "150M", "--vm-hang", "1"] # 自定义启动容器执行命令的参数
    
# polinux/test-stress这个镜像用于压力测试,在启动容器时传命令与参数就是相当于分配容器运行时需要的压力

[root@centos ~]# kubectl apply -f pod1.yml
pod/pod-test-stress created

3.查看pod信息

3.1.查看pod信息

[root@centos ~]# kubectl get pod
NAME          READY   STATUS    RESTARTS   AGE
pod-test-stress   1/1     Running   0          20s

3.2.查看pod详细信息

[root@centos ~]# kubectl get pods -o wide
NAME         READY   STATUS    RESTARTS   AGE   IP              NODE          NOMINATED NODE   READINESS GATES
pod-test-stress   1/1     Running   0          71s   10.244.194.72   k8s-worker1   <none>           <none>

3.3.描述pod详细信息

[root@k8s-master1 ~]# kubectl describe pod pod-test-stress 
......
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  102s  default-scheduler  Successfully assigned default/pod-test-stress to k8s-worker1
  Normal  Pulling    102s  kubelet            Pulling image "polinux/test-stress"
  Normal  Pulled     83s   kubelet            Successfully pulled image "polinux/test-stress" in 18.944533343s
  Normal  Created    83s   kubelet            Created container c1
  Normal  Started    82s   kubelet            Started container c1

4.删除pod

4.1.单个pod删除

# kubectl delete pod pod-test-stress
# kubectl delete -f pod1.yml

4.2.多个pod删除

# kubectl delete pod pod名1 pod名2 pod名3 ......

# kubectl get pods |awk 'NR>1 {print $1}' |xargs kubectl  delete pod

# kubectl delete ns xxxx

5.pod标签

5.1.查看pod标签

kubectl get pods --show-labels

5.2.打标签并查看

# kubectl label pod pod-test-stress region=huanai zone=A env=test-stress bussiness=game
pod/pod-test-stress labeled

# kubectl get pods --show-labels
NAME               READY   STATUS    RESTARTS   AGE     LABELS
pod-test-stress    1/1     Running   0          8m54s   bussiness=game,env=test-stress,region=huanai,zone=A

5.3.通过等值关系标签查询

# kubectl get pods -l zone=A
NAME              READY     STATUS    RESTARTS   AGE
pod-test-stress   1/1     Running   0          9m22s

5.4.通过集合关系标签查询

kubectl get pods -l "zone in (A,B,C)"
NAME              READY   STATUS    RESTARTS   AGE
pod-test-stress   1/1     Running   0          9m55s

5.5.删除标签后再验证

[root@k8s-master1 ~]# kubectl label pod pod-stress region- zone- env- bussiness-
pod/pod-test-stress labeled

[root@k8s-master1 ~]# kubectl get pods --show-labels
NAME               READY   STATUS    RESTARTS   AGE   LABELS
pod-test-stress    1/1     Running   0          16m     <none>

5.6.通过YAML创建Pod时添加标签

1.修改yaml
# vim pod1.yml
apiVersion: v1
kind: Pod
metadata:
  name: pod-test-stress
  namespace: default
  labels:
    env: dev
    app: nginx				# 直接在原来的yaml里加上多个标签
spec:
  containers:
  - name: c1
    image: polinux/stress
    command: ["stress"]
    args: ["--vm", "1", "--vm-bytes", "150M", "--vm-hang", "1"]
    imagePullPolicy: IfNotPresent
	
2.直接apply应用
# kubectl apply -f pod1.yaml
pod/pod-stress1 configured

3.验证
kubectl get pods --show-labels
NAME              READY   STATUS             RESTARTS   AGE     LABELS
pod-test-stress   1/1     Running            0          3m5s    app=nginx,env=dev
posted @ 2024-02-07 15:07  只为心情愉悦  阅读(37)  评论(0编辑  收藏  举报