service几种访问类型(集群外负载均衡访问LoadBalancer , 集群内访问ClusterIP,集群内访问headless ClusterIP,VPC内网负载均衡LoadBalancer ,集群外访问NodePort)
一、集群外访问(负载均衡)
kind: Service
apiVersion: v1
spec:
ports:
- protocol: TCP
port: 4341
targetPort: 8080
nodePort: 30875
type: LoadBalancer
externalIPs: #外部负载均衡
- 172.30.10.4
- 172.30.10.3
sessionAffinity: None
externalTrafficPolicy: Cluster
二、k8s集群内访问
# clusterip service
kind: Service apiVersion: v1 spec: clusterIP: 10.43.22.26 (如果不填,会自动添加) ports: - protocol: TCP port: 4321 targetPort: 8080 type: ClusterIP externalIPs: #集群内互访 可选 - 172.30.10.201 - 172.30.10.42
# headless service
spec: clusterIP: None ports: - name: tcp-postgresql port: 5432 protocol: TCP targetPort: tcp-postgresql selector: app: postgresql type: ClusterIP
三、vpc内网负载均衡
kind: Service
apiVersion: v1
metadata:
annotations:
kubernetes.io/elb.class: elasticity #表示选择的是经典负载均衡实例
kubernetes.io/elb.vpc.id: 0e86e303-7a82-4e03-a435-9be0c4771c93 #负载均衡所在vpc的ID
spec:
ports:
- name: cce-service-0
protocol: TCP
port: 4321
targetPort: 8080
nodePort: 31118
type: LoadBalancer
loadBalancerIP: 172.30.12.33
sessionAffinity: None
externalTrafficPolicy: Cluster
四、集群外访问,NodePort
apiVersion: v1
kind: Service
metadata:
labels:
app: prometheus-operator-operator
name: my-release-prometheus-oper-operator
namespace: default
spec:
ports:
- name: http
port: 8080
protocol: TCP
targetPort: http
nodePort: 30080
selector:
app: prometheus-operator-operator
sessionAffinity: None
type: NodePort
#clusterIP: None 这里不能设置为空,因为nodeport需要映射到clusterip ,因此这里要么不设置clusterIP,要么手动配ip