容器編排系統k8s之Service資源

1874發表於2020-12-20

  前文我們瞭解了k8s上的DemonSet、Job和CronJob控制器的相關話題,回顧請參考:https://www.cnblogs.com/qiuhom-1874/p/14157306.html;今天我們來了解下k8s上的service資源的相關話題;

  Service資源在k8s上主要用來解決pod訪問問題;我們知道在k8s上pod由於各種原因重建,對於重建後的podip地址和名稱都是變化的,這樣一來使得我們訪問pod就變得有些不便;為了解決pod訪問能有一個固定的端點,在k8s上就是用service來解決的;簡單來講,service物件就是工作在節點上的一組iptables或ipvs規則,用於將到達service物件ip地址的流量排程轉發至相應endpoint物件指向的ip地址和埠之上;工作於每個節點的kube-proxy元件通過apiserver持續監控著各service及其關聯的pod物件,並將其建立或變動實時反映至當前工作節點上相應的iptables或ipvs規則;其實service和pod或其他資源的關聯,本質上不是直接關聯,它依靠一箇中間元件endpoint;endpoint主要作用就是引用後端pod或其他資源(比如k8s外部的服務也可以被endpoint引用);所謂endpoint就是ip地址+埠;

  提示:在k8s上kube-proxy它會監視著apiserver上的service資源變動,及時將變動轉化為本機的iptables或ipvs規則;對應客戶端pod訪問對應serverpod,報文首先會從本機的iptables或ipvs規則所匹配,然後再由對應規則邏輯把請求排程到對應的pod上;

  service代理模式模式

  在k8s上service代理模式有三種,早期的k8s版本(1.1之前包含1.1的版本)預設的代理模式為userspace,後面的版本(1.11起)預設代理模式為ipvs,如果對應ipvs的模組沒有載入,它會自動降級為iptables;

  userspace代理模式

  提示:userspace是指Linux作業系統上的使用者空間;在這種代理模型下iptables只是做轉發並不排程,對應排程由kube-proxy完成;userspace這種排程模型使用者請求從核心空間到使用者空間再到核心空間,效能效率比較低下;

  iptables代理模式

  提示:iptables這種代理模式,對於每個service物件,kube-proxy會建立iptables規則直接捕獲到達Clusterip和port的流量,並將其重定向至當前service物件的後端pod資源;對於每個endpoint物件,service資源會為其建立iptables規則並關聯至挑選的後端pod資源物件;相對於userspace代理模式來說,該模式使用者請求無須在使用者空間和核心空間來回切換,因此效率高效;此種模式下kube-proxy就只負責生成iptalbes規則,排程有iptables規則完成;

 

   ipvs代理模式

  提示:ipvs代理模式,kube-proxy會跟蹤apiserver上service和endpoint物件的變動,據此來呼叫netlink介面建立ipvs規則,並確保與apiserver中的變動保持同步;這種模式與iptables的模式不同之處僅在於其請求排程由ipvs完成,餘下其他功能仍由iptables完成;比如流量捕獲,nat等等功能都會由iptables完成;ipvs代理模型類似iptables模型,ipvs構建於netfilter的鉤子函式之上,但它使用hash表作為底層資料結構並工作於核心空間,因此具有流量轉發速度快,規則同步效能好的特點,除此之外,ipvs還支援眾多排程演算法,比如rr,lc,sh,dh等等;

   service型別

  在k8s上service的型別有4種,第一種是clusterIP,我們在建立service資源時,如果不指定其type型別,預設就是clusterip;第二種是NodePort型別,第三種是LoadBalancer,第四種是ExternalName;不同型別的service,其功能和作用也有所不同;

  ClusterIP

  提示:如上所示,ClusterIP型別的service就是在k8s節點上建立一個滿足serviceip地址的iptables或ipvs規則;這種型別的service的ip地址一定是我們在初始化叢集時,指定的service網路(10.96.0.0/12)中的地址;這也意味著這種型別service不能被叢集外部客戶端所訪問,僅能在叢集節點上訪問;

  NodePort

  提示:NodePort型別的service,是建構在ClusterIP的基礎上做的擴充套件,主要解決了叢集外部客戶端訪問問題;如上圖所示,NodePort型別service在建立時,它會每個節點上建立一條DNAT規則,外部客戶端訪問叢集任意節點的指定埠,都會被DNAT到對應的service上,從而實現訪問叢集內部Pod;對於叢集內部客戶端的訪問它還是通過ClusterIP進行的;NodePort型別service與ClusterIP型別service唯一不同的是,NodePort型別service能夠被外部客戶端所訪問,在叢集每個節點上都有對應的DNAT規則;

  LoadBalancer

  提示:LoadBalancer這種型別的service是在NodePort的基礎上做的擴充套件,這種型別service只能在底層是雲環境的K8s上建立,如果底層是非雲環境,這種型別無法實現,只能手動搭建反向代理進行對NodePort型別的service進行反代;它主要解決NodePort型別service被叢集外部訪問時的埠對映以及負載;

  ExternalName

  提示:ExternalName這種型別service主要用來解決對應service引用叢集外部的服務;我們知道對於service來說,它就是一條iptables或ipvs規則,對於後端引用的資源是什麼,取決於對應endpoint關聯的是什麼資源的ip地址和埠;如果我們需要在叢集中使用叢集外部的服務,我們就可以建立ExternalName型別的service,指定後端關聯外部某個服務端ip地址或域名即可;它的工作流程如上圖所示,在叢集內部客戶端訪問對應service時,首先要去core-DNS上查詢對應域名的ip地址,然後再根據dns返回的ip地址去連線對應的服務;使用這種型別service的前提是對應的coredns能夠連線到外部網路解析對應的域名;

  service資源的建立

  示例:建立ClusterIP型別的service

