docker-compose 是一個可以快速進行多容器編排的神器
安裝:
pip install docker-compose
docker-compose -h 查看子命令:
常用的命令有:
docker-compose build # 構建鏡像
docker-compose up # 編排啓動容器 -d 以守護模式啓動
docker-compose start # 啓動容器
docker-compose stop # 停止容器
docker-compose ps # 查看容器
docker-compose rm # 刪除容器
docker-compose構建lnmp架構示例:
[root@CentOS7 docker]# tree ./
./
└── lnmp
├── docker-compose.yml
├── mysql
│ ├── conf
│ │ └── my.cnf
│ ├── data
│
├── nginx
│ ├── conf.d
│ │ └── default.conf
│ |
│ └── nginx.conf
└── web
└── index.php
編輯docker-compose文件:
vim docker-compose.yml
version: "2"
services:
php:
image: php:7.2.3-fpm
networks:
- lnmp
volumes:
- ./web:/web
environment:
- TZ=Asia/Shanghai
nginx:
image: nginx:1.13
networks:
- lnmp
ports:
- 80:80
volumes:
- ./web:/web
- ./nginx/nginx.conf:/etc/nginx/nginx.conf
- ./nginx/conf.d/default..conf:/etc/nginx/conf.d/default.conf
environment:
- TZ=Asia/Shanghai
links:
- php
mysql:
hostname: mysql
image: mysql:5.6
environment:
- TZ=Asia/Shanghai
ports:
- 3306:3306
networks:
- lnmp
volumes:
- ./mysql/conf:/etc/mysql/conf.d
- ./mysql/data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: 123456
networks:
lnmp:
執行docker-compose up -d 啓動lnmp(注意需要在文件所在的路徑下執行)
my.cnf
######### This is a conf of mysql
################ client配置 #####################
#[client]
#port = 3306
#socket = /usr/local/mysql/logs/mysql.sock
###############mysql服務端配置##################
[mysqld]
user = mysql
port = 3306
################### 存放路徑 #####################
#socket = /usr/local/mysql/logs/mysql.sock
#basedir = /usr/local/mysql
#datadir = /usr/local/mysql/data
#pid-file = /usr/local/mysql/logs/mysql.pid
skip-external-locking
skip-name-resolve
#default-character-set = utf8 #5.5版本
character-set-server = utf8 #5.6版本
lower_case_table_names = 1
max_connections = 10000
open_files_limit = 65535
wait_timeout = 600
interactive_timeout = 600
################# mysql日誌目錄基本配置 ###########
#log-error = /usr/local/mysql/logs/error-log/error.log
#####慢查詢設置
slow_query_log = ON
slow_query_log_file = slow.log
long_query_time = 2
log_queries_not_using_indexes
#####二進制日誌設置
binlog_format = "ROW"
log-bin = mysql-bin
log_bin_index = binlog.index
relay-log = relay-bin
relay_log_index = mysql_relay_log.index
expire_logs_days = 30
max_binlog_size = 60M
############# mysql主主複製配置項 ####################
server-id = 1
auto_increment_offset = 1
auto_increment_increment= 2
#log-slave-updates
#slave-skip-errors=all
slave-skip-errors = 1032,1062
sync_binlog = 0 # 默認爲0,不刷新,由系統決定 # 爲1則每一次binlog寫入與硬盤同步
#####主從忽略表庫配置
replicate-ignore-db = mysql
#replicate-ignore-table=db.table
############# mysql性能配置 ######################
sql_mode = NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
key_buffer_size = 200M
tmp-table-size = 32M
table_open_cache = 128
table_definition_cache = 200
query_cache_size = 32M
query_cache_limit = 1M
query_cache_min_res_unit= 2k
max_allowed_packet = 20M
#####與線程有關的配置
thread_concurrency = 8
thread_cache_size = 64
sort_buffer_size = 2M
read_buffer_size = 2M
read_rnd_buffer_size = 2M
join_buffer_size = 2M
#####performance_schema 配置
performance_schema = off
performance_schema_max_table_instances=100
################ mysql引擎優化 ###########################
myisam-recover-options = FORCE,BACKUP
myisam_sort_buffer_size = 64M
myisam_max_sort_file_size=10G
myisam_repair_threads = 1
myisam_recover
#innodb引擎優化
innodb_read_io_threads = 8 ####用來同步IO操作的IO線程的數量.
innodb_thread_concurrency = 8 ####使用InnoDB引擎,內核被允許的線程數,這個最佳值取決於應用程序,硬件還有操作系統的調度程序。太高的值肯定會導致線程抖動。
innodb_flush_log_at_trx_commit = 1 ####如果設置爲1 ,InnoDB會在每次提交後刷新(fsync)事務日誌到磁盤上,
innodb_log_buffer_size = 2M ####用來緩衝日誌數據的緩衝區的大小.
innodb_log_file_size = 100M ####在日誌組中每個日誌文件的大小,
innodb_log_files_in_group = 3 ####在日誌組中文件的總量,通常2-3就足夠了
innodb_max_dirty_pages_pct = 90 ####在InnoDB緩衝池中最大允許的髒頁面的比例.
innodb_lock_wait_timeout = 120 ####在被回滾前,一個InnoDB的事務應該等待一個鎖被批准多久.
################ mysql備份命令配置 #######################
[mysqldump]
quick
############## mysql熱備份命令配置 #######################
[mysqlhotcopy]
interactive-timeout
[mysql]
prompt = \\u@mysql \\r:\\m:\\s-> #修改登陸默認提示符
nginx.conf
#user nginx;
worker_processes auto; # cpu密集型寫cpu個數,其他寫cpu的2倍,偷懶寫auto
#worker_cpu_affinity 0001 0010 0100 1000; # 線程綁定cpu
worker_rlimit_nofile 65536;
error_log /var/log/nginx/error.log info;
#pid logs/nginx.pid;
events
{
use epoll; # 複用客戶端線程的輪詢方法
accept_mutex off;
worker_connections 65536;
}
http
{
include mime.types;
default_type text/html;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'"$upstream_cache_status"';
access_log /var/log/nginx/access.log main;
sendfile on;
server_tokens off; # 隱藏Nginx版本號
keepalive_timeout 60;
client_header_buffer_size 4k;
client_body_buffer_size 320k;
client_max_body_size 8m;
keepalive_requests 8192;
large_client_header_buffers 4 32k;
server_names_hash_bucket_size 128;
open_file_cache max=65536 inactive=60s;
open_file_cache_valid 80s;
open_file_cache_min_uses 1;
proxy_connect_timeout 50;
proxy_read_timeout 50;
proxy_send_timeout 50;
proxy_buffer_size 320k;
proxy_buffers 4 640k;
proxy_busy_buffers_size 1280k;
proxy_temp_file_write_size 1024m;
proxy_ignore_client_abort on;
proxy_temp_path /tmp/temp;
proxy_cache cache_one;
proxy_cache_valid 200 302 1m;
proxy_cache_valid 301 1m;
proxy_cache_path /tmp/cache levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
add_header X-Cache-Status $upstream_cache_status;
proxy_http_version 1.1;
proxy_set_header Connection "";
fastcgi_temp_path /tmp/fastcgi_temp; # php緩存配置
fastcgi_cache_path /tmp/fastcgi_cache levels=1:2 keys_zone=cache_fastcgi:128m inactive=30m max_size=1g;
fastcgi_cache_key $request_method://$host$request_uri;
fastcgi_cache_valid 200 302 1h;
fastcgi_cache_valid 301 1d;
fastcgi_cache_valid any 1m;
fastcgi_cache_min_uses 1;
fastcgi_cache_use_stale error timeout http_500 http_503 invalid_header;
gzip on;
gzip_min_length 1k; # 指定壓縮文件的最小尺寸
gzip_buffers 4 64k;
gzip_http_version 1.1;
gzip_comp_level 2; # 壓縮等級爲2
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; # 允許壓縮的文件類型
charset UTF-8;
include /etc/nginx/conf.d/*.conf;
#include /usr/local/nginx/conf/httpsconf/*.conf;
}
default.conf
server {
listen 80;
server_name localhost;
location / {
root /web;
index index.html index.htm;
}
location ~ \.php$ {
root /web;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /web$fastcgi_script_name;
include fastcgi_params;
}
location ~ .+\.(gif|jpg|jpeg|png|bmp|swf|txt|csv|doc|docx|xls|xlsx|ppt|pptx|flv)$
{
expires 30d; # 靜態文件緩存時間
}
location ~ .+\.(js|css|html|xml)$
{
expires 30d;
}
}