我有一个运行 ubuntu 22.04 的 Azure VM,我在 VM 内安装了 kubernetes 集群作为 docker 容器,并在 k8s 上部署了一些 pod。
现在我想通过虚拟机的公共IP访问一些pod。
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
extraPortMappings:
- containerPort: 8080
hostPort: 31190
protocol: TCP
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
但是我仍然无法从外部访问 POD 例如,http://<
您能指导我正确设置吗?
请注意,我尝试进行此设置是出于学习目的,这不是任何成熟的项目。
谢谢
嗨Minesh,您的步骤是正确的,但这似乎正在解决问题。 我们可以逐步匹配,看看您的设置是否一切正常。假设您已启用 docker 并在虚拟机上安装 Kind
创建带有端口映射的 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
使用此配置创建 Kind 集群
kind create cluster --name my-cluster --config kind-config.yaml
部署nginx
kubectl create deployment nginx --image=nginx
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
测试虚拟机的连接
此外,我建议您检查一次以下参数以获得更好的清晰度
首先是你的 pod 日志 例子-
kubectl logs nginx-76d6c9b8c-nzhhg
再次检查您的 nsg 规则列表
az network nsg rule list --resource-group arkorg --nsg-name LinuxVM-nsg --query "[?destinationPortRange=='31190']"
检查您的 IP 表
如果规则不存在,请手动添加它们以允许转发此端口的流量。
sudo iptables -A INPUT -p tcp --dport 31190 -j ACCEPT
sudo iptables -A FORWARD -p tcp --dport 31190 -j ACCEPT
或者检查docker是否正确绑定了端口
docker ps | grep my-cluster-control-plane