[root@master01 ~]# cat ngx-dep-svc-demo.yaml
apiVersion: v1
kind: Service
metadata:
  name: ngx-dep-svc
  namespace: default
spec:
  ports:
  - name: http
    port: 80
    targetPort: 80
  selector:
    app: ngx-dep-pod
[root@master01 ~]# 

  提示:在建立service資源時,主要要需要在spec欄位中指定port和targetPort,port是service的埠,targetPort是後端資源的埠;其次就是需要定義標籤選擇器,這裡的標籤選擇器用selector欄位指定,它的值是一個字典,即kv鍵值對;預設不指定type型別就是使用的ClusterIP型別,預設不指定ClusterIP就表示自動生成對應的ClusterIP;

  應用資源清單

[root@master01 ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   40h
[root@master01 ~]# kubectl apply -f ngx-dep-svc-demo.yaml
service/ngx-dep-svc created
[root@master01 ~]# kubectl get svc
NAME          TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes    ClusterIP   10.96.0.1       <none>        443/TCP   40h
ngx-dep-svc   ClusterIP   10.107.159.92   <none>        80/TCP    5s
[root@master01 ~]# 

  驗證:訪問對應的ClusterIP看看是否能夠訪問到對應的資源?

[root@master01 ~]# curl 10.107.159.92
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@master01 ~]# 

  提示:可以看到訪問對應serviceip地址能夠訪問到對應的pod;

  檢視service詳細資訊

[root@master01 ~]# kubectl describe svc ngx-dep-svc
Name:              ngx-dep-svc
Namespace:         default
Labels:            <none>
Annotations:       <none>
Selector:          app=ngx-dep-pod
Type:              ClusterIP
IP Families:       <none>
IP:                10.107.159.92
IPs:               10.107.159.92
Port:              http  80/TCP
TargetPort:        80/TCP
Endpoints:         10.244.2.93:80,10.244.3.86:80,10.244.4.14:80
Session Affinity:  None
Events:            <none>
[root@master01 ~]# 

  提示:可以看到對應service的type型別為ClusterIP,port為80,targetPort為80,endpoins對應了3個podip地址+targetPort;其實在建立service時,系統預設會建立一個同service相同名稱的endpoints;

  檢視endpoints

[root@master01 ~]# kubectl get endpoints
NAME          ENDPOINTS                                      AGE
kubernetes    192.168.0.41:6443                              40h
ngx-dep-svc   10.244.2.93:80,10.244.3.86:80,10.244.4.14:80   4m53s
[root@master01 ~]# kubectl describe endpoints/ngx-dep-svc
Name:         ngx-dep-svc
Namespace:    default
Labels:       <none>
Annotations:  endpoints.kubernetes.io/last-change-trigger-time: 2020-12-20T08:50:52Z
Subsets:
  Addresses:          10.244.2.93,10.244.3.86,10.244.4.14
  NotReadyAddresses:  <none>
  Ports:
    Name  Port  Protocol
    ----  ----  --------
    http  80    TCP

