개요



서버 사이 동기화 작업을 진행해야 하는 상황이 발생하게 된다. 서버 동기화에서 가장 중요한 물리적 요인중 하나로는 시간이다.

각 서버의 운영체제 시간(or 하드웨어 시간)이 조금이라도 틀어지게 된다면 연동이 되지 않거나, 연동이 되더라도 나중에 시스템에 문제가 발생했을 때 크게 아래와 같이 문제점이 발생할 수 있다.


① 로그를 확인하는 과정에서 시간 간극으로 인해 정확한 원인을 파악하지 못하는 경우가 발생한다.

② 각종 로그 설정이나 디버깅에 있어 서버마다 다른 시간을 바라보고 있으면 디버깅이 어려워지며 특정 어플리케이션에서는 서비스에 문제가 발생 될 수 있다.


이에, 모든 서버가 하나의 시간을 바라볼 수 있도록 시간을 동기화 해주어야 한다. 이로부터 나온 것이 NTP 서버이며 이후 chrony 서비스가 출시되었다.


? NTP (Network Time Protocol)?
NTP(Network Time Protocol)는 패킷교환, 가변 지연 데이터 네트워크를 통해 컴퓨터 시스템간의 시간 동기화를 위한 네트워킹 프로토콜



Amazon에서도 Time Sync 서비스를 기본적으로 제공하고 있으며 기본적으로 Chrony 방식으로 제공된다.

