k8s ingress-nginx网络无法访问到容器问题

安装ingress-nginx

参照这个网站: Installation Guide - NGINX Ingress Controller (kubernetes.github.io)
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.1/deploy/static/provider/cloud/deploy.yaml
我使用的是 with kubectl apply, using YAML manifests; 使用yaml的方式部署

注意在线的方式部署,需要将yaml中的image先down下来。如果down不下来,可以使用国内的一些镜像仓库

准备容器主要是学习B站上黑马的课程中的文件
Kubernetes(K8S) 入门进阶实战完整教程,黑马程序员K8S全套教程(基础+高级)_哔哩哔哩_bilibili

创建tomcat-nginx.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: dev
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx-pod
  template:
    metadata:
      labels:
        app: nginx-pod
    spec:
      containers:
      - name: nginx
        image: nginx:1.17.1
        ports:
        - containerPort: 80

---

apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-deployment
  namespace: dev
spec:
  replicas: 3
  selector:
    matchLabels:
      app: tomcat-pod
  template:
    metadata:
      labels:
        app: tomcat-pod
    spec:
      containers:
      - name: tomcat
        image: tomcat:8.5-jre10-slim
        ports:
        - containerPort: 8080

---

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
  namespace: dev
spec:
  selector:
    app: nginx-pod
  clusterIP: None
  type: ClusterIP
  ports:
  - port: 80
    targetPort: 80

---

apiVersion: v1
kind: Service
metadata:
  name: tomcat-service
  namespace: dev
spec:
  selector:
    app: tomcat-pod
  clusterIP: None
  type: ClusterIP
  ports:
  - port: 8080
    targetPort: 8080
# 创建
[root@master ~]# kubectl create -f tomcat-nginx.yaml

# 查看
[root@master ~]# kubectl get svc -n dev
NAME             TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE
nginx-service    ClusterIP   None                 80/TCP     48s
tomcat-service   ClusterIP   None                 8080/TCP   48s

Http代理

创建ingress-http.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-http
  namespace: dev
spec:
  rules:
  - host: nginx.itheima.com
    http:
      paths:
      - path: /
        backend:
          serviceName: nginx-service
          servicePort: 80
  - host: tomcat.itheima.com
    http:
      paths:
      - path: /
        backend:
          serviceName: tomcat-service
          servicePort: 8080

问题: 通过上述配置好之后,通过浏览器,不能访问?

image.png

开始分析问题

  1. 检查 ingress 服务
 kubectl get service -n ingress-nginx
image.png
  1. 检查ingress 的pod信息
kubectl get pods -n ingress-nginx
image.png
  1. 检查应用服务,主要工作空间
kubectl get service -n dev
image.png
  1. 检查应用的pod
kubectl get pods  -n dev -o wide
image.png
  1. 验证容器服务的正确性


    image.png

通过ingress service来查看NGINX也是正常的,不知道哪里问


image.png

最后在一个b站的视频上看到,要把这个配置给注释掉,也就是去掉,问题解决.

externalTrafficPolicy: Local

你可能感兴趣的:(k8s ingress-nginx网络无法访问到容器问题)