Events:  <none>
[root@master01 ~]# 

  提示:可以看到ngx-dep-svc這個endpoint關聯了3個podip地址;

  示例:建立NodePort型別service

[root@master01 ~]# cat ngx-dep-svc-demo.yaml
apiVersion: v1
kind: Service
metadata:
  name: ngx-dep-svc-nodeport
  namespace: default
spec:
  ports:
  - name: http
    port: 80
    nodePort: 30080
    targetPort: 80
  selector:
    app: ngx-dep-pod
  type: NodePort
[root@master01 ~]# 

  提示:建立nodeport型別service需要在spec欄位中使用type欄位來指定其型別為NodePort;只有type的值為NodePort,對應ports欄位中指定的nodePort 才有意義,預設不指定它會隨機生成一個埠,指定nodePort就相當於固定了埠;通常不建議指定nodePort;

  應用資源配置清單

[root@master01 ~]# kubectl get svc
NAME          TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes    ClusterIP   10.96.0.1       <none>        443/TCP   40h
ngx-dep-svc   ClusterIP   10.107.159.92   <none>        80/TCP    21m
[root@master01 ~]# kubectl apply -f ngx-dep-svc-demo.yaml
service/ngx-dep-svc-nodeport created
[root@master01 ~]# kubectl get svc
NAME                   TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes             ClusterIP   10.96.0.1       <none>        443/TCP        40h
ngx-dep-svc            ClusterIP   10.107.159.92   <none>        80/TCP         21m
ngx-dep-svc-nodeport   NodePort    10.97.166.233   <none>        80:30080/TCP   4s
[root@master01 ~]# 

  提示:可以看到nodeport型別的service,對應port就有兩個值,後面的30080就是外部客戶端訪問叢集內部資源的埠;

  驗證:使用瀏覽器訪問k8s任意節點的30080埠,看看是否能夠訪問到對應的pod?

  提示:可以看到叢集外部客戶端可以通過訪問叢集節點上的一個埠實現訪問對應叢集內部資源;

  示例:建立ExternalName型別service

[root@master01 ~]# cat externalname-svc.yaml
kind: Service
apiVersion: v1
metadata:
  name: external-www-svc
spec:
  type: ExternalName
  externalName: www.qiuhom.com
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    nodePort: 0
  selector: {}
[root@master01 ~]# 

  提示:以上配置清單表示建立一個名為external-www-svc的Service,對應Service的型別為ExternalName;引用外部服務為www.qiuhom.com;

  應用資源配置清單

[root@master01 ~]# kubectl apply -f externalname-svc.yaml
service/external-www-svc created
[root@master01 ~]# kubectl get svc
NAME                   TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)        AGE
external-www-svc       ExternalName   <none>          www.qiuhom.com   80/TCP         6s
kubernetes             ClusterIP      10.96.0.1       <none>          443/TCP        43h
ngx-dep-svc            ClusterIP      10.107.159.92   <none>          80/TCP         3h27m
ngx-dep-svc-nodeport   NodePort       10.97.166.233   <none>          80:30080/TCP   3h2m
[root@master01 ~]# kubectl describe svc/external-www-svc
Name:              external-www-svc
Namespace:         default
Labels:            <none>
Annotations:       <none>
Selector:          <none>
Type:              ExternalName
IP Families:       <none>
IP:                
IPs:               <none>
External Name:     www.qiuhom.com
Port:              <unset>  80/TCP
TargetPort:        80/TCP
Endpoints:         <none>
Session Affinity:  None
Events:            <none>
[root@master01 ~]# 

  提示:可以看到應用配置清單以後,service詳細資訊中,沒有標籤,沒有選擇器,沒有ip地址;只有externalName和對應targetPort;

  測試:把dns伺服器地址指向coredns,然後訪問對應服務名稱,看看對應服務是否會有響應?

