Try   HackMD

使用 Docker 方式來操作 GOVC

tags: govc podman


Podman vs Docker

podman

Podman 原來是 CRI-O 專案的一部分,後來被分離成一個單獨的專案叫 libpod。Podman 的使用體驗和 Docker 類似,不同的是 Podman 沒有 daemon。

Docker 架構

Podman 架構

這裡只是利用 Podman 簡化操作映像檔(Image),如果已經有 Docker 環境直接使用即可。想進一步了解 Podman,可以參考官網說明

安裝 Podman

$ sudo dnf install podman       

Last metadata expiration check: 1:42:02 ago on Thu 11 Nov 2021 01:08:28 PM CST.
Dependencies resolved.
================================================================================
 Package                        Arch      Version              Repository  Size
================================================================================
Installing:
 podman                         x86_64    3:3.4.1-1.fc35       updates     12 M
Installing dependencies:
 conmon                         x86_64    2:2.0.30-2.fc35      fedora      56 k
 containernetworking-plugins    x86_64    1.0.1-1.fc35         fedora     8.7 M
 shadow-utils-subid             x86_64    2:4.9-5.fc35         updates     84 k
Installing weak dependencies:
 catatonit                      x86_64    0.1.6-1.fc35         fedora     315 k
 podman-gvproxy                 x86_64    3:3.4.1-1.fc35       updates    3.5 M
 podman-plugins                 x86_64    3:3.4.1-1.fc35       updates    2.6 M

Transaction Summary
================================================================================
Install  7 Packages

Total download size: 27 M
Installed size: 131 M
Is this ok [y/N]: y
Downloading Packages:
(1/7): conmon-2.0.30-2.fc35.x86_64.rpm                                               276 kB/s |  56 kB     00:00    
(2/7): shadow-utils-subid-4.9-5.fc35.x86_64.rpm                                      189 kB/s |  84 kB     00:00    
(3/7): catatonit-0.1.6-1.fc35.x86_64.rpm                                             361 kB/s | 315 kB     00:00    
(4/7): podman-plugins-3.4.1-1.fc35.x86_64.rpm                                        1.1 MB/s | 2.6 MB     00:02    
(5/7): podman-gvproxy-3.4.1-1.fc35.x86_64.rpm                                        847 kB/s | 3.5 MB     00:04    
(6/7): podman-3.4.1-1.fc35.x86_64.rpm                                                1.3 MB/s |  12 MB     00:09    
(7/7): containernetworking-plugins-1.0.1-1.fc35.x86_64.rpm                           573 kB/s | 8.7 MB     00:15    
---------------------------------------------------------------------------------------------------------------------
Total                                                                                1.6 MB/s |  27 MB     00:16     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                             1/1 
  Installing       : shadow-utils-subid-2:4.9-5.fc35.x86_64                                                      1/7 
  Installing       : podman-gvproxy-3:3.4.1-1.fc35.x86_64                                                        2/7 
  Installing       : podman-plugins-3:3.4.1-1.fc35.x86_64                                                        3/7 
  Installing       : containernetworking-plugins-1.0.1-1.fc35.x86_64                                             4/7 
  Installing       : conmon-2:2.0.30-2.fc35.x86_64                                                               5/7 
  Installing       : catatonit-0.1.6-1.fc35.x86_64                                                               6/7 
  Installing       : podman-3:3.4.1-1.fc35.x86_64                                                                7/7 
  Running scriptlet: podman-3:3.4.1-1.fc35.x86_64                                                                7/7 
Failed to connect to bus: Invalid argument
Failed to connect to bus: Invalid argument

  Verifying        : catatonit-0.1.6-1.fc35.x86_64                                                               1/7 
  Verifying        : conmon-2:2.0.30-2.fc35.x86_64                                                               2/7 
  Verifying        : containernetworking-plugins-1.0.1-1.fc35.x86_64                                             3/7 
  Verifying        : podman-3:3.4.1-1.fc35.x86_64                                                                4/7 
  Verifying        : podman-gvproxy-3:3.4.1-1.fc35.x86_64                                                        5/7 
  Verifying        : podman-plugins-3:3.4.1-1.fc35.x86_64                                                        6/7 
  Verifying        : shadow-utils-subid-2:4.9-5.fc35.x86_64                                                      7/7 

Installed:
  catatonit-0.1.6-1.fc35.x86_64                                  conmon-2:2.0.30-2.fc35.x86_64                      
  containernetworking-plugins-1.0.1-1.fc35.x86_64                podman-3:3.4.1-1.fc35.x86_64                       
  podman-gvproxy-3:3.4.1-1.fc35.x86_64                           podman-plugins-3:3.4.1-1.fc35.x86_64               
  shadow-utils-subid-2:4.9-5.fc35.x86_64                        

