Sunday, June 11, 2017

Setting up Java in Linux Environment

JAVA installation in Linux

Majority of application servers and applications require Java . Here I am going to explain how to install JAVA in Linux environment. There are different vendors and different version of Java . You can get its details from Link. But here I am describing about Hotspot ( Oracle JDK ) 


1. Download JDK of required version from http://www.oracle.com/
    There will be files 2 kind of  extension  1) .rpm 2) .tar.gz download any one of it.
2. if downloaded file is with  .rpm  extension  use
    
         #rpm -ivh .rpm 
    if downloaded file is with .tar.gz extension
       #tat xvzf .tar.gz
             The above command will extract JDK .
   Next step is add JDK's  bin location in PATH variable.  suppose you have extracted JDK in to /opt/ directory and you got path like /opt/jdk1.8.0_56 then add following 2 lines  in /etc/profile ( just before  the line  "unset i " )
  export JAVA_HOME=/opt/jdk1.8.0_56/
export PATH=$PATH:/opt/jdk1.8.0_56/bin 

Just re-login ( or do the command   "source /etc/profile")  and type "java".


How to set default Java using alternative command

If we have downloaded and extracted multiple java in Linux system, we can make one version of Java as default

To add new java to the alternative list use following command

#sudo alternatives --install /usr/bin/java java  /opt/jdk1.8.0_56/bin/java 1000

To set new  Java as default   use following command
                                 
#alternatives --config java

Enter the number shown under "Selection" column corresponding to the Java which we want to set as default

Ensure the correct version has been set, by using command
#java -version 


Wednesday, March 15, 2017

Power shell to stop running Azure VM based on tags

$RGs =@()
$processed_vm =@()
$RGs = $(Get-AzureRMResourceGroup |Select-Object -Property  ResourceGroupName -Verbose:$false )
foreach ($RG in $RGs)
{
$ResourceGroupName = $RG.ResourceGroupName

 $vm_list = Get-AzureRmVM -ResourceGroupName $ResourceGroupName |  ? (  {($_.Tags.Keys -notcontains "Do Not Disturb") -or ($_.Tags.Keys -eq "Do Not Disturb" -and $_.Tags.Values -eq "No")   })
 foreach($vm in $vm_list)
 {
 $vm_name=$vm.Name

 Get-AzureRmVM -name $vm_name -Status -ResourceGroupName $ResourceGroupName | Select-Object -Property Name, Statuses|
  ForEach-Object  {
        $_.Statuses |
        Where-Object {$_.Code -like 'PowerState/*'} |
        ForEach-Object {
          New-Object -TypeName psobject -Property @{
           Status = $_.DisplayStatus
                     
          }
          if ( $_.DisplayStatus -eq "VM running" -And  $(Get-AzureRmResource -ResourceGroupName "$ResourceGroupName" -Name $VMName).Tags.'Do Not Disturb' -eq "No" ){

           #Stop-AzurermVM -ResourceGroupName "$ResourceGroupName" -Name $VMName -Force -ErrorAction SilentlyContinue
           $processed_vm += $vm_name

             
            }
         
        }
       
       
      }



 }
 

  }
 
write-output "  List of VMs Stopped " |Out-File vmlist.txt
write-output "-----------------------"  | Add-Content vmlist.txt
$processed_vm | Add-Content vmlist.txt

Friday, February 10, 2017

Terraform



provider "azurerm" {
  subscription_id = "XXXXXXXXXXXXXXXXXXXXX"
  client_id       = "XXXXXXXXXXXXXXXXXXXxx"
  client_secret   = "XXXXXXXXXXXXXX"
  tenant_id       = "XXXXXXXXXXXXXXXXXXXXX"
}

# create a resource group
resource "azurerm_resource_group" "dhanesh-rs" {
  name = "terraformtest"
  location = "Southeast Asia"
}

resource "azurerm_virtual_network" "dhanesh-net"{

  address_space = ["10.0.0.0/16"]
  location = "Southeast Asia"
  name = "dhanesh-net"
  resource_group_name = "${azurerm_resource_group.dhanesh-rs.name}"
}

resource "azurerm_subnet" "dhanesh-sub" {
  address_prefix = "10.0.2.0/24"
  name = "dhanesh-sub"
  resource_group_name = "${azurerm_resource_group.dhanesh-rs.name}"
  virtual_network_name = "${azurerm_virtual_network.dhanesh-net.name}"
}

resource "azurerm_network_interface" "dhanesh-if" {

  location = "Southeast Asia"
  name = "dhanesh-if"
  resource_group_name = "${azurerm_resource_group.dhanesh-rs.name}"

  "ip_configuration" {

    name = "dhanesh-ifip"
    private_ip_address_allocation = "dynamic"
    subnet_id = "${azurerm_subnet.dhanesh-sub.id}"
  }
}

