网站容器化升级—各模块分别运行一个容器

结构图

containers

容器互通

同主机下同网段的容器互通

# 创建网桥
docker network create --driver bridge blog
# 后边创建容器时指定网络名即可
--net blog

MySql模块

容器和数据持久化

# 拉一个mysql镜像
docker pull mysql:5.6.51
# 后台运行容器,挂载数据卷(持久化存储数据),端口暴露,admin密码,网桥
# 端口暴露给外部时方便连接管理,不暴露也可
# 数据库的数据全部存在这里  /mydata/mysql_data/data
docker run -dti --name mysql -v /mydata/mysql_data/data:/www/server/data -v /mydata/mysql_data/my.cnf:/etc/mysql/my.cnf -p8881:3306 -e MYSQL_ROOT_PASSWORD=mima --net blog mysql:5.6.51

# 如果出现
^G/usr/local/mysql/bin/mysqld: File './mysql-bin.000007' not found (Errcode: 2)
# 删掉
mysql-bin.index
# 文件即可

my.cnf为自定义配置文件

[client]
#password	= your_password
port		= 3306
socket		= /tmp/mysql.sock

[mysqld]
port		= 3306
socket		= /tmp/mysql.sock
datadir = /www/server/data
default_storage_engine = InnoDB
performance_schema_max_table_instances = 400
table_definition_cache = 400
skip-external-locking
key_buffer_size = 32M
max_allowed_packet = 100G
table_open_cache = 128
sort_buffer_size = 768K
net_buffer_length = 4K
read_buffer_size = 768K
read_rnd_buffer_size = 256K
myisam_sort_buffer_size = 8M
thread_cache_size = 16
query_cache_size = 16M
tmp_table_size = 32M
sql-mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES

explicit_defaults_for_timestamp = true
#skip-name-resolve
max_connections = 500
max_connect_errors = 100
open_files_limit = 65535

log-bin=mysql-bin
binlog_format=mixed
server-id = 1
expire_logs_days = 10
slow_query_log=1
slow-query-log-file=/www/server/data/mysql-slow.log
long_query_time=3
#log_queries_not_using_indexes=on


innodb_data_home_dir = /www/server/data
innodb_data_file_path = ibdata1:10M:autoextend
innodb_log_group_home_dir = /www/server/data
innodb_buffer_pool_size = 128M
innodb_log_file_size = 64M
innodb_log_buffer_size = 16M
innodb_flush_log_at_trx_commit = 1
innodb_lock_wait_timeout = 50
innodb_max_dirty_pages_pct = 90
innodb_read_io_threads = 2
innodb_write_io_threads = 2

[mysqldump]
quick
max_allowed_packet = 500M

[mysql]
no-auto-rehash

[myisamchk]
key_buffer_size = 32M
sort_buffer_size = 768K
read_buffer = 2M
write_buffer = 2M

[mysqlhotcopy]
interactive-timeout

查看容器

[root@k8s-node02 mysql]# docker  ps
CONTAINER ID   IMAGE                    COMMAND                  CREATED        STATUS        PORTS                                                                                                                 NAMES
23a9ab87cafb   mysql:5.6.51             "docker-entrypoint.s…"   4 hours ago    Up 4 hours    0.0.0.0:8881->3306/tcp, :::8881->3306/tcp                                                                                 mysql5.6.51

连接mysql数据库

mysql -uroot -h your_ip -P 8881 -p

redis

docker pull redis:6.2.6
# 只是用来做缓存的,不需要其他复杂配置,也无需暴露端口给外部访问
docker run -itd --name redis --net blog redis:6.2.6

Centos环境

docker pull centos:centos7

# 制作镜像
docker run -dti --name=blog --net blog  centos7

docker exec -ti dssadas /bin/bash

# 安装python3和依赖
yum install python3 -y
yum install gcc -y
yum install python3-devel.x86_64 -y

# 安装django程序的相关依赖包
# 打包之前的  pip3 freeze > requirements.txt
pip3 install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple/ 

docker commit -p ac337e68a768 docker.io/dachongming/centos7:v1
docker push dachongming/centos7:v1

# 在v3版本中添加gevent模块
python3 -m pip install --upgrade pip
pip3 install gevent==21.1.2 
-i https://mirrors.aliyun.com/pypi/simple/ 


# 使用
docker pull dachongming/centos7:v3
docker run -dti --name=blog  --net blog -v /mydata/BlogLee:/mydata/BlogLee -p888:80 dachongming/centos7:v3
docker exec -ti dssadas /bin/bash
export LC_ALL=en_US.utf-8
export LANG=en_US.utf-8
cd /mydata/BlogLee/
uwsgi --ini uwsgi.ini
nohup celery -A BlogLee worker -P gevent -c 1000 > celery.log 2>&1 &

NGINX

server
   {
        listen 80;
        # server_name www.liboer.top liboer.top;
        server_name www.liboer.top liboer.top;
        rewrite ^(.*)$ https://$host$1;
        location / {
          # 跳转到8080
          include uwsgi_params;
          uwsgi_pass 127.0.0.1:888;
        }
        # 静态文件
        location /static {
          root /mydata/mysql_data/BlogLee;
        }
    }



#以下属性中,以ssl开头的属性表示与证书配置有关。
server 
    {
    listen 443 ssl;
    #配置HTTPS的默认访问端口为443。
    #如果未在此处配置HTTPS的默认访问端口,可能会造成Nginx无法启动。
    #如果您使用Nginx 1.15.0及以上版本,请使用listen 443 ssl代替listen 443和ssl on。
    server_name liboer.top; #需要将yourdomain.com替换成证书绑定的域名。
    root html;
    index index.html index.htm;
    ssl_certificate /www/server/nginx/conf/cert/5388063_liboer.top.pem;  #需要将cert-file-name.pem替换成已上传的证书文件的名称。
    ssl_certificate_key /www/server/nginx/conf/cert/5388063_liboer.top.key; #需要将cert-file-name.key替换成已上传的证书密钥文件的名称。
    ssl_session_timeout 5m;
    ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
    #表示使用的加密套件的类型。
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2; #表示使用的TLS协议的类型。
    ssl_prefer_server_ciphers on;
    location / {
	        include uwsgi_params;
	        uwsgi_pass 127.0.0.1:888; #与uwsgi中ip:端口相同
	        uwsgi_send_timeout 600;
	        add_header Content-Security-Policy upgrade-insecure-requests;
	    }
	 # 静态文件
    location /static {
          root /mydata/mysql_data/BlogLee;
      }

}
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