需要帮助公开在 Azure VM 下运行的 Kubernetes pod

问题描述 投票:0回答:1

需要帮助公开在 Azure VM 下运行的 K8S pod >> 类型的 k8s docker 容器。

我有一个运行 ubuntu 22.04 的 Azure VM,我在 VM 内安装了 kubernetes 集群作为 docker 容器,并在 k8s 上部署了一些 pod。

现在我想通过虚拟机的公共IP访问一些pod。

我尝试了以下步骤:

  1. 使用如下所示重新创建 Kubernetes 集群。我的 Pod 正在端口 8080 上运行,我想将其公开到端口 31190。
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  extraPortMappings:
  - containerPort: 8080
    hostPort: 31190
    protocol: TCP
  1. 对于所需 pod 的 k8s 清单文件,我指定了服务类型 = NodeType 并分配了端口
apiVersion: v1
kind: Service
metadata:
  name: keycloak
  labels:
    app: keycloak
spec:
  type: NodePort  # Specifies the service type as NodePort
  selector:
    app: keycloak
  ports:
    - port: 8080             # The port on which the service is exposed internally
      targetPort: 8080        # The port on the container where the application is running
      nodePort: 31190         # The specific NodePort to expose the service externally 
  1. Azure VM >> NSG 防火墙,允许以下流量。
  • HTTP (TCP 80)
  • TCP 端口 31190

但是我仍然无法从外部访问 POD 例如,http://<>:31190

您能指导我正确设置吗?

请注意,我尝试进行此设置是出于学习目的,这不是任何成熟的项目。

谢谢

azure docker kubernetes ubuntu-22.04 kind
1个回答
0
投票

Minesh,您的步骤是正确的,但这似乎正在解决问题。 我们可以逐步匹配,看看您的设置是否一切正常。假设您已启用 docker 并在虚拟机上安装 Kind

enter image description here

创建带有端口映射的 Kind 配置文件。

cat <<EOF > kind-config.yaml
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
  extraPortMappings:
  - containerPort: 8080
    hostPort: 31190
    protocol: TCP
EOF

enter image description here

使用此配置创建 Kind 集群

kind create cluster --name my-cluster --config kind-config.yaml

enter image description here

部署nginx

kubectl create deployment nginx --image=nginx

enter image description here

nginx 服务

cat <<EOF > nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: NodePort
  selector:
    app: nginx
  ports:
  - port: 80
    targetPort: 80
    nodePort: 31190
EOF

enter image description here

测试虚拟机的连接

enter image description here

此外,我建议您检查一次以下参数以获得更好的清晰度

首先是你的 pod 日志 例子-

kubectl logs nginx-76d6c9b8c-nzhhg

再次检查您的 nsg 规则列表

az network nsg rule list --resource-group arkorg --nsg-name LinuxVM-nsg --query "[?destinationPortRange=='31190']"

enter image description here

检查您的 IP 表

enter image description here

如果规则不存在,请手动添加它们以允许转发此端口的流量。

sudo iptables -A INPUT -p tcp --dport 31190 -j ACCEPT
sudo iptables -A FORWARD -p tcp --dport 31190 -j ACCEPT

enter image description here

或者检查docker是否正确绑定了端口

docker ps | grep my-cluster-control-plane

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.