[root@master01 ~]# kubectl get svc -n kube-system
NAME       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)                  AGE
kube-dns   ClusterIP   10.96.0.10   <none>        53/UDP,53/TCP,9153/TCP   12d
[root@master01 ~]# cat /etc/resolv.conf 
# Generated by NetworkManager
search k8s.org
nameserver 10.96.0.10
[root@master01 ~]# curl external-www-svc.default.svc.cluster.local -I
HTTP/1.1 302 Found
Server: nginx/1.14.0 (Ubuntu)
Date: Sun, 20 Dec 2020 12:33:44 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 0
Connection: keep-alive
Vary: Accept-Language, Cookie
Location: /accounts/login/?next=/
Content-Language: en

[root@master01 ~]# 

  提示:可以看到訪問對應服務名稱,響應碼是302,說明我們的請求被代理出去了;

  進入任意pod內部,使用nslookup查詢coredns上對應服務名稱是否能夠解析?

[root@master01 ~]# kubectl get pods
NAME                           READY   STATUS    RESTARTS   AGE
deploy-demo-6d795f958b-6pjgw   1/1     Running   0          3h30m
deploy-demo-6d795f958b-m4vfb   1/1     Running   0          3h30m
deploy-demo-6d795f958b-qcl6m   1/1     Running   0          3h30m
[root@master01 ~]# kubectl exec -it deploy-demo-6d795f958b-6pjgw -- /bin/sh
/ # nslookup external-www-svc
nslookup: can't resolve '(null)': Name does not resolve

Name:      external-www-svc
Address 1: 47.99.205.203
/ # 

  提示:可以看到對應服務名稱也能正常被解析;

  示例:建立無頭service

[root@master01 ~]# cat handless-svc-demo.yaml
apiVersion: v1
kind: Service
metadata:
  name: handless-svc-demo 
  namespace: default
spec:
  clusterIP: None
  ports:
  - name: http
    port: 80
    targetPort: 80
  selector:
    app: ngx-dep-pod
[root@master01 ~]# 

  提示:所謂無頭service是指沒有clusterIP的service,我們知道clusterip是service作為訪問後端pod的入口,那麼沒有clusterip,它怎麼訪問後端pod呢?沒有ip地址我們只能使用名稱來訪問;在k8s上無頭service預設會被coredns把對應名稱的service解析為後端關聯的多個pod地址;

  應用資源配置清單

[root@master01 ~]# kubectl get svc
NAME                   TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)        AGE
external-www-svc       ExternalName   <none>          www.baidu.com   80/TCP         10m
kubernetes             ClusterIP      10.96.0.1       <none>          443/TCP        43h
ngx-dep-svc            ClusterIP      10.107.159.92   <none>          80/TCP         3h34m
ngx-dep-svc-nodeport   NodePort       10.97.166.233   <none>          80:30080/TCP   3h12m
[root@master01 ~]# kubectl apply -f handless-svc-demo.yaml 
service/handless-svc-demo created
[root@master01 ~]# kubectl get svc
NAME                   TYPE           CLUSTER-IP      EXTERNAL-IP     PORT(S)        AGE
external-www-svc       ExternalName   <none>          www.baidu.com   80/TCP         10m
handless-svc-demo      ClusterIP      None            <none>          80/TCP         5s
kubernetes             ClusterIP      10.96.0.1       <none>          443/TCP        43h
ngx-dep-svc            ClusterIP      10.107.159.92   <none>          80/TCP         3h34m
ngx-dep-svc-nodeport   NodePort       10.97.166.233   <none>          80:30080/TCP   3h12m
[root@master01 ~]# 

  提示:可以看到對應service沒有clusterIP;

  驗證:進入任意pod,使用名稱訪問service,看看對應名稱是否能夠訪問到pod?

[root@master01 ~]# kubectl get pod
NAME                           READY   STATUS    RESTARTS   AGE
deploy-demo-6d795f958b-6pjgw   1/1     Running   0          3h53m
deploy-demo-6d795f958b-m4vfb   1/1     Running   0          3h53m
deploy-demo-6d795f958b-qcl6m   1/1     Running   0          3h53m
[root@master01 ~]# kubectl exec -it pod/deploy-demo-6d795f958b-m4vfb -- /bin/sh
/ # wget -O - -q handless-svc-demo
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
/ # 

  提示:在pod內部使用名稱service名稱可以訪問到對應的pod;

  驗證:檢視coredns是否將對應service名稱解析為對應擁有service指定的標籤選擇器的podip呢?

