Kubernetes:在现有集群内启动一个容器,以运行cron作业

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Kubernetes:在现有集群内启动一个容器,以运行cron作业相关的知识,希望对你有一定的参考价值。

我正在Kubernetes集群上工作,我想在其中运行运行cron作业的单个容器。在同一集群中,我将部署3个其他运行应用程序代码的容器。仅在同一集群中添加不同类型的容器需要进行哪些更改?

service.yaml:

apiVersion: v1
kind: Service
metadata:
  labels:
    app.kubernetes.io/name: supplier-service
    app.kubernetes.io/instance: supplier-service-instance
    app.kubernetes.io/version: "1.0.0"
    app.kubernetes.io/component: backend
    app.kubernetes.io/managed-by: kubectl
  annotations:
      # Note that the backend talks over HTTP.
    service.beta.kubernetes.io/aws-load-balancer-backend-protocol: tcp
       # TODO: Fill in with the ARN of your certificate.
    service.beta.kubernetes.io/aws-load-balancer-ssl-cert: OUR_ARN
       # Only run SSL on the port named "https" below.
    service.beta.kubernetes.io/aws-load-balancer-ssl-ports: https
    service.beta.kubernetes.io/do-loadbalancer-redirect-http-to-https: "true"
    service.beta.kubernetes.io/do-loadbalancer-tls-ports: "443"
  name: supplier-service

spec:
  selector:
    app: supplier-service
  type: LoadBalancer
  ports:
  - name: http
    port: 80
    targetPort: 80
  - name: https
    port: 443
    targetPort: 443

deployment.yaml-此Yaml文件运行应用程序代码

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/name: supplier-service
    app.kubernetes.io/instance: supplier-service-instance
    app.kubernetes.io/version: '1.0.0'
    app.kubernetes.io/managed-by: kubectl
  name: supplier-service-deployment
spec:
  replicas: 3
  strategy:
     type: RollingUpdate
     rollingUpdate:
        maxUnavailable: 25%
        maxSurge: 1
  selector:
    matchLabels: 
      app: supplier-service
  template: 
    metadata:
      labels: 
        app: supplier-service
    spec:
      containers:
        - image: IMAGE-FROM-ECR/supplier-service:latest
          imagePullPolicy: Always
          name: supplier-service
          ports: 
            - containerPort: 80
            - containerPort: 443
            - containerPort: 6379

我准备好了一个新的DockerFile,可在容器内安装cron作业。

deployment-cron.yaml:

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/name: supplier-service
    app.kubernetes.io/instance: supplier-service-instance
    app.kubernetes.io/version: '1.0.0'
    app.kubernetes.io/managed-by: kubectl
  name: supplier-service-deployment
spec:
  replicas: 1
  strategy:
     type: RollingUpdate
     rollingUpdate:
        maxUnavailable: 25%
        maxSurge: 1
  selector:
    matchLabels: 
      app: supplier-service
  template: 
    metadata:
      labels: 
        app: supplier-service
    spec:
      containers:
        - image: AWS-ECR/supplier-service-cron:latest
          imagePullPolicy: Always
          name: supplier-service
          ports: 
            - containerPort: 80
            - containerPort: 443
            - containerPort: 6379

这是在现有集群中运行单个容器的正确方法,还是需要进行其他任何更改?谢谢。 :-)

答案

当您尝试在同一名称空间中部署两个具有相同名称的部署时,上述方法将向您抛出错误。

您尝试做的事如下所示

$ cat deploy1.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    deployment1: One
  name: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      deployment1: One
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        deployment1: One
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}

$ cat deploy2.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    deployment2: Two
  name: nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      deployment2: Two
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        deployment2: Two
    spec:
      containers:
      - image: nginx
        name: nginx
        resources: {}
status: {}

创建第一次部署。

$ kubectl create -f deploy1.yaml
deployment.apps/nginx created

在同一名称空间中创建第二个,并且您应该看到此错误

$ kubectl create -f deploy2.yaml
Error from server (AlreadyExists): error when creating "deploy2.yaml": deployments.apps "nginx" already exists
另一答案

名称和标签需要从原始部署Yaml进行更改。

如果保留metadata.name不变,则将修改kubectl apply -f上的现有部署,而不是创建新的部署。

如果您运行的都是cron,我认为您不需要端口,除非它运行的是supplier-service的完整实例。>

如果您在cron容器上留下标签Service,则现有的app: supplier-service选择器会感到困惑。

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app.kubernetes.io/name: supplier-service
    app.kubernetes.io/instance: supplier-service-instance
    app.kubernetes.io/version: '1.0.0'
    app.kubernetes.io/managed-by: kubectl
  name: supplier-service-cron-deployment
spec:
  replicas: 1
  selector:
    matchLabels: 
      app: supplier-service-cron
  template: 
    metadata:
      labels: 
        app: supplier-service-cron
    spec:
      containers:
        - image: AWS-ECR/supplier-service-cron:latest
          imagePullPolicy: Always
          name: supplier-service-cron

如果您不知道,kubernetes还提供了独立的cronjob功能。

以上是关于Kubernetes:在现有集群内启动一个容器,以运行cron作业的主要内容,如果未能解决你的问题,请参考以下文章

现有Kubernetes网络能力是否足够?

papertrail 的 fluentd 守护程序集容器无法在 kubernetes 集群中启动

Kubernetes编排工具

Kubernetes编排工具

Kubernetes 部署 Mysql 8

Kubernetes如何管理暴露服务