春风十里不如你 —— Taozi - 自动化 https://www.xiongan.host/index.php/tag/%E8%87%AA%E5%8A%A8%E5%8C%96/ 【k8s】标签Label与Label Selector https://www.xiongan.host/index.php/archives/207/ 2023-05-16T20:43:42+08:00 Label与Label Selector标签进入目录保存实验文件并创建一个yaml使用多个标签[root@master tz123]# cd /root/tz123/labfile/labelfile [root@master labelfile]# vim labelpod.yaml kind: Pod apiVersion: v1 metadata: name: labelpod labels:   app: busybox   version: new spec: containers:   - name: labelpod     image: busybox     args:     - /bin/sh     - -c     - sleep 30000创建Pod,并查看pod的label[root@master labelfile]# kubectl apply -f labelpod.yaml pod/labelpod created [root@master labelfile]# kubectl get pod --show-labels NAME       READY   STATUS   RESTARTS   AGE   LABELS labelpod   1/1     Running   0         11s   app=busybox,version=new为容器添加新标签[root@master labelfile]# kubectl label pod labelpod time=2019 pod/labelpod labeled [root@master labelfile]# kubectl get pod --show-labels NAME       READY   STATUS   RESTARTS   AGE   LABELS labelpod   1/1     Running   0         69s   app=busybox,time=2019,version=new标签选择器创建新的yaml[root@master labelfile]# vim labelpod2.yaml kind: Pod apiVersion: v1 metadata: name: labelpod2 labels:   app: httpd   version: new spec: containers:   - name: httpd     image: httpd创建并查看新创建的labelpod2[root@master labelfile]# kubectl apply -f labelpod2.yaml [root@master labelfile]# kubectl get pod --show-labels NAME       READY   STATUS             RESTARTS   AGE   LABELS labelpod   1/1     Running             0         12m   app=busybox,time=2019,version=new labelpod2   0/1     ContainerCreating   0         23s   app=httpd,version=new使用给予等值的标签选择器[root@master labelfile]# kubectl get pod -l app=httpd NAME       READY   STATUS   RESTARTS   AGE labelpod2   1/1     Running   0         100s 或 [root@master labelfile]# kubectl get pod -l app==httpd NAME       READY   STATUS   RESTARTS   AGE labelpod2   1/1     Running   0         114s使用基于不等值的标签选择器和查看pod针对某标签键的值[root@master labelfile]# kubectl get pod -l app!=httpd NAME       READY   STATUS   RESTARTS   AGE labelpod   1/1     Running   0         14m [root@master labelfile]# kubectl get pod -L app NAME       READY   STATUS   RESTARTS   AGE   APP labelpod   1/1     Running   0         15m   busybox labelpod2   1/1     Running   0         3m5s   httpd使用标签选择器实现调度将节点1打上标签并查看[root@master labelfile]# kubectl label node node env=test node/node labeled [root@master labelfile]# kubectl get node -L env NAME     STATUS   ROLES                 AGE   VERSION   ENV master   Ready   control-plane,master   91d   v1.20.6   node     Ready   <none>                 91d   v1.20.6   test使用nodeselector实现调度,创建新的yaml文件[root@master labelfile]# vim nsdeploy.yaml kind: Deployment apiVersion: apps/v1 metadata: name: nginx-dy labels:   app: nginx spec: replicas: 3 selector:   matchLabels:     app: nginx template:   metadata:     labels:       app: nginx   spec:     containers:     - name: nginx       image: nginx:1.7.9       ports:       - containerPort: 80     nodeSelector:       env: test查看deployment中的pod位置[root@master labelfile]# kubectl get pod -o wide NAME                       READY   STATUS   RESTARTS   AGE     IP               NODE   NOMINATED NODE   READINESS GATES labelpod                   1/1     Running   0         28m     10.244.167.145   node   <none>           <none> labelpod2                   1/1     Running   0         15m     10.244.167.146   node   <none>           <none> nginx-dy-6dd6c76bcb-667ss   1/1     Running   0         5m19s   10.244.167.148   node   <none>           <none> nginx-dy-6dd6c76bcb-q8tqh   1/1     Running   0         5m19s   10.244.167.149   node   <none>           <none> nginx-dy-6dd6c76bcb-xc9h7   1/1     Running   0         5m19s   10.244.167.147   node   <none>           <none>使用 node affinity 调度,创建一个新的 yaml 文件 nadeploy2.yaml[root@master labelfile]# vim nadeploy2.yaml kind: Deployment apiVersion: apps/v1 metadata: name: httpd-dy labels:   app: httpd spec: replicas: 3 selector:   matchLabels:     app: httpd template:   metadata:     labels:       app: httpd   spec:     containers:     - name: httpd       image: httpd     affinity:       nodeAffinity:         requiredDuringSchedulingIgnoredDuringExecution:           nodeSelectorTerms:           - matchExpressions:             - key: env               operator: In               values:               - test创建deployment并查看deployment中的pod位置,三个pod都在node上[root@master labelfile]# kubectl apply -f nadeploy2.yaml deployment.apps/httpd-dy created [root@master labelfile]# kubectl get pod -o wide NAME                       READY   STATUS             RESTARTS   AGE   IP               NODE   NOMINATED NODE   READINESS GATES httpd-dy-5b4bb9646-g4jzb   1/1     Running             0         33s   10.244.167.150   node   <none>           <none> httpd-dy-5b4bb9646-lb876   1/1     Running             0         33s   10.244.167.151   node   <none>           <none> httpd-dy-5b4bb9646-q7zcm   0/1     ContainerCreating   0         33s   <none>           node   <none>           <none> labelpod                   1/1     Running             0         38m   10.244.167.145   node   <none>           <none> labelpod2                   1/1     Running             0         26m   10.244.167.146   node   <none>           <none> nginx-dy-6dd6c76bcb-667ss   1/1     Running             0         15m   10.244.167.148   node   <none>           <none> nginx-dy-6dd6c76bcb-q8tqh   1/1     Running             0         15m   10.244.167.149   node   <none>           <none> nginx-dy-6dd6c76bcb-xc9h7   1/1     Running             0         15m   10.244.167.147   node   <none>           <none>实训任务创建一个deployment使用镜像nginx,5个副本deployment中的pod不能出现在node上[root@master labelfile]# vim shixun01.yaml kind: Deployment apiVersion: apps/v1 metadata: name: nginx-dy labels:   app: nginx spec: replicas: 1 selector:   matchLabels:     app: nginx template:   metadata:     labels:       app: nginx   spec:     containers:     - name: nginx       image: nginx     affinity:       nodeAffinity:         requiredDuringSchedulingIgnoredDuringExecution:           nodeSelectorTerms:           - matchExpressions:             - key: env               operator: Not In               values:               - node寻找一种方式搜索出kubernetes系统上提供core-dns,kubeproxy以及dashboard服务的pod先给core-dns,kubeproxy,dashboard打上标签[root@master labelfile]# kubectl label -n kube-system pod kube-proxy-kj8j5 app=kubeproxy [root@master labelfile]# kubectl label -n kube-system pod coredns-7f89b7bc75-n224r app=coredns查找关键词的pod搜索dashboard的pod使用标签和标签选择器,使用一条命令删除node2节点的nginx 【SDN】使用Postman下发流表 https://www.xiongan.host/index.php/archives/206/ 2023-05-11T10:25:41+08:00 使用Postman下发流表实验环境检查查看Opendaylight控制器登陆Opendaylight控制器,在查看控制器主机的6633端口监听状态root@guest-virtual-machine:/home/guest# netstat -an|grep 6633关闭防火墙sudo ufw disable访问WEB页面在OVS主机(Miniet主机)中创建拓扑结构,并测试连通性sudo mn --topo=single,3 --controller=remote,ip=192.168.123.10,port=6633 --switch ovsk,protocols=OpenFlow13在控制器页面查看到的拓扑图使用postman查看交换机id信息,交换机id为1http://192.168.123.10:8080/restconf/operational/network-topology:network-topology下发第一条流表PUThttp://192.168.123.10:8080/restconf/config/opendaylight-inventory:nodes/node/openflow:1/table/0/flow/1主机1的MAC地址:00:0c:29:91:9c:e6 主机2的MAC地址:42:59:6f:b2:ee:64<?xml version="1.0" encoding="UTF-8" standalone="no"?> <flow xmlns="urn:opendaylight:flow:inventory"> <priority>200</priority> <flow-name>Foo1</flow-name> <idle-timeout>0</idle-timeout> <hard-timeout>0</hard-timeout> <match> <ethernet-match> <ethernet-source> <address>00:0c:29:91:9c:e6</address> </ethernet-source> <ethernet-destination> <address>42:59:6f:b2:ee:64</address> </ethernet-destination> </ethernet-match> </match> <id>1</id> <table_id>0</table_id> <instructions> <instruction> <order>0</order> <apply-actions> <action> <order>0</order> <output-action> <output-node-connector>2</output-node-connector> </output-action> </action> </apply-actions> </instruction> </instructions> </flow>下发第二条流表http://192.168.123.10:8080/restconf/config/opendaylight-inventory:nodes/node/openflow:1/table/0/flow/2<?xml version="1.0" encoding="UTF-8" standalone="no"?> <flow xmlns="urn:opendaylight:flow:inventory"> <priority>200</priority> <flow-name>Foo1</flow-name> <idle-timeout>0</idle-timeout> <hard-timeout>0</hard-timeout> <match> <ethernet-match> <ethernet-source> <address>42:59:6f:b2:ee:64</address> </ethernet-source> <ethernet-destination> <address>00:0c:29:91:9c:e6</address> </ethernet-destination> </ethernet-match> </match> <id>2</id> <table_id>0</table_id> <instructions> <instruction> <order>0</order> <apply-actions> <action> <order>0</order> <output-action> <output-node-connector>1</output-node-connector> </output-action> </action> </apply-actions> </instruction> </instructions> </flow>登陆交换机,查看流表ovs-ofctl dump-flows s1删除第一条流表http://192.168.123.10:8080/restconf/config/opendaylight-inventory:nodes/node/openflow:1/table/0/flow/1删除第二条流表http://192.168.123.10:8080/restconf/config/opendaylight-inventory:nodes/node/openflow:1/table/0/flow/2