一、新建.net core 控制台程序:k8scron.job

 二、将发布程序拷贝到服务器:/root/k8swwwroot/k8scron.job

 三、编辑yaml文件k8scron-job-cron.yaml,每分钟执行一次

apiVersion: batch/v1beta1
kind: CronJob
metadata:
 name: k8scron-job-cron
 namespace:  default
 labels:
    name: k8scron-job-cron
spec:
  schedule: "*/1 * * * *"
  suspend: false
  concurrencyPolicy: Forbid 
  startingDeadlineSeconds: 1000  
  jobTemplate:
   spec:
    template:
      metadata:
        labels:
          app: k8scron-job-cron
      spec:
        restartPolicy: OnFailure
        containers:
        - name: k8scron-job-cron
          image: mcr.microsoft.com/dotnet/sdk:6.0
          command: ["/bin/sh"]
          args: ["-c", "echo 'VERSION-20200122175650';cd /mnt/k8scron.job;dotnet k8scron.job.dll"]
          volumeMounts:
          - mountPath: /etc/localtime
            name: volume-localtime
          - mountPath: /mnt/k8scron.job
            name: data
          - mountPath: /mnt/cert
            name: cert
          resources:
            requests:
                memory: "384Mi"
                cpu: "50m"
            limits:
                memory: "1024Mi"
                cpu: "1000m"                                
        volumes:
        - name: volume-localtime
          hostPath:
            path: /etc/localtime
        - name: data
          hostPath:
            path: /root/k8swwwroot/k8scron.job
        - name: cert
          hostPath:
            path: /root/k8swwwroot/cert

四、部属 

kubectl apply -f k8scron-job-cron.yaml 

附:部属为web

apiVersion: apps/v1
kind: Deployment
metadata:
  name: k8scron-job
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: k8scron-job
  template:
    metadata:
      labels:
       app: k8scron-job
    spec:
      containers:
      - name: k8scron-job
        image: mcr.microsoft.com/dotnet/sdk:6.0
        command: ["/bin/sh"]
        args: ["-c", "echo 'VERSION-20200122175650';cd /mnt/k8scron.job;ASPNETCORE_URLS='http://*:5000' dotnet k8scron.job.dll"]
        volumeMounts:
        - mountPath: /mnt/k8scron.job
          name: data
        - mountPath: /mnt/cert
          name: cert
        ports:
        - containerPort: 5000
      volumes:
      - name: data
        hostPath:
          path: /root/k8swwwroot/k8scron.job
      - name: cert
        hostPath:
          path: /root/k8swwwroot/cert
---
apiVersion: v1
kind: Service
metadata:
  name: k8scron-job
  labels: 
   app: k8scron-job
spec:
  type: NodePort
  ports:
  - port: 8002 #k8s内部访问端口
    protocol: TCP
    targetPort: 5000 #.netcore监听端口
    nodePort: 32167 #Node对外固定端口数值
  selector:
    app: k8scron-job

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