headless service with router

本文讲述,如果service是headless的情况下,k8s/ocp ingress如何处理,和普通headless有什么区别。

演示视频

结论是,不管是是不是headless service, openshift都会找到最终的pod ip,然后在ingress/router/haproxy里面,修改配置,让流量直接导向pod ip。

# 这里是演示环境部署脚本
cat << EOF > headless.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: slb-001
spec:
  replicas: 1
  selector: 
    matchLabels: 
      pod: slb-001
  template: 
    metadata: 
      labels: 
        pod: slb-001
    spec:
      restartPolicy: Always
      
      containers:
      - name: slb-001-pg
        image: registry.redhat.ren:5443/docker.io/etherpad/etherpad:latest
        imagePullPolicy: IfNotPresent

---
apiVersion: v1
kind: Service
metadata:
  name: slb-001-service
spec:
  selector:
    pod: slb-001
  ports:
    - port: 9001
      protocol: TCP
      targetPort: 9001
---
apiVersion: v1
kind: Service
metadata:
  name: slb-002-service
spec:
  selector:
    pod: slb-001
  clusterIP: None
  ports:
    - port: 9001
      protocol: TCP
      targetPort: 9001

---
kind: Route
apiVersion: route.openshift.io/v1
metadata:
  name: slb-001
spec:
  to:
    kind: Service
    name: slb-001-service
  port:
    targetPort: 9001
---
kind: Route
apiVersion: route.openshift.io/v1
metadata:
  name: slb-002
spec:
  to:
    kind: Service
    name: slb-002-service
  port:
    targetPort: 9001
EOF
oc apply -n demo -f headless.yaml