Docker安裝Redis及Warning解決方法


虛擬機環境:VirtualBox

操作系統:CentOS 7

宿主機: Microsoft Windows 10 家庭中文版

Docker簡介

Docker是一個輕量級容器技術。Docker直接運行在Linux上,而不是運行在虛擬機上,實現了虛擬機技術的資源格式,性能高於虛擬機技術。

1)工作流程:

Docker支持將軟件編譯成一個鏡像,在這個鏡像里做好軟件的各種配置,然后發布這個鏡像,使用者可以運行這個鏡像,運行中的鏡像被稱為容器。

2)核心概念:

鏡像(Image):Docker鏡像用於創建鏡像的模板,可以自己編譯或者從從Docker Hub上pull拉取到機器上

容器(Container):獨立運行運行的鏡像,每個容器都有唯一的container-id

客戶端(Client):客戶端通過命令行或者其他工具使用Docker

主機(Host):執行Docker守護進程(后台一直運行)和容器的物理資源。

倉庫(Registry):保存鏡像的遠程倉庫,可以搜鏡像,官方Docker Hub為https://hub.docker.com/。

配置鏡像加速器

倉庫可以申請阿里雲Docker代理倉庫加速,並且是免費的!

阿里雲傳送門:容器Hub服務控制台:https://cr.console.aliyun.com/

1. 安裝/升級Docker客戶端
推薦安裝1.10.0以上版本的Docker客戶端,參考文檔: https://yq.aliyun.com/articles/110806

2. 配置鏡像加速器
針對Docker客戶端版本大於 1.10.0 的用戶
您可以通過修改daemon配置文件/etc/docker/daemon.json來使用加速器
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://t5t8q6wn.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

建議使用雲服務器實驗,不許要本第安裝Windows和Linux雙系統或使用虛擬機軟件模擬,啟動方便迅速,推介阿里雲和騰訊雲,學生有優惠。

配置網絡

一般配置兩個網卡:NAT,Host-Only

NAT連接到外網,Host-Only和主機通信。安裝虛擬機時

管理>全局設定>網絡,點擊偏右上角位置的綠色十字按鈕添加新NAT網絡,進入端口轉發,在IPv4下有和上一步一樣的綠色十字按鈕,點擊添加新的端口轉發規則主機IP本地,子系統IP看自己Linux網卡地址。

端口轉發規則

上面只是個示例。實際使用中redis一般這樣定義規則。

名稱 協議 主機IP 主機端口 子系統IP 子系統端口
redis TCP 127.0.0.1 6979 10.0.2.15 6979

僅主機網卡配置靜態IP。

ip addr 查看網卡配置

vim /etc/sysconfig/network-scripts/ifcfg-ensp08
TYPE=Ethernet
BOOTPROTO=static
NAME=enp0s8
DEVICE=enp0s8
ONBOOT=yes

IPADDR=192.168.56.101
NETMASK=255.255.255.0
:wq 保存退出

systemctl retart network 重啟網絡

也許你會看到NM_CONTROLLED這個參數,下面解釋一下。
NM_CONTROLLED=yes表示NetworkManager daemon管理你的Network Interface,
NM_CONTROLLED=no表示用舊策略管理Network Interface,即用配置文件/etc/sysconfig/network-scripts/ifcfg-ethX管理。
建議yes或者不填寫,不然配置不好機子的網絡會出問題。

登出為什么用exit而不是logout?比如下面的情況。

