进入目录保存实验文件
并创建一个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>
使用镜像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
先给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
查看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为1
http://192.168.123.10:8080/restconf/operational/network-topology:network-topology
下发第一条流表
PUT
http://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
]]>