1. 默认方式
给pod分配多网卡,没有特殊配置,默认是default ns的。这时net-attach-def的网卡分别从net1开始往上加,eth0为k8s默认cni的网卡名字
1.1 YAML方式:
$ cat <<EOF | kubectl create -f -
apiVersion: v1
kind: Pod
metadata:
name: pod-case-01
annotations:
k8s.v1.cni.cncf.io/networks: macvlan-conf-1, macvlan-conf-2
spec:
containers:
- name: pod-case-01
image: docker.io/centos/tools:latest
command:
- /sbin/init
EOF
1.2 JSON方式:
$ cat <<EOF | kubectl create -f -
apiVersion: v1
kind: Pod
metadata:
name: pod-case-04
annotations:
k8s.v1.cni.cncf.io/networks: '[
{ "name" : "macvlan-conf-1" },
{ "name" : "macvlan-conf-2" }
]'
spec:
containers:
- name: pod-case-04
image: docker.io/centos/tools:latest
command:
- /sbin/init
EOF
2. 指定ns的net-attach-def
2.1 YAML方式:
$ cat <<EOF | kubectl create -f -
apiVersion: v1
kind: Pod
metadata:
name: pod-case-02
annotations:
k8s.v1.cni.cncf.io/networks: testns1/macvlan-conf-3
spec:
containers:
- name: pod-case-02
image: docker.io/centos/tools:latest
command:
- /sbin/init
EOF
2.2 JSON方式
$ cat <<EOF | kubectl create -f -
apiVersion: v1
kind: Pod
metadata:
name: pod-case-05
annotations:
k8s.v1.cni.cncf.io/networks: '[
{ "name" : "macvlan-conf-1",
"namespace": "testns1" }
]'
spec:
containers:
- name: pod-case-05
image: docker.io/centos/tools:latest
command:
- /sbin/init
EOF
3. 指定网卡名字
给容器中net-attach-def对应的网卡命名,方式:在annotation后面加@ifname,如给容器使用macvlan-conf-1的网卡命名为macvlan1,也可以使用interface参数
3.1 YAML方式
$ cat <<EOF | kubectl create -f -
apiVersion: v1
kind: Pod
metadata:
name: pod-case-03
annotations:
k8s.v1.cni.cncf.io/networks: macvlan-conf-1@macvlan1
spec:
containers:
- name: pod-case-03
image: docker.io/centos/tools:latest
command:
- /sbin/init
EOF
3.2 JSON方式
$ cat <<EOF | kubectl create -f -
apiVersion: v1
kind: Pod
metadata:
name: pod-case-06
annotations:
k8s.v1.cni.cncf.io/networks: '[
{ "name" : "macvlan-conf-1",
"interface": "macvlan1" },
{ "name" : "macvlan-conf-2" }
]'
spec:
containers:
- name: pod-case-06
image: docker.io/centos/tools:latest
command:
- /sbin/init
EOF
4 路由配置
pod的默认路由一般是eth0对应网段的路由,可以通过JSON格式的default-route来指定默认路由
4.1 修改默认网卡
将默认路由配置为net1对应的网关,其中eth0为k8s默认cni的,net1为net-attach-def定义的网卡。
cat <<EOF | kubectl create -f -
apiVersion: v1
kind: Pod
metadata:
name: samplepod
annotations:
k8s.v1.cni.cncf.io/networks: '[{
"name": "macvlan-conf",
"default-route": ["192.168.2.1"]
}]'
spec:
containers:
- name: samplepod
command: ["/bin/bash", "-c", "trap : TERM INT; sleep infinity & wait"]
image: dougbtv/centos-network
EOF
4.2 设置default network
还有另外一种方式改默认路由,通过v1.multus-cni.io/default-network来配置默认网卡
这种方式创建的pod,eth0就是vpc2-net1的,默认路由是vpc2-net1的,net1为share-net2的。用于可以通过 ovn.kubernetes.io/logical_switch 和attachnet1.ns1.ovn.kubernetes.io/logical_switch来选择对应的网络
apiVersion: v1
kind: Pod
metadata:
name: p1-df-route5
namespace: ns1
annotations:
v1.multus-cni.io/default-network: ns2/attachnet2
ovn.kubernetes.io/logical_switch: vpc2-net1
k8s.v1.cni.cncf.io/networks: ns1/attachnet1
attachnet1.ns1.ovn.kubernetes.io/logical_switch: share-net2
spec:
containers:
- image: busybox
command:
- sleep
- "3600"