简体   繁体   English

如何在 Kubernetes 集群内的 pod 中运行 cli 应用程序?

[英]How can I run a cli app in a pod inside a Kubernetes cluster?

I have a cli app written in NodeJS [not by me].我有一个用 NodeJS [不是我] 编写的 cli 应用程序。

I want to deploy this on a k8s cluster like I have done many times with web servers.我想在 k8s 集群上部署它,就像我在 web 服务器上做过很多次一样。

I have not deployed something like this before, so I am in a kind of a loss.我以前没有部署过这样的东西,所以我有点茫然。

I have worked with dockerized cli apps [like Terraform] before, and i know how to use them in a CICD.我以前使用过 dockerized cli 应用程序 [如 Terraform],并且我知道如何在 CICD 中使用它们。

But how should I deploy them in a pod so they are always available for usage from another app in the cluster?但是我应该如何将它们部署在一个 pod 中,以便它们始终可供集群中的另一个应用程序使用?

Or is there a completely different approach that I need to consider?还是我需要考虑一种完全不同的方法?

#EDIT# #编辑#

I am using this in the end of my Dockerfile..我在 Dockerfile 的末尾使用它。

# the main executable
ENTRYPOINT ["sleep", "infinity"]
# a default command
CMD ["mycli help"]

That way the pod does not restart and the cli inside is waiting for commands like mycli do this这样 pod 不会重新启动,内部的 cli 正在等待mycli do this

Is it a hacky way that is frowned upon or a legit solution?这是一种不受欢迎的hacky的方式还是合法的解决方案?

Your edit is one solution, another one if you do not want or cannot change the Docker image is to Define a Command for a Container to loop infinitely, this would achieve the same as the Dockerfile ENTRYPOINT but without having to rebuild the image.您的编辑是一种解决方案,如果您不想或无法更改 Docker 图像,则另一种解决方案是为容器定义无限循环的命令,这将与 Dockerfile ENTRYPOINT 相同,但无需重建图像。

Here's an example of such implementation:以下是此类实现的示例:

apiVersion: v1
kind: Pod
metadata:
  name: command-demo
  labels:
    purpose: demonstrate-command
spec:
  containers:
  - name: command-demo-container
    image: debian
    command: ["/bin/sh", "-ec", "while :; do echo '.'; sleep 5 ; done"]
  restartPolicy: OnFailure

As for your question about if this is a legit solution, this is hard to answer;至于您关于这是否是合法解决方案的问题,这很难回答; I would say it depends on what your application is designed to do.我会说这取决于您的应用程序的设计目的。 Kubernetes Pods are designed to be ephemeral, so a good solution would be one that is running until the job is completed; Kubernetes Pod 被设计为临时的,所以一个好的解决方案是运行直到作业完成; for a web server, for example, the job is never completed because it should be constantly listening to requests.例如,对于 web 服务器,该作业永远不会完成,因为它应该不断地监听请求。

If your pods are in the same cluster they are already available to other pods through Core-DNS.如果您的 pod 在同一个集群中,它们已经可以通过 Core-DNS 用于其他 pod。 An internal DNS service which allows you to access them by their internal DNS name.一个内部 DNS 服务,允许您通过其内部 DNS 名称访问它们。 Something like my-cli-app.my-namespace.svc.cluster .类似my-cli-app.my-namespace.svc.cluster东西。 DNS for service and pods DNS 用于服务和 pod

You would then create a deployment file with all your apps.然后,您将创建一个包含所有应用程序的部署文件。 Note this doesn't need ports to work and also doesn't include communication through the internet.请注意,这不需要端口即可工作,也不包括通过 Internet 进行的通信。

#deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何为Kubernetes群集确定适当的pod CIDR值? - How can I determine an appropriate pod CIDR value for a Kubernetes cluster? 如何在Kubernetes集群中使用现有的Statefulset运行Pod? - How to run pod with existing statefulset in kubernetes cluster? 我可以在Kubernetes Pod中运行Google Monitoring Agent吗? - Can I run Google Monitoring Agent inside a Kubernetes Pod? 我如何从集群上的容器内部在kubernetes集群上运行ksonnet命令 - How can i run ksonnet command on kubernetes cluster from inside a container on the cluster 为什么我不能在Kubernetes集群的一个Pod中连接其他Pod? - Why I can not connect other pod in one pod of the kubernetes cluster? 如何在 kube.netes 集群中的单个 pod 上运行 quartz 任务? - How to run quartz task on single pod in kubernetes cluster? 如何在kubernetes集群的Pod中重新启动Jenkins服务 - how to restart jenkins service inside pod in kubernetes cluster 如何在Windows中访问Kubernetes群集中的pod? - How do you access a pod inside a Kubernetes Cluster in Windows? 我想在pod内使用fabric8 kubernetes客户端(java)。 我如何获取其所部署集群的kubernetes客户端? - I want to use fabric8 kubernetes client (java) inside a pod. How do I obtain the kubernetes client for the cluster it is deployed on? 如何将字符串从一个 Kubernetes pod 中的供应商应用程序复制到另一个 pod 中的供应商应用程序? - How can I copy a string from a vendor app in one Kubernetes pod to a vendor app in another pod?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM