来奉献几分钟提交:2020年CNCF中国云原生问卷
问卷链接(https://www.wjx.cn/jq/9714648...)
作者:Alkin Tezuysal
Django是Python应用程序开发人员罕用的框架。它蕴含的包能够简化受权和内容治理等工作。Django反对包含MySQL在内的许多数据库,这使得不须要批改利用程序代码就能够在Vitess上运行Django应用程序。让咱们看看如何联合这两个开放源码框架的长处。
咱们应用Vitess操作器构建这个示例。你能够在这篇博客文章中看到Vitess操作器实现的细节。
先决条件
- 本地Python环境(3.X)
- Kubernetes环境(minikube、GKE)
- 通过Vitess反对Django ORM
在本例中,咱们将在GKE应用一个现有的Kubernetes集群。你也能够通过本地的minikube来实现此操作。
咱们应用的Django示例是“天气应用程序”。咱们首先应用提供的配置启动vitess操作器。
以下局部包含这些步骤:
- 创立Vitess操作器pod
- 构建Vitess集群组件(1x主tablet、1x复制tablet、3x etcd pod、1x vtgate、1x vtctld、1x vitessbackup)
- 创立“weatherapp”数据库模式和用户。
$ kubectl apply -f operator.yamlcustomresourcedefinition.apiextensions.k8s.io/etcdlockservers.planetscale.com createdcustomresourcedefinition.apiextensions.k8s.io/vitessbackups.planetscale.com createdcustomresourcedefinition.apiextensions.k8s.io/vitessbackupstorages.planetscale.com createdcustomresourcedefinition.apiextensions.k8s.io/vitesscells.planetscale.com createdcustomresourcedefinition.apiextensions.k8s.io/vitessclusters.planetscale.com createdcustomresourcedefinition.apiextensions.k8s.io/vitesskeyspaces.planetscale.com createdcustomresourcedefinition.apiextensions.k8s.io/vitessshards.planetscale.com createdserviceaccount/vitess-operator createdrole.rbac.authorization.k8s.io/vitess-operator createdrolebinding.rbac.authorization.k8s.io/vitess-operator createdpriorityclass.scheduling.k8s.io/vitess createdpriorityclass.scheduling.k8s.io/vitess-operator-control-plane createddeployment.apps/vitess-operator created$ kubectl get podsNAME READY STATUS RESTARTS AGEvitess-operator-7f9c9d58f6-q5zlf 1/1 Running 0 20s
用一个名为“weatherapp”的示例数据库初始化这个集群,拜访它的用户/明码将嵌入到配置文件中。咱们基本上是在创立一个数据库,相似于Vitess中的密钥空间。
$ kubectl apply -f 101_initial_cluster.yaml.django$ kubectl get podsNAME READY STATUS RESTARTS AGEexample-90089e05-vitessbackupstorage-subcontroller 1/1 Running 0 94sexample-etcd-faf13de3-1 1/1 Running 0 94sexample-etcd-faf13de3-2 1/1 Running 0 94sexample-etcd-faf13de3-3 1/1 Running 0 94sexample-vttablet-zone1-1542279354-edf1c7bf 2/3 Running 1 94sexample-vttablet-zone1-3763665199-476cbd65 2/3 Running 2 94sexample-weatherapp-x-x-vtbackup-init-75efaeeb 0/1 Completed 0 74sexample-zone1-vtctld-1d4dcad0-67bfd56b8b-4dr9s 1/1 Running 2 94sexample-zone1-vtgate-bc6cde92-59b88bc8d8-6wz86 1/1 Running 2 94svitess-operator-7f9c9d58f6-q5zlf 1/1 Running 0 4m30s
如你所见,这将带来一个功能齐全的托管Vitess集群,其中蕴含一个未分片的密钥空间,其中包含一个“主”和一个“正本”。
步骤1 - 设置portforward
$ cat pf.sh ; ./pf.sh &#!/bin/shkubectl port-forward --address localhost "$(kubectl get service --selector="planetscale.com/component=vtctld" -o name | head -n1)" 15000 15999 &process_id1=$!kubectl port-forward --address localhost "$(kubectl get service --selector="planetscale.com/component=vtgate,!planetscale.com/cell" -o name | head -n1)" 15306:3306 &process_id2=$!sleep 2echo "You may point your browser to http://localhost:15000, use the following aliases as shortcuts:"echo 'alias vtctlclient="vtctlclient -server=localhost:15999 -logtostderr"'echo 'alias mysql="mysql -h 127.0.0.1 -P 15306 -u user"'echo "Hit Ctrl-C to stop the port forwards"wait $process_id1wait $process_id2
查看Tablets:
$ vtctlclient ListAllTabletsHandling connection for 15999zone1-1542279354 weatherapp - replica 10.100.1.75:15000 10.100.1.75:3306 [] <null>zone1-3763665199 weatherapp - master 10.100.3.57:15000 10.100.3.57:3306 [] 2020-10-16T09:06:59Z
步骤2 - 验证数据库
$ alias mysql="mysql -h 127.0.0.1 -P 15306 -u djangouser -p"$ mysqlmysql: [Warning] Using a password on the command line interface can be insecure.Handling connection for 15306Welcome to the MySQL monitor. Commands end with ; or g.Your MySQL connection id is 1Server version: 5.7.9-Vitess MySQL Community Server (GPL)Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.Oracle is a registered trademark of Oracle Corporation and/or itsaffiliates. Other names may be trademarks of their respectiveowners.Type 'help;' or 'h' for help. Type 'c' to clear the current input statement.mysql> show databases;+------------+| Databases |+------------+| weatherapp |+------------+1 row in set (0.16 sec)
步骤3 - 设置应用程序环境
当初咱们曾经设置了具备MySQL后端的Vitess集群,接下来能够构建Django应用程序了。咱们将应用Django-admin命令构建一个Django我的项目。
$ mkdir my_weather_app$ cd my_weather_app$ python3 -m venv env$ . env/bin/activate(env) askdba:my_weather_app askdba$$ pip install django$ django-admin startproject weatherapp$ cd weatherapp/$ ls -latotal 8drwxr-xr-x 4 askdba staff 128 Oct 16 12:19 .drwxr-xr-x 4 askdba staff 128 Oct 16 12:18 ..-rwxr-xr-x 1 askdba staff 666 Oct 16 12:18 manage.pydrwxr-xr-x 7 askdba staff 224 Oct 16 12:18 weatherappEdit configuration file and update following section. $ vi weatherapp/settings.py [link to sample file]import os# Database# https://docs.djangoproject.com/en/3.0/ref/settings/#databasesDATABASES = { 'default': { 'ENGINE': 'custom_db_backends.vitess', 'OPTIONS': { 'read_default_file': '/usr/local/mysql/my.cnf', }, }}STATIC_ROOT = os.path.join(BASE_DIR, 'static')...# SECURITY WARNING: don't run with debug turned on in production!DEBUG = TrueALLOWED_HOSTS = ['127.0.0.1']# Application definition...
将customs_db_backends目录复制到我的项目目录。你能够将Vitess我的项目克隆到本地目录。
$ cp -r ~/vitess/support/django/custom_db_backends .$ vi /usr/local/mysql/my.cnf [link to sample my.cnf][client]database = weatherappuser = djangouserpassword = ********port = 15306host = 127.0.0.1default-character-set = utf8mb4
步骤4 - 装置MySQL客户端连接器
$ pip install mysqlclientCollecting mysqlclient Using cached mysqlclient-2.0.1.tar.gz (87 kB)Using legacy 'setup.py install' for mysqlclient, since package 'wheel' is not installed.Installing collected packages: mysqlclient Running setup.py install for mysqlclient ... doneSuccessfully installed mysqlclient-2.0.1
步骤5 - 在Vitess集群上构建Django框架
在这个阶段,咱们曾经筹备好运行迁徙来创立初始的Django元数据。
$ python manage.py migrateOperations to perform: Apply all migrations: admin, auth, contenttypes, sessionsRunning 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
步骤6 - 创立一个管理员用户
创立一个治理用户来拜访Django治理界面。
$ python manage.py createsuperuserUsername (leave blank to use 'askdba'): askdbaEmail address: alkin@planetscale.comPassword:Password (again):The password is too similar to the email address.This password is too short. It must contain at least 8 characters.Bypass password validation and create user anyway? [y/N]: ySuperuser created successfully.(env) askdba:weatherapp askdba$
步骤7 - 启动Django守护过程
$ python manage.py runserver 127.0.0.1:8000Watching for file changes with StatReloaderPerforming system checks...System check identified no issues (0 silenced).October 16, 2020 - 09:37:02Django version 3.1.2, using settings 'weatherapp.settings'Starting development server at http://127.0.0.1:8000/Quit the server with CONTROL-C.
步骤8 - 转到Django治理页面
将浏览器指向http://127.0.0.1:8000/admin
管理员登录界面
用户/角色治理屏幕
你能够通过这里持续构建应用程序。
总结
Vitess是一个十分弱小的切分框架,它带有一个内置的管制立体,容许后端开发人员轻松地调整他们的应用程序。联合弱小的应用程序框架(如Django),开发人员能够利用开源工具的弱小性能,开箱即用地创立可伸缩的应用程序。
参考文献
How To Make a Django Blog App and Connect it to MySQL
Getting Started With Django: Build A Weather App
Django MySQL Notes
点击浏览网站原文。
CNCF (Cloud Native Computing Foundation)成立于2015年12月,隶属于Linux Foundation,是非营利性组织。
CNCF(云原生计算基金会)致力于培养和保护一个厂商中立的开源生态系统,来推广云原生技术。咱们通过将最前沿的模式民主化,让这些翻新为公众所用。扫描二维码关注CNCF微信公众号。