Friday, July 27, 2018

synchronise time using ntpd and timesyncd

Imagine that you have many servers. It's important to have synchronized time between our servers - at least the log files should have same time in order to find out the correct sequence of the processes. We can use the following tools for time synchronization
  • ntpd - powerfull Network Time Protocol Daemon. Full implementation of  NTP protocol
  • systemd-timesyncd - it is lightweight daemon for synchronizing the system clock. This client implements an SNTP.  if we  have ntpd daemon we can use ntpdc - utility program used to monitor NTP daemo (now it's deprecated and we use ntpq).  If we have systemd-timesyncd we can use  timedatectl - it has very beautiful configurations and controls the system time/date very well.
  • chrony is also full implementation of NTP. it has really great performance because It quickly detects sudden time changes like ntpd.   
  • OpenNTPD - It's part of the Open BSD project that also implements NTP protocol.
There are another implementations too but know let's talk for about ntpd and timedatectl.

 How to Configure ntpd

  • Install and enable ntpd for CentOS:
yum install ntpd
systemctl enable ntpd
firewall-cmd --add-service=ntp --permanent
  • Install and enable ntpd for Debian:
apt-get install ntpd
apt-get install ntpdate
  • Add pools in  /etc/ntp.conf  server configuration
server 0.[YOUR_POOLl]
server 1.[YOUR_POOLl]
  • Add local clock in  /etc/ntp.conf  server configuration.  For example, we can add local clock as a stratum 10  server or we  can set up to stratum 15 - so that it will never be used unless internet access is lost.
server  127.127.1.0 
fudge   127.127.1.0 stratum 10
  • Add pool adress in /etc/ntp.conf client configuration
server  X.X.X.X
  • Force update time, we can create crontab too (optional)
ntpdate -u X.X.X.X
  • Start ntpd service for CentOS:
systemctl start ntpd
  • Start ntpd service for Debian:
service ntp start  or systemctl start ntp

Let's check if everything works well and print all the peers of the servers
[vq@centos etc]# ntpq -p
 remote   refid  st t when poll reach delay  offset jitter
===========================================================
*LOCAL(0) .LOCL. 10 l 40  64   17     0.000  0.000  0.000

 How to Configure timesyncd

Because of we have installed ntpd we can't use systemd-timesyncd. Even if you try to use systemd-timesyncd service you will have the follolwing error:

vq@debian:/var/log# systemctl status 
systemd-timesyncd.service
● systemd-timesyncd.service - Network Time Synchronization
   Loaded: loaded 
(/lib/systemd/system/systemd-timesyncd.service;
 enabled; vendor preset: enabled)
  Drop-In: /lib/systemd/system/systemd-timesyncd.service.d
           └─disable-with-time-daemon.conf
   Active: inactive (dead)
Condition: start condition failed at 
Fri 2018-07-27 13:31:23 +04; 35s ago
           └─ ConditionFileIsExecutable=
!/usr/sbin/ntpd was not met
     Docs: man:systemd-timesyncd.service(8)


So we should remove ntpd if we want to use  timesyncd and timedatectl. You can find  timesyncd configuration to the following URL:

/etc/systemd/timesyncd.conf

[Time]
NTP= ${URLS}
FallbackNTP=${URLS}
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048
  • check if NTP synchronization is enabled 
 
vq@debian:/etc# timedatectl
      Local time: Fri 2018-07-27 13:27:36 +04
  Universal time: Fri 2018-07-27 09:27:36 UTC
        RTC time: Fri 2018-07-27 09:46:02
       Time zone: Asia/Tbilisi (+04, +0400)
 Network time on: YES
NTP synchronized: YES
 RTC in local TZ: YES

Friday, July 13, 2018

How to install JDK on linux and how it really works?

How to install JDK on linux and how it really works?

You may , well, ask me the question - which installation file should you download for your Linux. You know, you have many options: prm or deb or tar.gz


What is difference between rpm and deb file formats?

  • RPM and DEB files files provide an easy way for software to be distributed, installed, upgraded, and removed 
  • tag.gz is g-zipped tar file. You should download it if you are going to install JDK manually.

What is difference between deb and rpm file formats?

To be honest, therey are both archive files, with some metadata. In order to install deb file you need dpkg - this is package manager for Debian-based systems like Ubuntu, Debian etc. For rpm file format we use RPM Package Manager.

What about dependency problem?

Everything seems great but sometimes we have dependency problems.  In the other words, we had better install all dependencies for our program to work correctly.  Therefor RPM and dpkg  is unable to automatically do it.   Fortunately we have many tools in order to sole the problem:

  • APT (the Advanced Packaging Tool) to install, update and remove software  on Debian-based systems. 
  • YUM  package-management utility that uses the RPM Package Manager.
  • Zypper this is my favorite  package manager for installing, updating and removing packages as well as for managing repositories.  

How To install RPM format JDK on linux?


This method is suitable for SUSE/Arch/Fedora/CentOS/RedHat etc. You should just have RPM package manager.
 

  •  Download oracle jdk RPM package
  • To install using terminal type: `rpm -ivh {jdk-name-version}.rpm`
  • Run the following commands
update-alternatives --install /usr/bin/java java /usr/java/latest/bin/java 100
update-alternatives --install /usr/bin/javac javac /usr/java/latest/bin/javac 100
view raw update_java_path.sh hosted with ❤ by GitHub
  • Set default Java for in system

update-alternatives --config java
update-alternatives --config javac
view raw default_java.sh hosted with ❤ by GitHub



  

How to install  tar.gz format zypped JDK on linux?


  •  Download oracle jdk tar.gz
  •  mkdir /opt/jdk
  •  tar -zxf {jdk-name-version}.tar.gz -C /opt/jdk
  • update-alternatives --install /usr/bin/java java /opt/jdk/{jdk-name-version}/bin/java 100
  • update-alternatives --install /usr/bin/javac javac /opt/jdk/{jdk-name-version}/bin/javac 100
Check if java is installed

  •  update-alternatives --display java
  •  update-alternatives --display javac
 Set default Java for in System
  •  update-alternatives --config java
  •  update-alternatives --config javac


how to change shceduler times in spring

How to change scheduler times in Spring

 

Imagine that you have scheduler but you want to change time at runtime. What Can you do? Off-course there are ways do solve the problem. So In this article I will write how to implement Trigger interface that is located to the following package: org.springframework.scheduling in the spring-context project.

1. First off all, let's create interface for our logic:

























2. In Spring we have org.springframework.scheduling.Trigger interface:
























So we can write our own implementation. It's very simple. We should just overwrite nextExecutionTime(_) method.  Something like that:



3. Now we can write configuration.





In this example we just choose ThreadPoolTaskScheduler that is child ofTaskScheduler interface.


4. So what is the next? Let's see how to use our scheduler. Everything is simple:



That's it!

Goodbye!
Have good day!