开始表演

1、kubeasz 一键安装部署 Ingress

kubectl apply -f https://raw.githubusercontent.com/qist/k8s/main/k8s-yaml/ingress-nginx/ingress-nginx-ipv4.yaml

Kubernetes - Ingress HTTP 负载搭建部署解决方案(新版本v1.21+)-LMLPHP

Kubernetes - Ingress HTTP 负载搭建部署解决方案(新版本v1.21+)-LMLPHP

Kubernetes - Ingress HTTP 负载搭建部署解决方案(新版本v1.21+)-LMLPHP

2、安装我们的服务应用 Service 对应的 Ingress

# nginx-ingress-controller-app-service.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: ingress-app
  namespace: default # 跟我们的应用 pod 在一个 namespace 即可
  annotations: 
    kubernetes.io/ingress.class: "nginx"
spec:
  rules:
  - host: api.app.com # 外部可以访问的域名
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: app-server # app service name
            port: 
              number: 38080 # app service port
kubectl apply -f nginx-ingress-controller-app-service.yaml

3、稍做分析

  • 附:app service yaml
kind: Service
apiVersion: v1
metadata:
  name: app-server
  namespace: default
  uid: e462e330-ea68-41ea-b351-71680ea4c685
  resourceVersion: '576970'
  creationTimestamp: '2023-10-26T11:07:01Z'
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: >
      {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"app-server","namespace":"default"},"spec":{"ports":[{"nodePort":38080,"port":38080,"protocol":"TCP","targetPort":38080}],"selector":{"app":"app-server"},"type":"NodePort"}}
  managedFields:
    - manager: kubectl-client-side-apply
      operation: Update
      apiVersion: v1
      time: '2023-10-26T11:07:01Z'
      fieldsType: FieldsV1
      fieldsV1:
        f:metadata:
          f:annotations:
            .: {}
            f:kubectl.kubernetes.io/last-applied-configuration: {}
        f:spec:
          f:externalTrafficPolicy: {}
          f:internalTrafficPolicy: {}
          f:ports: {}
          f:selector: {}
          f:sessionAffinity: {}
    - manager: dashboard
      operation: Update
      apiVersion: v1
      time: '2023-10-28T10:28:57Z'
      fieldsType: FieldsV1
      fieldsV1:
        f:spec:
          f:ports:
            k:{"port":12345,"protocol":"TCP"}:
              .: {}
              f:port: {}
              f:protocol: {}
              f:targetPort: {}
          f:type: {}
spec:
  ports:
    - protocol: TCP
      port: 12345
      targetPort: 38080
  selector:
    app: app-server
  clusterIP: 10.68.184.146
  clusterIPs:
    - 10.68.184.146
  type: ClusterIP
  sessionAffinity: None
  ipFamilies:
    - IPv4
  ipFamilyPolicy: SingleStack
  internalTrafficPolicy: Cluster
status:
  loadBalancer: {}
  • 重点关注 spec.ports 部分,service 这里的 targetPort = app port,port =  ingress port-number
  • 最后,可以用 api.app.com:80 访问即可命中这个程序~ 为什么这里就可以用 80 来访问呢?因为 Ingress-Controller 一键安装完后默认开放了 80、443、8443 端口噢!
  • 彩蛋:如果外部想用 port 12345 来访问 pod 该如何修改配置呢?!前提 38080 不变的情况下~欢迎评论哈!
10-28 20:43