Kubernetes:为Postgres + Tomcat应用程序建模作业/ Cron任务

时间:2019-08-01 22:00:36

标签: docker kubernetes kubernetes-pod

我在一个由Postgres数据库和tomcat服务器组成的开源系统上工作。我每个组件都有docker映像。我们目前使用docker-compose测试应用程序。

我正在尝试使用kubernetes对该应用程序进行建模。

这是我的第一次尝试。

apiVersion: v1
kind: Pod
metadata:
  name: dspace-pod
spec:
  volumes:
  - name: "pgdata-vol"
    emptyDir: {}
  - name: "assetstore"
    emptyDir: {}
  - name: my-local-config-map
    configMap:
      name: local-config-map
  containers:
  - image: dspace/dspace:dspace-6_x
    name: dspace
    ports:
    - containerPort: 8080
      name: http
      protocol: TCP
    volumeMounts:
    - mountPath: "/dspace/assetstore"
      name: "assetstore"
    - mountPath: "/dspace/config/local.cfg"
      name: "my-local-config-map"
      subPath: local.cfg
  #
  - image: dspace/dspace-postgres-pgcrypto
    name: dspacedb
    ports:
    - containerPort: 5432
      name: http
      protocol: TCP
    volumeMounts:
    - mountPath: "/pgdata"
      name: "pgdata-vol"
    env:
    - name: PGDATA
      value: /pgdata

我有一个configMap,它将主机名设置为Pod的名称。

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2016-02-18T19:14:38Z
  name: local-config-map
  namespace: default
data:
  local.cfg: |-
    dspace.dir = /dspace
    db.url = jdbc:postgresql://dspace-pod:5432/dspace
    dspace.hostname = dspace-pod
    dspace.baseUrl = http://dspace-pod:8080
    solr.server=http://dspace-pod:8080/solr

此应用程序具有许多从命令行运行的任务。

我创建了第三个Docker映像,其中包含命令行上所需的jar。

我对在Kubernetes中将这些命令行任务建模为 Jobs 感兴趣。假设这是处理这些任务的适当方法,我如何指定作业应在已经运行的Pod中运行?

这是我第一次定义工作。

apiVersion: batch/v1
kind: Job
#https://kubernetes.io/docs/concepts/workloads/controllers/jobs-run-to-completion/
metadata:
  name: dspace-create-admin
spec:
  template:
    spec:
      volumes:
      - name: "assetstore"
        emptyDir: {}
      - name: my-local-config-map
        configMap:
          name: local-config-map
      containers:
      - name: dspace-cli
        image: dspace/dspace-cli:dspace-6_x
        command: [
          "/dspace/bin/dspace",
          "create-administrator",
          "-e", "test@test.edu",
          "-f", "test",
          "-l", "admin",
          "-p", "admin",
          "-c", "en"
        ]
        volumeMounts:
        - mountPath: "/dspace/assetstore"
          name: "assetstore"
        - mountPath: "/dspace/config/local.cfg"
          name: "my-local-config-map"
          subPath: local.cfg
      restartPolicy: Never

1 个答案:

答案 0 :(得分:2)

以下配置使我能够按期望的方式启动服务(tomcat和postgres)。

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2016-02-18T19:14:38Z
  name: local-config-map
  namespace: default
data:
  # example of a simple property defined using --from-literal
  #example.property.1: hello
  #example.property.2: world
  # example of a complex property defined using --from-file
  local.cfg: |-
    dspace.dir = /dspace
    db.url = jdbc:postgresql://dspacedb-service:5432/dspace
    dspace.hostname = dspace-service
    dspace.baseUrl = http://dspace-service:8080
    solr.server=http://dspace-service:8080/solr
---
apiVersion: v1
kind: Service
metadata:
  name: dspacedb-service
  labels:
    app: dspacedb-app
