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

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

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

下载 virtctl 程序

在 Virtualization 的 Overview 中点击 Download virctl 连接下载 virctl 程序。
在这里插入图片描述

创建 VM

  1. 执行命令创建一个名为 testvm 的 VM。
$ oc apply -f - << EOF
apiVersion: kubevirt.io/v1
kind: VirtualMachine
metadata:
  name: testvm
spec:
  running: false
  template:
    metadata:
      labels:
        kubevirt.io/size: small
        kubevirt.io/domain: testvm
    spec:
      domain:
        devices:
          disks:
            - name: containerdisk
              disk:
                bus: virtio
            - name: cloudinitdisk
              disk:
                bus: virtio
          interfaces:
          - name: default
            masquerade: {}
        resources:
          requests:
            memory: 64M
      networks:
      - name: default
        pod: {}
      volumes:
        - name: containerdisk
          containerDisk:
            image: quay.io/kubevirt/cirros-container-disk-demo
        - name: cloudinitdisk
          cloudInitNoCloud:
            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 即可。注意:此种方法无需私钥即可登录 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 命令

注意:使用此种方法访问 RHEL/CentOS/Fedora,需要使用私钥才能登录 VM。

  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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值