Script to Create Multiple Datastores

Featured

There was a requirement in one of the projects to create multiple (approx. 60) datastores using a PowerCLI script. We wrote a PowerCLI script to perform this operation on multiple LUNs presented to all ESXi hosts in the cluster.

Requirements:

  • PowerCLI
  • PowerShell 5.1 or later

Script:

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Connect-VIServer “vCenter_Server_IP_Address/FQDN” -Credential (Get-Credential)

$datanames = Import-Csv ‘C:\Users\Admin\Desktop\File_with_datastore_name_NAA_Ids.csv

foreach ($dataname in $datanames)
{
$dataname.Datastore_Name
$dataname.Naa_Id
New-Datastore -VMHost ESXi-01.mycloud.lab -Name $dataname.Datastore_Name -Path $dataname.Naa_Id -Vmfs -FileSystemVersion 6
Get-Cluster -name “Cloud-Clu-01” | Get-VMhost | Get-VMHostStorage –RescanAllHBA
Start-Sleep -Seconds 15
}

Disconnect-VIServer -Confirm:$false

Input File Sample:

Sample of File_with_datastore_name_NAA_Ids.csv

Just replace the vCenter_Server_IP_Address/FQDN, Path of File Containing Datastore Names, NAA Ids, ESXi Host, Cluster Name and Sleep Interval after each Datastore creation operation with the details of your environment. BOOM!!

Happy Scripting!!

Script to Export VI Permissions

Featured

There was a requirement in one of the projects to export permissions assigned to each Virtual Machine hosted in a vSphere Environment. We wrote a PowerCLI script to fetch the list of users along with the Role assigned to each user and export these details to a CSV file.

Requirements:

  • PowerCLI
  • PowerShell 5.1 or later

Script:

Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
$cred = Get-Credential
Connect-VIServer vCenter_IP/FQDN -Credential $cred
$vms = Get-VM
$output = New-Object System.Collections.ArrayList($null)
$vmpermission = @()
foreach ($vm in $vms)
{
    $permissions = $vm | Get-VIPermission | Get-Unique
    Foreach ($permission in $permissions)
    {
        $formatpermission = $permission.Principal + " (" + $permission.Role + ")"
        $permission | Add-Member -NotePropertyName FormatPermission -NotePropertyValue $formatpermission
    }
    $vmpermission = [pscustomobject]@{VMName=$vm.Name;Role=$permissions.FormatPermission  -join ', '}
    [void]($output.Add($vmpermission))
}

$output | Export-csv C:\Users\VMPermissionsExport.csv -NoTypeInformation

Just replace the vCenter_Server_IP_Address/FQDN and Path of CSV File and run the script to export VI Permissions assigned to each Virtual Machine in vSphere environment. BOOM!!

Happy Scripting!!

Deploy a Kubernetes Cluster

In order to leverage Kubernetes platform for deploying Containerized Applications, we need to set up a Kubernetes cluster. In my Home Lab, i have used 2 Ubuntu 18.0.4.03 Virtual Machines to create a Kubernetes cluster. Ubuntu machines should have Internet connectivity, name resolution should work between both the machines and machines should be patched & updated.

Configuring a Kubernetes Cluster on Ubuntu:

Pre-requisites:

1. 2 X Ubuntu 18.04.3 VMs
2. Each VM should have minimum of 2 vCPUs and 2 GB RAM

Configuration Steps:

1. Install Docker on both the nodes:

sudo apt install docker.io

Run the below command to check the verion of Docker:

docker –version

2. Enable Docker on both the nodes:

sudo systemctl enable docker

Run the following command to install curl on both the VMs:
sudo apt install curl

4. Add the Kubernetes signing key on both the nodes:

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add

5. Add Xenial Kubernetes Repository on both the nodes:

sudo apt-add-repository “deb http://apt.kubernetes.io/ kubernetes-xenial main”

6. Install Kubeadm:

sudo apt install kubeadm

kubeadm version

7. Deploy Kubernetes Cluster:

Disable swap memory (if running) on both the nodes:

sudo swapoff -a

8. Give Unique hostnames to each node:

sudo hostnamectl set-hostname kube-master

hostnamectl set-hostname kube-node1

9. Initialize Kubernetes on the master node:

sudo kubeadm init –pod-network-cidr=10.244.0.0/16

Note down the output of the above command in a Notepad, you’ll require the discovery token to join the Worker Node to the cluster.

10. To start using your cluster, you need to run the following as a regular user:

mkdir -p $HOME/.kube

sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config

sudo chown $(id -u):$(id -g) $HOME/.kube/config

You can check the status of the master node by running the following command:

kubectl get nodes

11. Deploy a Pod Network through the master node:

sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

Use the following command in order to view the status of the network:

kubectl get pods –all-namespaces

Use the following command to check the status of the Master Node:

sudo kubectl get nodes

12. Create a Service Account to Access the Kuberntes UI Dashboard:

kubectl create serviceaccount dashboard -n default

Assign Cluster-Admin Privileges to the Service Account:

kubectl create clusterrolebinding dashboard-admin -n default –clusterrole=cluster-admin –serviceaccount=default:dashboard

Generate a Bearer Token for the Service Account to Access the Dashboard UI:

kubectl get secret $(kubectl get serviceaccount dashboard -o jsonpath=”{.secrets[0].name}”) -o jsonpath=”{.data.token}” | base64 –decode

Token:

eyJhbGciOiJSUzI1NiIsImtpZCI6Ik95R3ZaUXBCeWVYS01IQ3JfNms3eWJ0MlZrQzI3WHRH
NFhKUEtsc3VieEkifQ.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJu
ZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJkZWZhdWx0Iiwia3ViZXJuZXRl
cy5pby9zZXJ2aWNlYWNjb3VudC9zZWNyZXQubmFtZSI6ImRhc2hib2FyZC10b2tlbi14Znp3
biIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbW
UiOiJkYXNoYm9hcmQiLCJrdWJlcm5ldGVzLmlvL3NlcnZpY2VhY2NvdW50L3NlcnZpY2UtY
WNjb3VudC51aWQiOiIxMzk4ZWU3OS1hZjNmLTQ0YzctOTkxOC0zZTMxNWRkNWRlM2Ei
LCJzdWIiOiJzeXN0ZW06c2VydmljZWFjY291bnQ6ZGVmYXVsdDpkYXNoYm9hcmQifQ.LW
keqAnIsylBvH9fRL_e5Uabcdefashajshkjahsjkahsjabxabsnbavhgqw872y2hjbnasbnmasba

12. Deploying the Dashboard UI:

For those of you who didn’t knew, Kubernetes has a GUI based Dashboard which can be used for almost every admin task once the cluster has been set up. Use the below steps to deploy the Dashboard UI:

kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yaml

You can now access the Dashboard using the kubectl command-line tool by running the following command:

Kubectl proxy

Kubectl will make Dashboard available at:

http://localhost:8001/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/

Access the above dashboard by entering the above URL in a Web Browser and login using the Bearer Token:

Kubernetes UI Dashboard

14. Add the slave node to the network in order to form a cluster:

kubeadm join Kube-Master_FQDN:6443 –token kjq37t.2nqh0m4y31ytnzt4 \

–discovery-token-ca-cert-hash sha256:8da665906a2a1ff54ee50efa6c1d0088fb27399b2e649e4ca8cfe8ee115a9728

Use the following command to check the status of all the Cluster Nodes:

sudo kubectl get nodes

If you have followed all the above steps the Output should be something like this:

Pro-Tip:
Some people like to use Pycharm for writing YAML files for Kuberetes but i like to use Visual Studio Code for all my Command Line stuff. You can install an extension for Remote-SSH in Visual Studio Code and connect to your Kubernetes Cluster using Visual Stutio Code.

Enjoy your newly built Kubernetes Cluster. 🙂