在看这一篇之前,如果不了解 Ingress 在 K8s 当中的职责,建议看之前的一篇针对旧版本 Ingress 的部署搭建,在开头会提到它的一些简介Kubernetes - Ingress HTTP 负载搭建部署解决方案_放羊的牧码的博客-CSDN博客
1、kubeasz 一键安装部署 Ingress
kubectl apply -f https://raw.githubusercontent.com/qist/k8s/main/k8s-yaml/ingress-nginx/ingress-nginx-ipv4.yaml
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
- 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: {}
- # 通过命令查询随机生成的端口号
- kubectl get svc