[root@localhost ~]# logout
bash: logout: not login shell: use `exit' 

首先要弄清楚什么是login shell和no-login shell。

login shell就是使用之前我們還需要輸入帳號密碼,比如我們使用tty1~tty6或使用ssh客戶端遠程登陸登入時。
no-login shell當然就是不用登入了,比如我們用圖形化進入系統后直接點開終端程序或者在shell里直接輸入bash命令。

login shell使用時要加載全局配置文件(/etc/profile等)和個人配置文件(~/.bash_profile, ~/.bashrc, ~/.bash_login等);no-longin shell只會讀取~/.bashrc。

該提示就是說,你當前不是login shell,要用exit命令而不是logout來退出登錄。

打包安裝開發工具

yum update
yum install gcc
yum install gcc-c++
yum install make
yum install kernel-headers
yum install kernel-devel

(一定要先安裝gcc和gcc-c++,再安裝下面兩個kernel,否則kernel無法完全編譯)
或者

yum groupinstall "Development Tools"

通過yum group可安裝的程序列表(包括依賴):

bison
byacc
cscope
ctags
cvs
diffstat
doxygen
flex
gcc
gcc-c++
gcc-gfortran
gettext
git
indent
intltool
libtool
patch
patchutils
rcs
redhat-rpm-config
rpm-build
subversion
swig
systemtap

安裝Docker

CentOS安裝命令:

[root@localhost~]yum update
[root@localhost~]yum install docker

驗證安裝成功,這里通過查看版本來看:

docker -v
Docker version 1.7.0 build 0baf609

Docker開機自啟動

[root@localhost ~]# docker info
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
[root@localhost ~]# systemctl start docker
[root@localhost ~]# systemctl enable docker
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.

Docker鏡像本地位置

當前所有的鏡像、容器默認都存儲var/lib/docker,進去看一下

[root@localhost /]# cd var/lib/docker
[root@localhost docker]# ll
total 4
drwx------.  2 root root    6 May 31 16:02 containers
drwx------.  3 root root   22 May 31 14:43 image
drwxr-x---.  3 root root   19 May 31 14:43 network
drwx------. 13 root root 4096 May 31 18:35 overlay2
drwx------.  4 root root   32 May 31 14:43 plugins
drwx------.  2 root root    6 May 31 14:43 swarm
drwx------.  2 root root    6 May 31 18:35 tmp
drwx------.  2 root root    6 May 31 14:43 trust
drwx------.  3 root root   97 May 31 14:49 volumes

Docker服務

Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?

systemctl status docker - 檢查docker服務狀態
systemctl start docker - 啟動docker服務
systemctl stop docker - 停止docker服務

啟動redis出現的警告和解決方法

[root@localhost etc]# docker run -d -p 6378:6379 --name port-redis redis
25075e75cd5a387c4c807e2596a31b362efd7ee50ead5f45a18638ee96e66a66
[root@localhost etc]# docker logs port-redis
1:C 17 May 2019 06:36:35.043 # oO0OoO0OoO0Oo Redis is starting oO0OoO0OoO0Oo
1:C 17 May 2019 06:36:35.043 # Redis version=5.0.4, bits=64, commit=00000000, modified=0, pid=1, just started
1:C 17 May 2019 06:36:35.044 # Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf
1:M 17 May 2019 06:36:35.046 * Running mode=standalone, port=6379.
1:M 17 May 2019 06:36:35.048 # WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.
1:M 17 May 2019 06:36:35.048 # Server initialized
1:M 17 May 2019 06:36:35.048 # WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.
1:M 17 May 2019 06:36:35.048 # WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.
1:M 17 May 2019 06:36:35.048 * Ready to accept connections

解決順序上,建議將警告1放到最后解決。

  • 警告1:

Warning: no config file specified, using the default config. In order to specify a config file use redis-server /path/to/redis.conf

意思:未指定配置文件,使用缺省配置。使用redis /path/to/redis.conf(你的配置文件路徑)指定配置文件。

解決方法:

在指定目錄下創建配置文件redis.conf,內容自己填,這里偷懶下載了個現成的。
redis目錄下包含redis.conf,直接用這個或者將她復制到/etc/redis/下。

使用-v <你的外部配置文件路徑:目標路徑>掛載外部配置文件,目標路徑也就是后面用到的/etc/redis/redis.conf

具體操作:

[root@localhost ~]#mkdir -p docker/redis/conf
[root@localhost ~]#wget http://download.redis.io/redis-stable/redis.conf
[root@localhost ~]#docker run -d --privileged=true -p 6379:6379 -v /docker/redis/redis.conf:/etc/redis/redis.conf -v /docker/redis/data:/data --name testredis redis redis-server /etc/redis/redis.conf --appendonly yes

注意:

redis.conf可以自己創建或者下載對應版本。

在redis.conf內:

1)注釋掉bind 127.0.0.1,行首添加#即為注釋,

2)daemonize默認no,否則無法編譯。

配置解釋:

配置 作用
--privileged=true 讓容器內的root有真正的root權限,否則容器內的root只有外部普通用戶權限
-p 6379:6379 把容器內的6379端口映射到宿主機6379端口
-v /root/redis/redis.conf:/etc/redis/redis.conf 把宿主機配置好的redis.conf放到容器內的這個位置中
-v /root/redis/data:/data 把redis持久化的數據在宿主機內顯示,做數據備份
redis-server /etc/redis/redis.conf 讓redis按redis.conf的配置啟動,而不是五配置啟動
–appendonly yes redis啟動后開啟持久化
  • 警告2:

WARNING: The TCP backlog setting of 511 cannot be enforced because /proc/sys/net/core/somaxconn is set to the lower value of 128.

意思:TCP backlog區大不能設置為511,因為/proc/sys/net/core/somaxconn要求設為小於128的值

# echo "net.core.somaxconn=551" > /etc/sysctl.conf
# sysctl net.core.somaxconn=551
  • 警告3:

WARNING overcommit_memory is set to 0! Background save may fail under low memory condition. To fix this issue add 'vm.overcommit_memory = 1' to /etc/sysctl.conf and then reboot or run the command 'sysctl vm.overcommit_memory=1' for this to take effect.

意思: overcommit_memory的值設置為0! 在低內存條件下,后台保存可能會失敗。 要解決此問題,請將“vm.overcommit_memory = 1”添加到/etc/sysctl.conf,然后重新啟動或運行命令“sysctl vm.overcommit_memory = 1”以使其生效。

# echo 1 > /proc/sys/vm/overcommit_memory
# echo "vm.overcommit_memory=1" >> /etc/sysctl.conf
# sysctl vm.overcommit_memory=1
  • 警告4:

WARNING you have Transparent Huge Pages (THP) support enabled in your kernel. This will create latency and memory usage issues with Redis. To fix this issue run the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled' as root, and add it to your /etc/rc.local in order to retain the setting after a reboot. Redis must be restarted after THP is disabled.

意思:您在內核中啟用了透明大頁面(THP)支持。 這將導致Redis的延遲和內存使用問題。 要解決此問題,請以root身份運行命令'echo never > /sys/kernel/mm/transparent_hugepage/enabled',並將其添加到/etc/rc.local中,以便在重新啟動后保留設置。 禁用THP后必須重新啟動Redis。

[root@centos224]# echo never > /sys/kernel/mm/transparent_hugepage/enabled   [root@centos224]# vi /etc/rc.local 
if test -f /sys/kernel/mm/transparent_hugepage/enabled; then
   echo never > /sys/kernel/mm/transparent_hugepage/enabled 
fi

參考文章

docker后續安裝(如有權限問題)
What is the function of NM_CONTROLLED

docker安裝redis 指定配置文件且設置了密碼

docker安裝redis

Docker下安裝Redis鏡像&&使用自己的redis.conf文件&&遠程連接Redis

【Docker】:使用docker安裝redis,掛載外部配置和數據

Redis學習筆記五——啟動警告問題的解決

Linux 關於Transparent Hugepages的介紹
Installing the CentOS Development Tools (gcc, flex, etc)
https://stackoverflow.com/questions/43401645/cannot-connect-to-the-docker-daemon-at-unix-var-run-docker-sock-is-the-docke

附錄

Redis(Remote Dictionary Server)各版本配置文件redis.conf文件下載

Redis版本 下載地址
2.4 https://raw.githubusercontent.com/antirez/redis/2.4/redis.conf
2.6 https://raw.githubusercontent.com/antirez/redis/2.6/redis.conf
2.8 https://raw.githubusercontent.com/antirez/redis/2.8/redis.conf
3.0 https://raw.githubusercontent.com/antirez/redis/3.0/redis.conf
3.2 https://raw.githubusercontent.com/antirez/redis/3.2/redis.conf
4.0 https://raw.githubusercontent.com/antirez/redis/4.0/redis.conf

stable版本: http://download.redis.io/redis-stable/redis.conf

有4項需要注意

daemonize no # 不用守護線程的方式啟動,若為yes則容器無法啟動
# bind 192.168.1.1 # 注釋掉這部分,這是限制redis只能本地訪問
appendonly yes # 開啟redis持久化

讓docker非127.0.0.1的配置

requirepass yourpassword # 可選1,redis連接密碼
protected-mode no # 可選2,關閉保護模式


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM