service几种访问类型(集群外负载均衡访问LoadBalancer , 集群内访问ClusterIP,VPC内网负载均衡LoadBalancer ,集群外访问NodePort)

时间:2022-01-26 10:31:00

一、集群外访问(负载均衡)

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
status:
  loadBalancer:
    ingress:
    - {}

二、k8s集群内访问

kind: Service
apiVersion: v1
spec:
  ports:
    - protocol: TCP
      port: 4321
      targetPort: 8080
  type: ClusterIP
  externalIPs: #集群内互访
    - 172.30.10.201
     - 172.30.10.42
  sessionAffinity: None
status:
loadBalancer: {}

三、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
status:
  loadBalancer:
    ingress:
     - ip: 172.30.12.33

四、集群外访问,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
status:
  loadBalancer: {}