> 文章列表 > OpenShift 4 - 使用 virtctl 远程访问 OpenShift Virtualization 的虚拟机

OpenShift 4 - 使用 virtctl 远程访问 OpenShift Virtualization 的虚拟机

OpenShift 4 - 使用 virtctl 远程访问 OpenShift Virtualization 的虚拟机

《OpenShift / RHEL / DevSecOps 汇总目录》
说明:本文已经在支持 OpenShift 4.12 的 OpenShift 环境中验证

在《OpenShift 4 - 用 OpenShift Virtualization 运行容器化虚拟机 (视频)》一文中使用了 OpenShift 控制台直接访问运行在 OpenShift 上的 VM。本文将介绍如何通过 virtctl 方式从外部访问运行在 OpenShift 上的 VM。

文章目录

  • 下载 virtctl 命令
  • 创建 VM
  • virtctl start/stop 命令
  • virtctl console 命令
  • virtctl ssh 命令
  • virtctl port-forward 命令
  • virtctl expose 命令
  • virtctl scp 命令

下载 virtctl 命令

在 Virtualization 的 Overview 中点击 Download virctl 连接下载 virctl 命令。
OpenShift 4 - 使用 virtctl 远程访问 OpenShift Virtualization 的虚拟机

创建 VM

  1. 执行命令创建一个名为 testvm 的 VM。
$ oc apply -f - << EOF
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:name: testvm
spec:running: falsetemplate:metadata:labels:kubevirt.io/size: smallkubevirt.io/domain: testvmspec:domain:devices:disks:- name: containerdiskdisk:bus: virtio- name: cloudinitdiskdisk:bus: virtiointerfaces:- name: defaultmasquerade: {}resources:requests:memory: 64Mnetworks:- name: defaultpod: {}volumes:- name: containerdiskcontainerDisk:image: quay.io/kubevirt/cirros-container-disk-demo- name: cloudinitdiskcloudInitNoCloud:userDataBase64: SGkuXG4=
EOF
  1. 查看 VM 和 VMI 。
$ oc get vm
NAME                AGE   STATUS    READY
testvm              14h   Stopped   False
$ oc get vmi
No resources found in demo namespace.

virtctl start/stop 命令

启动 VM,然后查看运行的 VMI。

$ virtctl start testvm
VM testvm was scheduled to start
$ oc get vmi
NAME     AGE   PHASE     IP             NODENAME             READY
testvm   49s   Running   10.217.1.156   crc-rwwzd-master-0   True

virtctl console 命令

使用 virtctl 命令访问 testvm,根据提示登陆 VM 即可。

$ virtctl console testvm
Successfully connected to testvm console. The escape sequence is ^]login as 'cirros' user. default password: 'gocubsgo'. use 'sudo' for root.
testvm login: cirros
Password: 

使用 CTRL+] 组合键可退出 VM。

virtctl ssh 命令

  1. 远程登陆 VM。
$ virtctl ssh cirros@testvm
cirros@vmi/testvm.demo's password:
  1. 远程访问 VM 并执行命令。
$ virtctl ssh cirros@testvm.demo -c 'ls -al'
cirros@vmi/testvm.demo's password: 
total 16
drwxr-xr-x    2 cirros   cirros        1024 Apr  1 05:10 .
drwxrwxr-x    4 root     root          1024 Nov 20  2017 ..
-rw-------    1 cirros   cirros           5 Apr  1 04:55 .ash_history
-rwxr-xr-x    1 cirros   cirros          43 Nov 20  2017 .profile
-rwxr-xr-x    1 cirros   cirros          66 Nov 20  2017 .shrc

virtctl port-forward 命令

通过 virtctl port-forward 命令远程访问 VM。

$ ssh -o 'ProxyCommand=virtctl port-forward --stdio=true testvm.demo 22' cirros@testvm.demo

virtctl expose 命令

将 VM 的端口暴露为 Service。

$ virtctl expose vmi testvm --port=22 --name=myvm-ssh --type=NodePort
Service myvm-ssh successfully exposed for vmi testvm$ oc get svc myvm-ssh
NAME       TYPE       CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
myvm-ssh   NodePort   10.217.4.129   <none>        22:30465/TCP   38s

virtctl scp 命令

向 VM 复制或从 VM 复制文件。

$ touch hello
$ virtctl scp hello cirros@testvm.demo:./hello
cirros@vmi/testvm.demo's password: 

确认文件已经在 VM 中了。

$ virtctl ssh cirros@testvm.demo -c 'ls -l'
cirros@vmi/testvm.demo's password: 
total 11
-rw-------    1 cirros   cirros       0 Apr  1 05:10 hello