spec:
  type: NodePort
  selector:
    app: dspacedb-app
  ports:
  - protocol: TCP
    port: 5432
  #  targetPort: 5432
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: dspacedb-deploy
  labels:
    app: dspacedb-app
spec:
  selector:
    matchLabels:
      app: dspacedb-app
  template:
    metadata:
      labels:
        app: dspacedb-app
    spec:
      volumes:
      - name: "pgdata-vol"
        emptyDir: {}
      containers:
      - image: dspace/dspace-postgres-pgcrypto
        name: dspacedb
        ports:
        - containerPort: 5432
          name: http
          protocol: TCP
        volumeMounts:
        - mountPath: "/pgdata"
          name: "pgdata-vol"
        env:
        - name: PGDATA
          value: /pgdata
---
apiVersion: v1
kind: Service
metadata:
  name: dspace-service
  labels:
    app: dspace-app
spec:
  type: NodePort
  selector:
    app: dspace-app
  ports:
  - protocol: TCP
    port: 8080
    targetPort: 8080
    name: http
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: dspace-deploy
  labels:
    app: dspace-app
spec:
  selector:
    matchLabels:
      app: dspace-app
  template:
    metadata:
      labels:
        app: dspace-app
    spec:
      volumes:
      - name: "assetstore"
        emptyDir: {}
      - name: my-local-config-map
        configMap:
          name: local-config-map
      containers:
      - image: dspace/dspace:dspace-6_x-jdk8-test
        name: dspace
        ports:
        - containerPort: 8080
          name: http
          protocol: TCP
        volumeMounts:
        - mountPath: "/dspace/assetstore"
          name: "assetstore"
        - mountPath: "/dspace/config/local.cfg"
          name: "my-local-config-map"
          subPath: local.cfg

应用上面的配置后,我得到以下结果。

$ kubectl get services -o wide
NAME               TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE       SELECTOR
dspace-service     NodePort    10.104.224.245   <none>        8080:32459/TCP   3s        app=dspace-app
dspacedb-service   NodePort    10.96.212.9      <none>        5432:30947/TCP   3s        app=dspacedb-app
kubernetes         ClusterIP   10.96.0.1        <none>        443/TCP          22h       <none>

$ kubectl get pods
NAME                               READY     STATUS      RESTARTS   AGE
dspace-deploy-c59b77bb8-mr47k      1/1       Running     0          10m
dspacedb-deploy-58dd85f5b9-6v2lf   1/1       Running     0          10

我很高兴看到该服务名称可用于端口转发。

$ kubectl port-forward service/dspace-service 8080:8080
Forwarding from 127.0.0.1:8080 -> 8080
Forwarding from [::1]:8080 -> 8080

我还能够使用configMap中定义的服务名称来运行以下作业。

apiVersion: batch/v1
kind: Job
metadata:
  name: dspace-create-admin
spec:
  template:
    spec:
      volumes:
      - name: "assetstore"
        emptyDir: {}
      - name: my-local-config-map
        configMap:
          name: local-config-map
      containers:
      - name: dspace-cli
        image: dspace/dspace-cli:dspace-6_x
        command: [
          "/dspace/bin/dspace",
          "create-administrator",
          "-e", "test@test.edu",
          "-f", "test",
          "-l", "admin",
          "-p", "admin",
          "-c", "en"
        ]
        volumeMounts:
        - mountPath: "/dspace/assetstore"
          name: "assetstore"
        - mountPath: "/dspace/config/local.cfg"
          name: "my-local-config-map"
          subPath: local.cfg
      restartPolicy: Never

结果

$ kubectl get pods
NAME                               READY     STATUS      RESTARTS   AGE
dspace-create-admin-kl6wd          0/1       Completed   0          5m
dspace-deploy-c59b77bb8-mr47k      1/1       Running     0          10m
dspacedb-deploy-58dd85f5b9-6v2lf   1/1       Running     0          10m

我还有一些工作要做以保留卷。