Ingress 服务的 404的测试探索

摘要

近期对ingress的域名解析进行了初步分析,现将相关测试结果进行以下备忘

一旦配置了HOSTS,则IP就会失效
NodePort 和 Ingress 可以同时并存并对外提供访问服务
宽松模式下,假如不同的Pods内的网页内容不同会导致页面404的报错
严格模式下,只用执行以下3类,才能成功访问页面
curl 192.168.255.31/
curl http://192.168.255.31/
curl http://192.168.255.31
当使用严格模式(Exact) + 域名解析(host) + 地址重定向(rewrite-target)时候,结果只会访问index.html的内容。
并不能访问 info.html + info1.html的内容

Pods Preparation

---
apiVersion: v1
kind: Pod
metadata:
  labels:
    run: many-pods
  name: mypods-many
spec:
  terminationGracePeriodSeconds: 0
  containers:
  - name: myweb-1       #  具有先后顺序,建议name放在开头做标记
    image: httpd
    imagePullPolicy: IfNotPresent
    ports:
    - containerPort: 80
      name: http
      protocol: TCP
  - name: memcache
    image: memcache
    imagePullPolicy: IfNotPresent
  restartPolicy: Always

创建多个相同模板的Pod

for i in {1..2}; do sed -e "s,mypods-many,mypods-many-${i}," -e "s,myweb-1,c-${i}," 2-pods-many.yml | kubectl apply -f -; done

kubectl get pod | grep many

不同Pods的文件环境准备

pod name

index.html

info.html or info1.html

mypods-many

httpd is running .

hello world in info.html

mypods-many-1

httpd is running mypods-many-1 .

info1 in mypods-many-1 + info1.html

mypods-many-2

httpd is running mypods-many-2 .

# 进入容器,执行文件创建命令
kubectl exec -it mypods-many-1 -- sh
echo 'info1 in mypods-many-1 + info1.html' >> /var/www/info1.html

Ingress Prepare

通过使用资源文件创建ingress

宽松模式 + 严格模式

---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  creationTimestamp: null
  name: mypods-many-ingress
spec:
  ingressClassName: nginx
  rules:
  - host: www.mypods-many.com
    http:
      paths:
      - backend:
          service:
            name: mypods-many-svc
            port:
              number: 80
        #path: /mypods-many
        path: /             # 路径定义
        # pathType: Exact    # 严格模式
        pathType: Prefix    # 宽松模式

通过使用命令创建ingress模板

kubectl create ingress mypods-many-ingress --rule=www.mypods-many.com/mypods-many=mypods-many-svc:80 --class=nginx --dry-run=client -o yaml

Ingress resource

kubectl api-resources | grep ingress
# 查看ingress class
kubectl get ingressclasses

kubectl describe ingressclasses
# 在ingress controller pod内查看class
kubectl get pod  -n ingress-nginx | grep controller
# k8s.io/ingress-nginx
kubectl describe pod ingress-nginx-controller-5664857866-g7dn9 -n ingress-nginx

NodePort Prepare

---
apiVersion: v1
kind: Service
metadata:
  labels:
    run: many-pods
  name: mypods-many-svc
spec:
  ports:
  - port: 80
    protocol: TCP
    targetPort: http
    nodePort: 30080
  selector:
    run: many-pods
  type: NodePort

DNS 服务

# 在client的服务器执行
echo "192.168.255.31   www.mypods-many.com" >> /etc/hosts

ipvsadm 查看转发规则(轮询)

ipvsadm
kubectl get pod -o wide | grep many

猜你喜欢

转载自blog.csdn.net/m0_59267075/article/details/129482991