简体   繁体   English

在 k8s 集群上部署 Hello World

[英]Deploying Hello World on k8s cluster

I am trying to deploy a "Hello World" application on an EKS cluster I created using eksctl.我正在尝试在使用 eksctl 创建的 EKS 集群上部署“Hello World”应用程序。 I have the cluster running with 2 pods and am following a tutorial located at https://shahbhargav.medium.com/hello-world-on-kubernetes-cluster-6bec6f4b1bfd .我的集群使用 2 个 pod 运行,并且正在遵循位于https://shahbhargav.medium.com/hello-world-on-kubernetes-cluster-6bec6f4b1bfd的教程。 I created a deployment using the following yaml file:我使用以下 yaml 文件创建了一个部署:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world-deployment
  labels:
    app: hello-world
spec:
  selector:
    matchLabels:
      app: hello-world
  replicas: 2
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
      - name: hello-world
        image: bhargavshah86/kube-test:v0.1
        ports:
        - containerPort: 80
        resources:
          limits:
            memory: 256Mi
            cpu: "250m"
          requests:
            memory: 128Mi
            cpu: "80m"
---
apiVersion: v1
kind: Service
metadata:
  name: hello-world
spec:
  selector:
    app: hello-world
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30081   
  type: NodePort

I then created the deploying by running the following command:然后我通过运行以下命令创建了部署:

kubectl create -f hello-world.yaml

I am unable to access it on localhost.我无法在本地主机上访问它。 I believe I am missing a step because the I had created the cluster & deployment on a Linux EC2 instance that I SSH into with Putty and I am accessing localhost on my Windows machine.我相信我错过了一个步骤,因为我已经在 Linux EC2 实例上创建了集群和部署,我使用 Putty 进入了 SSH 并且我正在我的 ZAEA23489CE3AA9B6406EBB28E0CDA43Z 机器上访问本地主机。 Any advice how I can connect would be appreciated.任何我如何连接的建议将不胜感激。 Currently I am getting the following when trying to connect to http://localhost:30081/目前我在尝试连接到 http://localhost:30081/ 时收到以下信息

在此处输入图像描述

As you mention, the problem is that you are trying to access to your local machine at port 30081 but the pods that you have created are in your EKS cluster in the cloud.正如您所提到的,问题在于您试图通过端口 30081 访问本地计算机,但您创建的 pod 位于云中的 EKS 集群中。 If you want to try out that the application is working you can SSH into the worker node as you have done and use the curl command like this.如果您想尝试该应用程序是否正常工作,您可以将 SSH 像您所做的那样进入工作节点,并像这样使用curl命令。

curl localhost:30081

That command is going to return the website that you have running in the console (without any format).该命令将返回您在控制台中运行的网站(没有任何格式)。

I think that in your case the best course will be to use the kubectl port-forward command.我认为在您的情况下,最好的方法是使用kubectl port-forward命令。 This command is going to bind one of your local machine ports into one of the ports of the pods.此命令会将您的本地计算机端口之一绑定到 pod 的端口之一。

Here is the format of the command.这是命令的格式。

kubectl port-forward POD-NAME-CURRENTLY-RUNNING-IN-CLUSTER UNUSED-PORT-IN-YOUR-PC:APPLICATION-PORT

Here is an example of how to use it and to check out that's working.这是一个如何使用它并检查它是否有效的示例。

kubectl port-forward hello-world-xxxxxx-xxxxx 8000:80

curl localhost:8000

Notice here that I am not using the service port to access the pod.注意这里我没有使用服务端口来访问 pod。 This tool is great for debugging!这个工具非常适合调试!

Another approach could be to open port 30081 with a security group and hit the IPs of the worker nodes but I think that's insecure and also have a lot of extra steps.另一种方法可能是使用安全组打开端口 30081 并访问工作节点的 IP,但我认为这是不安全的,并且还有很多额外的步骤。 You should check out the difference between type of services .您应该检查一下服务类型之间的区别。

Let me know if you have any doubts about my answer.如果您对我的回答有任何疑问,请告诉我。 I am not an expert and I could be wrong!我不是专家,我可能是错的!

Also English is not my first language.此外,英语不是我的第一语言。

Cheers干杯

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM