kubernetes 入门系列之 Service 暴露服务的四种方式 – 21运维
通知: .-...

kubernetes 入门系列之 Service 暴露服务的四种方式

K8S 21运维 284浏览

kubernetes service暴露服务到目前为止,一共有以下几种方法

1,ClusterIP 方式
kubernetes 默认就是这种方式,是集群内部访问的方式。仅限于集群内部访问。

spec:
  clusterIP: 10.0.0.1
  ports:
  - name: http

2,NodePort 方式

NodePort方式主要通过每个节点IP加端口的形式暴露端口,访问任意一个node ip都可以访问到(前提没有指定node调度策略),其中端口可以通过apiserver的配置文件可以看到端口暴露范围。
访问方式:protocol://:.

heapster 10.3.129.117 80:30003/TCP 14d

apiVersion: v1
kind: Service
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"creationTimestamp":"2017-05-24T06:38:16Z","labels":{"kubernetes.io/name":"heapster","plugin":"heapster"},"name":"heapster","namespace":"kube-system","resourceVersion":"173906247","selfLink":"/api/v1/namespaces/kube-system/services/heapster","uid":"91470fbb-404b-11e7-ba41-5254eec04736"},"spec":{"clusterIP":"10.3.129.117","externalTrafficPolicy":"Cluster","ports":[{"nodePort":30003,"port":80,"protocol":"TCP","targetPort":8082}],"selector":{"k8s-app":"heapster"},"sessionAffinity":"None","type":"NodePort"},"status":{"loadBalancer":{}}}
  creationTimestamp: 2018-07-25T03:22:01Z
  labels:
    kubernetes.io/name: heapster
    plugin: heapster
  name: heapster
  namespace: kube-system
  resourceVersion: "789489505"
  selfLink: /api/v1/namespaces/kube-system/services/heapster
  uid: e56886a2-8fb9-11e8-acd2-5254171bf8db
spec:
  clusterIP: 10.3.129.117
  externalTrafficPolicy: Cluster
  ports:
  - nodePort: 30003
    port: 80
    protocol: TCP
    targetPort: 8082
  selector:
    k8s-app: heapster
  sessionAffinity: None
  type: NodePort
status:
  loadBalancer: {}

3,LoadBalancer 方式

这种方式主要是利用其他第三方的LB暴露服务的,阿里云或者亚马逊的LB等等

kind: Service
apiVersion: v1
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9376
  clusterIP: 10.0.171.239
  loadBalancerIP: 78.11.24.19
  type: LoadBalancer
status:
  loadBalancer:
    ingress:
    - ip: 146.148.47.155

4,ExternalName 方式

这种方式主要是通过CNAME实现的,在kubernetes 1.7.x以及以上才支持这种方式,例如:

kind: Service
apiVersion: v1
metadata:
  name: my-service
  namespace: prod
spec:
  type: ExternalName
  externalName: my.database.example.com

访问时,kube-dns服务直接解析到指定的Cname:my.database.example.com这个域名上

原文转载于 kubernetes Service 暴露服务的几种方式:https://blog.csdn.net/qq_21816375/article/details/81507196 并略作修改。

转载请注明:21运维 » kubernetes 入门系列之 Service 暴露服务的四种方式