将K8S部署应用提供给外部访问一般有三种方式:
- NodePort 暴露端口到节点,提供了集群外部访问的入口
- LoadBalancer 需要负载均衡器(通常都需要云服务商提供,裸机可以安装 METALLB 测试)
- Ingress 统一管理 svc的外部访问,用于实现用域名的方式访问k8s内部应用,常用类型
Treafik、Nginx
- 相对 NodePort 来说,Ingress 就实现外部访问的统一管理,类型配置 nginx.conf 一样,根据 域名、路由等转发到不同服务上
- 可以随时更改配置规则并生效,方便统一管理
NodePort 简单示例:
- spec.ports.nodePort 可以实现一个svc的外部访问配置
apiVersion: v1
kind: Service
metadata:labels:expose: "true"app: device-infoprovider: xiaoshuversion: "1.0"group: com.xiaoshuname: device-info-1namespace: xiaoshu
spec:type: NodePortports:- name: httpport: 30802 # 服务端口targetPort: 8300 # 容器端口protocol: TCPnodePort: 30802 # 外部访问端口selector:app: device-infoprovider: xiaoshuversion: "1.0"group: com.xiaoshu
Ingress 简单示例:
准备了两个简单的jar 服务,服务一 device-info-1 服务端口8001、服务二 device-info-2 服务端口8002
@ApiOperation(value = "测试接口")
@GetMapping("/server")
public String test(){return "服务一:192.168.2.207";
}
@ApiOperation(value = "测试接口")
@GetMapping("/server")
public String test(){return "服务二:192.168.2.208";
}
yml
apiVersion: apps/v1
kind: Deployment
metadata:labels:app: device-infoprovider: xiaoshuversion: "1.0"group: com.xiaoshuname: device-info-1namespace: xiaoshu
spec:replicas: 1selector:matchLabels:app: device-infoprovider: xiaoshuversion: "1.0"group: com.xiaoshutemplate:metadata:labels:app: device-infoprovider: xiaoshuversion: "1.0"group: com.xiaoshuspec:containers:- name: device-info-1image: core.harbor.domain/jar/device-info-1:v1nodeName: k8s-1
---
apiVersion: v1
kind: Service
metadata:labels:expose: "true"app: device-infoprovider: xiaoshuversion: "1.0"group: com.xiaoshuname: device-info-1namespace: xiaoshu
spec:type: ClusterIPports:- name: httpport: 8001 # 服务端口targetPort: 8300 # 容器端口protocol: TCPselector:app: device-infoprovider: xiaoshuversion: "1.0"group: com.xiaoshu
apiVersion: apps/v1
kind: Deployment
metadata:labels:app: device-info-2provider: xiaoshuversion: "1.0"group: com.xiaoshuname: device-info-2namespace: xiaoshu
spec:replicas: 1selector:matchLabels:app: device-info-2provider: xiaoshuversion: "1.0"group: com.xiaoshutemplate:metadata:labels:app: device-info-2provider: xiaoshuversion: "1.0"group: com.xiaoshuspec:containers:- name: device-info-2image: core.harbor.domain/jar/device-info-2:v1nodeName: k8s-1
---
apiVersion: v1
kind: Service
metadata:labels:expose: "true"app: device-info-2provider: xiaoshuversion: "1.0"group: com.xiaoshuname: device-info-2namespace: xiaoshu
spec:type: ClusterIPports:- name: httpport: 8002 targetPort: 8300 # 容器端口protocol: TCPselector:app: device-info-2provider: xiaoshuversion: "1.0"group: com.xiaoshu
Ingress yml
- 根据
域名
转发到不同的 服务上- 注意labels 配置,与svc可以匹配
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: device-info-ingressnamespace: xiaoshuannotations:#kubernetes.io/ingress.class: "nginx"#nginx.ingress.kubernetes.io/rewrite-target: / #路径重写,需要安装NGINX Ingress控制器生效labels:app: device-infoprovider: xiaoshuversion: "1.0"group: com.xiaoshu
spec:rules:- host: device.info.ingress1http:paths:- backend:service:name: device-info-1port:number: 8001 #服务端口path: /device-info/serverpathType: Prefix - host: device.info.ingress2http:paths:- backend:service:name: device-info-2port:number: 8002 #服务端口path: /device-info/serverpathType: Prefix