Docker (17)-Docker k8s-pod management-resource list and tags

1. Format and operation

- 格式如下:
	apiVersion: group/version  //指明api资源属于哪个群组和版本,同一个组可以有多个版本
	        $ kubectl api-versions		//查询命令
	
	kind: 		//标记创建的资源类型,k8s主要支持以下资源类别
	       Pod,ReplicaSet,Deployment,StatefulSet,DaemonSet,Job,Cronjob
	    
	metadata:	//元数据
	        name:	//对像名称
	        namespace:	//对象属于哪个命名空间
	        labels:	//指定资源标签,标签是一种键值数据
	        		
	spec: 		//定义目标资源的期望状态
	
	$ kubectl explain pod		//查询帮助文档

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

1.1 How to write format and content

## 方法一:书写时候的查找方法,可以之直接通过explain一步一步查找参数内容用法
[root@server2 ~]# kubectl explain pod      ##查看pod所有参数,-required-带有required是必须存在的参数
[root@server2 ~]# kubectl explain pod.apiVersion   ##查看api对应需要添加的参数

## 方法二: 直接打开一个已经有的deployment,然后生成相应的yaml文件,进行参考
[root@server2 ~]# kubectl get pod 
NAME   READY   STATUS    RESTARTS   AGE
demo   1/1     Running   0          3h51m 
[root@server2 ~]# kubectl get pod demo -o yaml   ##生成一个yaml文件



method one
Insert picture description here

Method Two
Bold style

1.2 Examples and execution

##如果俩个镜像使用的是同一个端口,只有一个可以成功
[root@server2 ~]# kubectl delete deployments.apps nginx  ##删除原来pod,为了下面进行实验
[root@server2 ~]# vim pod.yml    ##编写yml文件,可用性较高
[root@server2 ~]# cat pod.yml 
apiVersion: apps/v1
kind: Deployment    ##设置为deployment
metadata:
  name: nginx
  namespace: default   ##设置命名空间
spec:
  replicas: 1
  selector:
    matchLabels:
      run: nginx
  template:    ##模板
    metadata:
      labels:   ##标签
        run: nginx
    spec:
      #nodeSelector:        ##指定运行节点的俩种方法(方法一)
      #  kubernetes.io/hostname: server4
      #nodeName: server3    ##指定运行节点的俩种方法(方法二)
      #hostNetwork: true    ##可以通过直接访问节点ip的方式访问,[root@westos Desktop]# curl 172.25.13.4
      containers:
      - name: nginx
        image: myapp:v1
        imagePullPolicy: IfNotPresent
        resources:    ##资源限制
          requests:
            cpu: 100m
            memeory: 100Mi
          limits:
            cpu: 0.5
            memory: 512Mi
        #- name: busyboxplus
        #  image: busyboxplus
        #  imagePullPolicy: IfNotPresent  ##本地有镜像,优先本地拉取,没有就需要联网拉取
        #  stdin: true     ##stdin和tty是为了保证交互式,相当于执行参数-it
        #  tty: true
[root@server2 ~]# kubectl apply -f pod.yml    ##deployment使用apply,会自动更新修改内容
				## $ kubectl create -f demo.yaml  ##不是deployment使用create,不会自动更新修改内容
deployment.apps/nginx created
[root@server2 ~]# kubectl get pod
NAME                   READY   STATUS    RESTARTS   AGE
demo                   1/1     Running   0          4h47m
nginx-79cc587f-fs8l5   1/1     Running   0          2

Insert picture description here

Insert picture description here
Insert picture description here

2. Label

- 标签
	- $ kubectl get pod --show-labels		//查看标签
		NAME   READY   STATUS    RESTARTS   AGE   LABELS
		demo   2/2     Running   0          8s    app=demo
	  $ kubectl get pod -l app			//过滤包含app的标签
		NAME   READY   STATUS    RESTARTS   AGE
		demo   2/2     Running   0          34s
	  $ kubectl get pod -L app
		NAME   READY   STATUS    RESTARTS   AGE   APP
		demo   2/2     Running   0          39s   demo
	  $ kubectl label pod demo version=v1	//打标签
		pod/demo labeled
	  $ kubectl get pod --show-labels 
		NAME   READY   STATUS    RESTARTS   AGE    LABELS
		demo   2/2     Running   0          4m1s   app=demo,version=v1
	  $ kubectl label pod demo app=nginx --overwrite		//更改标签 
		pod/demo labeled
	  $ kubectl get pod --show-labels 
		NAME   READY   STATUS    RESTARTS   AGE     LABELS
		demo   2/2     Running   0          5m40s   app=nginx,version=v1

- 节点标签选择器
	- $ kubectl label nodes server2 disktype=ssd
		node/server2 labeled
	- $ kubectl get nodes -l disktype
		NAME      STATUS   ROLES    AGE   VERSION
		server2   Ready    <none>   6d    v1.17.2
	- 在yaml文件中增加标签选择器

## 1.修改节点标签
[root@server2 ~]# kubectl get node --show-labels    ##查看节点标签】
[root@server2 ~]# kubectl label nodes server3 app=nginx  ##打标签
[root@server2 ~]# kubectl get node server3 --show-labels    ##查看节点server3的标签
NAME      STATUS   ROLES    AGE   VERSION   LABELS
server3   Ready    <none>   42h   v1.20.2   app=nginx,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=server3,kubernetes.io/os=linux
[root@server2 ~]# kubectl label nodes server3 app=myapp --overwrite   ##重新覆盖
[root@server2 ~]# kubectl get node server3 --show-labels    ##查看修改内容
NAME      STATUS   ROLES    AGE   VERSION   LABELS
server3   Ready    <none>   42h   v1.20.2   app=myapp,beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,kubernetes.io/arch=amd64,kubernetes.io/hostname=server3,kubernetes.io/os=linux
[root@server2 ~]# kubectl label nodes server3 app-      ##删除标签

## 2. 修改pod标签
[root@server2 ~]# kubectl get pod --show-labels 
NAME                     READY   STATUS    RESTARTS   AGE     LABELS
demo                     1/1     Running   0          5h11m   run=demo
nginx-75d94ffc64-hs8kn   1/1     Running   0          21m     pod-template-hash=75d94ffc64,run=nginx
[root@server2 ~]# kubectl label pod demo run=demo2 --overwrite 
pod/demo labeled
[root@server2 ~]# kubectl get pod --show-labels 
NAME                     READY   STATUS    RESTARTS   AGE     LABELS
demo                     1/1     Running   0          5h12m   run=demo2

##3.查看有相应标签的项
[root@server2 ~]# kubectl get pod -L run

1. Modify the node label
Insert picture description here
Insert picture description here
Insert picture description here

2. Modify the pod tag
Insert picture description here

3. View items with corresponding tags

Insert picture description here

3. Clean up the deployment just set

[root@server2 ~]# kubectl delete -f pod.yml 
deployment.apps "nginx" deleted
[root@server2 ~]# kubectl get pod
NAME   READY   STATUS    RESTARTS   AGE
demo   1/1     Running   0          5h16m

Insert picture description here

Guess you like

Origin blog.csdn.net/qwerty1372431588/article/details/113917795