[ djangoblog项目搭建[kubesphere流水线] ]


djangoblog项目地址

本文使用了kubeshphere搭建的k8s平台并添加了devploy流水线部署

前端pod文件nginx_deploy.yaml - 39行volumeMounts为配置文件挂载, 需要提前创建好配置字典

kind: Deployment
apiVersion: apps/v1
metadata:
  name: django-blog-nginx
  namespace: chf-object
  labels:
    app: django-blog-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: django-blog-nginx
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: django-blog-nginx
    spec:
      volumes:
        - name: host-time
          hostPath:
            path: /etc/localtime
            type: ''
        - name: volume-xbbo0m
          configMap:
            name: django-blog-nginx-conf
            defaultMode: 420
        - name: volume-lyb81k
          persistentVolumeClaim:
            claimName: django-blog-static
      containers:
        - name: container-e8sn51
          image: nginx
          ports:
            - name: http-80
              containerPort: 80
              protocol: TCP
          resources: {}
          volumeMounts:
            - name: host-time
              readOnly: true
              mountPath: /etc/localtime
            - name: volume-xbbo0m
              readOnly: true
              mountPath: /etc/nginx/nginx.conf
              subPath: nginx.conf
            - name: volume-lyb81k
              readOnly: true
              mountPath: /code/djangoblog/collectedstatic
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: IfNotPresent
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      dnsPolicy: ClusterFirst
      serviceAccountName: default
      serviceAccount: default
      securityContext: {}
      schedulerName: default-scheduler
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 25%
      maxSurge: 25%
  revisionHistoryLimit: 10
  progressDeadlineSeconds: 600

后端pod文件python_deploy.yaml - 39行 image这里是使用了Jenkinsfile里的环境变量来获取镜像 - 44行 env 是容器pod配置的环境变量 - 52行 shopping-mysql-service.chf-object 是使用了提前部署好的Mysql的pod添加的服务service的dns地址 - 56行 shopping-redis-8dwn.chf-object 是提前部署的redis的pod添加的服务service的dns地址 - 58行 volumeMounts 为配置挂载卷里的内容

kind: Deployment
apiVersion: apps/v1
metadata:
  name: django-blog
  namespace: chf-object
  labels:
    app: django-blog
  annotations:
    deployment.kubernetes.io/revision: '1'
    kubesphere.io/creator: admin
spec:
  replicas: 1
  selector:
    matchLabels:
      app: django-blog
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: django-blog
      annotations:
        kubesphere.io/creator: admin
        kubesphere.io/imagepullsecrets: '{"container-dzbghj":"chf-docker-hub"}'
        logging.kubesphere.io/logsidecar-config: '{}'
    spec:
      volumes:
        - name: host-time
          hostPath:
            path: /etc/localtime
            type: ''
        - name: volume-nh1umt
          persistentVolumeClaim:
            claimName: django-blog-static
        - name: volume-ze2zy4
          persistentVolumeClaim:
            claimName: django-blog-logs
      containers:
        - name: container-dzbghj
          image: '$REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:v$BUILD_NUMBER'
          ports:
            - name: http-8000
              containerPort: 8000
              protocol: TCP
          env:
            - name: DJANGO_MYSQL_DATABASE
              value: djangoblog
            - name: DJANGO_MYSQL_USER
              value: root
            - name: DJANGO_MYSQL_PASSWORD
              value: chenhaifeng
            - name: DJANGO_MYSQL_HOST
              value: shopping-mysql-service.chf-object
            - name: DJANGO_MYSQL_PORT
              value: '3306'
            - name: DJANGO_REDIS_URL
              value: 'shopping-redis-8dwn.chf-object:6379'
          resources: {}
          volumeMounts:
            - name: host-time
              readOnly: true
              mountPath: /etc/localtime
            - name: volume-nh1umt
              mountPath: /code/djangoblog/collectedstatic
            - name: volume-ze2zy4
              mountPath: /code/djangoblog/logs
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          imagePullPolicy: IfNotPresent
      restartPolicy: Always
      terminationGracePeriodSeconds: 30
      dnsPolicy: ClusterFirst
      serviceAccountName: default
      serviceAccount: default
      securityContext: {}
      imagePullSecrets:
        - name: chf-docker-hub
      schedulerName: default-scheduler
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 25%
      maxSurge: 25%
  revisionHistoryLimit: 10
  progressDeadlineSeconds: 600
