从0开始梳理django项目_2.多容器部署(day2)

公司用的flask+gunicorn+nginx,竟然放在一个容器里了。规模小,也是省事。不过我没看出哪里省事,需要进容器里分别启动所有服务。我用django+uwsgi,对比着来。现在的问题是多容器用compose肯定更好,但时间有限,加之对network和volume的配置上有抵触,今天先用dockerfile把多容器做起来,事情也不少,主从同步...当然,在一个物理机上配置主从有点那个....


 

规划准备

目录结构

直接把自己的django项目放进来了,看看大致结构就好。有一处错误,把主库的路径错挂在从库了。不影响大局,从库在别的服务器,主库名文件夹无用!

 

启动顺序

nginx 依赖 django;django 依赖 mysql;启动顺序一步了然~

 

mysql

Dockerfile

mysql的dockerfile写的相对简单,或者说研究的不够深入。这里有一个问题,修改时区后,mysql会变,但是error.log时区没变!配置文件里又修改了一次,也不行。

FROM mysql:5.7.35

MAINTAINER WG

# ENV mysql_allow_empty_password=yes

ENV LANG=C.UTF-8

ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone

RUN rm -rf /etc/mysql/mysql.conf.d

 

主库my.cnf

这里的问题主要有以下几个:1.大多数配置含义忘记了;2.不用重复配置时区,对error.log没影响;3.basedir默认路径应该是ent...那个。早忘了,回头找找看;4.最重要的忘记写了,log目录必须是777权限,否则容器无法启动!肯定有其他解决办法,回头研究;总结:挂目录的和时区回头屡屡,还有关于root权限问题。

[mysqld]
user                =root
port                =3306

# 主从需要的配置项,主和从必须不一样
server-id           =3331

# 直接在dockerfile里同步时间,这里不用
# default-time-zone='+8:00'
# basedir=/usr/local/mysql    找不到容器里mysql目录,注释掉也没事

datadir             =/var/lib/mysql
tmpdir              =/tmp
pid-file            =/var/run/mysqld/mysqld.pid
socket              =/var/run/mysqld/mysqld.sock

log-error           =/var/log/mysql/error.log
slow_query_log_file =/var/log/mysql/mysql-slow.log

# 主从复制需要的配置项
log-bin             =/var/lib/mysql/mysql-bin
log-bin-index       =binlog.index

# 主从复制需要的配置项,设置混合模式
binlog_format       =mixed
binlog_cache_size   =4M
expire_logs_days    =10
sync_binlog         =0

slow_query_log=1
long_query_time=1

# skip-name-resolve            禁止域名解析的参数,远程访问推荐开启

skip-external-locking
key_buffer_size=32M
max_allowed_packet=1024M
table_open_cache=128
sort_buffer_size=768K
net_buffer_length=8K
read_buffer_size=768K
read_rnd_buffer_size=512K
myisam_sort_buffer_size=8M
thread_cache_size=16
query_cache_size=16M
tmp_table_size=32M
performance_schema_max_table_instances=1000
explicit_defaults_for_timestamp=true
#skip-networking
max_connections=500
max_connect_errors=100
open_files_limit=65535
log_bin=mysql-bin
early-plugin-load=""
default_storage_engine=InnoDB
innodb_file_per_table=1
innodb_buffer_pool_size=128M
innodb_log_file_size=32M
innodb_log_buffer_size=8M
innodb_flush_log_at_trx_commit=1
innodb_lock_wait_timeout=50
character-set-server=utf8mb4
#bind-address= localhost            一定要注释掉,mysql所在容器和django所在容器不同IP

[client]
port=3306
default-character-set=utf8mb4

[mysqldump]
quick
max_allowed_packet=16M

[mysql]
no-auto-rehash
default-character-set=utf8mb4

 

执行步骤

在主从配置上花了大量时间,主要是很久没配置了,都用什么回忆不清了。

[root@ansible data]# docker build -t mysql_img:v1 .

# privileged没感觉到有啥作用
[root@ansible data]# docker run -id --restart=always -p 3331:3306 \
                     -e MYSQL_ROOT_PASSWORD=556775#@wg --name dysql3331 \
                     -v /data/django/DYTMS/DYTMS_MasterDB/conf:/etc/mysql/conf.d \
                     -v /data/django/DYTMS/DYTMS_MasterDB/data:/var/lib/mysql \
                     -v /data/django/DYTMS/logs/3331:/var/log/mysql \
                     --privileged=true mysql_img:v1

# 直接进容器的方式
[root@ansible data]# docker exec -it dysql3331 /bin/bash

# 直接进mysql的方式
[root@ansible data]# docker exec -it dysql3331 mysql -uroot -p556775#@wg

mysql> create database dydb;

mysql> show global variables like '%log_bin%';
+---------------------------------+-----------------------------+
| Variable_name                   | Value                       |
+---------------------------------+-----------------------------+
| log_bin                         | ON                          |
| log_bin_basename                | /var/lib/mysql/mysql-bin    |
| log_bin_index                   | /var/lib/mysql/binlog.index |
| log_bin_trust_function_creators | OFF                         |
| log_bin_use_v1_row_events       | OFF                         |
+---------------------------------+-----------------------------+

# 给两个权限,从库ip
mysql> grant replication slave,replication client on *.* to 'repl'@'172.16.1.184' identified by '556775#@wg';

mysql> flush privileges;

mysql> use mysql;
mysql> show tables;
mysql> select * from user\G

# 此时此刻,因为数据量比较少,我在从原来的mysql数据库迁移数据库到3331....

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000004 |   680189 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+

# 主库有数据了,目前主从库用的同一个images,省略了....做从库....

 

从库my.cnf

