前面的教程,已经带着大家初步了解了K8S,接下来我们要通过实战,使用K8S去设计和构建常用项目。第一个项目,我选择Django,这是一个非常成熟的Web后台系统,通过Django我们可以构建一个稳健的Web业务。
准备
K8S
前期需要准备一个K8S环境,在前面的教程,我们已经可以通过自动化脚本搭建。可以参考这边博文
Images
django2.02
Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
FROM centos7:ssh MAINTAINER from cgls RUN yum -y install openssh* tar zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gcc make && mkdir /usr/local/python3 && cd /usr/local/python3 && wget https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tgz && tar -zxf Python-3.6.4.tgz && cd Python-3.6.4 && ./configure --prefix=/usr/local/python3 && make && make install && cd /usr/bin && mv python python.bk && ln -s /usr/local/python3/bin/python3 /usr/bin/python && sed -i "s/python/python2.7/" /usr/bin/yum && sed -i "s/python/python2.7/" /usr/libexec/urlgrabber-ext-down && sed -i "s/python/python2.7/" /usr/bin/yum-config-manager && cd && python -m pip install --upgrade pip && python -m pip install django==2.0.2 && python -m pip install django-bootstrap3 && python -m pip install pymysql && python -m pip install requests && python -m pip install bs4 CMD ["/usr/sbin/init"]
这里我是在centos7:ssh镜像基础上构建的,这个镜像就是在官方镜像上添加了ssh服务。大家可以自行构建~~
docker build -t centos:django2.02 .
mysql:5.7
Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14
FROM centos7:ssh MAINTAINER from cgls RUN yum -y install wget && cd /usr/local/src/ && wget http://repo.mysql.com/mysql57-community-release-el7-8.noarch.rpm && rpm -ivh mysql57-community-release-el7-8.noarch.rpm && yum -y install mysql-server && mkdir -p /raiddisk/mysqldata && chown mysql:mysql /raiddisk/mysqldata && /usr/sbin/mysqld --initialize-insecure --datadir=/raiddisk/mysqldata --user=mysql && sed -i 's/datadir=/var/lib/mysql/# datadir=/var/lib/mysql/g' /etc/my.cnf && echo -e "# addncharacter-set-server = utf8ndatadir = /raiddisk/mysqldatanlower_case_table_names = 1nexpire_logs_days = 2ninnodb_buffer_pool_size = 512Mnmax_connections=1500nmax_connect_errors = 50nmax_allowed_packet = 64Mninnodb_file_per_table=1nkey_buffer_size = 4096Mnsort_buffer_size=64Mnmyisam_sort_buffer_size=64Mnread_buffer_size=2Mntable_open_cache = 2048ntmp_table_size = 128Mnmax_heap_table_size = 128Mnread_rnd_buffer_size = 128Mnthread_cache_size = 64nbulk_insert_buffer_size = 64Mnmyisam_max_sort_file_size = 30Gnmyisam_repair_threads = 1nquery_cache_size = 64Mnquery_cache_limit = 2Mnthread_stack = 192Knslow_query_log" >> /etc/my.cnf && systemctl enable mysqld.service CMD ["/usr/sbin/init"]
create.sh
1 2 3 4 5 6 7
!/bin/bash mysql -uroot<<EOF alter user 'root'@'localhost' identified by '123qwe'; grant all privileges on *.* to 'cgls'@'%' identified by '123qwe'; flush privileges; exit EOF
install.sh
1 2 3 4 5 6 7 8
!/bin/bash docker build -t centos7:mysql . sleep 3 docker run -itd --name mysql_create centos7:mysql docker cp create.sh mysql_create:/root docker -it exec mysql_create sh create.sh docker commit mysql_create centos7:mysql5.7 docker rm -f -v mysql_create
sh install.sh
设计
我们的django项目,精简版只需要2个服务,mysql服务、django服务。现在镜像已经准备好了,现在要设计服务。
之前我们学习过健康检查,如果服务不能正常运行,一切都是白搭,所以健康检查是一定要有的。除此之外,我们要考虑获取django工程,在构建django服务时,获取最新的django工程并运行。如果django工程需要正常运行,那么我们还需要mysql服务能够正常运行,mysql服务这次暂不做读写分离,仅仅固定cluster_ip即可。
扫描二维码关注公众号,回复:
7499109 查看本文章
mysql_deployment.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
apiVersion: extensions/v1beta1 kind: Deployment metadata: labels: app: mysql name: mysql spec: replicas: 1 selector: matchLabels: app: mysql template: metadata: labels: app: mysql spec: containers: - image: mysql name: mysql imagePullPolicy: IfNotPresent env: - name: MYSQL_ROOT_PASSWORD value: 123 qwe readinessProbe: exec: command: - /bin/sh - "-c" - MYSQL_PWD="${MYSQL_ROOT_PASSWORD}" - mysql -h 127.0 .0 .1 -u root -e "SELECT 1" initialDelaySeconds: 10 timeoutSeconds: 1 successThreshold: 1 failureThreshold: 3 periodSeconds: 5
mysql_service.yaml
1 2 3 4 5 大专栏 K8S实战-构建Django项目-01-初始化 ass="line">6 7 8 9 10 11 12
apiVersion: v1 kind: Service metadata: name: mysql-svc spec: selector: run: mysql clusterIP: 10.101 .1 .1 ports: - protocol: TCP port: 3306 targetPort: 3306
django_deployment.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
apiVersion: extensions/v1beta1 kind: Deployment metadata: labels: app: django name: django spec: replicas: 1 selector: matchLabels: app: django template: metadata: labels: app: django spec: containers: - image: centos7:django2.02 name: django args: - /bin/sh - -c - cd /root; wget http://172.16.1.150/healthy.conf; sleep 365 d readinessProbe: exec: command: - cat - /root/healthy.conf initialDelaySeconds: 10 periodSeconds: 5
django_service.yaml
1 2 3 4 5 6 7 8 9 10 11 12
apiVersion: v1 kind: Service metadata: name: django-svc spec: selector: run: django clusterIP: 10.101 .1 .2 ports: - protocol: TCP port: 8000 targetPort: 8000
启动的时候,需要按照顺序依次执行即可~~
kubectl apply -f mysql_deployment.yaml
kubectl apply -f mysql_service.yaml
kubectl apply -f django_deployment.yaml
kubectl apply -f django_service.yaml
如果嫌麻烦,可以把所有文件整成一个
django_deploy.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
apiVersion: extensions/v1beta1 kind: Deployment metadata: labels: app: mysql name: mysql spec: replicas: 1 selector: matchLabels: app: mysql template: metadata: labels: app: mysql spec: containers: - image: mysql name: mysql imagePullPolicy: IfNotPresent env: - name: MYSQL_ROOT_PASSWORD value: 123 qwe readinessProbe: exec: command: - /bin/sh - "-c" - MYSQL_PWD="${MYSQL_ROOT_PASSWORD}" - mysql -h 127.0 .0 .1 -u root -e "SELECT 1" initialDelaySeconds: 10 timeoutSeconds: 1 successThreshold: 1 failureThreshold: 3 periodSeconds: 5 --- apiVersion: v1 kind: Service metadata: name: mysql-svc spec: selector: run: mysql clusterIP: 10.101 .1 .1 ports: - protocol: TCP port: 3306 targetPort: 3306 --- apiVersion: extensions/v1beta1 kind: Deployment metadata: labels: app: django name: django spec: replicas: 1 selector: matchLabels: app: django template: metadata: labels: app: django spec: containers: - image: centos7:django2.02 name: django args: - /bin/sh - -c - cd /root; wget http://172.16.1.150:/healthy.conf; sleep 365 d readinessProbe: exec: command: - cat - /root/healthy.conf initialDelaySeconds: 10 periodSeconds: 5 --- apiVersion: v1 kind: Service metadata: name: django-svc spec: selector: run: django clusterIP: 10.101 .1 .2 ports: - protocol: TCP port: 8000 targetPort: 8000
最基础的架子已经打好,接下来我们要一般般优化直至可以用于生产环境。
大家可以思考一下,如何在资源创建后立马启动django工程~~~