Kubernetes | Command Cheat Sheet(KB#00092)

Overview

Well yes, you are thinking right that I am learning Kubernetes so wanted to share some useful insights and will continue to share stuff on this. Below are some commands for daily operations while working with Kubernetes. I will keep on adding stuff here.

 

Command to Command
Check Minikube version $minikube version
Start Minikube cluster $minikube start
Check if Kubectl is installed $kubectl version
Check kubectl cluster info $kubectl cluster-info
Check kubectl node info $kubectl get node
, ,

PS | How to get HA restarted VM's Org and OrgvDC info with VM Name

Overview

You will see many blogs giving solution for fetching the VM names which are restarted by HA in event of esxi host failures using Get-VIEvent powercli command. But the extracted VM Name too is not in well format to use as it is. You have to use excel and text to column and then extract the VM Name etc. For me, I have vCD also so at the time of ESXi host failures and HA events, I not only need to fetch the VM Name but also Org and OrgvDC info to share it with my customer. It becomes more lengthy for me and I need to make it quick. So it is extended solution for such kind of scenario. Hope you will find it useful.

Let's see how I could do it using powershell.

Script

#Start here

Write-Host "This script will help you out to have VM name restarted by HA due to esxi host failuers" -ForegroundColor Yellow

Function Get-HAVM{
$Date=Get-Date
$HAVMrestartold=1
$raw = Get-VIEvent  -maxsamples 10000000 -Start ($Date).AddDays(-$HAVMrestartold) -type warning | Where {$_.FullFormattedMessage -match "restarted"} |select CreatedTime,FullFormattedMessage |sort CreatedTime -Descending
$raw.vm.name
Remove-Item -Path C:\Temp\vmlist.csv
$raw.vm.name | Out-File C:\Temp\vmlist.csv
}
Get-HAVM
$allvms = Get-Content -Path C:\Temp\vmlist.csv
$vms = Get-VM -Name $allvms
$myView = @()
foreach ($vm in $vms){
$Report = [PSCustomObject] @{
 VM_Name = $vm.Name
 Org_Name = $vm.Folder.Parent.Parent.Name
 OrgvDC_Name = $vm.Folder.Parent.Name
}
$MyView += $Report
}
$myView | Out-GridView

#End here

Any doubt? Comment box is yours :)

Let's give it more power

If you have smtp configured in your environment then simply you can mail it from the same script using Send-MailMessage command but for that you might have to do some tweak in above script. 

Hint is, You have to save final report. Change in the last line of above script like

$myView | Out-File C:\Temp\vmsrestartedbyHA.csv

then use below command

Send-MailMessage -From 'gautam.johar@vcnotes.in' -To 'my.reader@home.com', 'myreader2@home.com' -Subject 'HA Event is triggered and VM list is attached' -Body "Please find the attachment" -Attachments C:\Temp\vmsrestartedbyHA.csv -Priority High -DeliveryNotificationOption OnSuccess, OnFailure -SmtpServer 'smtp.vcnotes.in'

Change wherever applicable.

If you are good enough in PowerShell then you can have many ways to enhance the ideas. For me this is basic script which is working fine for me.

Side Note

I created this script to run perfectly in PowerShell ISE so run in that please or if you have any error in running it in simple powershell cli terminal then you might need to fix the visible errors.

Good Luck!









vRA | How to manually assign the unassigned shards

 Overview

In one of the vRA upgrade from 7.4 to 7.6, I faced this issue post upgrade. All went well except below error on VAMI page of both vRA appliances (as I had two nodes). If you have more and if you stuck with this error then you will see this error on all the nodes. 

================

Elasticsearch validation failed:

status: red
number_of_nodes: 2
unassigned_shards: 4
number_of_pending_tasks: 0
number_of_in_flight_fetch: 0
timed_out: False
active_primary_shards: 113
cluster_name: horizon
relocating_shards: 0
active_shards: 226
initializing_shards: 0
number_of_data_nodes: 2
delayed_unassigned_shards: 0

=================

If you read above error then you will understand that there are 4 unassigned shards which were not automatically assigned to any of the available vra node. 

Cause 

It happens if and when DB sync between primary and slave vra nodes are not good. When primary node was not having updated data but slave nodes were running with some additional data. Total break between Master and Replica DB replication. In my case also before upgrading there were many issues with DB.

If you recover the cluster state even then these shards might not assign automatically and give above alert. Now you have to assign the unassigned shards manually. Let's see the process.

Resolution

1. Check the state from Master node CLI with below command

#curl http://localhost:9200/_cluster/health?pretty=true

You will have this error in output

{
  "cluster_name" : "horizon",
  "status" : "red",
  "timed_out" : false,
  "number_of_nodes" : 2,
  "number_of_data_nodes" : 2,
  "active_primary_shards" : 113,
  "active_shards" : 226,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 4,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0
}

2. Check the cluster information with below command

#curl -s -XGET http://localhost:9200/_cat/nodes

You will have similar output

master.mylab.local 172.25.3.199 8   d * Dreadknight
replica.mylab.local 172.25.3.200 8   d m Masque

3. Search for unassigned shards

#curl -XGET 'http://localhost:9200/_cat/shards' | grep UNAS

You will see similar output as below

 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 15870  100 15870    0     0   484k      0 --:--:-- --:--:-- --:--:--  484k