[mysqld]
user                =root
port                =3306

# 主从需要的配置项
server-id           =3002

default-time-zone='+8:00'
# 直接在dockerfile里同步时间,这里不用
# basedir=/usr/local/mysql    找不到容器里mysql目录,注释掉也没事

datadir             =/var/lib/mysql
tmpdir              =/tmp
pid-file            =/var/run/mysqld/mysqld.pid
socket              =/var/run/mysqld/mysqld.sock

log-error           =/var/log/mysql/error.log
slow_query_log_file =/var/log/mysql/mysql-slow.log

# 主从复制需要的配置项
# log-bin           =/var/lib/mysql/mysql-bin
# log-bin-index     =binlog.index

# 主从复制需要的配置项,设置混合模式
binlog_format       =mixed
binlog_cache_size   =4M
expire_logs_days    =10
sync_binlog         =0

relay_log_purge     =1
relay_log_recovery  =1
relay-log           =/var/lib/mysql/relay-bin
relay-log-index     =relay-bin.index

slow_query_log      =1
long_query_time     =1

# skip-name-resolve            禁止域名解析的参数,远程访问推荐开启

skip-external-locking
key_buffer_size=32M
max_allowed_packet=1024M
table_open_cache=128
sort_buffer_size=768K
net_buffer_length=8K
read_buffer_size=768K
read_rnd_buffer_size=512K
myisam_sort_buffer_size=8M
thread_cache_size=16
query_cache_size=16M
tmp_table_size=32M
performance_schema_max_table_instances=1000
explicit_defaults_for_timestamp=true
#skip-networking
max_connections=500
max_connect_errors=100
open_files_limit=65535
log_bin=mysql-bin
early-plugin-load=""
default_storage_engine=InnoDB
innodb_file_per_table=1
innodb_buffer_pool_size=128M
innodb_log_file_size=32M
innodb_log_buffer_size=8M
innodb_flush_log_at_trx_commit=1
innodb_lock_wait_timeout=50
character-set-server=utf8mb4
#bind-address= localhost            一定要注释掉,mysql所在容器和django所在容器不同IP

[client]
port=3306
default-character-set=utf8mb4

[mysqldump]
quick
max_allowed_packet=16M

[mysql]
no-auto-rehash
default-character-set=utf8mb4

 

从库执行步骤

[root@ansible data]# docker build -t mysql_img:v1 .

[root@localhost DYTMS_1_SLAVEDB]# docker run -id --restart=always -p 3332:3306 \
                                  -e MYSQL_ROOT_PASSWORD=556775#@wg --name dymysql3332 \
                                  -v /data/django/DYTMS_1_SLAVEDB/conf:/etc/mysql/conf.d \
                                  -v /data/django/DYTMS_1_SLAVEDB/data:/var/lib/mysql \
                                  -v /data/django/logs/3332:/var/log/mysql \
                                  --privileged=true mysql_img:v1

[root@localhost DYTMS_1_SLAVEDB]#  docker exec -it dymysql3332 mysql -uroot -p556775#@wg

mysql> show global variables like '%relay%';
+---------------------------+--------------------------------+
| Variable_name             | Value                          |
+---------------------------+--------------------------------+
| max_relay_log_size        | 0                              |
| relay_log                 | /var/lib/mysql/relay-bin       |
| relay_log_basename        | /var/lib/mysql/relay-bin       |
| relay_log_index           | /var/lib/mysql/relay-bin.index |
| relay_log_info_file       | relay-log.info                 |
| relay_log_info_repository | FILE                           |
| relay_log_purge           | ON                             |
| relay_log_recovery        | ON                             |
| relay_log_space_limit     | 0                              |
| sync_relay_log            | 10000                          |
| sync_relay_log_info       | 10000                          |
+---------------------------+--------------------------------+

# 此时此刻我在复制数据库到从库

mysql> change master to master_host='172.16.4.40',master_user='repl',master_password='556775#@wg',master_port=3331,
       master_log_file='mysql-bin.000004',master_log_pos=680189;

mysql> set global read_only=1;
mysql> flush privileges;

mysql> show slave status\G
mysql> start slave;
mysql> show slave status\G

 

django之settings

settings必须提前配置了,不然没办法起服务测试

"""
Django settings for DYTMS_1 project.

Generated by 'django-admin startproject' using Django 3.2.10.

For more information on this file, see
https://docs.djangoproject.com/en/3.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.2/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-ox+oyhp!3t$h_gnrcs=b_%ysdb#rbsh6f^5j*#x&&@2rbl^wz^'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['*']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'test01.apps.Test01Config',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    # 'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'DYTMS_1.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': []
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'DYTMS_1.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dydb',
        'USER': 'root',
        'PASSWORD': '556775#@wg',
        'HOST': '172.16.4.40',
        'POST': 3331,
        'CHARSET': 'utf8mb4'
    },

    'slave3332': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dydb',
        'USER': 'root',
        'PASSWORD': '556775#@wg',
        'HOST': '172.16.1.184',
        'POST': 3332,
        'CHARSET': 'utf8mb4'
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.2/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.2/topics/i18n/

# LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-hans'

# TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'

USE_I18N = True

USE_L10N = True

# USE_TZ = True
USE_TZ = False


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.2/howto/static-files/

STATIC_URL = '/static/'

# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field


DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

# 变更默认用户表
AUTH_USER_MODEL = "test01.UserInfo"

LOGIN_URL = '/index/'

截止目前,单独操作数据库测试也行,我测试过了,666;起django项目测试也行,我准备把nginx也起来之后一并测试。

 

nginx

 

django

posted @ 2022-11-04 02:29  梵高de画笔  阅读(71)  评论(0编辑  收藏  举报