在Kubernetes中运行简单的Hello World静态HTTP应用

时间:2019-04-17 22:11:37

标签: apache docker kubernetes

我在Dockerhub上有一个简单的Hello World应用程序,我试图在Kubernetes中运行它,但是没有运气,什么也没有出现。

Dockerfile:

FROM centos:7

RUN  yum install httpd -y

RUN echo "Hello World" > /var/www/html/index.html

RUN chown -R apache:apache /var/www/html

EXPOSE 80

CMD  [ "/usr/sbin/httpd", "-D", "FOREGROUND" ]

Kubernetes YAML:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ingress-test
  labels:
    app: hello-world
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello-world
  template:
    metadata:
      labels:
        app: hello-world
    spec:
      containers:
      - name: helloworld
        image: 56789/world:v1
        ports:
        - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: hello-world
spec:
  selector:
    app: hello-world
  ports:
  - protocol: "TCP"
    port: 80
    targetPort: 80
  type: LoadBalancer

1 个答案:

答案 0 :(得分:1)

当您运行一个简单的hello world应用程序时,我假设您可能会使用minikube,而您并未在云中进行此操作。

删除服务并创建服务,如下所示。现在您可以访问您的应用程序import sys, os def mainmenu(): print() print("________________________________________________________") print("Hello and welcome to the password checker and generator.") print("________________________________________________________") input() print("Mainmenu") input() print("Press 1 to Check a password.") print("Press 2 to Generate a password.") print("Press 3 to quit.") UserOp = int(input("What is your choice?:")) if UserOp == 1: checkpass() elif UserOp == 2: generatepass() elif UserOp == 3: sys.exit(0) else: print("This option is seen to be invalid.") mainmenu() def checkpass(): print() print("You have chosen to check a password.") def generatepass(): print() print("You have chosen to generate a password.")

http://<minikube-ip>:30080

apiVersion: v1 kind: Service metadata: name: hello-world spec: selector: app: hello-world ports: - protocol: "TCP" port: 80 targetPort: 30080 type: NodePort 服务适用于AWS / Azure / Google Cloud等云,因此它无法在本地minikube中创建任何LoadBalancer。有一些变通办法可以通过使用外部IP使其起作用,您可以在此处找到https://kubernetes.io/docs/concepts/services-networking/service/


要调试此问题,请假设Pod正在运行并且相应的端口已打开,请创建ClusterIP服务。

LoadBalancer

现在,首先检查您的应用程序是否可以在群集中访问。

apiVersion: v1
kind: Service
metadata:
  name: hello-world
spec:
  selector:
    app: hello-world
  ports:
  - protocol: "TCP"
    port: 80
    targetPort: 80
  type: ClusterIP

如果它不起作用,则表明Pod本身有问题!