1. 程式人生 > 其它 >|NO.Z.00158|——————————|CloudNative|——|KuberNetes&服務釋出.V09|-------------------------------------------------------|service.v01|service代理外部服務|

|NO.Z.00158|——————————|CloudNative|——|KuberNetes&服務釋出.V09|-------------------------------------------------------|service.v01|service代理外部服務|



[CloudNative:KuberNetes&服務釋出.V09]                                                               [Applications.KuberNetes] [在k8s中如何釋出服務|service|使用service代理k8s外部服務|使用service反代外部域名|service常用型別|]








一、使用service代理k8s外部應用
### --- service代理k8s外部應用使用場景

~~~     希望在生產環境中使用某個固定的名稱而非IP地址進行訪問外部的中介軟體服務
~~~     希望Service指向另一個Namespace中或其他叢集中的服務
~~~     某個專案正在遷移至k8s叢集,但是一部分服務仍然在叢集外部,
~~~     此時可以使用service代理至k8s叢集外部的服務
二、使用service代理k8s外部應用
### --- 編寫使用service代理k8s外部應用yaml配置檔案
~~~     # 編寫service.yaml配置檔案
~~~     它是沒有selector,不去匹配它的namespace下的Pod,而是去建立一個

[root@k8s-master01 ~]# cat nginx-svc-external.yaml 
apiVersion: v1
kind: Service
metadata:
  labels:
    app: nginx-svc-external
  name: nginx-svc-external
spec:
  ports:
  - name: http                                                                      # Service埠的名稱
    port: 80                                                                        # Service自己的埠, servicea --> serviceb http://serviceb,  http://serviceb:8080 
    protocol: TCP                                                                   # UDP TCP SCTP default: TCP
    targetPort: 80                                                                  # 後端應用的埠
  sessionAffinity: None
  type: ClusterIP
### --- 建立service代理k8s外部應用的pod
~~~     # 建立service.pod

[root@k8s-master01 ~]# kubectl create -f nginx-svc-external.yaml 
service/nginx-svc-external created
~~~     # 檢視建立的service

[root@k8s-master01 ~]# kubectl get svc
NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes           ClusterIP   10.96.0.1       <none>        443/TCP          11d
nginx-svc            ClusterIP   10.101.145.83   <none>        80/TCP,443/TCP   19h
nginx-svc-external   ClusterIP   10.97.162.95    <none>        80/TCP           4s
~~~     # 檢視建立的endpoints
~~~     沒有生成endpoints,可以自己手動建立一個

[root@k8s-master01 ~]# kubectl get ep                                               //可以看到沒有selector引數的service它是沒有ep的
NAME         ENDPOINTS                                                          AGE
kubernetes   192.168.1.11:6443,192.168.1.12:6443,192.168.1.13:6443              11d
nginx-svc    172.17.125.13:443,172.18.195.18:443,172.17.125.13:80 + 1 more...   19h
### --- 手動建立一個service對應的endpoints
~~~     # 建立endpoints.yaml配置檔案

[root@k8s-master01 ~]# kubectl get ep nginx-svc -oyaml > nginx-ep-external.yaml 
[root@k8s-master01 ~]# vim nginx-ep-external.yaml
apiVersion: v1
kind: Endpoints
metadata:
  labels:
    app: nginx-svc-external
  name: nginx-svc-external                      // 它要和service的名稱一致,一致就會去建立連線
  namespace: default
subsets:
- addresses:
  - ip: 220.181.38.148                          // 寫外部服務的IP地址,比如我們需要訪問百度的吧。
  ports:
  - name: http
    port: 80                                    // 外部服務的埠號。
    protocol: TCP                               // 協議也要一致
~~~     # 獲取百度外部代理IP地址 

[root@k8s-master01 ~]# ping baidu.com
PING baidu.com (220.181.38.148) 56(84) bytes of data.
### --- 建立service對應的endpoints
~~~     # 建立service對應的endpoints

[root@k8s-master01 ~]# kubectl create -f nginx-ep-external.yaml 
endpoints/nginx-svc-external created
You have new mail in /var/spool/mail/root
~~~     # 檢視建立的endpoints

[root@k8s-master01 ~]# kubectl get ep
NAME                 ENDPOINTS                                                          AGE
kubernetes           192.168.1.11:6443,192.168.1.12:6443,192.168.1.13:6443              11d
nginx-svc            172.17.125.13:443,172.18.195.18:443,172.17.125.13:80 + 1 more...   20h
nginx-svc-external   220.181.38.148:80            // 這是我們手動建立endPoint,和我們的service是同名的
### --- 驗證代理的外部應用百度是否代理成功
~~~     # curl訪問百度是否可以獲取到資料

[root@k8s-master01 ~]# curl baidu.com -I
HTTP/1.1 200 OK                                     // curl請求值是200,
Date: Wed, 21 Apr 2021 06:46:59 GMT
Server: Apache
Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
ETag: "51-47cf7e6ee8400"
Accept-Ranges: bytes
Content-Length: 81
Cache-Control: max-age=86400
Expires: Thu, 22 Apr 2021 06:46:59 GMT
Connection: Keep-Alive
Content-Type: text/html
~~~     # 請求一下對應的service,是否和直接curl百度的資料一致

[root@k8s-master01 ~]# kubectl get svc
NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes           ClusterIP   10.96.0.1       <none>        443/TCP          11d
nginx-svc            ClusterIP   10.101.145.83   <none>        80/TCP,443/TCP   20h
nginx-svc-external   ClusterIP   10.97.162.95    <none>        80/TCP           8m41s
~~~     # 通過service代理獲取的百度的資料
~~~     可以看到直接curl百度資料和curl.service的資料一致,
~~~     說明service代理k8s外部應用OK

[root@k8s-master01 ~]# curl 10.97.162.95 -I     // 可以看到返回值是一樣的,所以說它已經代理到百度上去了。
HTTP/1.1 200 OK
Date: Wed, 21 Apr 2021 06:48:37 GMT
Server: Apache
Last-Modified: Tue, 12 Jan 2010 13:48:00 GMT
ETag: "51-47cf7e6ee8400"
Accept-Ranges: bytes
Content-Length: 81
Cache-Control: max-age=86400
Expires: Thu, 22 Apr 2021 06:48:37 GMT
Connection: Keep-Alive
Content-Type: text/html 








===============================END===============================


Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart                                                                                                                                                    ——W.S.Landor



來自為知筆記(Wiz)