《OpenShift / RHEL / DevSecOps 汇总目录》
说明:本文已经在 OpenShift 4.12 的环境中验证
缺省情况下运行在 OpenShift Virtualization 中的 VM 使用的是 Pod 网络,因此只能通过 Pod 网络实现从 OpenShift 集群外部访问 VM 或从 VM 访问外部资源。由于每次 VM 重启后其对应的 Pod 都会变更其 IP,因此即便是在集群内部也无法直接用 IP 地址访问 VM。如果只需要从集群内部访问 VM,使用 Service 对应 Pod 即可,但如果需要从 OpenShift 集群外部访问 VM,还需要为 Service 绑定 NodePort 才可以。
可参考《OpenShift 4 - 通过 SSH 远程访问 OpenShift Virtualization 的虚拟机》一文。该文以运行在 22 端口的 SSH 服务为例,也适用于其他服务。
为了能够以固定 IP 访问OpenShift Virtualization 中 VM,我们可以为运行在 OpenShift Virtualization 中的 VM 配置第二个 Network Interface,并将其 IP 配置到外部网络(例如外部主机网段)。
此种方法虽然需要在 OpenShift 上进行单独的配置,但由于可通过外部主机使用的 IP 网段直接访问 VM,因此具有更高的网络性能。此方法另外的优势是由于该 VM 在第二个 Network Interface 上使用了外部 IP,因此当 VM 重启后,第二个 Network Interface 对应的 IP 地址不会发生变化。
VM 使用的外部 IP 可以是通过集群外部的 DHCP 分配的,也可是通过手动直接配置的。另外该 IP 的 DNS 解析也需要通过集群外部的 DNS 完成。
以下为 BareMetal 部署模式的 OpenShift 集群。
$ oc new-project my-vm
$ oc debug node/<WORKER_NODE_NAME> -- ip a | grep -B 2 192.168
Temporary namespace openshift-debug-f5gxs is created for debugging node...
Starting pod/ocp4-worker1aioexamplecom-debug ...
To use host binaries, run `chroot /host`
3: enp2s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 52:54:00:00:00:04 brd ff:ff:ff:ff:ff:ff
inet 192.168.123.104/24 brd 192.168.123.255 scope global dynamic noprefixroute enp2s0
apiVersion: nmstate.io/v1
kind: NodeNetworkConfigurationPolicy
metadata:
name: br-flat
spec:
nodeSelector:
node-role.kubernetes.io/worker: ""
desiredState:
interfaces:
- name: br-flat
description: Linux bridge with enp3s0 as a port
type: linux-bridge
state: up
ipv4:
dhcp: true
enabled: true
bridge:
options:
stp:
enabled: true
port:
- name: enp3s0
$ oc debug node/ocp4-worker1.aio.example.com -- ip a | grep -A 1 br-flat
Temporary namespace openshift-debug-z5w2p is created for debugging node...
Starting pod/ocp4-worker1aioexamplecom-debug ...
To use host binaries, run `chroot /host`
4: enp3s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel master br-flat state UP group default qlen 1000
link/ether 52:54:00:00:01:04 brd ff:ff:ff:ff:ff:ff
9: br-flat: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 52:54:00:00:01:04 brd ff:ff:ff:ff:ff:ff
inet 192.168.3.108/24 brd 192.168.3.255 scope global dynamic noprefixroute br-flat
valid_lft 2307sec preferred_lft 2307sec
$ oc debug node/ocp4-worker1.aio.example.com -- ip a | grep 192.168
Temporary namespace openshift-debug-lzqjb is created for debugging node...
Starting pod/ocp4-worker1aioexamplecom-debug ...
To use host binaries, run `chroot /host`
inet 192.168.123.104/24 brd 192.168.123.255 scope global dynamic noprefixroute enp2s0
inet 192.168.3.108/24 brd 192.168.3.255 scope global dynamic noprefixroute br-flat
$ oc debug node/ocp4-worker1.aio.example.com -- cat /host/etc/sysconfig/network-scripts/ifcfg-enp3s0
Temporary namespace openshift-debug-v6pms is created for debugging node...
Starting pod/ocp4-worker1aioexamplecom-debug ...
To use host binaries, run `chroot /host`
TYPE=Ethernet
NAME=enp3s0
DEVICE=enp3s0
ONBOOT=yes
BRIDGE_PORT_VLANS=2-4094
UUID=63aa2036-8665-f54d-9a92-c3035bad03f7
LLDP=no
BRIDGE_UUID=1d670e6a-944c-427b-994f-b4d2690e00b2
BRIDGE=br-flat
[fedora@fedora-1 ~] $ ip a show dev eth1
3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 02:9e:7c:00:00:01 brd ff:ff:ff:ff:ff:ff
altname enp2s0
inet 192.168.3.103/24 brd 192.168.3.255 scope global dynamic noprefixroute eth1
valid_lft 2905sec preferred_lft 2905sec
inet6 fe80::9fe3:1227:d23:1d9b/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[fedora@fedora-1 ~] $ nmcli con show 'Wired connection 1' | grep DHCP4
DHCP4.OPTION[1]: broadcast_address = 192.168.3.255
DHCP4.OPTION[2]: dhcp_client_identifier = 01:02:9e:7c:00:00:01
DHCP4.OPTION[3]: dhcp_lease_time = 3600
DHCP4.OPTION[4]: dhcp_server_identifier = 192.168.3.1
DHCP4.OPTION[5]: domain_name_servers = 192.168.3.1
DHCP4.OPTION[6]: expiry = 1684047661
DHCP4.OPTION[7]: host_name = fedora-1
DHCP4.OPTION[8]: ip_address = 192.168.3.103
DHCP4.OPTION[9]: next_server = 192.168.3.1
DHCP4.OPTION[10]: requested_broadcast_address = 1
DHCP4.OPTION[11]: requested_domain_name = 1
DHCP4.OPTION[12]: requested_domain_name_servers = 1
DHCP4.OPTION[13]: requested_domain_search = 1
DHCP4.OPTION[14]: requested_host_name = 1
DHCP4.OPTION[15]: requested_interface_mtu = 1
DHCP4.OPTION[16]: requested_ms_classless_static_routes = 1
DHCP4.OPTION[17]: requested_nis_domain = 1
DHCP4.OPTION[18]: requested_nis_servers = 1
DHCP4.OPTION[19]: requested_ntp_servers = 1
DHCP4.OPTION[20]: requested_rfc3442_classless_static_routes = 1
DHCP4.OPTION[21]: requested_root_path = 1
DHCP4.OPTION[22]: requested_routers = 1
DHCP4.OPTION[23]: requested_static_routes = 1
DHCP4.OPTION[24]: requested_subnet_mask = 1
DHCP4.OPTION[25]: requested_time_offset = 1
DHCP4.OPTION[26]: requested_wpad = 1
DHCP4.OPTION[27]: routers = 192.168.3.1
DHCP4.OPTION[28]: subnet_mask = 255.255.255.0
在上一步正式运行 fedora-1 前先进入 VM 的 YAML,在 cloudInitNoCloud 下面增加以下使用固定 IP 的网卡配置,保存后再创建 VM。
networkData: |
version: 2
ethernets:
eth1:
addresses:
- 192.168.3.201/24
$ virtctl console fedora-1
Successfully connected to fedora-1 console. The escape sequence is ^]
fedora-1 login:
[fedora@fedora-1 ~] ip a show eth1
[fedora@fedora-1 ~] nmcli con show
[fedora@fedora-1 ~] sudo nmcli con modify "Wired connection 1" +ipv4.addresses 192.168.3.201/24
[fedora@fedora-1 ~] sudo nmcli con modify "Wired connection 1" -ipv4.addresses 192.168.3.103/24
[fedora@fedora-1 ~] sudo nmcli con down "Wired connection 1"
[fedora@fedora-1 ~] sudo nmcli con up "Wired connection 1"
[fedora@fedora-1 ~] sudo yum install -y nginx
[fedora@fedora-1 ~] sudo systemctl start nginx
$ ping 192.168.3.201
$ curl 192.168.3.201
视频
https://www.cnblogs.com/djlsunshine/p/9733182.html
http://kubevirt.io/2023/OVN-kubernetes-secondary-networks.html
https://cloud.redhat.com/blog/using-the-multus-cni-in-openshift
https://github.com/nmstate/kubernetes-nmstate/blob/main/docs/examples/static-ip.yaml