Exception in thread “main” java.lang.UnsupportedClassVersionError: org/elasticsearch/plugins/PluginCli : Unsupported major.minor version 52.0

The aws ec2-instance has older version has Java 1.7. The latest version of the elastic-search requires Java 1.8. If you try to install the plugin like x-pack now, you will get this error:

[ec2-user@ip-10-0-20-199 bin]$ sudo ./elasticsearch-plugin install x-pack[ec2-user@ip-10-0-20-199 bin]$ sudo ./elasticsearch-plugin install x-packException in thread “main” java.lang.UnsupportedClassVersionError: org/elasticsearch/plugins/PluginCli : Unsupported major.minor version 52.0 at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:803) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:442) at java.net.URLClassLoader.access$100(URLClassLoader.java:64) at java.net.URLClassLoader$1.run(URLClassLoader.java:354) at java.net.URLClassLoader$1.run(URLClassLoader.java:348) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:347) at java.lang.ClassLoader.loadClass(ClassLoader.java:425) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:312) at java.lang.ClassLoader.loadClass(ClassLoader.java:358) at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:482)[ec2-user@ip-10-0-20-199 bin]$ packet_write_wait: Connection to 35.154.108.46 port 22: Broken pipe

The fix is simple, update the Java version to 1.8.

This how we do it on aws ec2 instance:

[ec2-user@ip-10-0-20-199 ~]$ sudo yum install java-1.8.0
[ec2-user@ip-10-0-20-199 ~]$ sudo /usr/sbin/alternatives –config java
[ec2-user@ip-10-0-20-199 ~]$ sudo /usr/sbin/alternatives –config java

There are 2 programs which provide ‘java’.

Selection Command
———————————————–
*+ 1 /usr/lib/jvm/jre-1.7.0-openjdk.x86_64/bin/java
2 /usr/lib/jvm/jre-1.8.0-openjdk.x86_64/bin/java

Enter to keep the current selection[+], or type selection number: 2

 

 

Advertisements

Quick tip on MySQL dump

Terminal command to dump MySQL data: 
mysqldump -u -p > /path/to/sqldump/db_database.sql

eg: mysqldump -uroot -padmin123 db_sports > /home/ec2-user/db_sports.sql

Import sql dump:
mysql -u -p < /path/of/sqldump/db_database.sql

ex: mysql -uroot -padmin123 db_sports < /home/ec2-user/db_sports.sql

AWS S3 permissions

A quick tip on aws S3 permissions to a specific folder in a bucket.

{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"s3:Put*",
"s3:Get*",
"s3:List*"
],
"Resource": [
"arn:aws:s3:::nameofmybucket/profile/*"
]
}]
}

Try to avoid bucket root level permission as much as possible. Also, try to use cognito if the client wants to directly access aws resources.

Monitoring Memory Utilisation of aws EC2 instance in CloudWatch

Connect to your EC2 instance using ssh.
ssh -i ec2-user@my_ec2_ip_address

Install following perl packages
sudo yum install -y perl-Switch perl-DateTime perl-Sys-Syslog perl-LWP-Protocol-https

Download the monitoring scripts
curl http://aws-cloudwatch.s3.amazonaws.com/downloads/CloudWatchMonitoringScripts-1.2.1.zip -O

Install the monitoring scripts
unzip CloudWatchMonitoringScripts-1.2.1.zip
rm CloudWatchMonitoringScripts-1.2.1.zip
cd aws-scripts-mon

run the following command to check the memory utilisation.
./mon-get-instance-stats.pl --recent-hours=12

In order to push this data periodically to cloud watch, we need to create an IAM user with relevant permissions, then schedule a cron job to periodically push the data to aws cloud watch.

Create a new IAM user and provide him access to the ec2 instance and cloud watch. Make a note of the aws credentials i.e. access key and secret key. You need this later. Make sure the IAM user has following access.

cloudwatch:PutMetricData
cloudwatch:GetMetricStatistics
cloudwatch:ListMetrics
ec2:DescribeTags

For the purpose of the demo, you can provide full access to EC2 and CloudWatch (This is not recommended for production.)

run the following command
cp awscreds.template awscreds.con