resource "azurerm_availability_set" "dhanesh-as" {

  name = "dhanesh-az"
  location = "Southeast Asia"
  resource_group_name = "${azurerm_resource_group.dhanesh-rs.name}"
  tags{
    environment="production"
  }

}

resource "azurerm_storage_account" dhanesh-ds {

  name = "dhaneshds"
  account_type = "Standard_LRS"
  location = "Southeast Asia"
  resource_group_name = "${azurerm_resource_group.dhanesh-rs.name}"
  tags {
    environment ="production"
  }
}

resource "azurerm_storage_container" "dhansh-sc" {
  name = "dhanesh-sc"
  resource_group_name = "${azurerm_resource_group.dhanesh-rs.name}"
  storage_account_name = "${azurerm_storage_account.dhanesh-ds.name}"
  container_access_type = "private"
}

resource "azurerm_virtual_machine" "dhanesh-vm" {
  name = "dhaneshvm"
  location = "Southeast Asia"
  resource_group_name = "${azurerm_resource_group.dhanesh-rs.name}"
  network_interface_ids = ["${azurerm_network_interface.dhanesh-if.id}"]
  vm_size = "Standard_A0"


  storage_image_reference {
    publisher = "Canonical"
    offer = "UbuntuServer"
    sku = "14.04.2-LTS"
    version = "latest"
  }

  storage_os_disk {
    name ="myosdisk"
    vhd_uri = "${azurerm_storage_account.dhanesh-ds.primary_blob_endpoint}${azurerm_storage_container.dhansh-sc.name}/myos.vhd"
    caching = "ReadWrite"
    create_option = "FromImage"
  }

  os_profile {

    admin_password = "Dhanesh@1234567"
    admin_username = "dhanesh"
    computer_name = "dhanesh-vm"
  }

  os_profile_linux_config {

    disable_password_authentication = false
  }

  tags {
   environment = "production"
  }

}

Thursday, February 9, 2017

Important Linux logs for auditing


/var/log/message – Whole system logs
/var/log/auth.log – Authentication logs.
/var/log/kern.log – Kernel logs.
/var/log/cron.log – Crond logs (cron job).
/var/log/maillog – Mail server logs.
/var/log/boot.log – System boot log.
/var/log/mysqld.log – MySQL database server log file.
/var/log/secure – Authentication log.
/var/log/utmp or /var/log/wtmp : Login records file.
/var/log/yum.log: Yum log files.

Removing sudo account


/usr/sbin/waagent -force -deprovision+user && export HISTSIZE=0 && sync

Tuesday, February 7, 2017

Creating Load in Linux Servers

Stress for Auto scaling testing [LINUX] 

For performance testing or auto scale testing we usually search for a method to generate CPU / Memory  load generator. Here is one of the tool which I have used for generating CPU load to test  auto scaling in Microsoft Azure is "stress" 

By default I didn't find the stress package in Centos so I had to enable epel repository .


yum install epel-release 
yum install stress

Monday, December 26, 2016

Creating multiple VM using Vagrant

This is a sample configuration for vagrant to create custom stack of servers.

Steps1. create file  "Vagrantfile"


#--DDL
require 'yaml'
records = YAML.load_file('config.yaml')
$x = records['server_name'].size
puts "You have asked to install #{$x} of systems "
puts "#{records['server_name']}"
cluster  = {}
(0..($x-1)).each do |i|
cluster["hostname" => "#{records['server_name'][i]['hostname']}" , "ip" => "#{records['server_name'][i]['ip']}"]="hostname-#{i}"
end
Vagrant.configure(2) do |config|

  cluster.each_with_index do |(info, hostname), index|

    config.vm.define hostname do |cfg|
      cfg.vm.provider :virtualbox do |vb, override|
        config.vm.box = "centos/7"
override.vm.synced_folder '.', '/vagrant' , type: "virtualbox"
override.vm.network :private_network, ip: "#{info['ip']}"
        override.vm.hostname = "#{info['hostname']}"
        vb.name = "#{info['hostname']}"
override.vm.provision "ansible_local" do |ansible|
ansible.playbook = "playbook.yaml"
end
      end # end provider
    end # end config

  end # end cluster
end


step 2. Create file config.yaml ( --- also needs to include )


---
 server_name:
         - hostname: "webserver"
           ip: "10.0.15.2"
         - hostname: "database"
           ip: "10.0.15.3"
         - hostname: "appserver"
           ip: "10.0.15.4"




step3. place one sample "playbook.yaml" file


---
- hosts: all
  become: yes
  tasks:
    - name: ensure ntpd is at the latest version
      yum: pkg=ntp state=latest
      notify:
      - restart ntpd
  handlers:
    - name: restart ntpd
      service: name=ntpd state=restarted



step4. run "vagrant up"



Note:  I got some issues while  starting vagrant related to sharing the directory to guest vm. At that time I used following command to resolve it.    "vagrant plugin install vagrant-vbguest "




Please test it and send me if there is any issues. 

Which operating system you like most?