如何使用清单文件中的kubectl端口转发(通常是清单文件与kubectl运行)
我正在尝试在本地使用Kubernetes运行我的第一个应用程序(或者我应该说minikube).
I am trying to run my first app with Kubernetes locally (or i should say minikube).
我有一个非常基本的Web服务器(一个本地docker映像),和一个正式的mongodb(我想从dockerhub中理想地拉出)映像.
I have a pretty basic web server (one local docker image), and the official mongodb (that i would like to pull ideally from dockerhub) image.
我不打算部署mongodb集群,只是使我的应用程序在本地运行的最低限度的东西才是一个不错的开始!
I am not trying to deploy a mongodb cluster, just the minimum stuff to get my app running locally would be a great start!
首先,我成功地使用kubectl run <MY_APP> --image=<MY_IMAGE> --port 3030 --image-pull-policy=IfNotPresent
单独运行了我的Web服务器,然后运行了kubectl port-forward <MY_POD> 3030:80
,并且运行良好,我可以从3030端口访问该应用程序(该应用程序正在侦听并且该容器暴露了端口80).
First, i succeed to run my web server alone with kubectl run <MY_APP> --image=<MY_IMAGE> --port 3030 --image-pull-policy=IfNotPresent
, then kubectl port-forward <MY_POD> 3030:80
and it works fine, i can hit the app from the 3030 port (the app is listening and the container expose the port 80).
但是我想将其转换为清单文件,以描述我需要轻松运行的所有容器.
But i would like to translate that into a manifest file to describe all the containers i need to run easily.
我的第一个问题是我找不到应该如何将kubectl port-forward
转换为清单文件.
我当时在考虑targetPort
,但尝试此操作时出现验证错误,看来我们不能在pods容器端口说明中使用targetPort
.
My first issue is i can't find how kubectl port-forward
is supposed to be translated into a manifest file.
I was thinking about targetPort
but i have a validation error when trying that, it looks like we can't use targetPort
in pods containers ports description.
我的第二个问题是,通过尝试仅描述一个容器来运行该堆栈,我不太确定自己在做什么.它可能还需要其他内容,我认为service
是可选的,我不确定deployment
,但是我已经看到了endpoint
类型,我可以忽略其他内容...
My second issue is that i am not really sure about what i am doing by trying to run that stack by describing a pod only. It may need other pieces, service
is i think optional for my need, i am not sure about deployment
, but i have seen an endpoint
kind, and i could ignore other ones...
由于kubectl run
似乎在创建pod,部署和副本集,所以我有点困惑,我不确定是否必须从清单文件中创建所有这些内容.
I am a little bit confuse since kubectl run
seems to create a pod, a deployment, and a replica sets, i am not sure if i have to create all of that from my manifest file.
我只想在本地运行这两个容器来处理代码,并在每次进行更改并对其进行测试时刷新它.
I just want to run my both containers locally to work on the code and refresh it everytime i make a change, and test it.
由于我对Kubernetes的知识不足,因此我的问题有一些子问题,但是基本上,我想知道如何将kubectl run <MY_APP>
和kubectl port-forward <MY_POD> 3030:80
转换为清单文件,以便可以添加mongodb容器和用单个kubectl create -f ./local.yaml
命令行开始整个堆栈.
So my question have some sub questions due to my lack of knowledges about Kubernetes, but basically, i would like to know how to translate my kubectl run <MY_APP>
and kubectl port-forward <MY_POD> 3030:80
into a manifest file so i can add the mongodb container and start the whole stack with a single kubectl create -f ./local.yaml
command line.
这是初稿:
apiVersion: v1
kind: Pod
metadata:
name: my_app
spec:
containers:
- name: web-server
image: my_app
imagePullPolicy: IfNotPresent
ports:
- name: my_app
containerPort: 3030
targetPort: 80
protocol: TCP
- name: mongodb
image: mongodb
ports:
- name: mongodb
containerPort: 27017
protocol: TCP
是的,您是正确的.您可以使用键入NodePort 将应用作为服务公开. (尽管在文档中不是很清楚),并且您服务的Yaml如下所示:
Yes, you are right. You can expose your app as a service with Type NodePort (though it's not very clear in documentation), and your service's yaml will look like:
apiVersion: v1
kind: Service
metadata:
name: app-service
spec:
type: NodePort
ports:
- port: 3030
targetPort: 80
selector:
app: app-server
您的部署的Yaml如下所示:
Your deployment's yaml will look like:
apiVersion: apps/v1
kind: Deployment
metadata:
name: app-server
labels:
app: app-server
spec:
selector:
matchLabels:
app: app-server
template:
metadata:
labels:
app: app-server
spec:
containers:
- name: web-server
image: my_app
ports:
- containerPort: 80
- name: mongodb
image: mongodb
ports:
- containerPort: 27017
如您所见,我仅公开了您的Web服务器.现在,要从Kubernetes外部访问Mongo,您也需要公开它.
As you see, I exposed only your web server. Now, to get access to Mongo from outside the Kubernetes, you need to expose it too.
您可以将应用部署为命令:
You can deploy your app as a command:
kubctl apply -f ./file_with_service.yaml ./file_with_deployment.yaml
您可以使用所有示例作为开始,并阅读更多文档以了解它清楚.
And you can use all that as an example to begin, and read more documentation to understand it clearly.