v3_2020-10-02  4 p UNASSIGNED
v3_2020-10-02  4 r UNASSIGNED
v3_2020-10-02  2 p UNASSIGNED
v3_2020-10-02  2 r UNASSIGNED

4. Re-assigned these using the following command, where index = v3_2020-10-02, and shards to be re-assigned are '2' and '4', while running on the master node - 'Dreadknight. Change your command according to your environment. for example, value after index will be changed, value after shard, after node will  be changed. Other infos will be same.


curl -XPOST 'localhost:9200/_cluster/reroute' -d '{"commands":[{"allocate":{"index":"v3_2020-10-02","shard":2,"node":"Dreadknight","allow_primary":"true"}}]}'

and

curl -XPOST 'localhost:9200/_cluster/reroute' -d '{"commands":[{"allocate":{"index":"v3_2020-10-02","shard":4,"node":"Dreadknight","allow_primary":"true"}}]}'

That's it. Now shards have been assigned or allocated automatically manually.

Log out all the nodes VAMI and log in back. You will not see any such error.


Miscellaneous Notes

This is dynamic post and I will keep on adding points in here. I generally add small but useful things here which is not worthy to create long post.

 


How to Explanation
transfer the tech-support bundle to FTP on Arista Router copy flash:/EOS-4.18.2F.swi ftp:/user:password@192.168.10.15/EOS-4.18.2F.swi
user = username of ftp server account
password = password of ftp server account
192.168.10.15 = IP address of ftp server
EOS-4.18.2F.swi = tech-support bundle file name
Encrypt a PowerShell script https://drive.google.com/open?id=19Bvik1FcSTC57eJ0CZPE4D-8hnQfyCi-
Reboot Windows with PowerShell command powershell.exe -encodedCommand cwBoAHUAdABkAG8AdwBuACAALQByACAALwB0ACAAMAAxACAA
To create a digital clock Download and run these PS script to create the clock on your PC.
EST Clock | CST Clock | IST Clock
Do few things in Linux 1. Check Kernal Version in Linux - Rpm -qa | grep -I kernel
2. Change IP on an interface - ifconfig eth1 192.168.2.2 netmask 255.255.255.0
3. To set or change DG of any VM - route add default gw 192.168.2.1
4. File location to change the IP - vi /etc/sysconfig/network-scripts/ifcfg-eth0
5. To Search specific text in linux server -
grep -rnw '/path/to/somewhere/' -e 'pattern'
How to ping with the MTU value ping www.yahoo.com -f -l 1492
Add Network Components in vRNI Check this article
How to encode and decode Base64 script Check this here
Ping an entire subnet in Windows I have documented it here
Some Useful ESXi Commands Check speed and other info of HBA card - esxcli storage san fc list
vCloud API Guide for NSX Here is the vendor page for pdf
To create static routes in multiple esxi hosts $esx = Get-VMHost -Name esxihost_Name
$esxcli = Get-EsxCli -VMHost $esx -V2
$parms = @{
network = '192.168.102.0/24'
gateway = '192.168.3.1'
}
$esxcli.network.ip.route.ipv4.add.Invoke($parms)
$esxcli.network.ip.route.ipv4.list.Invoke()
Send mail to any mail account using PS Download the powershell script from Google Drive. Click here
How to delete any iso file in all datastores which is older than 15 days foreach($ds in Get-datastore){
New-PSDrive -Name GJ -PSProvider VimDatastore -Root '/' -Datastore $ds > $null
Get-Childitem -Path GJ:\ -Recurse -Include *.iso | Remove-Item -Confirm:$true | Where ((Get-date).AddDays(-15))
#This will search each and every folder in your datastore and show you the file to delete it.
Remove-PSDrive -Name GJ -Confirm:$false}
Replace false to true in command (Remove-Item -Confirm:$false to Remove-Item -Confirm:$true)if you want to check and delete each file one by one
How to edit Login Banner in Vmware Cloud Director Appliance 1. Create or edit a file in /etc/login.warn and put your message in here.
2. Edit /etc/sshd/sshd_config file and change the line from #Banner none to #Banner /etc/login.warn
How to search largest files in Linux sudo du -a /dir/ | sort -n -r | head -n 20
This command will search top 20 largest files in any directory
How to search particular word or string in file in Linux tr ‘[:space]’ ‘[/n*] < /var/log/apache2/access.log | grep -I -c 172.18.101.16
172.18.101.16 is to search in access.log file
,

PS | To extract DRS rules with VM names

Hi Guys,

This is not a big thing but still I wanted to document it for my own reference. I got a request like which VMs are in which DRS rules so I got below script.

#Start here

$VC = Read-host "Enter the FQDN\IP of vCenter Server"

Connect-VIServer $VC
$DRSRules = Get-Cluster | Get-DrsRule
$Results = ForEach ($DRSRule in $DRSRules)
     {
    "" | Select-Object -Property @{N="Cluster";E={(Get-View- Id $DRSRule.Cluster.Id).Name}},
    @{N="Name";E={$DRSRule.Name}},
    @{N="Enabled";E={$DRSRule.Enabled}},
    @{N="DRS Type";E={$DRSRule.KeepTogether}},
    @{N="VMs";E={$VMIds=$DRSRule.VMIds -split ","
     $VMs = ForEach ($VMId in $VMIds)
        {
        (Get-View -Id $VMId).Name
        }
      $VMs -join ","}}
     }
$Results | out-gridview

#End here

Another window will open and copy entire output into excel if you want.

Cheers!