Kubernetes で PowerCLI を起動してみる。

Kubernetes で PowerCLI を起動してみる。

vSphere の運用では、PowerCLI が利用できると便利です。

PowerCLI は PowerShell ベースのツールです。

そのため、以前は Windows が必要でしたが、最近では Linux の PowerShell でも利用でき、

Docker コンテナ イメージ(vmware/powerclicore)も用意されていたりします。

Doker Hub の PowerCLI Core コンテナ イメージ

https://hub.docker.com/r/vmware/powerclicore

そこで、今回は Kubernetes で PowerCLI コンテナを起動してみます。

今回の環境。

Kubernetes は、vSphere with Kubernetes によるスーパーバイザー クラスタを利用して、

コンテナは vSphere Pod として起動してみます。

スーパーバイザー クラスタの環境は下記のように構築しています。

vSphere with Kubernetes ラボ環境構築。(まとめ)

kubectl では、下記のようにスーパーバイザ クラスタに login してあります。

vSphere with Kubernetes ラボ環境構築。Part-11: kubectl で vSphere Pod 起動編

Kubernetes での PowerCLI の起動。

それでは、kubectl run コマンドで、Pod として起動します。

  • Pod の名前は「pcli01」にしています。
  • 「-it」を指定して、そのままコンテナに接続して対話的にコマンド実行できるようにします。
  • vmware/powerclicore コンテナ イメージを起動します。イメージは Docker Hub からダウンロードしているので、インターネットへの接続が必要です。
  • kubectl run コマンドに、「--restart=Never」オプションを指定すると、Pod(Deployment リソースではなく)として起動できます。
  • 「--rm」を指定すると、コンテナ終了と同時に Pod が自動削除されます。

Pod が起動されると、そのままコンテナの中の PowerShell プロンプトが表示されます。

$ kubectl run pcli01 --image=vmware/powerclicore -it --restart=Never --rm

If you don't see a command prompt, try pressing enter.

PowerShell 7.0.0

Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/powershell

Type 'help' to get help.

PS /root>

これで、PowerCLI が利用できます。

※今回は CEIP の警告は無視しています。

PS /root> Connect-VIServer -Force -Server lab-vc-41.go-lab.jp

WARNING: Please consider joining the VMware Customer Experience Improvement Program, so you can help us make PowerCLI a better product. You can join using the following command:

Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $true

VMware's Customer Experience Improvement Program ("CEIP") provides VMware with information that enables VMware to improve its products and services, to fix problems, and to advise you on how best to deploy and use our products.  As part of the CEIP, VMware collects technical information about your organization?s use of VMware products and services on a regular basis in association with your organization?s VMware license key(s).  This information does not personally identify any individual.

For more details: type "help about_ceip" to see the related help article.

To disable this warning and set your preference use the following command and restart PowerShell:

Set-PowerCLIConfiguration -Scope User -ParticipateInCEIP $true or $false.

Specify Credential

Please specify server credential

User: administrator@vsphere.local ★ユーザー名とパスワードを入力。

Password for user administrator@vsphere.local: ********

Name                           Port  User

----                           ----  ----

lab-vc-41.go-lab.jp            443   VSPHERE.LOCAL\Administrator

PS /root>

接続した vCenter から、情報取得ができました。

ちなみに、今回はスーパーバイザー クラスタを管理している vCenter に接続しており、

VM の一覧には vSphere Pod(pcli01)も表示されています。

PS /root> Get-Cluster

Name                           HAEnabled  HAFailover DrsEnabled DrsAutomationLe

                                          Level                 vel

----                           ---------  ---------- ---------- ---------------

wcp-cluster-41                 True       1          True       FullyAutomated

PS /root> Get-VMHost

Name                 ConnectionState PowerState NumCpu CpuUsageMhz CpuTotalMhz

----                 --------------- ---------- ------ ----------- -----------

lab-wcp-esxi-41.go-… Connected       PoweredOn       2        4322        6000

lab-wcp-esxi-42.go-… Connected       PoweredOn       2        1526        4608

lab-wcp-esxi-43.go-… Connected       PoweredOn       2        1990        4608

PS /root> Get-VM

Name                 PowerState Num CPUs MemoryGB

----                 ---------- -------- --------

SupervisorControlPl… PoweredOn  2        8.000

SupervisorControlPl… PoweredOn  2        8.000

SupervisorControlPl… PoweredOn  2        8.000

