k8s的service和endpoint
生活随笔
收集整理的這篇文章主要介紹了
k8s的service和endpoint
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
service: 在k8s中,pod之間是通信是一般通過service名稱完成的
endpoint: pod和service之間的關聯關系,是通過endpoint實現的。 Endpoints表示了一個Service對應的所有Pod副本的訪問地址,而Endpoints Controller負責生成和維護所有Endpoints對象的控制器。它負責監聽Service和對應的Pod副本的變化。
對于pod,endpoint是集群自動創建的,用于將service和pod關聯起來;而對于外部服務(部署在集群外邊的數據庫啥的),我們可以人工的創建endpoint和service。使其能夠更規范的和pod進行通信。
這里主要記錄一下endpoint和service的書寫規則。兩種場景:
一個端口,多個地址
創建腳本
[root@10-9-71-11 ~]# cat redis.yaml kind: Endpoints apiVersion: v1 metadata:name: redis-cluster subsets:- addresses:- ip: 10.130.25.161- ip: 10.130.25.162- ip: 10.130.25.163 ports:- port: 6379 --- kind: Service apiVersion: v1 metadata:name: redis-cluster spec:ports:- protocol: TCPport: 6379targetPort: 6379查看servcie和endpoint詳情,可看到ip和端口的對應關系
[root@10-9-71-11 ~]# kubectl describe endpoints redis-cluster Name: redis-cluster Namespace: default Labels: <none> Annotations: kubectl.kubernetes.io/last-applied-configuration:{"apiVersion":"v1","kind":"Endpoints","metadata":{"annotations":{},"name":"redis-cluster","namespace":"default"},"subsets":[{"addresses":[... Subsets:Addresses: 10.130.25.161,10.130.25.162,10.130.25.163NotReadyAddresses: <none>Ports:Name Port Protocol---- ---- --------<unset> 6379 TCPEvents: <none> [root@10-9-71-11 ~]# kubectl describe svc redis-cluster Name: redis-cluster Namespace: default Labels: <none> Annotations: kubectl.kubernetes.io/last-applied-configuration:{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"redis-cluster","namespace":"default"},"spec":{"ports":[{"port":63... Selector: <none> Type: ClusterIP IP: 10.0.147.90 Port: <unset> 6379/TCP TargetPort: 6379/TCP Endpoints: 10.130.25.161:6379,10.130.25.162:6379,10.130.25.163:6379 Session Affinity: None Events: <none>簡單查看service和endpoint
[root@10-9-71-11 ~]# kubectl get endpoints redis-cluster NAME ENDPOINTS AGE redis-cluster 10.130.25.161:6379,10.130.25.162:6379,10.130.25.163:6379 2m3s [root@10-9-71-11 ~]# [root@10-9-71-11 ~]# kubectl get svc redis-cluster NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE redis-cluster ClusterIP 10.0.147.90 <none> 6379/TCP 2m16s一個地址,多個端口
創建腳本
[root@10-9-71-11 ~]# cat redis.yaml kind: Endpoints apiVersion: v1 metadata:name: redis subsets:- addresses:- ip: 10.130.25.161 ports:- name: port1port: 26379- name: port2port: 26380- name: port3port: 26381 --- kind: Service apiVersion: v1 metadata:name: redis spec:ports:- name: port1protocol: TCPport: 26379targetPort: 26379- name: port2protocol: TCPport: 26380targetPort: 26380- name: port3protocol: TCPport: 26381targetPort: 26381- 使用多個端口時,端口必須有名字
- 多端口之間通過列表的形式展現
查看servcie和endpoint詳情,可看到ip和端口的對應關系
[root@10-9-71-11 ~]# kubectl describe endpoints redis Name: redis Namespace: default Labels: <none> Annotations: kubectl.kubernetes.io/last-applied-configuration:# {"apiVersion":"v1","kind":"Endpoints","metadata":{"annotations":{},"name":"redis","namespace":"default"},"subsets":[{"addresses":[{"ip":"1...}] Subsets:Addresses: 10.130.25.161NotReadyAddresses: <none>Ports:Name Port Protocol---- ---- --------port3 26381 TCPport2 26380 TCPport1 26379 TCPEvents: <none> -------------------------------------------------- [root@10-9-71-11 ~]# kubectl describe service redis Name: redis Namespace: default Labels: <none> Annotations: kubectl.kubernetes.io/last-applied-configuration:{"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"redis","namespace":"default"},"spec":{"ports":[{"name":"port1","p... Selector: <none> Type: ClusterIP IP: 10.0.92.98 Port: port1 26379/TCP TargetPort: 26379/TCP Endpoints: 10.130.25.161:26379 Port: port2 26380/TCP TargetPort: 26380/TCP Endpoints: 10.130.25.161:26380 Port: port3 26381/TCP TargetPort: 26381/TCP Endpoints: 10.130.25.161:26381 Session Affinity: None Events: <none>簡單查看service和endpoint
[root@10-9-71-11 ~]# kubectl get endpoints redis NAME ENDPOINTS AGE redis 10.130.25.161:26381,10.130.25.161:26380,10.130.25.161:26379 48m [root@10-9-71-11 ~]# kubectl get svc redis NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE redis ClusterIP 10.0.92.98 <none> 26379/TCP,26380/TCP,26381/TCP 49m總結
以上是生活随笔為你收集整理的k8s的service和endpoint的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 微软ERP Axapta与Sap、Ora
- 下一篇: 软件项目开发报价(一)