k8s-生產環境部署django項目k8s-dashboard管理系統


1. k8s-生產環境部署django項目k8s-dashboard管理系統

nginx 前端web服務,接收到動態請求通過uwsgi模塊將請求轉發給uwsgi服務器,uwsgi服務器通過django處理完后返回給Nginx,Nginx返回用戶瀏覽器展示。

  • 既然uwsgi是一個可以獨立部署的服務器,為什么還用Nginx代理?

    • Nginx作為入口可配置安全策略,並且可以為uwsgi提供負載均衡。
    • Nginx處理靜態資源能力強

2. 將本地開發的項目打包

2.1 導入依賴模塊列表

 pip freeze > ~/requirements.txt

是用pip工具導出安裝的pip list的顯示的包

2.2 修改數據庫為mysql

# devops_orm/settings.py
DATABASES = {
	'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'k8s',
        'USER': 'k8s',
        'PASSWORD': '12345678',
        'HOST': '192.168.0.78',
        'PORT': '3306',
	}
}

關閉debug模式和白名單

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False     # 關閉調試模式

# 開啟用戶訪問IP
ALLOWED_HOSTS = ['*']     # 白名單,只允許列表中的ip訪問,*代表所有

3. 服務器環境准備

3.1 安裝python3

  • 安裝依賴包

    yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel
    
  • 第一種方式用yum安裝

    • Linux安裝:

      yum install python36 -y
      

      一條命令完成安裝。

3.2 安裝依賴模塊列表

  • 壓縮本地代碼和導出的模塊依賴列表

壓縮為zip包,方便后面上傳

  • 上傳壓縮到服務器

    [root@k8s-master ~]# rz
    rz waiting to receive.
    Starting zmodem transfer.  Press Ctrl+C to cancel.
    Transferring k8s.zip...
      100%    1267 KB    1267 KB/sec    00:00:01       0 Errors  
    
    [root@k8s-master ~]# ll
    total 1272
    drwxr-xr-x 2 root root    4096 Jul  4 21:07 k8s-tools
    -rw-r--r-- 1 root root 1297880 Jul  4 21:04 k8s.zip
    

    這里通過rz命令上傳的,可以使用sftp等

  • 解壓壓縮包

    [root@k8s-master ~]# yum install unzip -y
    [root@k8s-master ~]# unzip k8s.zip
    [root@k8s-master ~]# ll
    total 1284
    drwxr-xr-x 3 root root    4096 Jul  5 14:28 devops
    drwxr-xr-x 2 root root    4096 Jul  4 21:07 k8s-tools
    -rw-r--r-- 1 root root 1297880 Jul  4 21:04 k8s.zip
    drwxr-xr-x 2 root root    4096 Jul  4 21:37 __MACOSX
    -rw-r--r-- 1 root root      58 Jul  4 21:46 requirements.txt
    [root@k8s-master ~]# mkdir -p /opt/k8s
    [root@k8s-master ~]# mv devops/* /opt/k8s/
    
  • 安裝依賴包

    [root@k8s-master ~]# pip3 install -r requirements.txt -i  http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
    
    [root@k8s-master ~]# cat requirements.txt 
    Django
    kubernetes
    PyMySQL
    channels
    channels-redis
    PyYAML
    

3.3 安裝數據庫

  • mysql安裝

    [root@k8s-master ~]# docker run -d --name db -p 3306:3306 -v mysqldata:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 mysql:5.7 --character-set-server=utf8
    [root@k8s-master ~]# docker exec  -it db bash
    root@f992b2375c5d:/# mysql -uroot -p$MYSQL_ROOT_PASSWORD 
    mysql: [Warning] Using a password on the command line interface can be insecure.
    Welcome to the MySQL monitor.  Commands end with ; or \g.
    Your MySQL connection id is 2
    Server version: 5.7.34 MySQL Community Server (GPL)
    
    Copyright (c) 2000, 2021, Oracle and/or its affiliates.
    
    Oracle is a registered trademark of Oracle Corporation and/or its
    affiliates. Other names may be trademarks of their respective
    owners.
    
    Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
    mysql> create database k8s;
    Query OK, 1 row affected (0.00 sec)
    
  • 安裝redis:

    [root@k8s-master ~]# docker run --name redis -d -p 6379:6379 redis:3
    

3.4 啟動開發環境

  • 啟動程序,驗證依賴模塊

    [root@k8s-master devops]# python3 manage.py runserver 0.0.0.0:8000
    Watching for file changes with StatReloader
    Performing system checks...
    
    System check identified no issues (0 silenced).
    
    You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
    Run 'python manage.py migrate' to apply them.
    July 05, 2021 - 11:36:44
    Django version 3.2.5, using settings 'devops.settings'
    Starting ASGI/Channels version 3.0.3 development server at http://0.0.0.0:8000/
    
  • 測試沒有問題,同步數據庫

    [root@k8s-master devops]# python3 manage.py migrate
    Operations to perform:
      Apply all migrations: admin, auth, contenttypes, sessions
    Running migrations:
      Applying contenttypes.0001_initial... OK
      Applying auth.0001_initial... OK
      Applying admin.0001_initial... OK
      Applying admin.0002_logentry_remove_auto_add... OK
      Applying admin.0003_logentry_add_action_flag_choices... OK
      Applying contenttypes.0002_remove_content_type_name... OK
      Applying auth.0002_alter_permission_name_max_length... OK
      Applying auth.0003_alter_user_email_max_length... OK
      Applying auth.0004_alter_user_username_opts... OK
      Applying auth.0005_alter_user_last_login_null... OK
      Applying auth.0006_require_contenttypes_0002... OK
      Applying auth.0007_alter_validators_add_error_messages... OK
      Applying auth.0008_alter_user_username_max_length... OK
      Applying auth.0009_alter_user_last_name_max_length... OK
      Applying auth.0010_alter_group_name_max_length... OK
      Applying auth.0011_update_proxy_permissions... OK
      Applying auth.0012_alter_user_first_name_max_length... OK
      Applying sessions.0001_initial... OK
    

