Openshift Service不僅僅能負(fù)載Pod,它還能負(fù)載VM等外部服務(wù)

  • Service是Openshift最核心的概念,它可以為一組具有相同功能的Pod提供一個統(tǒng)一的入口,并且將請求負(fù)載均衡分發(fā)到后端的各個Pod應(yīng)用上。同時Service在集群下的IP是不變的,保證了請求的可靠性。
  • Service僅僅只能負(fù)載一組Pod嗎?No。它還可以對接VM的服務(wù),甚至是物理機(jī)的服務(wù)。

Service負(fù)載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作為被負(fù)載Pod
  • type:Service的類型,指定Service的訪問方式,默認(rèn)為ClusterIP。
    • ClusterIP:虛擬的服務(wù)IP地址,該IP可以被集群下的Pod訪問
    • NodePort:使用宿主機(jī)的端口,通過訪問任意Node的對應(yīng)端口,就能訪問Service的端口
    • LoadBalancer:使用外部負(fù)載均衡器完成到服務(wù)的負(fù)載分發(fā),需要在.spec.status.loadBalancer指定外部負(fù)載均衡器的IP地址,同時定義nodePort和clusterIP。
  • port:Service監(jiān)聽的端口號
  • targetPort:需要轉(zhuǎn)發(fā)到后端Pod的端口號
  • nodePort:當(dāng)type=NodePort時,指定映射到物理機(jī)的端口(范圍30000-32767)

Service負(fù)載集群外部Service(可以是VM,物理機(jī)等)

  1. 創(chuàng)建一個無Label Selector的Service(即無法選擇后端的Pod,系統(tǒng)不會自動創(chuàng)建Endpoint,需要手動創(chuàng)建Endpoint)
apiVersion: v1
kind: Service
metadata:
  name: nginx-out
spec:
  ports:
    - port: 80
      name: nginx-out
      protocol: TCP
      targetPort: 8080
  type: ClusterIP
  1. 創(chuàng)建Endpoint指向后端服務(wù)(該Endpoint的name必須與Service的name相同)
kind: Endpoints
apiVersion: v1
metadata:
  name: nginx-out
subsets:
  - addresses:
      - ip: 192.168.0.6
    ports:
      - port: 8080

集群中的pod訪問Service nginx-out:80,請求最終會被指向192.168.0.6:8080。


Service指向外部服務(wù)

實戰(zhàn):將外部的Jenkins用Openshift的Service接管,并創(chuàng)建Route來訪問Jenkins服務(wù)

  • Jenkins服務(wù): 192.168.0.6:8080
  • route的Hostname:jenkins.apps.openshift.com
  1. 創(chuàng)建project jenkins
oc new-project jenkins
  1. 創(chuàng)建jenkinsvm Service
apiVersion: v1
kind: Service
metadata:
  name: jenkinsvm
spec:
  ports:
    - port: 8080
      name: jenkinsvm
      protocol: TCP
      targetPort: 8080
  type: ClusterIP
  1. 創(chuàng)建Endpoints
kind: Endpoints
apiVersion: v1
metadata:
  name: nginx-out
subsets:
  - addresses:
      - ip: 192.168.0.6
    ports:
      - port: 8080
  1. 創(chuàng)建Route
apiVersion: v1
kind: Route
metadata:
  name: jenkins
spec:
  host: jenkins.apps.openshift.com
  port: 
    targetPort: jenkinsvm
  to:
    kind: Service
    name: jenkinsvm
    weight: 100
使用Openshift的Service訪問的集群外部的Jenkins服務(wù)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容