快速安装Kubernetes集群与Django应用示例

k8s使用实例

以web应用为例

django后端 + postgresql + redis

使用k8s需要搭建私有的镜像仓库,一些基础的应用镜像可以从docker hub上直接拉取,但是自己的包含代码的项目镜像需要提前打包好上传到自己的镜像仓库,k8s不能像docker-compose那样通过Dockerfile直接生成镜像

应确保master,node1,node2三个节点都能从你的私有仓库拉取镜像

项目结果

├── build.sh
├── Dockerfile
├── requirements.txt
└── src
    ├── src为django项目根目录

项目镜像打包Dockerfile实例:

FROM python:3.6

# 如果在中国,apt 使用镜像
RUN curl -s ifconfig.co/json | grep "China" > /dev/null && \
    curl -s http://mirrors.163.com/.help/sources.list.jessie > /etc/apt/sources.list || true

# 安装开发所需要的一些工具,同时方便在服务器上进行调试
RUN apt-get update;\
    apt-get install -y vim gettext postgresql-client;\
    true

COPY . /opt/demo
WORKDIR /opt/demo/src

# 先判断是否在中国,如果在中国,使用镜像下载
RUN curl -s ifconfig.co/json | grep "China" > /dev/null && \
    pip install -r /opt/demo/requirements.txt -i https://pypi.doubanio.com/simple --trusted-host pypi.doubanio.com || \
    pip install -r /opt/demo/requirements.txt

RUN mkdir /opt/logging
RUN mkdir /opt/running

打包镜像 build.sh

docker build -t 127.0.0.1:5000/backend:v1.0 . && docker push 127.0.0.1:5000/backend:v1.0

k8s 配置文件

backend.yaml

# backend
# dns: backend-service.demo.svc.cluster.local
---
apiVersion: v1
kind: Service
metadata:
  name: backend-service
  namespace: demo
spec:
  ports:
  - port: 80
    targetPort: 8000
  selector:
    app: backend-pod

# ingress 负载均衡
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: backend-ingress
  namespace: demo
spec:
  rules:
      paths:
        - path: /
          backend:
            serviceName: backend-service
            servicePort: 80

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: demo-backend
  namespace: demo
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: backend-pod
    spec:
      containers:
        - name: demo-backend
          image: 140.xx.xx.181:5000/backend:v1.0   # 你的后端打包的镜像地址(私有)
          imagePullPolicy: Always

          ports:
            - containerPort: 8000
          command: ["/bin/sh"]
          args: ["-c", "python manage.py runserver 0.0.0.0:8000"]
          # python manage.py runserver 0.0.0.0:8000 为测试使用,应使用uwsgi等方式

      initContainers:
        - name: wait-redis
          image: busybox
          command: ['sh', '-c', 'until nslookup redis.demo.svc.cluster.local; do echo waiting for redis service; sleep 2; done;']
        - name: wait-postgresql
          image: busybox
          command: ['sh', '-c', 'until nslookup postgresql.demo.svc.cluster.local; do echo waiting for postgresql service; sleep 2; done;']

---
apiVersion: batch/v1
kind: Job
metadata:
  name: sync-db
spec:
  template:
    metadata:
      name: sync-db
      labels:
        app: backend-sync-db-job
    spec:
      containers:
      - name: backend-db-migrate
        image: 140.xx.xx.181:5000/backend:v1.0
        command:
        - "/bin/sh"
        - "-c"
        - "python manage.py makemigrations && python manage.py migrate"
      restartPolicy: Never

postgres.yaml

# postgresql
# dns: postgresql.demo.svc.cluster.local
---
apiVersion: v1
kind: Service
metadata:
  name: postgresql
  namespace: demo
spec:
  ports:
    - port: 5432
      targetPort: postgresql-port
  selector:
    app: postgresql-pod

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: demo-postgresql
  namespace: demo
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: postgresql-pod
    spec:
      nodeName: 140.xx.xx.164   # 为了数据的持久化指定调度节点为node1: 140.xx.xx.164 
      containers:
        - name: demo-postgresql
          image: postgres:9.6.3
          imagePullPolicy: Always

          env:
            - name: POSTGRES_DB
              value: demo
            - name: POSTGRES_USER
              value: root
            - name: POSTGRES_PASSWORD
              value: devpwd

          ports:
            - name: postgresql-port
              containerPort: 5432

          volumeMounts:
            - name: postgresql-storage
              mountPath: /var/lib/postgresql

      volumes:
        - name: postgresql-storage
          hostPath:
            path: /data/postgresql   # 为了数据的持久化,使用主机hostPath方式挂载数据卷

redis.yaml

# redis
# dns: redis.demo.svc.cluster.local
---
apiVersion: v1
kind: Service
metadata:
  name: redis
  namespace: demo
spec:
  ports:
    - port: 9200
      targetPort: redis-port
  selector:
    app: redis-pod

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: demo-redis
  namespace: demo
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: redis-pod
    spec:
      containers:
        - name: demo-redis
          image: redis:3.0.7
          imagePullPolicy: Always

          ports:
            - name: redis-port
              containerPort: 6379

django后端配置

django的settings.py中关于postgres和redis配置

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'demo',
        'USER': 'root',
        'PASSWORD': 'devpwd',
        'HOST': 'postgresql.demo.svc.cluster.local',
        'PORT': '',
    }
}

REDIS_HOST = "redis.demo.svc.cluster.local"
# 对于不解析dns的应用配置,可以在配置文件中手动解析,如
# import socket 
# REDIS_HOST = socket.gethostbyname("redis.demo.svc.cluster.local")

注意: 基于dns的服务发现需要k8s-dns支持(rke默认已安装)

部署(master)

kubectl create namespace demo  # create namespace

kubectl -n demo apply -f .  # apply backend.yaml postgres.yaml yaml

查看结果

kubectl -n demo get pods

访问 140.xx.xx.181/admin/

总结

k8s更适合那种无状态的微服务类型应用, 浮动的pod,服务的动态伸缩在容器化应用方面有着巨大的优势 对于以数据为中心且没有集群概念的应用比如mysql等数据库,数据的持久化比较麻烦

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券