3.4 安裝與配置uwsgi

  • Centos7 服務器安裝依賴包

    [root@k8s-master devops]# yum -y install gcc gcc-c++ gd cmake patch  automakemake autoconf libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devellibxml2 libxml2-devel zlib zlib-devel glibc glibc-devel glib2 glib2-devel bzip2bzip2-devel ncurses ncurses-devel curl curl-devel e2fsprogs e2fsprogs-develkrb5 krb5-devel libidn libtools-libs libidn-devel openssl openssl-developenldap openldap-devel nss_ldap openldap-clients openldap-servers pcre-devel libmcrypt-devel readline-devellibcap-devel ntpdate vim tree wget python3-devel.x86_64
    
  • uWSGI是一個Web服務器,也是Python的一個模塊,直接pip安裝即可:

    [root@k8s-master devops]# pip3 install uwsgi -i http://mirrors.aliyun.com/pypi/simple --trusted-host mirrors.aliyun.com
    
  • 創建uwsgi配置文件,路徑任意,

    [root@k8s-master ~]# mkdir -p /opt/uwsgi
    [root@k8s-master ~]# cd /opt/uwsgi
    [root@k8s-master uwsgi]# vim uwsgi.ini 
    [root@k8s-master uwsgi]# cat uwsgi.ini 
    [uwsgi]
    # 項目目錄
    chdir = /opt/k8s
    
    # 指定sock的文件路徑
    socket = /opt/k8s/uwsgi.sock
    # 指定監聽端口
    http = 0.0.0.0:8080
    
    # 靜態資源
    static-map = /static=/opt/k8s/static
    
    # wsgi文件(django入口)
    wsgi-file=devops/wsgi.py
    
    # 進程個數
    processes = 1
    
    # 指定項目的應用
    # module = devops.wsgi
    
    # 進程pid
    pidfile = /opt/k8s/uwsgi.pid
    
    # 日志路徑
    daemonize = /opt/k8s/uwsgi.log
    
  • 啟動

    [root@k8s-master uwsgi]# uwsgi --ini uwsgi.ini 
    [uWSGI] getting INI configuration from uwsgi.ini
    [uwsgi-static] added mapping for /static => /opt/k8s/static
    
  • 驗證服務啟動

    [root@k8s-master uwsgi]# ss -antp |grep 8080
    LISTEN     0      100          *:8080                     *:*                   users:(("uwsgi",pid=4871,fd=3),("uwsgi",pid=4870,fd=3))
    

3.5 安裝nginx

  • 安裝nginx

    [root@k8s-master uwsgi]# yum localinstall http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
    [root@k8s-master uwsgi]# yum -y install nginx
    [root@k8s-master uwsgi]# service nginx start
    Redirecting to /bin/systemctl start nginx.service
    [root@k8s-master uwsgi]# systemctl enable nginx.service
    Created symlink from /etc/systemd/system/multi-user.target.wants/nginx.service to /usr/lib/systemd/system/nginx.service.
    
  • 配置nginx

    [root@k8s-master uwsgi]# cd /etc/nginx/
    [root@k8s-master nginx]# ll
    total 76
    drwxr-xr-x 2 root root 4096 Jun  2 08:24 conf.d
    drwxr-xr-x 2 root root 4096 Jun  2 08:24 default.d
    -rw-r--r-- 1 root root 1077 Jun  2 08:24 fastcgi.conf
    -rw-r--r-- 1 root root 1077 Jun  2 08:24 fastcgi.conf.default
    -rw-r--r-- 1 root root 1007 Jun  2 08:24 fastcgi_params
    -rw-r--r-- 1 root root 1007 Jun  2 08:24 fastcgi_params.default
    -rw-r--r-- 1 root root 2837 Jun  2 08:24 koi-utf
    -rw-r--r-- 1 root root 2223 Jun  2 08:24 koi-win
    -rw-r--r-- 1 root root 5231 Jun  2 08:24 mime.types
    -rw-r--r-- 1 root root 5231 Jun  2 08:24 mime.types.default
    -rw-r--r-- 1 root root 2336 Jun  2 08:23 nginx.conf
    -rw-r--r-- 1 root root 2656 Jun  2 08:24 nginx.conf.default
    -rw-r--r-- 1 root root  636 Jun  2 08:24 scgi_params
    -rw-r--r-- 1 root root  636 Jun  2 08:24 scgi_params.default
    -rw-r--r-- 1 root root  664 Jun  2 08:24 uwsgi_params
    -rw-r--r-- 1 root root  664 Jun  2 08:24 uwsgi_params.default
    -rw-r--r-- 1 root root 3610 Jun  2 08:24 win-utf
    [root@k8s-master nginx]# cd conf.d/
    [root@k8s-master conf.d]# vim k8s-dashboard.conf 
    [root@k8s-master conf.d]# cat k8s-dashboard.conf 
    server {
            listen       80;
            server_name  k8s-dashboard.scajy.cn;
    
            location / {
               include     uwsgi_params;  # 導入模塊用於與uwsgi通信
               uwsgi_pass unix:/opt/k8s/uwsgi.sock; 
            }
            # 靜態文件目錄
            location /static {
               alias /opt/k8s/static;
            }
    }
    
  • 重啟nginx

    [root@k8s-master conf.d]# nginx -t
    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful
    [root@k8s-master conf.d]# nginx -s reload
    
  • 域名解析

    這個根據自己域名進行配置域名解析

4. 瀏覽器訪問




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM