Docker容器時間與宿主機不一致/宿主機時間不同步校驗問題
一、Docker容器時間與宿主機不一致
如果在啟動Docker容器的過程中沒有單獨配置localtime,很可能造成Docker容器時間與主機時間不一致的情況,比如UTC和CST相差8小時,換句話來說就是容器時間與北京時間相差8個小時。
問題描述
問題:容器時間與北京時間相差8個小時
# 查看主機時間 [root@localhost ~]# date 2020年07月27日 星期三 22:42:44 CST # 查看容器時間 # docker exec -it <containerid> /bin/sh root@b43340ecf5ef:/# date Wed Jul 27 14:43:31 UTC 2020
原因:宿主機設置了時區(qū),而Docker容器并沒有設置,導致兩者相差8小時
- CST應該是指(China Shanghai Time,東八區(qū)時間)
- UTC應該是指(Coordinated Universal Time,標準時間)
所以,這2個時間實際上應該相差8個小時
所以,必須統(tǒng)一兩者的時區(qū)
解決方案
方法一:初始化容器時,容器時間與宿主機同步,docker run 添加時間參數(shù)
docker run -itd --privileged=true -v /etc/localtime:/etc/localtime:ro #實例 docker run -itd \ -e MYSQL_ROOT_PASSWORD=123456 \ -v /home/mysql/data:/var/lib/mysql \ -v /home/mysql/log:/var/log/mysql \ -v /home/mysql/etc/my.cnf:/etc/mysql/mysql.conf.d/mysqld.cnf \ -v /etc/localtime:/etc/localtime:ro \ --name mysql-server \ --network host \ --restart=always \ --privileged=true \ mysql:5.7.37
方法二:Dockerfile解決方案
# 方法1 # 添加時區(qū)環(huán)境變量,亞洲,上海 ENV TimeZone=Asia/Shanghai # 使用軟連接,并且將時區(qū)配置覆蓋/etc/timezone RUN ln -snf /usr/share/zoneinfo/$TimeZone /etc/localtime && echo $TimeZone > /etc/timezone # 方法2 # CentOS RUN echo "Asia/shanghai" > /etc/timezone # Ubuntu RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
方法三:docker-compose解決方案
#第一種方式(推薦): environment: TZ: Asia/Shanghai #第二種方式: environment: SET_CONTAINER_TIMEZONE=true CONTAINER_TIMEZONE=Asia/Shanghai #第三種方式: volumes: - /etc/timezone:/etc/timezone - /etc/localtime:/etc/localtime
方法四:正在運行的容器,可以宿主機直接執(zhí)行命令給某個容器同步時間
#方法1 直接在宿主機操作 docker cp /etc/localtime 【容器ID或者NAME】:/etc/localtime docker cp -L /usr/share/zoneinfo/Asia/Shanghai 【容器ID或者NAME】:/etc/localtime #方法2 登錄容器同步時區(qū)timezone,一般是因為時區(qū)不同導致時間差 ln -sf /usr/share/zoneinfo/Asia/Singapore /etc/localtime
在完成后,再通過date命令進行查看當前時間
但是,在容器中運行的程序的時間不一定能更新過來,比如在容器運行的mysql服務,在更新時間后,通過sql查看mysql的時間
select now() from dual;
可以發(fā)現(xiàn),時間并沒有更改過來
這時候必須要重啟mysql服務或者重啟docker容器,mysql才能讀取到更改過后的時間
二、宿主機時間不同步
ntp時間服務器——時間同步
具體兩種模式:
1、服務器數(shù)量比較少,可以直接與時間服務器同步
2、本地服務器較多,在本地自建時間同步服務器
時間同步的兩個命令:
ntpd
:校準時間,一點點的校準過來時間的,最終把時間慢慢的校正對。ntpd服務可以在修正時間的同時,修正cpu tick。ntpd有一個自我保護設置: 如果本機與上源時間相差太大, ntpd不運行ntpdate
:不會考慮其他程序是否會陣痛,直接調整時間,有可能會對程序造成影響
ntpdate [-nv] [ IP/hostname] # ntpdate -u 192.168.0.2 # ntpdate -u time.ntp.org # ntpdate -u ntp.sjtu.edu.cn >>/var/log/ntp.log 2>&1;hwclock -w
注釋:-u:指定使用無特權的端口發(fā)送數(shù)據(jù)包,可以越過防火墻與主機同步,這個參數(shù)可用可不用,看你自己
理想的做法是:
使用ntpd來校準時鐘,而不是調整計算機時鐘上的時間。
在計算機剛剛啟動,但還沒有啟動很多服務的那個時候可以使用ntpdate同步時間
直接用命令與時間服務器進行同步
實例:使用ntpdate 進行同步:
ntpdate -u x.x.x.x #這里的x.x.x.x可以是公網時間服務器IP地址,也可以是下面兩種我們自建的ntp服務器地址
#公網時間服務器IP地址 #阿里云時間服務器 http://time1.aliyun.com http://time2.aliyun.com http://time3.aliyun.com http://time4.aliyun.com http://time5.aliyun.com http://time6.aliyun.com http://time7.aliyun.com time.pool.aliyun.com #centos也有幾個時間服務器(最好還是用國內的吧) 0.centos.pool.ntp.org 1.centos.pool.ntp.org 2.centos.pool.ntp.org
可以加一個定時計劃,定時同步
注釋:在有時間同步服務器的情況下,直接使用該命令就可以讓宿主機實現(xiàn)時間同步
crontab -e */1 * * * * /usr/sbin/ntpdate -u x.x.x.x > /dev/null 2&1
自建ntpd 時間同步服務器
NTP通信協(xié)議原理: 首先主機啟動NTP。 客戶端會向NTP服務器發(fā)送調整時間的message。 然后NTP server會送出當前的標準時間給client client接受來自server的時間后,會根據(jù)這個信息來調整自己的時間。這樣就實現(xiàn)了網絡對時。 #NTP這個deamon采用了UDP 123端口 遠程服務器的層級(stratum): 由于NTP是層型結構,有頂端的服務器,多層的Relay Server再到客戶端. 所以服務器從高到低級別可以設定為1-16. 為了減緩負荷和網絡堵塞,原則上應該避免直接連接到級別為1的服務器的
1、配置ntp服務器
1)安裝
# yum -y install ntp
2)開機自啟動
# systemctl start ntpd # systemctl enable ntpd
3)修改配置文件:(詳解)
[root@localhost ~]# less /etc/ntp.conf # For more information about this file, see the man pages # ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5). driftfile /var/lib/ntp/drift #系統(tǒng)時間與BIOS事件的偏差記錄 配置與本機同步的機器權限↓ # Permit time synchronization with our time source, but do not # permit the source to query or modify the service on this system. restrict default kod nomodify notrap nopeer noquery restrict -6 default kod nomodify notrap nopeer noquery
默認拒絕任何操作,任何ip4地址、ip6地址 不能修改、不能trap遠程登錄、不能嘗試對等、不能校對時間,
restrict 控制相關權限。
語法為: restrict [ 客戶端IP ] mask [ IP掩碼 ] [參數(shù)]
其中IP地址也可以是default ,default 就是指所有的IP
參數(shù)有以下幾個:
nomodify
:客戶端不能更改服務端的時間參數(shù),但是客戶端可以通過服務端進行網絡校時。notrust
:拒絕沒有認證用戶端noquery
:客戶端不能使用ntpq,ntpc來查詢ntp服務器,等于不提供校對時間服務notrap
:不提供trap遠程登錄功能,trap服務是一種遠程時間日志服務 拒絕為匹配的主機提供模式 6 控制消息陷阱服務。陷阱服務是 ntpdq 控制消息協(xié)議的子系統(tǒng),用于遠程事件日志記錄程序。nopeer
:用于阻止主機嘗試與服務器對等kod
: 訪問違規(guī)時發(fā)送 KoD 包,向不安全的訪問者發(fā)送Kiss-Of-Death報文。 restrict -6 表示IPV6地址的權限設置。
如何控制客戶的范圍 # Permit all access over the loopback interface. This could #下面的配置 允許運行任何權限的訪問在本地接口, # be tightened as well, but to do so would effect some of #這里最好收緊權限,但是會影響一些管理功能 # the administrative functions. restrict 127.0.0.1 restrict ::1 # Hosts on local network are less restricted. #配置 給本地局域網絡配置小一些的權限 #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap 本機與公共時間服務器同步選項 設定NTP主機來源(其中prefer表示優(yōu)先主機) # Use public servers from the pool.ntp.org project. 使用公共服務器從 時間池工程中 # Please consider joining the pool (http://www.pool.ntp.org/join.html). 請考慮加入這個 時間池 項目 server 0.centos.pool.ntp.org iburst server 1.centos.pool.ntp.org iburst server 2.centos.pool.ntp.org iburst server 3.centos.pool.ntp.org iburst
server host [ key n ] [ version n ] [ prefer ] [ mode n ] [ minpoll n ] [ maxpoll n ] [ iburst ] 其中host是上層NTP服務器的IP地址或域名,隨后所跟的參數(shù)解釋如下所示: ◆ key: 表示所有發(fā)往服務器的報文包含有秘鑰加密的認證信息,n是32位的整數(shù),表示秘鑰號。 ◆ version: 表示發(fā)往上層服務器的報文使用的版本號,n默認是3,可以是1或者2。 ◆ prefer: 優(yōu)先使用。 ◆ mode: 指定數(shù)據(jù)報文mode字段的值。 ◆ minpoll: 指定與查詢該服務器的最小時間間隔為2的n次方秒,n默認為6,范圍為4-14。 ◆ maxpoll: 指定與查詢該服務器的最大時間間隔為2的n次方秒,n默認為10,范圍為4-14。 ◆ iburst: 當初始同步請求時,采用突發(fā)方式接連發(fā)送8個報文,時間間隔為2秒。
#broadcast 192.168.1.255 autokey # broadcast server #broadcastclient # broadcast client #broadcast 224.0.1.1 autokey # multicast server #multicastclient 224.0.1.1 # multicast client #manycastserver 239.255.254.254 # manycast server #manycastclient 239.255.254.254 autokey # manycast client 當外部時間不可用時,以本地(本機)時間作為服務時間,本地時間以 127.127.1.0 表示 級別為10 # Undisciplined Local Clock. This is a fake driver intended for backup # and when no outside source of synchronized time is available. #server 127.127.1.0 # local clock #fudge 127.127.1.0 stratum 10 下面是一些加密認證的配置 # Enable public key cryptography. #crypto includefile /etc/ntp/crypto/pw # Key file containing the keys and key identifiers used when operating # with symmetric key cryptography. keys /etc/ntp/keys # Specify the key identifiers which are trusted. #trustedkey 4 8 42 # Specify the key identifier to use with the ntpdc utility. #requestkey 8 # Specify the key identifier to use with the ntpq utility. #controlkey 8 # Enable writing of statistics records. #statistics clockstats cryptostats loopstats peerstats
一個簡單實例如下:
restrict default kod nomodify notrap nopeer noquery #拒絕所有ip4 的請求 # restrict -6 default kod nomodify notrap nopeer noquery #針對ipv6設置 # 允許本地所有操作 restrict 127.0.0.1 #restrict -6 ::1 # 允許的局域網絡段或單獨ip 某些權限 restrict 10.0.0.0 mask 255.0.0.0 nomodify motrap restrict 192.168.0.0 mask 255.255.255.0 nomodify motrap restrict 192.168.1.123 mask 255.255.255.255 nomodify motrap # 使用上層的internet ntp服務器 restrict cn.pool.ntp.org restrict 1.cn.poo.ntp.org restrict 0.asia.pool.ntp.org restrict 3.asia.pool.ntp.org server cn.pool.ntp.org prefer server 1.cn.poo.ntp.org server 0.asia.pool.ntp.org server 3.asia.pool.ntp.org # 如果無法與上層ntp server通信以本地時間為標準時間 server 127.127.1.0 # local clock fudge 127.127.1.0 stratum 10 driftfile /var/lib/ntp/drift # 計算本ntp server 與上層ntpserver的頻率誤差 # Key file containing the keys and key identifiers used when operating # with symmetric key cryptography. keys /etc/ntp/keys logfile /var/log/ntp.log #日志文件
配置/etc/sysconfig/ntpd文件
ntp服務,默認只會同步系統(tǒng)時間。如果想要讓ntp同時同步硬件時間,可以設置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 這樣,就可以讓硬件時間與系統(tǒng)時間一起同步。
# 允許BIOS與系統(tǒng)時間同步,也可以通過hwclock -w 命令
# vim /etc/sysconfig/ntpd # 添加 SYNC_HWCLOCK=yes
4)啟動ntpd 服務
# systemctl start ntpd
查看ntp的端口,可以看到123端口
# netstat -unlnp
查看ntp服務器有無和上層連通
# ntpstat synchronised to NTP server (84.16.73.33) at stratum 2 time correct to within 184 ms polling server every 128 s
剛啟動的時候,一般是:
# ntpstat unsynchronised time server re-starting polling server every 64 s
ntptrace -n 127.0.0.1 127.0.0.1: stratum 3, offset -0.001095, synch distance 0.532610 116.193.83.174: timed out, nothing received
查看ntp服務器與上層ntp服務器的狀態(tài):ntpq
# ntpq -p # 其中: # remote - 本機和上層ntp的ip或主機名,“+”有連線可做候選,“*”正在使用的 # refid - 更上一層的ntp地址 # st - stratum的 級別 # when - 多少秒前曾經同步過時間 # poll - 下次更新在多少秒后 # reach - 已經向上層ntp服務器要求更新的次數(shù) # delay - 網絡延遲 # offset - 時間補償 # jitter - 系統(tǒng)時間與bios時間差 # ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== *time4.aliyun.co 10.137.38.86 2 u 111 128 377 27.480 -5.995 1.852
2、配置NTP客戶端
在所有客戶端上vim /etc/ntp.conf,添加:
server x.x.x.x #x.x.x.x 為上面配置的ntp服務器地址
注意:當server與client之間的時間誤差過大時(可能是1000秒),處于對修改時間可能對系統(tǒng)和應用帶來不可預知的問題,NTP將停止時間同步!
所以如果發(fā)現(xiàn)NTP啟動之后時間并不進行同步時,應該考慮到可能是時間差過大引起的,此時需要先手動進行時間同步 ntpdate !
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
如何使用Docker部署briefing視頻聊天系統(tǒng)
briefing是一個開源的、安全的直接視頻群聊平臺,這篇文章主要介紹了使用Docker部署briefing視頻聊天系統(tǒng)的詳細過程,需要的朋友可以參考下2024-01-01解決Docker啟動Elasticsearch7.x報錯的問題
這篇文章主要介紹了解決Docker啟動Elasticsearch7.x報錯的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-11-11詳解docker中使用systemctl啟動服務報錯的解決辦法
這篇文章主要介紹了詳解docker中使用systemctl啟動服務報錯的解決辦法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08Docker安裝Redis并使用Another?Redis?Desktop?Manager連接的方法
Redis?是一個使用C語言編寫的、開源的(遵守?BSD?協(xié)議)、高性能的、支持網絡、可基于內存亦可持久化的日志型、Key-Value的NoSQL數(shù)據(jù)庫,這篇文章主要介紹了Docker安裝Redis并使用Another?Redis?Desktop?Manager連接,需要的朋友可以參考下2022-09-09