目录
3、通过脚本安装 vCenter,指定第一参数为 OVA 的绝对路径。运行完后将会自动将 ova 导入到 vCenter,并启动虚拟机;
govc-github项目链接
一、安装
RHEL/CentOS环境
1、安装go环境
安装包下载地址为:https://golang.org/dl/。
如果打不开可以使用这个地址:Downloads - The Go Programming Language
1、下载二进制包:go1.17.7.linux-amd64.tar.gz;
# ls
go1.17.7.linux-amd64.tar.gz
2、将下载的二进制包解压至 /usr/local目录;
# tar -C /usr/local -xzf go1.17.7.linux-amd64.tar.gz
3、将 /usr/local/go/bin 目录添加至 PATH 环境变量;
# echo "export PATH=$PATH:/usr/local/go/bin" >> /etc/profile
4、添加后执行;
# source /etc/profile
5、检查go是否安装成功。
# go version
go version go1.17.7 linux/amd64
2、安装govc及其依赖
压缩包下载地址为:govc下载地址
1、下载govc二进制包:govc_Linux_x86_64.tar.gz;
# ls
govc_Linux_x86_64.tar.gz
2、 将下载的二进制包解压至 /usr/local目录;
# tar -xf govc_Linux_x86_64.tar.gz -C /usr/local/bin
3、安装jq;
# yum install jq -y
4、检查govc是否安装成功。
# govc version
govc 0.27.4
二、配置govc环境变量
前提:ESXI已经安装完成
设置 govc 环境变量
# vim /etc/profile
#行末添加
# ESXi 节点的 IP
export ESXI_IP="192.168.98.210"
# ESXi 登录的用户名,初次安装后默认为 root
export GOVC_USERNAME="root"
# 在 ESXi 安装时设置的 root 密码
export GOVC_PASSWORD="admin@2022"
# 允许不安全的 SSL 连接
export GOVC_INSECURE=true
export GOVC_URL="https://${ESXI_IP}"
export GOVC_DATASTORE=datastore1
# source /etc/profile
测试govc 是否能正常连接 ESXi 主机
# govc env
GOVC_USERNAME=root
GOVC_PASSWORD=admin@2022
GOVC_URL=192.168.98.210
GOVC_INSECURE=true
# govc about
FullName: VMware ESXi 7.0.0 build-15843807
Name: VMware ESXi
Vendor: VMware, Inc.
Version: 7.0.0
Build: 15843807
OS type: vmnix-x86
API type: HostAgent
API version: 7.0.0.0
Product ID: embeddedEsx
UUID:
# govc host.info
Name: localhost.localdomain
Path: /ha-datacenter/host/localhost.localdomain/localhost.localdomain
Manufacturer: VMware, Inc.
Logical CPUs: 8 CPUs @ 1996MHz
Processor type: AMD Ryzen 7 4700U with Radeon Graphics
CPU usage: 69 MHz (0.4%)
Memory: 16382MB
Memory usage: 1296 MB (7.9%)
Boot time: 2022-02-15 02:46:15.566396 +0000 UTC
State: connected
三、安装vCenter
1、挂载vCenter的iso;
# mount VMware-VCSA-all-7.0.0-15952498.iso /mnt
# ls /mnt/vcsa/VMware-vCenter-Server-Appliance-7.0.0.10100-15952498_OVF10.ova
/mnt/vcsa/VMware-vCenter-Server-Appliance-7.0.0.10100-15952498_OVF10.ova
2、根据自己的环境信息修改下面安装脚本中的相关配置;
#!/usr/bin/env bash
VCSA_OVA_FILE=$1
set -o errexit
set -o nounset
set -o pipefail
# ESXi 的 IP 地址
export ESXI_IP="192.168.98.210"
# ESXi 的用户名
export GOVC_USERNAME="root"
# ESXI 的密码
export GOVC_PASSWORD="admin@2022"
# 安装 vCenter 虚拟机使用的 datastore 名称
export GOVC_DATASTORE=datastore1
export GOVC_INSECURE=true
export GOVC_URL="https://${ESXI_IP}"
# vCenter 的登录密码
VM_PASSWORD="admin@2022"
# vCenter 的 IP 地址
VM_IP=192.168.98.220
# vCenter 虚拟机的名称
VM_NAME=vCenter-Server-Appliance
# vCenter 虚拟机使用的网络
VM_NETWORK="VM Network"
# DNS 服务器
VM_DNS="223.6.6.6"
# NTP 服务器
VM_NTP="0.pool.ntp.org"
deploy_vcsa_vm(){
config=$(govc host.info -k -json | jq -r '.HostSystems[].Config')
gateway=$(jq -r '.Network.IpRouteConfig.DefaultGateway' <<<"$config")
route=$(jq -r '.Network.RouteTableInfo.IpRoute[] | select(.DeviceName == "vmk0") | select(.Gateway == "0.0.0.0")' <<<"$config")
prefix=$(jq -r '.PrefixLength' <<<"$route")
opts=(
cis.vmdir.password=${VM_PASSWORD}
cis.appliance.root.passwd=${VM_PASSWORD}
cis.appliance.root.shell=/bin/bash
cis.deployment.node.type=embedded
cis.vmdir.domain-name=vsphere.local
cis.vmdir.site-name=VCSA
cis.appliance.net.addr.family=ipv4
cis.appliance.ssh.enabled=True
cis.ceip_enabled=False
cis.deployment.autoconfig=True
cis.appliance.net.addr=${VM_IP}
cis.appliance.net.prefix=${prefix}
cis.appliance.net.dns.servers=${VM_DNS}
cis.appliance.net.gateway=$gateway
cis.appliance.ntp.servers="${VM_NTP}"
cis.appliance.net.mode=static
)
props=$(printf -- "guestinfo.%s\n" "${opts[@]}" | jq --slurp -R 'split("\n") | map(select(. != "")) | map(split("=")) | map({"Key": .[0], "Value": .[1]})')
cat <<EOF | govc import.${VCSA_OVA_FILE##*.} -options - "${VCSA_OVA_FILE}"
{
"Name": "${VM_NAME}",
"Deployment": "tiny",
"DiskProvisioning": "thin",
"IPProtocol": "IPv4",
"Annotation": "VMware vCenter Server Appliance",
"PowerOn": false,
"WaitForIP": false,
"InjectOvfEnv": true,
"NetworkMapping": [
{
"Name": "Network 1",
"Network": "${VM_NETWORK}"
}
],
"PropertyMapping": ${props}
}
EOF
}
deploy_vcsa_vm
govc vm.change -vm "${VM_NAME}" -g vmwarePhoton64Guest
govc vm.power -on "${VM_NAME}"
govc vm.ip -a "${VM_NAME}"
3、通过脚本安装 vCenter,指定第一参数为 OVA 的绝对路径。运行完后将会自动将 ova 导入到 vCenter,并启动虚拟机;
# 执行该脚本,第一个参数传入 vCenter ISO 中 vcsa ova 文件的绝对路径
# bash install-vcsa.sh /mnt/vcsa/VMware-vCenter-Server-Appliance-7.0.0.10100-15952498_OVF10.ova
4、使用 govc 配置 vCenter 登录的环境变量;
#vim /etc/profile
#行末添加
#vCenter的IP地址
export GOVC_URL="https://192.168.98.220"
export GOVC_USERNAME="administrator@vsphere.local"
#vCenter登录密码
export GOVC_PASSWORD="admin@2022"
export GOVC_INSECURE=true
export GOVC_DATASTORE=datastore1
5、查看vCenter信息;
虚拟机启动后将自动进行 vCenter 的安装配置,等待一段时间 vCenter 安装好之后,使用 govc about 查看 vCenter 的信息,如果能正确或渠道说明 vCenter 就安装好了。
# govc about
FullName: VMware vCenter Server 7.0.0 build-15952599
Name: VMware vCenter Server
Vendor: VMware, Inc.
Version: 7.0.0
Build: 15952599
OS type: linux-x64
API type: VirtualCenter
API version: 7.0.0.0
Product ID: vpx
UUID: 9174ab35-9071-4865-8add-5fa80e528cfa
四、配置vCenter
这一步骤主要是配置 vCenter:创建 Datacenter、cluster、folder 等资源,并将 ESXi 主机添加到 cluster 中;
# 创建 Datacenter 数据中心
# govc datacenter.create YZH
# 创建 Cluster 集群
# govc cluster.create -dc=YZH YZH
# 将 ESXi 主机添加到 Cluster 当中
# govc cluster.add -dc=YZH -cluster=YZH -hostname=192.168.98.210 --username=root -password='admin@2022' -noverify
# govc host.info
Name: 192.168.98.210
Path: /YZH/host/YZH/192.168.98.210
Manufacturer: VMware, Inc.
Logical CPUs: 8 CPUs @ 1996MHz
Processor type: AMD Ryzen 7 4700U with Radeon Graphics
CPU usage: 4310 MHz (27.0%)
Memory: 16382MB
Memory usage: 10678 MB (65.2%)
Boot time: 2022-02-15 02:46:15.566396 +0000 UTC
State: connected
# 创建 folder,用于存放虚机模板
# govc folder.create /SH-IDC/vm/Test-node
# 导入 测试虚拟机 ova 模版
# govc import.ova -dc='SH-IDC' -ds='datastore1' xxx.ova
# 将虚拟机转换为模版
# govc vm.markastemplate xxx