[root@master01 ~]# kubectl get pod --show-labels -o wide 
NAME                           READY   STATUS    RESTARTS   AGE     IP            NODE             NOMINATED NODE   READINESS GATES   LABELS
deploy-demo-6d795f958b-6pjgw   1/1     Running   0          3h54m   10.244.2.93   node02.k8s.org   <none>           <none>            app=ngx-dep-pod,pod-template-hash=6d795f958b
deploy-demo-6d795f958b-m4vfb   1/1     Running   0          3h54m   10.244.4.14   node04.k8s.org   <none>           <none>            app=ngx-dep-pod,pod-template-hash=6d795f958b
deploy-demo-6d795f958b-qcl6m   1/1     Running   0          3h54m   10.244.3.86   node03.k8s.org   <none>           <none>            app=ngx-dep-pod,pod-template-hash=6d795f958b
[root@master01 ~]# kubectl exec -it pod/deploy-demo-6d795f958b-m4vfb -- /bin/sh
/ #  nslookup handless-svc-demo
nslookup: can't resolve '(null)': Name does not resolve

Name:      handless-svc-demo
Address 1: 10.244.4.14 deploy-demo-6d795f958b-m4vfb
Address 2: 10.244.3.86 10-244-3-86.ngx-dep-svc-nodeport.default.svc.cluster.local
Address 3: 10.244.2.93 10-244-2-93.ngx-dep-svc.default.svc.cluster.local
/ # 

  提示:可以看到coredns把對應名稱解析成了3個地址;這三個地址都是對應pod上擁有service指定的標籤選擇器上的標籤的podip地址;

  配置k8s使用ipvs代理模式

  編寫載入ipvs相關模組指令碼

[root@master01 ~]# cat /etc/sysconfig/modules/ipvs.modules
#!/bin/bash
ipvs_mods_dir="/usr/lib/modules/$(uname -r)/kernel/net/netfilter/ipvs"
for mod in $(ls $ipvs_mods_dir | grep -o "^[^.]*"); do
    /sbin/modinfo -F filename $mod  &> /dev/null
    if [ $? -eq 0 ]; then
        /sbin/modprobe $mod
    fi
done
[root@master01 ~]# 

  提示:以上指令碼主要做了一件事,就是把ipvs_mods_dir所指定的目錄下的所有模組載入到核心;

  給指令碼新增執行許可權

[root@master01 ~]# ll /etc/sysconfig/modules/ipvs.modules
-rw-r--r-- 1 root root 253 Dec 20 18:50 /etc/sysconfig/modules/ipvs.modules
[root@master01 ~]# chmod +x /etc/sysconfig/modules/ipvs.modules
[root@master01 ~]# ll /etc/sysconfig/modules/ipvs.modules
-rwxr-xr-x 1 root root 253 Dec 20 18:50 /etc/sysconfig/modules/ipvs.modules
[root@master01 ~]# 

  複製該指令碼到其他節點

[root@master01 ~]# scp -p /etc/sysconfig/modules/ipvs.modules node01:/etc/sysconfig/modules/ipvs.modules
ipvs.modules                                                                                     100%  253   132.8KB/s   00:00    
[root@master01 ~]# scp -p /etc/sysconfig/modules/ipvs.modules node02:/etc/sysconfig/modules/ipvs.modules
ipvs.modules                                                                                     100%  253   102.5KB/s   00:00    
[root@master01 ~]# scp -p /etc/sysconfig/modules/ipvs.modules node03:/etc/sysconfig/modules/ipvs.modules
ipvs.modules                                                                                     100%  253    80.8KB/s   00:00    
[root@master01 ~]# scp -p /etc/sysconfig/modules/ipvs.modules node04:/etc/sysconfig/modules/ipvs.modules
ipvs.modules                                                                                     100%  253   121.0KB/s   00:00    
[root@master01 ~]# 

  提示:把指令碼放在/etc/sysconfig/modules目錄下以點modules結尾的指令碼,在系統重啟以後,它會自動執行對應目錄下的指令碼;

  執行指令碼,載入模組

