@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 端口
**,如图所示:
简洁版
装置 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