[没接触过kubevirt?]15分钟快速入门kubevirt

举报
秋意零 发表于 2022/04/17 17:41:20 2022/04/17
【摘要】 @TOC 本文介绍本文有两个版本,详细版、简洁版。前者适合新手,后者适合老手**(方便大家查找,从而过滤掉某些步骤,节约时间成本)** 所以大家按需查看哟。详细版简洁版简洁版:包含所有步骤,以及命令的执行过程(适合新手)简洁版:只包含命令(适合有一定熟练度的人) 前言什么是kubevirt?kubevirt是一个容器方式运行虚拟机的项目。kubevirt是附加kubernetes集群上的,它...

@TOC

本文介绍

本文有两个版本,详细版、简洁版

前者适合新手,后者适合老手**(方便大家查找,从而过滤掉某些步骤,节约时间成本)** 所以大家按需查看哟。

详细版 简洁版

简洁版:包含所有步骤,以及命令的执行过程(适合新手)

简洁版:只包含命令(适合有一定熟练度的人)

前言

  • 什么是kubevirt?

    kubevirt是一个容器方式运行虚拟机的项目。kubevirt是附加kubernetes集群上的,它是通过 CustomResourceDefinition(CRD)部署到Kubernetes API变成资源对象。使用方式类似创建deploy、pod…这些资源清单。

环境准备

  • 环境保证网络出外网
系统 服务
centos7 kubernetes集群

官网地址:https://kubevirt.io/

详细版

搭建步骤

安装KubeVirt

[root@master ~]# export RELEASE=v0.35.0
[root@master ~]# kubectl apply -f https://github.com/kubevirt/kubevirt/releases/download/${RELEASE}/kubevirt-operator.yaml
[root@master ~]# kubectl apply -f https://github.com/kubevirt/kubevirt/releases/download/${RELEASE}/kubevirt-cr.yaml
[root@master ~]# kubectl get pods -n kubevirt
NAME                               READY   STATUS    RESTARTS   AGE
virt-api-64999f7bf5-fblkh          1/1     Running   0          5m24s
virt-api-64999f7bf5-l8wzr          1/1     Running   0          5m24s
virt-controller-8696ccdf44-k9qpv   1/1     Running   0          4m52s
virt-controller-8696ccdf44-tpxwp   1/1     Running   0          4m52s
virt-handler-kl8tl                 1/1     Running   0          4m52s
virt-operator-78fbcdfdf4-468h4     1/1     Running   0          8m4s
virt-operator-78fbcdfdf4-nvvxx     1/1     Running   0          8m4s

所以服务启动后,执行下面这条命令

[root@master ~]# kubectl -n kubevirt wait kv kubevirt --for condition=Available
kubevirt.kubevirt.io/kubevirt condition met

安装virtctl客户端工具

[root@master ~]# export VERSION=v0.41.0
[root@master ~]# wget https://github.com/kubevirt/kubevirt/releases/download/${VERSION}/virtctl-${VERSION}-linux-amd64
[root@master kubevirt]# cp virtctl-v0.41.0-linux-amd64 /usr/local/bin/virtctl
[root@master kubevirt]# chmod +x /usr/local/bin/virtctl ; ll /usr/local/bin/
total 64000
-r-xr-xr-x. 1 root root 17586312 Jan 11 10:52 docker-compose
-rwxr-xr-x. 1 root root 47944250 Apr 16 16:58 virtctl
[root@master kubevirt]# virtctl version
Client Version: version.Info{GitVersion:"v0.41.0", GitCommit:"b77b858ac9345ae6858cf409e1833f4548ec2809", GitTreeState:"clean", BuildDate:"2020-11-09T13:38:36Z", GoVersion:"go1.13.14", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{GitVersion:"{gitVersion}", GitCommit:"{gitCommit}", GitTreeState:"{gitTreeState}", BuildDate:"{buildDate}", GoVersion:"go1.13.14", Compiler:"gc", Platform:"linux/amd64"}

创建VirtualMachine

  • VirtualMachine(vm)类似于docker镜像一个模板可以启动很多运行实例vmi:
[root@master kubevirt]# cat test.yaml
apiVersion: kubevirt.io/v1alpha3
kind: VirtualMachine
metadata:
  labels:
    kubevirt.io/vm: vm-cirros
  name: vm-cirros
spec:
  running: false
  template:
    metadata:
      labels:
        kubevirt.io/vm: vm-cirros
    spec:
      domain:
        devices:
          disks:
          - disk:
              bus: virtio
            name: containerdisk
          - disk:
              bus: virtio
            name: cloudinitdisk
        machine:
          type: ""
        resources:
          requests:
            memory: 64M
      terminationGracePeriodSeconds: 0
      volumes:
      - name: containerdisk
        containerDisk:
          image: kubevirt/cirros-container-disk-demo:latest
      - cloudInitNoCloud:
          userDataBase64: IyEvYmluL3NoCgplY2hvICdwcmludGVkIGZyb20gY2xvdWQtaW5pdCB1c2VyZGF0YScK
        name: cloudinitdisk
[root@master kubevirt]# kubectl apply -f test.yaml
virtualmachine.kubevirt.io/vm-cirros created

[root@master kubevirt]# kubectl get vm
NAME        AGE   VOLUME
vm-cirros   21m

启动VirtualMachineInstance

  • VirtualMachineInstance(vmi)类似于docker镜像的运行实例容器:
[root@master kubevirt]# virtctl start vm-cirros
VM vm-cirros was scheduled to start
[root@master kubevirt]# kubectl get vmi
NAME        AGE   PHASE     IP            NODENAME
vm-cirros   62s   Running   10.244.0.15   master

[root@master kubevirt]# virtctl console vm-cirros  # 进入虚拟机
Successfully connected to vm-cirros console. The escape sequence is ^]

login as 'cirros' user. default password: 'gocubsgo'. use 'sudo' for root.
vm-cirros login: cirros
Password:
$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue qlen 1
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1450 qdisc pfifo_fast qlen 1000
    link/ether 2e:3e:2a:46:29:94 brd ff:ff:ff:ff:ff:ff
    inet 10.244.0.16/24 brd 10.244.0.255 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::2c3e:2aff:fe46:2994/64 scope link tentative flags 08
       valid_lft forever preferred_lft forever
       
$    #  按 ctrl+]  退出虚拟机
$ [root@master kubevirt]#

启动和停止命令

spec.running 字段如果设置为true为启动、false为停止**

  • 创建 VirtualMachine后,可以像这样打开或关闭它:
# Start the virtual machine:  启动虚拟机
virtctl start vm   

# Stop the virtual machine:  停止虚拟机
virtctl stop vm
  • kubectl也可以使用:
# Start the virtual machine:  启动虚拟机
kubectl patch virtualmachine vm --type merge -p \
    '{"spec":{"running":true}}'

# Stop the virtual machine:  停止虚拟机
kubectl patch virtualmachine vm --type merge -p \
    '{"spec":{"running":false}}'

vm作为服务公开

VirtualMachine可以作为服务公开。实际服务将在 VirtualMachineInstance 启动后可用。

例如, 在创建VirtualMachine 后,将 SSH端口 (22) 公开为NodePort服务

[root@master kubevirt]# virtctl expose virtualmachine  vm-cirros --name vmiservice-node  --target-port 22  --port 24 --type NodePort
Service vmiservice-node successfully exposed for virtualmachine vm-cirros
[root@master kubevirt]# kubectl get svc
NAME              TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes        ClusterIP   10.96.0.1       <none>        443/TCP        95d
vmiservice-node   NodePort    10.106.62.191   <none>        24:31912/TCP   3s

最后使用远程工具连接即可:

** 注意 端口使用svc NodePort端口,如图所示:

255qE5oqA5pyvTG9ncw==,size_20,color_FFFFFF,t_70,g_se,x_16)在这里插入图片描述在这里插入图片描述

简洁版

安装KubeVirt

export RELEASE=v0.35.0
kubectl apply -f https://github.com/kubevirt/kubevirt/releases/download/${RELEASE}/kubevirt-operator.yaml
kubectl apply -f https://github.com/kubevirt/kubevirt/releases/download/${RELEASE}/kubevirt-cr.yaml
kubectl get pods -n kubevirt
kubectl -n kubevirt wait kv kubevirt --for condition=Available

安装virtctl客户端工具

export VERSION=v0.41.0
wget https://github.com/kubevirt/kubevirt/releases/download/${VERSION}/virtctl-${VERSION}-linux-amd64
cp virtctl-v0.41.0-linux-amd64 /usr/local/bin/virtctl
chmod +x /usr/local/bin/virtctl ; ll /usr/local/bin/
virtctl version

创建VirtualMachine

[root@master kubevirt]# cat test.yaml
apiVersion: kubevirt.io/v1alpha3
kind: VirtualMachine
metadata:
  labels:
    kubevirt.io/vm: vm-cirros
  name: vm-cirros
spec:
  running: false
  template:
    metadata:
      labels:
        kubevirt.io/vm: vm-cirros
    spec:
      domain:
        devices:
          disks:
          - disk:
              bus: virtio
            name: containerdisk
          - disk:
              bus: virtio
            name: cloudinitdisk
        machine:
          type: ""
        resources:
          requests:
            memory: 64M
      terminationGracePeriodSeconds: 0
      volumes:
      - name: containerdisk
        containerDisk:
          image: kubevirt/cirros-container-disk-demo:latest
      - cloudInitNoCloud:
          userDataBase64: IyEvYmluL3NoCgplY2hvICdwcmludGVkIGZyb20gY2xvdWQtaW5pdCB1c2VyZGF0YScK
        name: cloudinitdisk
[root@master kubevirt]# kubectl apply -f test.yaml
virtualmachine.kubevirt.io/vm-cirros created

[root@master kubevirt]# kubectl get vm
NAME        AGE   VOLUME
vm-cirros   21m

启动VirtualMachineInstance

virtctl start vm-cirros
kubectl get vmi
virtctl console vm-cirros  # 进入虚拟机
按 ctrl+]  退出虚拟机

启动和停止命令

virtctl start vm   
virtctl stop vm

# Start the virtual machine:  启动虚拟机
kubectl patch virtualmachine vm --type merge -p \
    '{"spec":{"running":true}}'

# Stop the virtual machine:  停止虚拟机
kubectl patch virtualmachine vm --type merge -p \
    '{"spec":{"running":false}}'

vm作为服务公开

[root@master kubevirt]# virtctl expose virtualmachine  vm-cirros --name vmiservice-node  --target-port 22  --port 24 --type NodePort
[root@master kubevirt]# kubectl get svc
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。