内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。
本文档
风哥提示:
详细介绍企业服务的性能优化方法和最佳实践。
Part01-Web服务性能优化
1.1 Nginx性能优化
$ sudo tee /etc/nginx/nginx.conf << EOF user nginx; worker_processes auto; worker_cpu_affinity auto; worker_rlimit_nofile 65535; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 65535; use epoll; multi_accept on; } http { include /etc/nginx/mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main buffer=32k flush=5s; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; keepalive_requests 100; reset_timedout_connection on; client_body_buffer_size 128k; client_max_body_size 50m; client_body_timeout 10; send_timeout 10; gzip on; gzip_min_length 1k; gzip_buffers 4 16k; gzip_comp_level 6; gzip_types text/plain text/css text/javascript application/json application/javascript application/xml; gzip_vary on; gzip_proxied any; open_file_cache max=65535 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; fastcgi_c更多学习教程公众号风哥教程itpux_comonnect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300; fastcgi_buffer_size 64k; fastcgi_buffers 4 64k; fastcgi_busy_buffers_size 128k; fastcgi_temp_file_write_size 128k; upstream backend { server 192.168.1.20:80 weight=3 max_fails=3 fail_timeout=30s; server 192.168.1.21:80 weight=2 max_fails=3 fail_timeout=30s; server 192.168.1.22:80 weight=1 max_fails=3 fail_timeout=30s; keepalive 32; } server { listen 80; server_name www.fgedu.net.cn; location / { proxy_pass http://backend; proxy_http_version 1.1; proxy_set_header Connection ""; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_connect_timeout 60s; proxy_send_timeout 60s; proxy_read_timeout 60s; proxy_buffering on; proxy_buffer_size 4k; proxy_buffers 8 4k; } } } EOF # 优化系统参数 $ sudo tee -a /etc/sysctl.conf << EOF # 网络优化 net.core.somaxconn = 65535 net.core.netdev_max_backlog = 65535 net.ipv4.tcp_max_syn_backlog = 65535 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 1200 net.ipv4.tcp_keepalive_probes =from PG视频:www.itpux.com 5 net.ipv4.tcp_keepalive_intvl = 30 net.ipv4.tcp_max_tw_buckets = 5000 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_max_orphans = 32768 net.ipv4.tcp_synack_retries = 2 net.ipv4.tcp_syn_retries = 2 # 文件描述符 fs.file-max = 65535 EOF $ sudo sysctl -p # 优化文件描述符限制 $ sudo tee -a /etc/security/limits.学习交流加群风哥微信: itpux-comconf << EOF * soft nofile 65535 * hard nofile 65535 * soft nproc 65535 * hard nproc 65535 EOF # 重启Nginx $ sudo systemctl restart nginx
Part02-数据库性能优化
2.1 MySQL性能优化
$ sudo tee /etc/my.cnf << EOF [mysqld] datadir=/var/lib/mysql socket=/var/lib/mysql/mysql.sock log-error=/var/log/mysqld.log pid-file=/var/run/mysqld/mysqld.pid # 字符集 character-set-server=utf8mb4 collation-server=utf8mb4_unicode_ci # 连接优化 max_connections=1000 max_connect_errors=1000 wait_timeout=600 interactive_timeout=600 thread_cache_size=100 back_log=512 # InnoDB优化 innodb_buffer_pool_size=4G innodb_buffer_pool_instances=4 innodb_log_file_size=1G innodb_log_buffer_size=64M innodb_flush_log_at_trx_commit=2 innodb_flush_method=O_DIRECT innodb_file_per_table=1 innodb_io_capacity=2000 innodb_io_capacity_max=4000 innodb_read_io_threads=8 innodb_write_io_threads=8 innodb_thread_concurrency=0 innodb_lock_wait_timeout=50 # 查询优化 query_cache_type=0 query_cache_size=0 join_buffer_size=8M sort_buffer_size=8M read_buffer_size=8M read_rnd_buffer_size=16M table_open_cache=4000 table_definition_cache=2000 open_files_limit=65535 # 临时表优化 tmp_table_size=256M max_heap_table_size=256M # 慢查询日志 slow_query_log=1 slow_query_log_file=/var/log/mysql-slow.log long_query_time=2 log_queries_not_using_indexes=1 # 二进制日志 log-bin=mysql-bin server-id=1 binlog_format=ROW expire_logs_days=7 max_binlog_size=1G binlog_cache_size=2M [client] default-character-set=utf8mb4 socket=/var/lib/mysql/mysql.sock [mysql] default-character-set=utf8mb4 EOF # 重启MySQL $ sudo systemctl restart mysqld # 分析慢查询 $ mysqldumpslow -s t -t 10 /var/log/mysql-slow.log Reading mysql slow query log from /var/log/mysql-slow.log Count: 100 Time=5.00s (500s) Lock=0.00s (0s) Rows=1000.0 (100000), root[root]@localhost SELECT * FROM users WHERE name LIKE 'S' Count: 50 Time=3.00s (150s) Lock=0.00s (0s) Rows=500.0 (25000), root[root]@localhost SELECT * FROM orders WHERE status = 'S' # 优化查询 $ mysql -u root -p << EOF EXPLAIN SELECT * FROM users WHERE name LIKE 'John%'; +----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+ | 1 | SIMPLE | users | NULL | ALL | NULL | NULL | NULL | NULL | 100000 | 11.11 | Using where | +----+-------------+-------+------------+------+---------------+------+---------+------+--------+----------+-------------+ 1 row in set, 1 warning (0.00 sec) CREATE INDEX idx_name ON users(name); Query OK, 0 rows affected (1.23 sec) Records: 0 Duplicates: 0 Warnings: 0 EXPLAIN SELECT * FROM users WHERE name LIKE 'John%'; +----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+ | 1 | SIMPLE | users | NULL | range | idx_name | idx_name | 123 | NULL | 100 | 100.00 | Using index condition | +----+-------------+-------+------------+-------+---------------+----------+---------+------+------+----------+-----------------------+ 1 row in set, 1 warning (0.00 sec) EOF
Part03-系统性能优化
3.1 内核参数优化
$ sudo tee /etc/sysctl.conf << EOF # 网络优化 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.core.rmem_default = 262144 net.core.wmem_default = 262144 net.ipv4.tcp_rmem = 4096 87380 16777216 net.ipv4.tcp_wmem = 4096 65536 16777216 net.ipv4.tcp_mem = 786432 1048576 1572864 net.core.somaxconn = 65535 net.core.netdev_max_backlog = 65535 net.ipv4.tcp_max_syn_backlog = 65535 net.ipv4.tcp_fin_timeout = 30 net.ipv4.tcp_keepalive_time = 1200 net.ipv4.tcp_keepalive_probes = 5 net.ipv4.tcp_keepalive_intvl = 30 net.ipv4.tcp_max_tw_buckets = 5000 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_syncookies = 1 net.ipv4.tcp_max_orphans = 32768 net.ipv4.tcp_synack_retries = 2 net.ipv4.tcp_syn_retries = 2 net.ipv4.ip_local_port_range = 1024 65535 # 内存优化 vm.swappiness = 10 vm.dirty_ratio = 15 vm.dirty_background_ratio = 5 vm.overcommit_memory = 1 vm.max_map_count = 262144 # 文件系统 fs.file-max = 65535 fs.inotify.max_user_instances = 8192 fs.inotify.max_user_watches = 524288 # 内核调度 kernel.sched_min_granularity_ns = 10000000 kernel.sched_wakeup_granularity_ns = 15000000 kernel.sched_migration_cost_ns = 5000000 kernel.sched_autogroup_enabled = 0 EOF $ sudo sysctl -p # 优化磁盘I/O调度 $ cat /sys/block/sda/queue/scheduler [mq-deadline] none $ sudo tee /sys/block/sda/queue/scheduler << EOF deadline EOF # 永久配置 $ sudo tee /etc/udev/rules.d/60-scheduler.rules << EOF ACTION=="add|change", KERNEL=="sda", ATTR{queue/scheduler}="deadline" EOF # 优化文件系统挂载选项 $ sudo tee -a /etc/fstab << EOF /dev/mapper/rhel-root / xfs defaults,noatime,nodiratime 0 0 EOF # 优化内存大页 $ sudo tee /etc/default/grub << EOF GRUB_CMDLINE_LINUX="... transparent_hugepage=never" EOF $ sudo grub2-mkconfig -o /boot/grub2/grub.cfg
Part04-缓存优化
4.1 Redis缓存配置
$ sudo dnf install -y redis
# 优化Redis配置
$ sudo tee /etc/redis/redis.conf << EOF
bind 0.0.0.0
port 6379
daemonize yes
pidfile /var/run/redis/redis-server.pid
logfile /var/log/redis/redis-server.log
dir /var/lib/redis
# 内存配置
maxmemory 2gb
maxmemory-policy allkeys-lru
# 持久化配置
save 900 1
save 300 10
save 60 10000
appendonly yes
appendfilename "appendonly.aof"
appendfsync everysec
# 性能优化
tcp-backlog 511
tcp-keepalive 300
timeout 0
databases 16
# 慢查询日志
slowlog-log-slower-than 10000
slowlog-max-len 128
# 客户端配置
maxclients 10000
EOF
# 启动Redis
$ sudo systemctl start redis
$ sudo systemctl enable redis
# 测试Redis性能
$ redis-benchmark -t set,get -n 100000 -c 50
====== SET ======
100000 requests completed in 1.23 seconds
50 parallel clients
3 bytes payload
keep alive: 1
host configuration "save": 900 1 300 10 60 10000
host configuration "appendonly": yes
multi-thread: no
Latency by percentile distribution:
0.000% <= 0.031 milliseconds (cumulative count 1)
50.000% <= 0.127 milliseconds (cumulative count 50000)
75.000% <= 0.159 milliseconds (cumulative count 75000)
87.500% <= 0.191 milliseconds (cumulative count 87500)
93.更多视频教程www.fgedu.net.cn750% <= 0.223 milliseconds (cumulative count 93750)
96.875% <= 0.255 milliseconds (cumulative count 96875)
98.438% <= 0.287 milliseconds (cumulative count 98438)
99.219% <= 0.319 milliseconds (cumulative count 99219)
99.609% <= 0.351 milliseconds (cumulative count 99609)
99.805% <= 0.383 milliseconds (cumulative count 99805)
99.902% <= 0.415 milliseconds (cumulative count 99902)
99.951% <= 0.447 milliseconds (cumulative count 99951)
99.976% <= 0.479 milliseconds (cumulative count 99976)
99.988% <= 0.511 milliseconds (cumulative count 99988)
99.994% <= 0.543 milliseconds (cumulative count 99994)
99.997% <= 0.575 milliseconds (cumulative count 99997)
99.998% <= 0.607 milliseconds (cumulative count 99998)
99.999% <= 0.639 milliseconds (cumulative count 99999)
100.000% <= 0.671 milliseconds (cumulative count 100000)
Cumulative distribution of latencies:
0.000% <= 0.103 milliseconds (cumulative count 0)
0.001% <= 0.111 milliseconds (cumulative count 1)
50.000% <= 0.127 milliseconds (cumulative count 50000)
100.000% <= 0.671 milliseconds (cumulative count 100000)
Summary:
throughput summary: 81300.81 requests per second
latency summary (msec):
avg min p50 p95 p99 max
0.128 0.032 0.127 0.231 0.295 0.671
====== GET ======
100000 requests completed in 1.12 seconds
50 parallel clients
3 bytes payload
keep alive: 1
host configuration "save": 900 1 300 10 60 10000
host configuration "appendonly": yes
multi-thread: no
Latency by percentile distribution:
0.000% <= 0.031 milliseconds (cumulative count 1)
50.000% <= 0.095 milliseconds (cumulative count 50000)
75.000% <= 0.119 milliseconds (cumulative count 75000)
87.500% <= 0.143 milliseconds (cumulative count 87500)
93.750% <= 0.167 milliseconds (cumulative count 93750)
96.875% <= 0.191 milliseconds (cumulative count 96875)
98.438% <= 0.215 milliseconds (cumulative count 98438)
99.219% <= 0.239 milliseconds (cumulative count 99219)
99.609% <= 0.263 milliseconds (cumulative count 99609)
99.805% <= 0.287 milliseconds (cumulative count 99805)
99.902% <= 0.311 milliseconds (cumulative count 99902)
99.951% <= 0.335 milliseconds (cumulative count 99951)
99.976% <= 0.359 milliseconds (cumulative count 99976)
99.988% <= 0.383 milliseconds (cumulative count 99988)
99.994% <= 0.407 milliseconds (cumulative count 99994)
99.997% <= 0.431 milliseconds (cumulative count 99997)
99.998% <= 0.455 milliseconds (cumulative count 99998)
99.999% <= 0.479 milliseconds (cumulative count 99999)
100.000% <= 0.503 milliseconds (cumulative count 100000)
Cumulative distribution of latencies:
0.000% <= 0.103 milliseconds (cumulative count 0)
0.001% <= 0.111 milliseconds (cumulative count 1)
50.000% <= 0.095 milliseconds (cumulative count 50000)
100.000% <= 0.503 milliseconds (cumulative count 100000)
Summary:
throughput summary: 89285.71 requests per second
latency summary (msec):
avg min p50 p95 p99 max
0.096 0.032 0.095 0.183 0.231 0.503
1. 根据硬件配置调整参数
2. 监控性能指标变化
3. 逐步调整避免过度优化
4. 记录优化前后对比
5. 定期重新评估优化效果
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
