對k8s service的一些理解


對k8s service的一些理解

 

服務service

service是一個抽象概念,定義了一個服務的多個pod邏輯合集和訪問pod的策略,一般把service稱為微服務

舉個例子一個a服務運行3個pod,b服務怎么訪問a服務的pod,pod的ip都不是持久化的重啟之后就會有變化。
這時候b服務可以訪問跟a服務綁定的service,service信息是固定的提前告訴b就行了,service通過Label Selector跟a服務的pod綁定,無論a的pod如何變化對b來說都是透明的

復制代碼
kind: Service
apiVersion: v1
metadata:
  name: my-service
spec:
  selector:
    app: MyApp
  ports:
  - protocol: TCP
    port: 80
    targetPort: 9376
復制代碼

port 端口是service對外暴露的端口,任何人訪問80端口都會被service代理到后端pod的9376端口

服務代理

k8s群集中的每個節點都運行一個kube-proxy的組件,kube-proxy其實是一個代理層負責實現service

kube-proxy代理模式有兩種:

代理模式:userspace

客戶端訪問ServiceIP(clusterIP)請求會先從用戶空間到內核中的iptables,然后回到用戶空間kube-proxy,kube-proxy負責代理工作。

具體細節:

每個service都會由kube-proxy在node節點上起一個隨機的代理端口,iptables會捕捉clusterIP上的端口(port)流量重定向代理端口,訪問代理端口的任何連接都會被代理到service后端的某一個pod,默認情況下對后端pod的選擇是輪詢

 

代理模式:iptables

客戶端訪問ServiceIP(clusterIP)請求會由iptables直接重定向到后端

具體細節:

每個service都會由kube-proxy(監控 kube-control)生成一組iptables規則,iptables(nat 表)會捕捉clusterIP上的端口(targetPort)流量重定向后端某一個pod,默認對pod的選擇是隨機的

 

Kubernetes v1.2之前默認是userspace之后是iptables模式,iptables模式性能和可靠性更好,但是iptables模式依賴健康檢查,在沒有健康檢查的情況下如果一個pod不響應,iptables模式不會切換另一個pod上

Kubernetes v1.9版本會支持lvs的ipvs模式目前還是beta版

service的類型

  • ClusterIP               默認模式,只能在集群內部訪問
  • NodePort              在每個節點上都監聽一個同樣的端口號(30000-32767),ClusterIP和路由規則會自動創建。集群外部可以訪問<NodeIP>:<NodePort>聯系到集群內部服務,可以配合外部負載均衡使用(我現在公司用的就是這個模式配合阿里雲的SLB)
  • LoadBalancer       要配合支持公有雲負載均衡使用比如GCE、AWS。其實也是NodePort,只不過會把<NodeIP>:<NodePort>自動添加到公有雲的負載均衡當中
  • ExternalName       創建一個dns別名指到service name上,主要是防止service name發生變化,要配合dns插件使用

 

kubectl  edit service myapp1
# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Service
metadata:
  annotations:
    field.cattle.io/publicEndpoints: '[{"addresses":["10.60.188.141"],"port":30514,"protocol":"TCP","serviceName":"default:myapp1","allNodes":true}]'
  creationTimestamp: "2020-04-21T02:01:43Z"
  labels:
    run: myapp1
  name: myapp1
  namespace: default
  resourceVersion: "29767039"
  selfLink: /api/v1/namespaces/default/services/myapp1
  uid: 0c81cfda-8374-11ea-aa70-fa92dd475100
spec:
  clusterIP: 10.43.196.193
  externalTrafficPolicy: Cluster
  ports:
  - nodePort: 30514
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    run: myapp1
  sessionAffinity: None
  type: NodePort
status:
  loadBalancer: {}
~                     

C創建service

$ kubectl run myapp --image=nginx --restart=Always --replicas=2 --port=80

查看service

查看service中的內容

[root@node1 ~]# kubectl edit service myapp

 

# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
kind: Service
metadata:
  annotations:
    field.cattle.io/publicEndpoints: '[{"addresses":["10.60.188.141"],"port":31206,"protocol":"TCP","serviceName":"default:myapp","allNodes":true}]'
  creationTimestamp: "2020-04-11T14:17:49Z"
  labels:
    run: myapp
  name: myapp
  namespace: default
  resourceVersion: "27403024"
  selfLink: /api/v1/namespaces/default/services/myapp
  uid: 390d603c-7bff-11ea-be2e-fa92dd475100
spec:
  clusterIP: 10.43.96.137
  externalTrafficPolicy: Cluster
  ports:
  - nodePort: 31206
    port: 80
    protocol: TCP
    targetPort: 80
  selector:
    run: myapp
  sessionAffinity: None
  type: NodePort
status:
  loadBalancer: {}
~                                                                             
~  

 

 

 

訪問service   ip+port

 

 kubectl get pods -o wide

 

 在k8s集群中使用pods的 ip+port方式訪問

 

 

 也可以是用nodeip+ port方式訪問

 

 非此集群的機器也可以使用 nodeip + port 的方式訪問此pod

 

 

 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM