笺注:Docker的安装可参考 CentOS8_在Docker中使用Nginx的反向代理
查看Docker的版本信息:
[root@centos8 ~]# docker -v
Docker version 20.10.21, build baeda1f
[root@centos8 ~]#
从公网下载镜像:( 以下是下载CentOS6.9的镜像 )
[root@centos8 ~]# docker pull centos:6.9
6.9: Pulling from library/centos
831490506c47: Pull complete
Digest: sha256:6fff0a9edc920968351eb357c5b84016000fec6956e6d745f695e5a34f18ecd2
Status: Downloaded newer image for centos:6.9
docker.io/library/centos:6.9
[root@centos8 ~]#
查看宿主机的所有镜像:
[root@centos8 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 6.9 2199b8eb8390 4 years ago 195MB
[root@centos8 ~]#
使用镜像“centos:6.9”创建并启动容器“Rhel6_1”:
[root@centos8 ~]# docker run -itd --name Rhel6_1 -p 80:80 -v /opt/share:/home/dir_1 centos:6.9
3fde472646986b281e1d4dd8d1b6cf4d2862395e04ce30a4db4f259455b658bd
[root@centos8 ~]#
注释:
-itd : 以交互模式情况下后台运行。
--name : 指定容器名称。
-p 端口映射 : 第一个80是宿主机的端口,暴露给外部直接访问;第二个80是容器的端口。
-v 挂载目录 : 这里是把宿主机的目录/opt/share挂载到容器的目录/home/dir_1;假如目录不存在,就会自动创建。
centos:6.9 : 镜像名称:版本号
在宿主机查看正在运行的容器:
[root@centos8 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3fde47264698 centos:6.9 "/bin/bash" 20 seconds ago Up 18 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp Rhel6_1
[root@centos8 ~]#
注释:宿主机的TCP 80端口,映射到容器“Rhel6_1”的TCP 80端口。
###
进入容器“Rhel6_1”:
[root@centos8 ~]# docker exec -it Rhel6_1 /bin/bash
[root@3fde47264698 /]#
[root@3fde47264698 /]# pwd
/
[root@3fde47264698 /]# ls
bin dev etc home lib lib64 lost+found media mnt opt proc root sbin selinux srv sys tmp usr var
[root@3fde47264698 /]#
查看容器“Rhel6_1”的系统版本信息:
[root@3fde47264698 /]# cat /etc/redhat-release
CentOS release 6.9 (Final)
[root@3fde47264698 /]# uname -r
4.18.0-193.el8.x86_64
[root@3fde47264698 /]#
查看当前语言环境:(英文)
[root@3fde47264698 ~]# echo $LANG
en_US.UTF-8
[root@3fde47264698 ~]#
[root@3fde47264698 ~]# cat /etc/sysconfig/i18n
LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"
[root@3fde47264698 ~]#
SELinux默认是不可用:
[root@3fde47264698 ~]# getenforce
Disabled
[root@3fde47264698 ~]#
[root@3fde47264698 ~]# cat /etc/selinux/config
cat: /etc/selinux/config: No such file or directory
[root@3fde47264698 ~]#
[root@3fde47264698 /]# wget
bash: wget: command not found
[root@3fde47264698 /]#
要使用CentOS6的新公网Yum源,可参考:CentOS6使用新Yum源+EPEL
确认可以使用公网Yum源安装rpm软件包:
[root@3fde47264698 ~]# yum -y install wget
###
在容器“Rhel6_1”中安装Nagios:
先安装LAMP环境和相关软件依赖包:
[root@3fde47264698 ~]# yum -y install httpd php php-mysql mysql mysql-server net-snmp-utils rrdtool-* gcc-c++ patch unzip openssl-devel
修改Apache的配置文件:
[root@3fde47264698 ~]# sed -i 's/#ServerName www.example.com:80/ServerName www.example.com:80/g' /etc/httpd/conf/httpd.conf
[root@3fde47264698 ~]#
启动Apache:
[root@3fde47264698 ~]# service httpd start
启动MySQL:
[root@3fde47264698 ~]# service mysqld start
设置MySQL的数据库管理员root@localhostroot的密码为 123456
[root@3fde47264698 ~]# mysqladmin -u root password 123456
[root@3fde47264698 ~]#
使用数据库管理员root@localhostroot本地登录MySQL:
[root@3fde47264698 ~]# mysql -u"root" -h"localhost" -p"123456"
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.73 Source distribution
Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
查看数据库里有哪些库:(以下3个库是默认就有的)
mysql> Show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
+--------------------+
3 rows in set (0.00 sec)
mysql>
切换到库mysql:
mysql> Use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql>
在MySQL下查看当前使用的是哪个库:
mysql> Select database();
+------------+
| database() |
+------------+
| mysql |
+------------+
1 row in set (0.00 sec)
mysql>
查看所有数据库用户及其主机信息:(显示当前库mysql中的表user里的字段“user”、“host”的所有数据)
mysql> Select user,host from user;
+------+--------------+
| user | host |
+------+--------------+
| root | 127.0.0.1 |
| | 3fde47264698 |
| root | 3fde47264698 |
| | localhost |
| root | localhost |
+------+--------------+
5 rows in set (0.00 sec)
mysql>
###
创建Nagios的用户、组和相关目录:
adduser -s /sbin/nologin nagios
mkdir /usr/local/nagios
chown nagios.nagios /usr/local/nagios/
安装Nagios:
tar -zxvf nagios-4.1.1.tar.gz
cd nagios-4.1.1
./configure --prefix=/usr/local/nagios/ --with-gd-lib=/usr/lib --with-gd-inc=/usr/include/
make all
make install
make install-init
make install-config
make install-commandmode
make install-config
安装nagios-plugins:
tar -zxvf nagios-plugins-2.1.1.tar.gz
cd nagios-plugins-2.1.1
./configure --prefix=/usr/local/nagios/
make
make install
启动Nagios:
[root@3fde47264698 ~]# service nagios start
Starting nagios: done.
[root@3fde47264698 ~]#
安装NRPE:
tar -zxvf nrpe-3.2.1.tar.gz
cd nrpe-3.2.1
./configure --prefix=/usr/local/nagios/
make all
make install
给NRPE创建配置文件:
[root@3fde47264698 ~]# cp /root/nrpe-3.2.1/sample-config/nrpe.cfg /usr/local/nagios/etc/nrpe.cfg
[root@3fde47264698 ~]#
启动NRPE:
[root@3fde47264698 ~]# /usr/local/nagios/bin/nrpe -c /usr/local/nagios/etc/nrpe.cfg -d
[root@3fde47264698 ~]#
Web页面登录Nagios时的用户名和密码:(文件、用户名、密码都是自定义的)
[root@3fde47264698 ~]# find / -name "htpasswd"
/usr/bin/htpasswd
[root@3fde47264698 ~]#
[root@3fde47264698 ~]# /usr/bin/htpasswd -bc /usr/local/nagios/etc/my_passwd zhuohua 888
Adding password for user zhuohua
[root@3fde47264698 ~]#
修改Apache的配置文件:
[root@3fde47264698 ~]# vi /etc/httpd/conf/httpd.conf
在文件的最后追加Nagios的代码:
ScriptAlias /nagios/cgi-bin /usr/local/nagios/sbin
<Directory "/usr/local/nagios/sbin">
Options ExecCGI
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/my_passwd
Require valid-user
</Directory>
Alias /nagios /usr/local/nagios/share
<Directory "/usr/local/nagios/share">
Options None
AllowOverride None
Order allow,deny
Allow from all
AuthName "Nagios Access"
AuthType Basic
AuthUserFile /usr/local/nagios/etc/my_passwd
Require valid-user
</Directory>
重启Apache:
[root@3fde47264698 ~]# service httpd restart
Stopping httpd: [ OK ]
Starting httpd: [ OK ]
[root@3fde47264698 ~]#
###
Windows客户端网页登录Nagios:
http://192.168.168.154/nagios/
Nagios的主界面:
容器“Rhel6_1”里的Nagios可以监控其他远程主机:
在容器“Rhel6_1”里使用Nagios的命令检测其他远程主机:
[root@3fde47264698 ~]# /usr/local/nagios/libexec/check_nrpe -H 192.168.168.130
NRPE v3.2.1
[root@3fde47264698 ~]#
[root@3fde47264698 ~]# /usr/local/nagios/libexec/check_icmp 192.168.168.130
OK - 192.168.168.130: rta 0.182ms, lost 0%|rta=0.182ms;200.000;500.000;0; pl=0%;40;80;; rtmax=0.392ms;;;; rtmin=0.101ms;;;;
[root@3fde47264698 ~]#
[root@3fde47264698 ~]# /usr/local/nagios/libexec/check_tcp 192.168.168.130 -p 22
TCP OK - 0.002 second response time on port 22|time=0.001757s;;;0.000000;10.000000
[root@3fde47264698 ~]#
[root@3fde47264698 ~]# /usr/local/nagios/libexec/check_tcp 192.168.168.130 -p 80
TCP OK - 0.001 second response time on port 80|time=0.000553s;;;0.000000;10.000000
[root@3fde47264698 ~]#
笺注:
Docker宿主机的防火墙其实无需特意打开任何TCP端口:
[root@centos8 ~]# firewall-cmd --zone=public --list-ports
[root@centos8 ~]#
相关文章:
Oracle Linux6.9安装Nagios
使用SQLyog远程管理MySQL
最小化安装CentOS6
CentOS8_在Docker中安装Tomcat+MariaDB
CentOS8_在Docker中安装Zabbix4.4.5 |