ITPub博客

首页 > IT职业 > IT生活 > Openshift Service不仅仅能负载Pod,它还能负载VM等

Openshift Service不仅仅能负载Pod,它还能负载VM等

IT生活 作者:sealee 时间:2021-09-09 10:20:42 0 删除 编辑
  • Service是Openshift最核心的概念,它可以为一组具有相同功能的Pod提供一个统一的入口,并且将请求负载均衡分发到后端的各个Pod应用上。同时Service在集群下的IP是不变的,保证了请求的可靠性。

  • Service仅仅只能负载一组Pod吗?No。它还可以对接VM的服务,甚至是物理机的服务。

Service负载Pod

apiVersion: v1
kind: Service
metadata:
  name: nginx
spec:
  selector:
    app: nginx  type: NodePort
  ports:
    - port: 8080
      name: nginx
      protocol: TCP
      targetPort: 8080
      nodePort: 38080
  • selector:Label选择器,将选择指定Label的Pod作为被负载Pod

  • type:Service的类型,指定Service的访问方式,默认为ClusterIP。

    • ClusterIP:虚拟的服务IP地址,该IP可以被集群下的Pod访问

    • NodePort:使用宿主机的端口,通过访问任意Node的对应端口,就能访问Service的端口

    • LoadBalancer:使用外部负载均衡器完成到服务的负载分发,需要在.spec.status.loadBalancer指定外部负载均衡器的IP地址,同时定义nodePort和clusterIP。

  • port:Service监听的端口号

  • targetPort:需要转发到后端Pod的端口号

  • nodePort:当type=NodePort时,指定映射到物理机的端口(范围30000-32767)

Service负载集群外部Service(可以是VM,物理机等)

  1. 创建一个无Label Selector的Service(即无法选择后端的Pod,系统不会自动创建Endpoint,需要手动创建Endpoint)

apiVersion: v1
kind: Service
metadata:
  name: nginx-outspec:
  ports:
    - port: 80
      name: nginx-out
      protocol: TCP
      targetPort: 8080
  type: ClusterIP
  1. 创建Endpoint指向后端服务(该Endpoint的name必须与Service的name相同)

kind: EndpointsapiVersion: v1metadata:  name: nginx-outsubsets:  - addresses:      - ip: 192.168.0.6
    ports:      - port: 8080

集群中的pod访问Service nginx-out:80,请求最终会被指向192.168.0.6:8080。


图片描述

Service指向外部服务

实战:将外部的Jenkins用Openshift的Service接管,并创建Route来访问Jenkins服务

  • Jenkins服务: 192.168.0.6:8080

  • route的Hostname:jenkins.apps.openshift.com

  1. 创建project jenkins

oc new-project jenkins
  1. 创建jenkinsvm Service

apiVersion: v1
kind: Service
metadata:
  name: jenkinsvm
spec:
  ports:
    - port: 8080
      name: jenkinsvm
      protocol: TCP
      targetPort: 8080  type: ClusterIP
  1. 创建Endpoints

kind: EndpointsapiVersion: v1metadata:  name: nginx-outsubsets:  - addresses:      - ip: 192.168.0.6
    ports:      - port: 8080
  1. 创建Route

apiVersion: v1kind: Routemetadata:  name: jenkinsspec:  host: jenkins.apps.openshift.com
  port: 
    targetPort: jenkinsvm
  to:    kind: Service
    name: jenkinsvm
    weight: 100

图片描述

使用Openshift的Service访问的集群外部的Jenkins服务



作者:潘晓华Michael
链接:https://www.jianshu.com/p/cf1bb9af9661


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3355/viewspace-2820639/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-30

  • 博文量
    82
  • 访问量
    620