[root@master01 ~]# bash /etc/sysconfig/modules/ipvs.modules       
[root@master01 ~]# ssh node01 'bash /etc/sysconfig/modules/ipvs.modules'
[root@master01 ~]# ssh node02 'bash /etc/sysconfig/modules/ipvs.modules' 
[root@master01 ~]# ssh node03 'bash /etc/sysconfig/modules/ipvs.modules' 
[root@master01 ~]# ssh node04 'bash /etc/sysconfig/modules/ipvs.modules' 
[root@master01 ~]# 

  驗證:檢視對應模組是否載入?

[root@master01 ~]# lsmod |grep ip_vs
ip_vs_wlc              12519  0 
ip_vs_sed              12519  0 
ip_vs_pe_sip           12697  0 
nf_conntrack_sip       33860  1 ip_vs_pe_sip
ip_vs_nq               12516  0 
ip_vs_lc               12516  0 
ip_vs_lblcr            12922  0 
ip_vs_lblc             12819  0 
ip_vs_ftp              13079  0 
ip_vs_dh               12688  0 
ip_vs_sh               12688  0 
ip_vs_wrr              12697  0 
ip_vs_rr               12600  0 
ip_vs                 141092  26 ip_vs_dh,ip_vs_lc,ip_vs_nq,ip_vs_rr,ip_vs_sh,ip_vs_ftp,ip_vs_sed,ip_vs_wlc,ip_vs_wrr,ip_vs_pe_sip,ip_vs_lblcr,ip_vs_lblc
nf_nat                 26787  4 ip_vs_ftp,nf_nat_ipv4,xt_nat,nf_nat_masquerade_ipv4
nf_conntrack          133387  8 ip_vs,nf_nat,nf_nat_ipv4,xt_conntrack,nf_nat_masquerade_ipv4,nf_conntrack_netlink,nf_conntrack_sip,nf_conntrack_ipv4
libcrc32c              12644  4 xfs,ip_vs,nf_nat,nf_conntrack
[root@master01 ~]# 

  提示:能夠看到以上資訊表示ipvs相關模組已經載入;

  編輯kube-proxy的配置

[root@master01 ~]# kubectl edit cm kube-proxy -n kube-system

  修改mode欄位的值為“ipvs”,然後儲存退出

  刪除現有k8s kube-proxy pod

[root@master01 ~]# kubectl get pod -n kube-system --show-labels
NAME                                       READY   STATUS    RESTARTS   AGE   LABELS
coredns-7f89b7bc75-k9gdt                   1/1     Running   10         12d   k8s-app=kube-dns,pod-template-hash=7f89b7bc75
coredns-7f89b7bc75-kp855                   1/1     Running   8          12d   k8s-app=kube-dns,pod-template-hash=7f89b7bc75
etcd-master01.k8s.org                      1/1     Running   11         12d   component=etcd,tier=control-plane
kube-apiserver-master01.k8s.org            1/1     Running   8          12d   component=kube-apiserver,tier=control-plane
kube-controller-manager-master01.k8s.org   1/1     Running   9          12d   component=kube-controller-manager,tier=control-plane
kube-flannel-ds-cx8d5                      1/1     Running   12         12d   app=flannel,controller-revision-hash=64465d999,pod-template-generation=1,tier=node
kube-flannel-ds-jz6r4                      1/1     Running   3          46h   app=flannel,controller-revision-hash=64465d999,pod-template-generation=1,tier=node
kube-flannel-ds-ndzl6                      1/1     Running   13         12d   app=flannel,controller-revision-hash=64465d999,pod-template-generation=1,tier=node
kube-flannel-ds-rjtn9                      1/1     Running   14         12d   app=flannel,controller-revision-hash=64465d999,pod-template-generation=1,tier=node
kube-flannel-ds-zgq92                      1/1     Running   12         12d   app=flannel,controller-revision-hash=64465d999,pod-template-generation=1,tier=node
kube-proxy-8wfcx                           1/1     Running   3          46h   controller-revision-hash=c449f5b75,k8s-app=kube-proxy,pod-template-generation=1
kube-proxy-dl8jd                           1/1     Running   8          12d   controller-revision-hash=c449f5b75,k8s-app=kube-proxy,pod-template-generation=1
kube-proxy-lz4zc                           1/1     Running   9          12d   controller-revision-hash=c449f5b75,k8s-app=kube-proxy,pod-template-generation=1
kube-proxy-pjv9s                           1/1     Running   12         12d   controller-revision-hash=c449f5b75,k8s-app=kube-proxy,pod-template-generation=1
kube-proxy-pwl5v                           1/1     Running   8          12d   controller-revision-hash=c449f5b75,k8s-app=kube-proxy,pod-template-generation=1
kube-scheduler-master01.k8s.org            1/1     Running   9          12d   component=kube-scheduler,tier=control-plane
[root@master01 ~]# kubectl get pod -n kube-system -l k8s-app=kube-proxy        
NAME               READY   STATUS    RESTARTS   AGE
kube-proxy-8wfcx   1/1     Running   3          46h
kube-proxy-dl8jd   1/1     Running   8          12d
kube-proxy-lz4zc   1/1     Running   9          12d
kube-proxy-pjv9s   1/1     Running   12         12d
kube-proxy-pwl5v   1/1     Running   8          12d
[root@master01 ~]# kubectl delete pod -n kube-system -l k8s-app=kube-proxy    
pod "kube-proxy-8wfcx" deleted
pod "kube-proxy-dl8jd" deleted
pod "kube-proxy-lz4zc" deleted
pod "kube-proxy-pjv9s" deleted
pod "kube-proxy-pwl5v" deleted
[root@master01 ~]# 

  提示:kube-proxy是一個ds控制器所管理的pod,它能容忍主節點上的汙點在叢集每個節點上建立對應pod;我們手動刪除它,對應控制器會重新新建對應數量的pod,從而實現應用新配置的目的;生產環境不提倡這樣修改,應該在叢集初始化前就規劃好使用哪種代理模式;

  驗證:在叢集任意節點安裝ipvs客戶端工具,看看是否有對應的ipvs規則生成?

  安裝ipvsadm