Complete!

檢視 Podman 版本

$ podman --version       
podman version 3.4.1

登入 Docker Hub

$ podman login docker.io 

Username: container
Password: 
Login Succeeded!

安裝 GOVC Docker 檔

$ sudo podman pull vmware/govc
✔ docker.io/vmware/govc:latest
Trying to pull docker.io/vmware/govc:latest...
Getting image source signatures
Copying blob a80ed8e4c7b7 done  
Copying blob f1fdef5ed194 done  
Copying blob f33be5948b2f done  
Copying blob 7864930444cf done  
Copying blob 63dc2a7c315c done  
Copying config 39743666d9 done  
Writing manifest to image destination
Storing signatures
39743666d9c809229ca1c2628203ffa586b685cfccc9ab3c45270f588a152c17

檢視目前的 Docker 映像檔

$ sudo podman images          
REPOSITORY                       TAG         IMAGE ID      CREATED      SIZE
docker.io/vmware/govc            latest      39743666d9c8  3 weeks ago  39.7 MB
localhost/hello-fedora-magazine  latest      b3b747b1354c  2 years ago  469 MB
docker.io/library/fedora         latest      8c568f104326  3 years ago  275 MB

檢視 GOVC 版本

透過 podman run 執行 govc version -l 檢視版本,並確認 docker 運行正常。

$ sudo podman run --rm -it vmware/govc /govc version -l
Build Version: 0.27.1
Build Commit: 6209be5b
Build Date: 2021-10-20T20:05:44Z

確認連接 vCenter 系統

使用 govc about 連接 vCenter 運行環境。

注意 使用 govc 需要提供 vCenter 環境變數。

$ sudo podman run --rm -it vmware/govc /govc about
/govc: specify an ESX or vCenter URL

根據 Docker Hub 上 GOVC 的使用說明,根據實際環境至少提供以下所需環境變數。

  • GOVC_URL
  • GOVC_USERNAME
  • GOVC_PASSWORD
  • GOVC_INSECURE
$ docker run -e GOVC_URL=https://administrator@vsphere.local:Passw0rd@vc.local -e GOVC_INSECURE=true --rm -it vmware/govc /govc ls

我們可以將環境變數寫入檔案 env.conf,之後讀入檔案內容即可。

-e GOVC_URL=vcsa7 -e GOVC_USERNAME=administrator@vsphere.local -e GOVC_PASSWORD=VMware1!VMware1! -e GOVC_INSECURE=true

可正常取得 vCenter Server 資訊。

$ sudo podman run $(cat env.conf) --rm -it vmware/govc /govc about                      
FullName:     VMware vCenter Server 7.0.3 build-18700403
Name:         VMware vCenter Server
Vendor:       VMware, Inc.
Version:      7.0.3
Build:        18700403
OS type:      linux-x64
API type:     VirtualCenter
API version:  7.0.3.0
Product ID:   vpx
UUID:         c6b3cf98-f987-411b-a1b5-b06c216988f1

測試其他操作

使用相同的執行方式,就跟以前執行 govc 的操作相同。

  • 查詢包含 Neo 的虛擬機器。
$ sudo podman run $(cat env.conf) --rm -it vmware/govc /govc find / -type m | grep Neo  
 
/Datacenter/vm/Neo/Neo_Win2012R2_MSSQL2014_2
/Datacenter/vm/Neo/Neo_WINDOWS2012_AD
/Datacenter/vm/Neo/Neo_Win2012R2_MSSQL2014_1
/Datacenter/vm/Neo/Neo_WINDOWS2012_AD2
/Datacenter/vm/Neo/Neo_Win2012R2_MSSQL2014_4
/Datacenter/vm/Neo/Neo_Win2012R2_MSSQL2014_3
/Datacenter/vm/Neo/Neo_Win2012R2_MSSQL2014_5
  • 檢視虛擬機器概要資訊
$ sudo podman run $(cat labVC.env) --rm -it vmware/govc /govc vm.info Neo_Win2012R2_MSSQL2014_1 
Name:           Neo_Win2012R2_MSSQL2014_1
  Path:         /Datacenter/vm/Neo/Neo_Win2012R2_MSSQL2014_1
  UUID:         422b19c3-085d-f206-fa6c-517c1991569a
  Guest name:   Microsoft Windows Server 2012 (64-bit)
  Memory:       8192MB
  CPU:          4 vCPU(s)
  Power state:  poweredOn
  Boot time:    2021-11-01 07:34:32.555999 +0000 UTC
  IP address:   169.254.1.91
  Host:         192.168.1.2

其他功能就請自行練習。

參考