K8s-Service

k8s-Services

簡(jiǎn)介:

Kubernetes 在設(shè)計(jì)之初就充分考慮了針對(duì)容器的服務(wù)發(fā)現(xiàn)與負(fù)載均衡機(jī)制,提供了 Service 資源,并通過 kube-proxy 配合 cloud provider 來適應(yīng)不同的應(yīng)用場(chǎng)景。隨著 kubernetes 用戶的激增,用戶場(chǎng)景的不斷豐富,又產(chǎn)生了一些新的負(fù)載均衡機(jī)制。目前,kubernetes 中的負(fù)載均衡大致可以分為以下幾種機(jī)制,每種機(jī)制都有其特定的應(yīng)用場(chǎng)景:

Service:直接用 Service 提供 cluster 內(nèi)部的負(fù)載均衡,并借助 cloud provider 提供的 LB 提供外部訪問
Ingress Controller:還是用 Service 提供 cluster 內(nèi)部的負(fù)載均衡,但是通過自定義 Ingress Controller 提供外部訪問
Service Load Balancer:把 load balancer 直接跑在容器中,實(shí)現(xiàn) Bare Metal 的 Service Load Balancer
Custom Load Balancer:自定義負(fù)載均衡,并替代 kube-proxy,一般在物理部署 Kubernetes 時(shí)使用,方便接入公司已有的外部服務(wù)


image.png

Service 是對(duì)一組提供相同功能的 Pods 的抽象,并為它們提供一個(gè)統(tǒng)一的入口。借助 Service,應(yīng)用可以方便的實(shí)現(xiàn)服務(wù)發(fā)現(xiàn)與負(fù)載均衡,并實(shí)現(xiàn)應(yīng)用的零宕機(jī)升級(jí)。Service 通過標(biāo)簽來選取服務(wù)后端,一般配合 Replication Controller 或者 Deployment 來保證后端容器的正常運(yùn)行。這些匹配標(biāo)簽的 Pod IP 和端口列表組成 endpoints,由 kube-proxy 負(fù)責(zé)將服務(wù) IP 負(fù)載均衡到這些 endpoints 上。
Service有四種類型:
ClusterIP:默認(rèn)類型,自動(dòng)分配一個(gè)僅 cluster 內(nèi)部可以訪問的虛擬 IP
NodePort:在 ClusterIP 基礎(chǔ)上為 Service 在每臺(tái)機(jī)器上綁定一個(gè)端口,這樣就可以通過 <NodeIP>:NodePort 來訪問該服務(wù)。如果 kube-proxy 設(shè)置了 --nodeport-addresses=10.240.0.0/16(v1.10 支持),那么僅該 NodePort 僅對(duì)設(shè)置在范圍內(nèi)的 IP 有效。
LoadBalancer:在 NodePort 的基礎(chǔ)上,借助 cloud provider 創(chuàng)建一個(gè)外部的負(fù)載均衡器,并將請(qǐng)求轉(zhuǎn)發(fā)到 <NodeIP>:NodePort
ExternalName:將服務(wù)通過 DNS CNAME 記錄方式轉(zhuǎn)發(fā)到指定的域名(通過 spec.externlName 設(shè)定)。需要 kube-dns 版本在 1.7 以上。
另外,也可以將已有的服務(wù)以 Service 的形式加入到 Kubernetes 集群中來,只需要在創(chuàng)建 Service 的時(shí)候不指定 Label selector,而是在 Service 創(chuàng)建好后手動(dòng)為其添加 endpoint。

使用Cluster類型創(chuàng)建一個(gè)services

vim nginx.svc.yaml
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: nginx-deploy
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 80
          protocol: TCP 
---
apiVersion: v1
kind: Service
metadata:
  name: nginx
  namespace: default
spec:
  selector:
    app: nginx
  clusterIP: 10.96.88.8
  type: ClusterIP
  ports:
  - port: 80
    targetPort: 80

查看service

[root@k8s-master daem]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   71d
nginx        ClusterIP   10.96.88.8   <none>        80/TCP    5m38s

[root@k8s-master daem]# kubectl describe svc nginx 
Name:              nginx
Namespace:         default
Labels:            <none>
Annotations:       <none>
Selector:          app=nginx
Type:              ClusterIP
IP:                10.96.88.8
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         10.244.1.104:80,10.244.1.108:80,10.244.2.66:80 + 1 more...
Session Affinity:  None
Events:            <none>

使用NodePort創(chuàng)建一個(gè)Service

vim myapp.svc.yaml
apiVersion: apps/v1
kind: Deployment
metadata: 
  name: myapp
  namespace: default
spec:
  replicas: 2
  selector:
    matchLabels:
      app: myapp
  template:
    metadata:
      labels:        
        app: myapp
    spec:
      containers:
      - name: myapp
        image: ikubernetes/myapp:v1
        imagePullPolicy: IfNotPresent
        ports:
        - name: http
          containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
  name: myapp
  namespace: default
spec:
  selector:
    app: myapp
  clusterIP: 10.96.99.99 
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
查看創(chuàng)建的svc和pod
root@master:~/k8s_yaml# kubectl get svc
NAME         TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1     <none>        443/TCP        2d5h
myapp        NodePort    10.96.99.99   <none>        80:30107/TCP   19m
root@master:~/k8s_yaml# kubectl get pods
NAME                     READY   STATUS    RESTARTS   AGE
myapp-5cbd66595b-2dghs   1/1     Running   0          15m
myapp-5cbd66595b-m5d6l   1/1     Running   0          21m

NodePort暴露端口在集群外部可以訪問


image.png

工作原理圖:

image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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