• k8s部署python3项目


    1. git拉取python项目代码仓库代码dockerfile文件放在里面requirements.txt文件写需要的模块
    2. dockerfile制作:
    FROM  fnndsc/ubuntu-python3:latest
    USER root
    WORKDIR /opt/app
    COPY  ./  /opt/app/
    RUN  pip  install -r  requirements.txt -i https://pypi.mirrors.ustc.edu.cn/simple/
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. k8s文件comfigmap、service、deployment、hpa
    ---
    #cat configmap.yaml
    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: python-config
      namespace: default
      labels:
        name: python
    
    
    
    
    data:
      app.conf: |
        #基础配置
        [base]
         host_root = "192.168.99.1"
         port = "8118"
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    #cat deployment.yaml
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name:  python
      namespace: default
      labels:
        name: python
    spec:
      replicas: 1
      selector:
        matchLabels:
          name: python
      template:
        metadata:
          labels:
            name: python
        spec:
          containers:
            - name: python
              securityContext:
                runAsNonRoot: false
                runAsUser: 0
              image: "harborip-python:base"
              command: ["/bin/bash"]
              args: ["-c", "/usr/bin/python3 /opt/app/main.py"]
              volumeMounts:
              - name: config
                mountPath: /opt/app/conf/
              ports:
                - name: http
                  containerPort: 8118
                  protocol: TCP
              livenessProbe:
                tcpSocket:
                  port: 8118
                initialDelaySeconds: 5
                periodSeconds: 20
                timeoutSeconds: 3
              readinessProbe:
                tcpSocket:
                  port: 8118
                initialDelaySeconds: 5
                periodSeconds: 10
                timeoutSeconds: 3
              resources:
                requests:
                  cpu: 1000m
                  memory: 1024Mi
                limits:
                  cpu: 2000m
                  memory: 2048Mi
    
          volumes:
          - name: config
            configMap:
              name: python-config
     
    
    • 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
    ---
    #cat service.yaml
    apiVersion: v1
    kind: Service
    metadata:
      name: python
      namespace: default
    spec:
      ports:
        - port: 8118
          targetPort: 8118
          protocol: TCP
      selector:
        name: python
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    ---
    #cat hpa.yaml
    apiVersion: autoscaling/v1
    kind: HorizontalPodAutoscaler
    metadata:
      name: python
    spec:
      maxReplicas: 3
      minReplicas: 1
      scaleTargetRef:
        apiVersion: extensions/v1beta1
        kind: Deployment
        name: python
      targetCPUUtilizationPercentage: 80
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    1. 启动应用
    kubectl apply -f ./
    
    • 1
    1. gitlab配置ci
    #cat .gitlab-ci.yml
    stages:
      - docker
      - deploy
    
    
    docker:
      stage: docker
      image: kaniko-executor:debug
      script:
      - mkdir -p /kaniko/.docker
      - echo "${DOCKER_AUTH_CONFIG}" > /kaniko/.docker/config.json
      - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination 192.168.10.100:8080/test/${CI_PROJECT_NAME}:$CI_COMMIT_SHORT_SHA  
      only:
        - master
        
    deploy:
      stage: deploy
      image: kubectl:1.18.1
      variables:
        GIT_STRATEGY: none
        K8S_NAME_SPACE: default
      script:
      - mkdir -p $HOME/.kube
      - echo "$KUBERNETES_SECRET" >> "$HOME/.kube/config"
      - kubectl version
      - kubectl get deployments.apps -n ${K8S_NAME_SPACE}
      - kubectl set image deployment/${CI_PROJECT_NAME}  python=192.168.10.100:8080/test/test2:${CI_COMMIT_SHORT_SHA} 
      only:
        - master
        
    
    • 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

    在这里插入图片描述

  • 相关阅读:
    近期的感想与2024年的计划
    Python实操 PDF自动识别并提取Excel文件
    H5新Api | requestIdleCallback - requestAnimationFram
    saas系统隐私面单自定义教程
    MBTI答题应用小程序
    阿里云服务器登录、安装MySql、配置Python、GO环境
    开发好APP了如何上架apple store市场
    算法——双指针
    Clickhouse安装问题
    Spring框架(四):Spring命名空间和配置标签
  • 原文地址:https://blog.csdn.net/weixin_43606975/article/details/126054031