【问题标题】:Can't get to web page hello-kubernetes using KIND, how do I get Kind to bind to localhost:80?无法使用 KIND 访问网页 hello-kubernetes,如何让 Kind 绑定到 localhost:80?
【发布时间】:2020-09-09 04:51:43
【问题描述】:

由于 kind 的嵌套性质,我不知道要使用哪个端口或如何配置它,因此我只需键入 localhost 即可访问它。

种类 YAML:

kind: Cluster
apiVersion: kind.sigs.k8s.io/v1alpha3
nodes:
- role: control-plane
- role: worker
- role: worker
- role: worker
- role: worker

也试过了:

kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  extraPortMappings:
  - containerPort: 8080
    hostPort: 80
    protocol: TCP
- role: worker
- role: worker
- role: worker
- role: worker

启动节点:kind create cluster --config ~/go/kindconfigs/kind-config.yaml

工作 YAML:

# hello-kubernetes.yaml
apiVersion: v1
kind: Service
metadata:
  name: hello-kubernetes
spec:
  type: LoadBalancer
  ports:
  - port: 80
    targetPort: 8080
  selector:
    app: hello-kubernetes
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-kubernetes
spec:
  replicas: 3
  selector:
    matchLabels:
      app: hello-kubernetes
  template:
    metadata:
      labels:
        app: hello-kubernetes
    spec:
      containers:
      - name: hello-kubernetes
        image: paulbouwer/hello-kubernetes:1.8
        ports:
        - containerPort: 8080

运行它:kubectl apply -f ~/go/kindconfigs/hello-kubernetes.yaml

【问题讨论】:

  • kubectl port-forward有什么原因不能用吗?通过这种方式,您将获得更准确的表示。如果没有,extraPortMappings 是要走的路。

标签: kubernetes kind


【解决方案1】:

docs引用你需要使用extraPortMappings来允许本地主机通过端口80向hello-kubernetes发出请求

cat <<EOF | kind create cluster --config=-
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  extraPortMappings:
  - containerPort: 8080
    hostPort: 80
    protocol: TCP
  
EOF

部署也需要更改

apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-kubernetes
spec:
  replicas: 3
  selector:
    matchLabels:
      app: hello-kubernetes
  template:
    metadata:
      labels:
        app: hello-kubernetes
    spec:
      containers:
      - name: hello-kubernetes
        image: paulbouwer/hello-kubernetes:1.8
        securityContext:
          capabilities:
            drop:
              - ALL
            add:
              - NET_BIND_SERVICE
        ports:
        - containerPort: 8080
          hostPort: 80

通过以上配置,您可以使用localhost:80 访问hello-kubernetes 应用程序。

注意一:如果没有上述配置,您可以通过NODEIP:NODEPORT访问它

获取NODEIP

kubectl get nodes -o wide

获取NODEPORT

kubectl describe svc hello-kubernetes

Node-2:LoadBalancer 类型服务仅适用于受支持的云环境。这就是为什么在您的系统上本地运行的 kind 集群中,您需要使用NODEIP NODEPORT 来访问它。

注3:您可以尝试metallb 与kind 一起使LoadBalancer 类型服务工作。这应该可以解决EXTERNAL-IP 未决问题。

【讨论】:

  • 不走运,我已经尝试过该配置和 IP 地址的变体。仅供参考,外部 IP 为空白,仅显示内部 IP。
  • 部署中也需要进行一些更改..检查更新的答案。 EXTERNAL-IP 将为空,因为您不在受支持的云提供商上
  • 所以我想对于这种特殊情况我只能有 1 个节点。
  • 不支持负载均衡吗?
猜你喜欢
  • 1970-01-01
  • 2022-01-17
  • 2022-12-10
  • 2020-11-12
  • 2015-08-18
  • 1970-01-01
  • 2023-02-11
  • 2021-05-23
  • 1970-01-01
相关资源
最近更新 更多