pcli01               PoweredOn  1        0.500

vSphere Clinet でも、PowerCLI の vSphere Pod が起動されたことが確認できます。

k8s-powercli-01.png

PowerCLI では、vCenter Server に接続する際に DNS による名前解決が重要になります。

そこで、コンテナの中の DNS サーバ参照の設定を確認してみると、

Kubernetes の機能による DNS サーバのアドレスが指定されています。

これは、このラボの DNS サーバのアドレスではありません。

PS /root> cat /etc/resolv.conf

nameserver 10.96.0.254

search sc-ns-01.svc.cluster.local svc.cluster.local cluster.local

起動した Pod は、デフォルトで「dnsPolicy: ClusterFirst」が設定されます。

これにより、Kubernetes の機能による DNS サーバで名前解決できなくても、

外部の DNS サーバで名前解決できるようになっています。

dnsPolicy については、下記が参考になります。

https://kubernetes.io/ja/docs/concepts/services-networking/dns-pod-service/ 

ちなみにスーパーバイザー クラスタでは、外部の DNS サーバのアドレスは、

vSphere クラスタで「ワークロード管理」を有効化する際に設定したものです。

この設定は、kubectl get pods -o yaml といったコマンドラインや、

Pod 起動時の --dry-run -o yaml オプションなどで確認することもできます。

$ kubectl run pcli01 --image=vmware/powerclicore --restart=Never --dry-run -o yaml

apiVersion: v1

kind: Pod

metadata:

  creationTimestamp: null

  labels:

    run: pcli01

  name: pcli01

spec:

  containers:

  - image: vmware/powerclicore

    name: pcli01

    resources: {}

  dnsPolicy: ClusterFirst

  restartPolicy: Never

status: {}

vSphere Pod の YAML は、vSphere Client でも確認できます。

vSphere Pod の「サマリ」→「メタデータ」などから、Pod の YAML を表示できます。

k8s-powercli-02.png

dnsPolicy は ClusterFirst になっています。

k8s-powercli-03.png

ちなみに、この Pod から exit すると、自動的に Pod は停止 ~ 削除されます。(--rm オプションにより)

PS /root> exit

pod "pcli01" deleted

$

DNS サーバ設定を指定した Pod の起動。

Pod の DNS サーバ設定は、Pod 起動時に YAML で指定することもできます。

kubectl run であっても、下記のように Pod 起動時に、dnsPolicy と dnsConfig を上書きできたりします。

今回は、自宅ラボの DNS サーバのアドレスとして、192.168.1.101 と 192.168.1.102 を指定して、

見やすくなるように JSON 部分を整形してあります。

kubectl run pcli01 --image=vmware/powerclicore -it --restart=Never --rm --overrides='

{

  "apiVersion": "v1",

  "spec": {

    "dnsPolicy": "None",

    "dnsConfig": {

      "nameservers": ["192.168.1.101", "192.168.1.102"],

      "searches": ["go-lab.jp"]

    }

  }

}'

実際に実行してみると、下記のようになります。

起動後の Pod では、/etc/resolv.conf ファイルを確認すると、DNS サーバの設定が変更できています。

$ kubectl run pcli01 --image=vmware/powerclicore -it --restart=Never --rm --overrides='

> {

>   "apiVersion": "v1",

>   "spec": {

>     "dnsPolicy": "None",

>     "dnsConfig": {

>       "nameservers": ["192.168.1.101", "192.168.1.102"],

>       "searches": ["go-lab.jp"]

>     }

>   }

> }'

If you don't see a command prompt, try pressing enter.

PowerShell 7.0.0

Copyright (c) Microsoft Corporation. All rights reserved.

https://aka.ms/powershell

Type 'help' to get help.

PS /root> cat /etc/resolv.conf

nameserver 192.168.1.101

nameserver 192.168.1.102

search go-lab.jp

PS /root>

vSphere Client で表示できる  Pod の YAML でも、同様に DNS 設定が変更できています。

k8s-powercli-04.png

vSphere with Kubernetes の動作確認やデモなどでも利用できそうかなと思います。

また、スーパーバイザー クラスタ特有の機能を利用するものではないので、

Kubernetes があればどこでも同じように PowerCLI コンテナが起動できるはず・・・

以上、Kubernetes で PowerCLI を起動してみる話でした。

Version history
Revision #:
1 of 1
Last update:
‎08-31-2020 04:05 PM
Updated by: