在看这一篇之前,如果不了解 Ingress 在 K8s 当中的职责,建议看之前的一篇针对旧版本 Ingress 的部署搭建,在开头会提到它的一些简介Kubernetes - Ingress HTTP 负载搭建部署解决方案_放羊的牧码的博客-CSDN博客
开始表演
1、kubeasz 一键安装部署 Ingress
bash
kubectl apply -f https://raw.githubusercontent.com/qist/k8s/main/k8s-yaml/ingress-nginx/ingress-nginx-ipv4.yaml
- 如果因为网络问题,建议先下载 ingress-nginx-ipv4.yaml 到 Linux 里面先,然后再执行即可
- 如果之前对 kubeasz 脚手架没了解,建议可以先看看这篇文章噢~ Kubernetes - 一键安装部署 K8S(附:Kubernetes Dashboard)-CSDN博客文章浏览阅读792次。Kubernetes - 一键安装部署 K8S(附:Kubernetes Dashboard)https://lux-sun.blog.csdn.net/article/details/134000358
2、安装我们的服务应用 Service 对应的 Ingress
bash
# 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
bash
kubectl apply -f nginx-ingress-controller-app-service.yaml
3、稍做分析
- 附:app service yaml
bash
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 不变的情况下~欢迎评论哈!