无法在Kubernetes集群上部署简单的“ Hello World”应用程序

问题描述

我一直在遵循有关创建Hello World应用程序的教程

https://medium.com/@bhargavshah2011/hello-world-on-kubernetes-cluster-6bec6f4b1bfd

首先,我在gcloud上创建一个集群(称为hello-world2)

enter image description here

然后在本地连接它:

enter image description here

接下来,我对文章中列出的项目进行 git克隆

git clone https://github.com/skynet86/hello-world-k8s.git
cd hello-world-k8s/

在目录中,我找到此 hello-world.yaml

它基本上列出了部署和服务(我已经将所有名称从hello-world都重命名为hello-world2)

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-world2-deployment
  labels:
    app: hello-world2
spec:
  selector:
    matchLabels:
      app: hello-world2
  replicas: 2
  template:
    metadata:
      labels:
        app: hello-world2
    spec:
      containers:
      - name: hello-world2
        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-world2
spec:
  selector:
    app: hello-world2
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30081   
  type: NodePort

我通过运行来应用此文件:

kubectl create -f hello-world.yaml

下一步,我运行 kubectl get all ,所有服务都列出了。

enter image description here

现在,该文章声称,我可以通过以下URL来查看我的新应用程序: http:// localhost:30081

但是当我导航到此链接时我什么也没得到:

enter image description here

我在做什么错了?

另外,另一个问题(对不起..)。如何将此服务/部署连接到群集?我需要执行某种集群应用服务命令吗?我的服务已经隐式连接了吗?

解决方法

  1. 您需要将服务移植到本地主机。他们最方便的方法是使用https://github.com/txn2/kubefwd。这将批量转发名称空间中的所有服务,甚至会使DNS名称在本地工作。当您在本地从IDE调试1个服务并且其他所有内容都保留在云中时,此功能非常有用。

  2. 集群DNS。每个Service对象都会在群集DNS服务器内部自动获得一个DNS记录:https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#services。在集群内部(其他Pod),您使用service_name.service_namespace.svc.cluster.local:service_port表示法调用服务。

,

您可以进行端口转发使用

kubectl port-forward <pod_name> 8080:80 

,然后转到浏览器http://localhost:8080

关于您的问题

好吧,您在部署在本地主机而不是Google Cloud上的应用程序中共享的链接。因此,如果您想在Google云上进行部署并想在Web浏览器中访问应用程序,则必须创建入口。

但是您仍然可以查看您的应用程序是否响应,只需在kubernetes集群上将ssh插入您的一个VM并执行curl http://localhost:30081。因为这些节点端口是内部公开的,而不是外部公开的,所以您不能通过Web浏览器访问它,因为您需要某种入口代理。我希望这些东西能清除您的理解

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...