简体   繁体   中英

Deploying Hello World on k8s cluster

I am trying to deploy a "Hello World" application on an EKS cluster I created using eksctl. 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 . I created a deployment using the following yaml file:

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. Any advice how I can connect would be appreciated. Currently I am getting the following when trying to connect to 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. 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.

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. This command is going to bind one of your local machine ports into one of the ports of the pods.

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. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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