(참조: https://docs.aws.amazon.com/ko_kr/AWSEC2/latest/UserGuide/set-time.html )


(ntpd는 구동을 하고 있지 않고 있고 chrony가 구동되고 있음을 확인할 수 있다.)


[ec2-user@localhost ~]$ date
Tue Aug 24 04:03:26 UTC 2021

#(Line 2) server 169.254.169.123 행이 추가되었는지 확인해보고 안되어 있으면 수정한다.
[ec2-user@localhost ~]$ sudo vim /etc/chrony.conf

1 # use the Amazon Time Sync Service (if available)
2 server 169.254.169.123 prefer iburst minpoll 4 maxpoll 4

# 시스템 부팅될 때마다 시작하도록 설정
[ec2-user@localhost ~]$ sudo chkconfig chronyd on
Note: Forwarding request to 'systemctl enable chronyd.service'.



( vim /etc/chrony.conf )


특징


  •  Amazon Linux2 OS는 기본적으로 Amazon Time Sync Service로 설정이 되어 있다.
    다시 말해, chrony로 시간을 동기화 하고 있고 chrony 설정은 Amazon Time Sync Service IP로 되어있다.
  •  Amazon Time Sync Service IP 는 169.254.169.123 로컬 IP 주소이며 외부 인터넷 Access를 별도로 구성할 필요 없이 Private Subnet 안에서도 서비스에 안전하게 액세스할 수 있다.


시간 동기화 확인

# chrony가 169.254.169.123 IP 주소를 사용하여 시간 동기화를 하고 있는지 확인
[ec2-user@localhost ~]$ chronyc sources -v

-----
  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current best, '+' = combined, '-' = not combined,
| /             'x' = may be in error, '~' = too variable, '?' = unusable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
> 169.254.169.123               3   4   377    14    -15us[  -17us] +/-  505us
^- time2.uni-paderborn.de        1   8   327  1093  -4175us[-4113us] +/-  122ms
^- any.time.nl                   2   9   377   455  +3696us[+3732us] +/-   72ms
^- dadns.cdnetworks.co.kr        2  10   377    72   +680us[ +664us] +/-   71ms
^- 193.123.243.2                 2   9   377    65   +777us[ +760us] +/-   35ms
-----

# 시간 동기화 지표 확인
[ec2-user@localhost ~]$ chronyc tracking

Reference ID    : A9FEA97B (169.254.169.123)
Stratum         : 4
Ref time (UTC)  : Tue Aug 24 04:32:38 2021
System time     : 0.000000527 seconds fast of NTP time
Last offset     : -0.000000187 seconds
RMS offset      : 0.000000937 seconds
Frequency       : 23.054 ppm slow
Residual freq   : -0.000 ppm
Skew            : 0.021 ppm
Root delay      : 0.000476114 seconds
Root dispersion : 0.000272936 seconds
Update interval : 16.2 seconds
Leap status     : Normal

타임존 설정


EC2 생성시 기본적으로 UTC로 전역 타임존이 설정되어 있다. 타임존 설정을 통해 로컬환경과 시간동기화를 할 수 있다. 서버 시간대역을 UTC에서 KST 로 변경을 해보자



? UTC 단순 설명? 
UTC는 영국을 기준(UTC +0:00)으로 각 지역의 시차를 규정한 것이다. 한국은 영국보다 9시간 빠르므로 UTC +9:00이라고 표시한다.
UTC에 대하여 상세적인 내용을 알아보고 싶다면 아래 링크를 추천한다. 
https://springleaves.tistory.com/28



방법 1. 시스템 설정

# 현재 시간 확인
[root@localhost ~] date
Tue Aug 24 04:40:30 UTC 2021

# 시스템 전역 timezone은 /etc/sysconfig/clock 에 정의, 한국 기준으로 재설정하자.
vim /etc/sysconfig/clock

# 수정 전
ZONE="UTC"
UTC=true

# 수정 후
ZONE="Asia/Seoul"
KST=True

# /etc/localtime 내용 확인 -> UTC로 내용 구성이 되어있음
[root@localhost ~] cat /etc/localtime
TZif2UTCTZif2 
UTC0

# 기존(UTC 시간) /etc/localtime 삭제
[root@localhost ~] rm /etc/localtime

# 한국 표준시간대 정보를 심볼릭 링크 설정
[root@localhost ~] ln -s /usr/share/zoneinfo/Asia/Seoul /etc/localtime

# 변경사항 확인
[root@localhost ~] date
Tue Aug 24 13:43:32 KST 2021

# (기타) cp 명령을 통한 덮어쓰기를 통해서도 설정이 가능하다.
[root@localhost ~] cp -p /usr/share/zoneinfo/Asia/Seoul /etc/localtime



방법 2. tzselect 명령어를 통한 설정


2-1. 대륙 선택


tzselect 명령어를 입력하면 대륙을 선택하는 옵션을 확인할 수 있다. 현재 아시아(Asia)이므로 '4'를 입력해주자. 

[root@test ~]# vim /etc/sysconfig/clock
[root@test ~]# tzselect
Please identify a location so that time zone rules can be set correctly.
Please select a continent, ocean, "coord", or "TZ".
 1) Africa
 2) Americas
 3) Antarctica
 4) Asia
 5) Atlantic Ocean
 6) Australia
 7) Europe
 8) Indian Ocean
 9) Pacific Ocean
10) coord - I want to use geographical coordinates.
11) TZ - I want to specify the time zone using the Posix TZ format.
#? 4


2-2. 나라 선택

그 다음 나라를 선택하는 옵션이 나온다. 한국(South Korea)는 23번에 있다.


확인 메시지가 출력되며 현재 시각과 매칭되는지 확인 후 맞다면 '1'을 입력하면 된다.


The following information has been given:

  Korea (South)

Therefore TZ='Asia/Seoul' will be used.
Selected time is now:  Mon Dec  6 16:54:06 KST 2021.
Universal Time is now:  Mon Dec  6 07:54:06 UTC 2021.
Is the above information OK?
1) Yes
2) No
#? Yes
Please enter a number in range.
#? 1


2-3. 환경변수 저장


# 아직 변경되지 않음을 확인할 수 있다.
[root@test ~]# date
Mon Dec  6 08:05:46 UTC 2021


개인 로컬 환경 시  .bash_profile 로 호스팅 환경 및 여러 서버를 관리 시  /etc/profile 에 설정한다.


[root@test ~]# vim /etc/profile

 78 TZ='Asia/Seoul'
 79 export TZ

# 환경변수 적용
[root@test ~]# source /etc/profile


 

       ( vim /etc/profile )