课前练习题-20250919
文章目录
- 9.19
- 训练
- 需求:
- 配置过程
- 课前练习
- 21. 软件包管理
- 22. 计划任务管理
- 23. 进程调度管理
- 步骤 1:分析系统资源使用情况
- 步骤 2:分析 Java 进程状态
- 步骤 3:常见问题及解决方案
9.19
训练
需求:
server通过web服务器提供yum仓库,client端就不需要使用公网的yum仓库。
- server 挂载光盘 /dev/sr0 到 /usr/share/nginx/html/dvd
- 部署nginx服务器,将/usr/share/nginx/html/dvd仓库通过http发布
- 配置client,删除本地所有仓库,并使用该仓库安装软件。
配置过程
-
挂载/dev/sr0
#查看光盘是否存在打开状态 [root@webapp ~ 14:02:14]# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 100G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 99G 0 part ├─centos-root 253:0 0 50G 0 lvm /├─centos-swap 253:1 0 2G 0 lvm [SWAP]└─centos-home 253:2 0 47G 0 lvm /home sdb 8:16 0 20G 0 disk sr0 11:0 1 4.4G 0 rom #我们需要使用nginx,查看是否有占用,有则关闭 [root@server ~ 15:07:03]# ss -lntp | grep :80 LISTEN 0 128 [::]:80 [::]:* users:(("httpd",pid=15992,fd=4),("httpd",pid=15908,fd=4),("httpd",pid=15907,fd=4),("httpd",pid=15906,fd=4),("httpd",pid=15905,fd=4)) [root@server ~ 15:07:17]# systemctl stop httpd [root@server ~ 15:07:39]# systemctl status httpd ● httpd.service - The Apache HTTP ServerLoaded: loaded (/etc/systemd/system/httpd.service; disabled; vendor preset: disabled)Active: inactive (dead)9月 19 10:12:18 webapp.dyx.cloud systemd[1]: Starting The Apache... 9月 19 10:12:18 webapp.dyx.cloud systemd[1]: Can't open PID file... 9月 19 10:12:18 webapp.dyx.cloud systemd[1]: Started The Apache ... 9月 19 15:07:39 server.dyx.cloud systemd[1]: Stopping The Apache... 9月 19 15:07:39 server.dyx.cloud systemd[1]: Stopped The Apache ... Hint: Some lines were ellipsized, use -l to show in full. [root@server ~ 15:07:50]# ss -lntp | grep :80#挂载 [root@server ~ 15:09:00]# mkdir /usr/share/nginx/html/dvd [root@server ~ 15:09:16]# mount /dev/sr0 /usr/share/nginx/html/dvd mount: /dev/sr0 写保护,将以只读方式挂载 [root@server ~ 15:09:19]# df 文件系统 1K-块 已用 可用 已用% 挂载点 devtmpfs 485744 0 485744 0% /dev tmpfs 497832 0 497832 0% /dev/shm tmpfs 497832 14104 483728 3% /run tmpfs 497832 0 497832 0% /sys/fs/cgroup /dev/mapper/centos-root 52403200 2914656 49488544 6% / /dev/mapper/centos-home 49250820 33068 49217752 1% /home /dev/sda1 1038336 142304 896032 14% /boot tmpfs 99568 0 99568 0% /run/user/0 /dev/sr0 4635056 4635056 0 100% /usr/share/nginx/html/dvd[root@server html 15:32:37]# ls dvd
-
部署nginx服务器
#下载nginx
[root@server ~ 15:10:19]# yum install -y nginx
已加载插件:fastestmirror, product-id, search-disabled-repos,: subscription-managerThis system is not registered with an entitlement server. You can use subscription-manager to register.Loading mirror speeds from cached hostfile* base: mirrors.aliyun.com* extras: mirrors.aliyun.com* updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 nginx.x86_64.1.1.20.1-10.el7 将被 安装
--> 解决依赖关系完成依赖关系解决====================================================================Package 架构 版本 源 大小
====================================================================
正在安装:nginx x86_64 1:1.20.1-10.el7 epel 588 k事务概要
====================================================================
安装 1 软件包总下载量:588 k
安装大小:1.7 M
Downloading packages:
nginx-1.20.1-10.el7.x86_64.rpm | 588 kB 00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction正在安装 : 1:nginx-1.20.1-10.el7.x86_64 1/1 验证中 : 1:nginx-1.20.1-10.el7.x86_64 1/1 已安装:nginx.x86_64 1:1.20.1-10.el7 完毕!
[root@server ~ 15:10:56]# systemctl enable nginx.service
Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /etc/systemd/system/nginx.service.[root@server ~ 15:11:08]# cd /usr/share/nginx/html/[root@server html 15:33:44]# curl http://localhost/#刷新网站
[root@server html 15:35:46]# vim /etc/nginx/nginx.conf
...
http {# 添加以下记录autoindex on;
...
[root@server html 15:37:28]# systemctl restart nginx.service
如果有冲突,需卸载
[root@server html 15:20:21]# cd /etc/systemd/system/[root@server system 15:20:52]# ls
basic.target.wants
dbus-org.freedesktop.nm-dispatcher.service
default.target
default.target.wants
getty.target.wants
graphical.target.wants
httpd.service
local-fs.target.wants
multi-user.target.wants
network-online.target.wants
nginx.service.d
remote-fs.target.wants
sersyncd.service
sockets.target.wants
sysinit.target.wants
system-update.target.wants
vmtoolsd.service.requires[root@server system 15:30:03]# rm -rf httpd.service
[root@server ~ 15:30:39]# systemctl daemon-reload
3.配置client
[root@client ~ 15:37:45]# cd /etc/yum.repos.d/
[root@client yum.repos.d 15:39:08]# ls
CentOS-Base.repo CentOS-Sources.repo
CentOS-CR.repo CentOS-Vault.repo
CentOS-Debuginfo.repo CentOS-x86_64-kernel.repo
CentOS-fasttrack.repo epel.repo
CentOS-Media.repo[root@client yum.repos.d 15:39:10]# mkdir old
[root@client yum.repos.d 15:39:35]# mv *.repo old
[root@client yum.repos.d 15:39:45]# ls
old
[root@client yum.repos.d 15:39:47]# vim dvd.repo
[base]
name=CentOS-$releasever - Base - server.dyx.cloud
baseurl=http://server.dyx.cloud/dvd
enabled=1
gpgcheck=0[root@client yum.repos.d 15:41:03]# ls
dvd.repo old
[root@client yum.repos.d 15:41:33]# cd
[root@client ~ 15:41:40]# yum install -y dhcp
#安装出现问题
[root@client ~ 15:41:40]# yum install -y dhcp
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
http://server.laoma.cloud/dvd/repodata/repomd.xml: [Errno 14] curl#6 - "Could not resolve host: server.laoma.cloud; 未知的错误"
正在尝试其它镜像。
正在解决依赖关系
--> 正在检查事务
---> 软件包 dhcp.x86_64.12.4.2.5-82.el7.centos 将被 安装
--> 正在处理依赖关系 dhcp-libs(x86-64) = 12:4.2.5-82.el7.centos,它被软件包 12:dhcp-4.2.5-82.el7.centos.x86_64 需要
--> 正在处理依赖关系 dhcp-common = 12:4.2.5-82.el7.centos,它被软件包 12:dhcp-4.2.5-82.el7.centos.x86_64 需要
--> 解决依赖关系完成
错误:软件包:12:dhcp-4.2.5-82.el7.centos.x86_64 (base)需要:dhcp-libs(x86-64) = 12:4.2.5-82.el7.centos已安装: 12:dhcp-libs-4.2.5-83.el7.centos.1.x86_64 (@anaconda)dhcp-libs(x86-64) = 12:4.2.5-83.el7.centos.1可用: 12:dhcp-libs-4.2.5-82.el7.centos.x86_64 (base)dhcp-libs(x86-64) = 12:4.2.5-82.el7.centos
... ...您可以尝试添加 --skip-broken 选项来解决该问题您可以尝试执行:rpm -Va --nofiles --nodigest
[root@client ~ 15:41:58]# yum remove dhcp-libs-4.2.5-83.el7.centos.1.x86_64[root@client ~ 15:42:30]# yum clean all
已加载插件:fastestmirror
正在清理软件源: base
Cleaning up list of fastest mirrors
Other repos take up 225 M of disk space (use --verbose for details)
删除目录下所有文件,除了指定文件,例如dvd
1. ls -1 | grep -v dvd | xargs rm -fr
2. rm -fr $(ls -1 | grep -v dvd)
3. find . -maxdepth 1 ! -name 'dvd' -delete
课前练习
21. 软件包管理
某金融科技公司新上线业务系统,需部署高可用 Web 服务。因生产环境安全合规要求:
- 禁止使用 yum 源安装软件,需通过源码编译方式安装 Apache 2.4.60 服务器
- 要求支持SSL 加密、URL 重写
- 配置为系统服务(systemd 管理)
软件包位置:httpd-2.4.60.tar.gz
[root@server ~]# yum install -y gcc make apr-devel apr-util-devel pcre-devel openssl-devel zlib-devel[root@server ~]# wget https://archive.apache.org/dist/httpd/httpd-2.4.60.tar.gz
[root@server ~]# tar -xf httpd-2.4.60.tar.gz
[root@server ~]# cd httpd-2.4.60/
[root@server httpd-2.4.60]## ./configure \
--prefix=/usr/local/apache2 \
--with-ssl \
--enable-ssl \
--enable-rewrite
[root@server httpd-2.4.60]# make && make install
[root@server ~]# cd ~
[root@server ~]# vim /usr/local/apache2/conf/httpd.conf
```常见配置修改:- 取消注释`ServerName www.example.com:80`
- 修改DocumentRoot和Directory指向网站根目录
- 启用虚拟主机:取消注释 `Include conf/extra/httpd-vhosts.conf`##### 配置 Apache 服务```bash
[root@server ~]# vim /etc/systemd/system/httpd.service
``````bash
[Unit]
Description=The Apache HTTP Server
After=network.target remote-fs.target nss-lookup.target[Service]
Type=forking
PIDFile=/usr/local/apache2/logs/httpd.pid
ExecStart=/usr/local/apache2/bin/apachectl start
ExecReload=/usr/local/apache2/bin/apachectl graceful
ExecStop=/usr/local/apache2/bin/apachectl stop
PrivateTmp=true[Install]
WantedBy=multi-user.target
``````bash
[root@server ~]# systemctl daemon-reload
[root@server ~]# systemctl start httpd
[root@server ~]# curl http://localhost
<html><body><h1>It works!</h1></body></html>
[root@webapp ~ 08:58:56]# yum install -y apr-devel apr-util-devel pcre-devel openssl-devel[root@webapp ~ 09:44:18]# wget https://archive.apache.org/dist/httpd/httpd-2.4.60.tar.gz[root@webapp ~ 22:22:14]# ls
10.sersync2.5.4_64bit_binary_stable_final.tar.gz
anaconda-ks.cfg
cmatrix-1.2a
cmatrix-1.2a.tar.gz
confxml.xml
etc.tar
etc.tar.gz
GNU-Linux-x86
httpd-2.4.60.tar.gz
httpd-2.4.60.tar.gz.1
httpd-2.4.60.tar.gz.2
lrzsz-0.12.20-36.el7.x86_64.rpm
lrzsz-0.12.20-36.el7.x86_64.rpm.1
lrzsz-0.12.20-36.el7.x86_64.rpm.2
myscript.sh
rsyncd.secrets
selinux
vsftpd-3.0.2-29.el7_9.x86_64.rpm
wget-log
[root@webapp ~ 22:22:19]# tar -xf httpd-2.4.60.tar.gz
[root@webapp ~ 22:22:46]# ls
10.sersync2.5.4_64bit_binary_stable_final.tar.gz
anaconda-ks.cfg
cmatrix-1.2a
cmatrix-1.2a.tar.gz
confxml.xml
etc.tar
etc.tar.gz
GNU-Linux-x86
httpd-2.4.60
httpd-2.4.60.tar.gz
httpd-2.4.60.tar.gz.1
httpd-2.4.60.tar.gz.2
lrzsz-0.12.20-36.el7.x86_64.rpm
lrzsz-0.12.20-36.el7.x86_64.rpm.1
lrzsz-0.12.20-36.el7.x86_64.rpm.2
myscript.sh
rsyncd.secrets
selinux
vsftpd-3.0.2-29.el7_9.x86_64.rpm
wget-log
[root@webapp ~ 22:22:48]# ls |http
bash: http: 未找到命令
[root@webapp ~ 22:22:58]# cd httpd-2.4.60/
[root@webapp httpd-2.4.60 22:23:06]# ./configure \
> --prefix=/usr/local/apache2 \
> --enable-ssl \
> --enable-rewrite \
> --enable-so \
> --with-apr=/usr \
> --with-apr-util=/usr \
> --with-pcre \
> --with-ssl
checking for chosen layout... Apache
checking for working mkdir -p... yes
checking for grep that handles long lines and -e... /usr/bin/grep
checking for egrep... /usr/bin/grep -E
checking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking target system type... x86_64-pc-linux-gnu
configure:
configure: Configuring Apache Portable Runtime library...
configure:
checking for APR... configure: error: the --with-apr parameter is incorrect. It must specify an install prefix, a build directory, or an apr-config file.
checking for APR... configure: error: the --with-apr parameter is incorrect. It must specify an install prefix, a build directory, or an apr-config file.
[root@webapp httpd-2.4.60 22:29:28]# yum provides 'arp*'
[root@webapp httpd-2.4.60 22:29:37]# yum install -y arprec-devel
yum install -y openssl-develmake && make installcat > /usr/lib/systemd/system/httpd.service << EOF
[Unit]
Description=The Apache HTTP Server
After=network.target[Service]
Type=forking
ExecStart=/usr/local/apache2/bin/apachectl start
ExecReload=/usr/local/apache2/bin/apachectl graceful
ExecStop=/usr/local/apache2/bin/apachectl stop
PrivateTmp=true[Install]
WantedBy=multi-user.target
EOFsystemctl daemon-reload# 启动服务
systemctl start httpd
Unregistered Authentication Agent for unix-process:25581:1754866 (system bus name :1.396, object path /org/freedesktop/PolicyKit1/AuthenticationAgent, locale zh_CN.UTF-8) (disconnected from bus)# 设置开机自启
systemctl enable httpd# 检查状态
systemctl status httpd
22. 计划任务管理
你是某公司的 Linux 运维工程师,负责维护生产环境 CentOS7 服务器。现需要对系统定义以下定时任务:
-
每小时更新一次mandb数据库
-
每分钟收集一次系统资源使用情况,收集的结果保存到/var/log/system_load.log。
收集的资源使用情况包括:
- CPU 使用率
- 内存 使用率
- 存储 使用率
- 系统中运行的进程,需包含进程号、父进程号、CPU、内存使用率属性,并根据CPU使用率降序排序
[root@webapp ~ 19:53:55]# cat <<EOF> /usr/local/bin/collect_system_load.sh[root@webapp ~ 19:56:48]# cat /usr/local/bin/collect_system_load.sh
#!/bin/bash# 记录当前时间
echo "=== 系统资源使用情况 - 2025-09-18 19:55:46 ===" >> /var/log/system_load.log# CPU使用率
echo "CPU 使用率:" >> /var/log/system_load.log
top -bn1 | grep "Cpu(s)" | awk '{print " 总使用率: " 100 - "%"}' >> /var/log/system_load.log
echo "" >> /var/log/system_load.log# 内存使用率
echo "内存 使用率:" >> /var/log/system_load.log
free -h | awk 'NR==2{printf " 总内存: %s, 已使用: %s, 使用率: %.2f%%\n", , , /*100}' >> /var/log/system_load.log
echo "" >> /var/log/system_load.log# 存储使用率
echo "存储 使用率:" >> /var/log/system_load.log
df -h | grep -v tmpfs | grep -v loop | awk '{if(NR>1) printf " %s: 总容量: %s, 已使用: %s, 使用率: %s\n", , , , }' >> /var/log/system_load.log
echo "" >> /var/log/system_load.log# 进程信息(按CPU使用率降序)
echo "进程信息(按CPU使用率降序):" >> /var/log/system_load.log
ps -eo pid,ppid,%cpu,%mem,cmd --sort=-%cpu | head -n 20 >> /var/log/system_load.log
echo "==========================================" >> /var/log/system_load.log
echo "" >> /var/log/system_load.log[root@webapp ~ 19:56:54]# chmod +x /usr/local/bin/collect_system_load.sh[root@webapp ~ 19:58:00]# ll /usr/local/bin/collect_system_load.sh
-rwxr-xr-x 1 root root 1134 9月 18 19:55 /usr/local/bin/collect_system_load.sh[root@webapp ~ 21:49:25]# crontab -e
no crontab for root - using an empty one
crontab: installing new crontab
[root@webapp ~ 21:50:02]# crontab -l
0 * * * * /usr/bin/mandb # 每小时更新mandb数据库
* * * * * /usr/local/bin/collect_system_load.sh # 每分钟收集系统资源[root@webapp ~ 21:50:45]# tail -f /var/log/system_load.log11 2 0.0 0.0 [watchdog/0]12 2 0.0 0.0 [watchdog/1]13 2 0.0 0.0 [migration/1]14 2 0.0 0.0 [ksoftirqd/1]16 2 0.0 0.0 [kworker/1:0H]18 2 0.0 0.0 [kdevtmpfs]19 2 0.0 0.0 [netns]20 2 0.0 0.0 [khungtaskd]
23. 进程调度管理
你是某电商公司的运维工程师,负责维护订单系统服务器。该服务器运行着 Java 开发的订单处理服务,近期频繁出现系统卡顿,严重影响订单处理效率。系统重启,问题依旧。
请尽快找到问题原因,并解决问题。
topcpu-renice -n 3 pid
步骤 1:分析系统资源使用情况
bash
# 实时监控系统资源(重点关注CPU、内存、I/O)
top# 查看内存使用详情
free -h# 查看磁盘I/O
iostat# 查看网络情况
netstat -tulnp
步骤 2:分析 Java 进程状态
bash
# 查找Java订单处理服务进程ID
ps -ef | grep java | grep -i order# 假设进程ID为12345,查看其资源占用
top -p 12345# 查看进程的线程状态
pstack 12345# 查看JVM内存使用情况
jstat -gc 12345 1000 10# 生成线程快照分析
jstack 12345 > /tmp/java_threads.log
步骤 3:常见问题及解决方案
-
CPU 使用率过高
bash
# 查找消耗CPU最高的线程 top -Hp 12345# 将线程ID转换为十六进制(用于jstack日志分析) printf "%x\n" 线程ID# 临时调整进程优先级 renice -n -5 12345 # 提高优先级
-
内存泄漏 / 占用过高
bash
# 生成堆内存快照 jmap -dump:format=b,file=/tmp/java_heapdump.hprof 12345# 调整JVM内存参数(修改启动脚本) # 示例:-Xms2g -Xmx4g -XX:+HeapDumpOnOutOfMemoryError
-
I/O 等待过高
bash
# 查找进程打开的文件 lsof -p 12345# 检查磁盘空间 df -h
-
永久解决方案
- 优化 Java 应用程序代码,修复内存泄漏
- 根据系统负载调整 JVM 参数
- 考虑水平扩展,增加服务器节点
- 配置服务自动重启机制(通过 systemd):
bash
# 在服务配置中添加自动重启 echo "Restart=always" >> /etc/systemd/system/order-service.service systemctl daemon-reload systemctl restart order-service
# 生成堆内存快照 jmap -dump:format=b,file=/tmp/java_heapdump.hprof 12345# 调整JVM内存参数(修改启动脚本) # 示例:-Xms2g -Xmx4g -XX:+HeapDumpOnOutOfMemoryError
-
I/O 等待过高
bash
# 查找进程打开的文件 lsof -p 12345# 检查磁盘空间 df -h
-
永久解决方案
- 优化 Java 应用程序代码,修复内存泄漏
- 根据系统负载调整 JVM 参数
- 考虑水平扩展,增加服务器节点
- 配置服务自动重启机制(通过 systemd):
bash
# 在服务配置中添加自动重启 echo "Restart=always" >> /etc/systemd/system/order-service.service systemctl daemon-reload systemctl restart order-service