---
kind: Service
apiVersion: v1
metadata:
  name: django-blog-serivce
  namespace: chf-object
  labels:
    app: django-blog-serivce
  annotations:
    kubesphere.io/creator: admin
spec:
  ports:
    - name: http-8000
      protocol: TCP
      port: 8000
      targetPort: 8000
  selector:
    app: django-blog
  clusterIP: None
  clusterIPs:
    - None
  type: ClusterIP
  sessionAffinity: None
  ipFamilies:
    - IPv4
  ipFamilyPolicy: SingleStack
  internalTrafficPolicy: Cluster

流水线Jenkinsfile文件

pipeline {
  agent {
    node {
      label 'python'
    }

  }
  stages {
    stage('拉取代码') {
      agent none
      steps {
        container('python') {
          git(url: 'https://gitee.com/chen-haifeng1/django-blog-test.git', credentialsId: 'chf-gitee', branch: 'master', changelog: true, poll: false)
          sh 'ls -al'
        }

      }
    }

    stage('打包镜像&推送镜像') {
      agent none
      steps {
        container('python') {
          sh 'docker build -t $APP_NAME:v$BUILD_NUMBER .'
          withCredentials([usernamePassword(credentialsId : 'chf-docker-hub' ,passwordVariable : 'DOCKER_PASSWORD' ,usernameVariable : 'DOCKER_USERNAME' ,)]) {
            sh 'echo "$DOCKER_PASSWORD" | docker login $REGISTRY -u "$DOCKER_USERNAME" --password-stdin'
            sh 'docker tag $APP_NAME:v$BUILD_NUMBER $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:v$BUILD_NUMBER'
            sh 'docker push $REGISTRY/$DOCKERHUB_NAMESPACE/$APP_NAME:v$BUILD_NUMBER'
          }

        }

      }
    }

    stage('运行后端服务') {
      agent none
      steps {
        container('python') {
          input(message: '启动后端服务?', submitter: '')
          withCredentials([kubeconfigContent(credentialsId : 'kubeconfig-key' ,variable : 'KUBECONFIG_CONFIG' ,)]) {
            sh 'mkdir -p ~/.kube/'
            sh 'echo "$KUBECONFIG_CONFIG" > ~/.kube/config'
            sh 'envsubst < ./python_deploy.yaml | kubectl apply -f -'
          }

        }

      }
    }

    stage('运行前端服务') {
      agent none
      steps {
        container('python') {
          input(message: '启动前端服务?', submitter: '')
          withCredentials([kubeconfigContent(credentialsId : 'kubeconfig-key' ,variable : 'KUBECONFIG_CONFIG' ,)]) {
            sh 'mkdir -p ~/.kube/'
            sh 'echo "$KUBECONFIG_CONFIG" > ~/.kube/config'
            sh 'envsubst < ./nginx_deploy.yaml | kubectl apply -f -'
          }

        }

      }
    }

  }
  environment {
    DOCKER_CREDENTIAL_ID = 'dockerhub-id'
    GITHUB_CREDENTIAL_ID = 'github-id'
    KUBECONFIG_CREDENTIAL_ID = 'demo-kubeconfig'
    REGISTRY = 'ag.wdnmdnszz.top:444'
    DOCKERHUB_NAMESPACE = 'free'
    GITHUB_ACCOUNT = 'kubesphere'
    APP_NAME = 'django-blog'
  }
  parameters {
    string(name: 'TAG_NAME', defaultValue: '', description: '')
  }
}