#k8s构建python应用(django)

######k8s构建python应用(django)
mkdir
-p pydemo && cd pydemo

cat >Dockerfile <<EOF FROM centos:6.8 RUN mkdir -p /data/soft && cd /data/soft RUN yum install -y vim net-tools zip wget curl python-setuptools gcc unzip zlib zlib-devel openssh-clients tree crontabs RUN yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel expat-devel RUN [ "$(/usr/bin/python -V 2>&1 |awk '{print $2}')" != "2.7.15" ] && ([ ! -e Python-2.7.15.tgz ] && wget http://indoor.xxxx.com/download/centos/Python-2.7.15.tgz ) && tar zxf Python-2.7.15.tgz && cd Python-2.7.15 && ./configure && make && make install && mv -f /usr/bin/python /usr/bin/python2.6 && ln -sf /usr/local/bin/python2.7 /usr/bin/python && sed -i 's/^#!\/usr\/bin\/python$/#!\/usr\/bin\/python2.6/g' /usr/bin/yum RUN yum install -y python-setuptools RUN [ ! -e /usr/local/bin/pip -a ! -e /usr/bin/pip ] && (curl -sl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && python get-pip.py) RUN pip install django==1.9.7 RUN pip install pymysql RUN pip install python-crontab RUN pip install pexpect RUN pip install dwebsocket==0.4.2 RUN pip install chardet RUN pip install psutil RUN pip install paramiko RUN cd /data && django-admin.py startproject pydir && tree -if EXPOSE 8080 CMD service crond start && python /data/pydir/manage.py makemigrations --merge && python /data/pydir/manage.py migrate && python /data/pydir/manage.py runserver 0.0.0.0:8080 EOF #############################构建镜像 docker build . -t k.xxxx.com:5000/py-web6.8:0 #############################上传到镜像仓库 docker push k.xxxx.com:5000/py-web6.8:0 ##############################k8s 启动服务 cat >pydemo.yaml <<EOF apiVersion: v1 kind: Service metadata: name: pydemo namespace: default spec: selector: app: pydemo ports: - port: 80 targetPort: 8080 --- apiVersion: apps/v1 kind: Deployment metadata: name: pydemo namespace: default spec: replicas: 1 selector: matchLabels: app: pydemo template: metadata: labels: app: pydemo spec: containers: - name: pydemo image: k.xxxx.com:5000/py-web6.8:0 ports: - containerPort: 8080 volumeMounts: - name: pydir mountPath: /data/pydir volumes: - name: pydir hostPath: path: /data/pydir/vpnmgt --- apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress-pydemo namespace: default annotations: kubernetes.io/ingress.class: "nginx" spec: rules: - host: vpn.testweb.com http: paths: - path: / backend: serviceName: pydemo servicePort: 80 EOF kubectl apply -f pydemo.yaml kubectl get pods -o wide ### kubectl describe pod $(kubectl get pods |grep pydemo |awk '{print $1}') ####查看日志 kubectl logs -f $(kubectl get pods |grep pydemo |awk '{print $1}') #### kubectl get ingress
01-08 17:20