[root@master01 ~]# yum install -y ipvsadm

  使用ipvsadm檢視是否生成的有ipvs規則?

[root@master01 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
  -> RemoteAddress:Port           Forward Weight ActiveConn InActConn
TCP  172.17.0.1:30080 rr
  -> 10.244.2.93:80               Masq    1      0          0         
  -> 10.244.3.86:80               Masq    1      0          0         
  -> 10.244.4.14:80               Masq    1      0          0         
TCP  192.168.0.41:30080 rr
  -> 10.244.2.93:80               Masq    1      0          0         
  -> 10.244.3.86:80               Masq    1      0          0         
  -> 10.244.4.14:80               Masq    1      0          0         
TCP  10.96.0.1:443 rr
  -> 192.168.0.41:6443            Masq    1      0          0         
TCP  10.96.0.10:53 rr
  -> 10.244.0.20:53               Masq    1      0          0         
  -> 10.244.0.21:53               Masq    1      0          0         
TCP  10.96.0.10:9153 rr
  -> 10.244.0.20:9153             Masq    1      0          0         
  -> 10.244.0.21:9153             Masq    1      0          0         
TCP  10.97.166.233:80 rr
  -> 10.244.2.93:80               Masq    1      0          0         
  -> 10.244.3.86:80               Masq    1      0          0         
  -> 10.244.4.14:80               Masq    1      0          0         
TCP  10.107.159.92:80 rr
  -> 10.244.2.93:80               Masq    1      0          0         
  -> 10.244.3.86:80               Masq    1      0          0         
  -> 10.244.4.14:80               Masq    1      0          0         
TCP  10.244.0.0:30080 rr
  -> 10.244.2.93:80               Masq    1      0          0         
  -> 10.244.3.86:80               Masq    1      0          0         
  -> 10.244.4.14:80               Masq    1      0          0         
TCP  10.244.0.1:30080 rr
  -> 10.244.2.93:80               Masq    1      0          0         
  -> 10.244.3.86:80               Masq    1      0          0         
  -> 10.244.4.14:80               Masq    1      0          0         
TCP  127.0.0.1:30080 rr
  -> 10.244.2.93:80               Masq    1      0          0         
  -> 10.244.3.86:80               Masq    1      0          0         
  -> 10.244.4.14:80               Masq    1      0          0         
UDP  10.96.0.10:53 rr
  -> 10.244.0.20:53               Masq    1      0          0         
  -> 10.244.0.21:53               Masq    1      0          0         
[root@master01 ~]# 

  提示:能夠看到有ipvs規則生成,說明此時k8s就是使用的ipvs代理模式;

相關文章