open the file  awscreds.conf and enter the key & secret.
The content of the file should look something like this

AWSAccessKeyId=AKAWSACESSKEYSA
AWSSecretKey=z/NOAWSSECRETkeyzt

Run the following command to push the data to cloud watch
./mon-put-instance-data.pl --mem-util --mem-used --mem-avail

Now, configure the cron tab to automate this process at eveny 5 minutes interval.
crontab -e
*/5 * * * * ~/aws-scripts-mon/mon-put-instance-data.pl --mem-util --disk-space-util --disk-path=/ --from-cron

In your aws console, go to cloud watch section, and select Metrics => custom namespaces => Linux System. Select the required metrics.

aws_cloud_watch_memory.png

 

Detailed documnetation from aws can be found here

Installing node.js on amazon EC2 instance

Launch an EC2 instance of type Amazon Linux AMI from your aws console.

SSH into your ec2 instance
ssh -i ec2-user@my_ec2_ip_address

Update the instance
sudo yum update -y

#install developer tools
sudo yum groupinstall -y "Development Tools"

Install the node using nvm as it allows you to switch between any version of the node 🙂
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.32.0/install.sh | bash
. ~/.nvm/nvm.sh
nvm install 4.4.5
node -e "console.log('Running Node.js ' + process.version)"

Install pm2
npm install pm2 -g --save

pull the source code in ec2 instance. run your node app using pm2
pm2 start app.js --name="api"

Setting git on aws EC2 instance

Launch an EC2 instance of type Amazon Linux AMI from your aws console.

SSH into your ec2 instance
ssh -i ec2-user@my_ec2_ip_address

Update the instance
sudo yum update -y

install developer tools
sudo yum groupinstall -y "Development Tools"

install git
sudo yum install git

checkout the source code
git clone https://my.git.repo.git
cd my_local_git_folder
git checkout -f branch_to_checkout

Setting up redis on aws EC2 instance

Launch an EC2 instance of type Amazon Linux AMI from your aws console.

SSH into your ec2 instance
ssh -i ec2-user@my_ec2_ip

Update the instance
sudo yum update -y

Install the developer tools to compile the redis source
sudo yum groupinstall -y "Development Tools"

Install tcl
sudo yum install -y tcl

Download the latest redis source
wget http://download.redis.io/redis-stable.tar.gz

unzip the source
tar xvzf redis-stable.tar.gz

go to the redis folder and run the make command to compile the source
cd redis-stable
make
make test
sudo make install

to run the server, go to the src folder and run the daemonize command.
cd src
./redis-server --daemonize yes

Run the redis-cli command to check if server resonds.
redis-cli
ping

If the response is PONG then you are good to go 🙂

Setting up MySQL server on aws EC2 instance

Launch an EC2 instance of type Amazon Linux AMI from your aws console.

SSH into your ec2 instance
ssh -i ec2-user@my_ec2_ip_address

Update the instance
sudo yum update -y

Install the mysqld server
sudo yum install -y mysql55-server

start the mysqld instance
sudo service mysqld start

the following command ensure launches mysqld on server restart
sudo chkconfig mysqld on

run the following command to set password for root user and delete test databases.
sudo mysql_secure_installation

make a note of the root password 🙂

Let’s try to create a user and database. This way we can control the database access levels.
mysql -uroot -pmy_root_password

I’m going to create a db_demo with demo_user having password demo123.
CREATE DATABASE db_demo;
USE db_demo;
CREATE USER 'demo_user'@'localhost' IDENTIFIED BY 'demo123';
GRANT ALL PRIVILEGES ON *.* TO 'demo_user'@'localhost' WITH GRANT OPTION;
CREATE USER 'demo_user'@'%' IDENTIFIED BY 'demo123';
GRANT ALL PRIVILEGES ON *.* TO 'demo_user'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;

That’s it. You are all set.

Note:

  • details on mysql privileges can be found here
  • Don’t forget to open the default port 3306 if you want to access the database outside the ec2 instance boundary
  • useful mysqld commands
    • sudo service mysqld start
    • sudo service mysqld stop
    • sudo service mysqld restart
    • sudo service mysqld status