機(jī)票特價(jià)網(wǎng)站建設(shè)seo咨詢解決方案
使用 Service 把前端連接到后端
如何創(chuàng)建前端(Frontend)微服務(wù)和后端(Backend)微服務(wù)。后端微服務(wù)是一個(gè) hello 歡迎程序。 前端通過 nginx 和一個(gè) Kubernetes 服務(wù)暴露后端所提供的服務(wù)。
- 使用部署對象(Deployment object)創(chuàng)建并運(yùn)行一個(gè)
hello
后端微服務(wù) - 使用一個(gè) Service 對象將請求流量發(fā)送到后端微服務(wù)的多個(gè)副本
- 同樣使用一個(gè) Deployment 對象創(chuàng)建并運(yùn)行一個(gè)
nginx
前端微服務(wù) - 配置前端微服務(wù)將請求流量發(fā)送到后端微服務(wù)
- 使用
type=NodePort
的 Service 對象將前端微服務(wù)暴露到集群外部
使用Depolyment創(chuàng)建后端
backend-deploy.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:name: backend
spec:selector:matchLabels:app: hellotier: backendtrack: stablereplicas: 3template:metadata:labels:app: hellotier: backendtrack: stablespec:containers:- name: helloimage: "gcr.io/google-samples/hello-go-gke:1.0"ports:- name: httpcontainerPort: 80
...
查看后端deployment信息
kubectl describe deployment backend
Name: backend
Namespace: default
CreationTimestamp: Wed, 18 Oct 2023 21:55:25 +0800
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=hello,tier=backend,track=stable
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:Labels: app=hellotier=backendtrack=stableContainers:hello:Image: gcr.io/google-samples/hello-go-gke:1.0Port: 80/TCPHost Port: 0/TCPEnvironment: <none>Mounts: <none>Volumes: <none>
Conditions:Type Status Reason---- ------ ------Available True MinimumReplicasAvailableProgressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: backend-685445b9db (3/3 replicas created)
Events:Type Reason Age From Message---- ------ ---- ---- -------Normal ScalingReplicaSet 85s deployment-controller Scaled up replica set backend-685445b9db to 3
創(chuàng)建Service對象
將請求從前端發(fā)送到后端的關(guān)鍵是后端 Service。Service 創(chuàng)建一個(gè)固定 IP 和 DNS 解析名入口, 使得后端微服務(wù)總是可達(dá)。Service 使用 選擇算符來尋找目標(biāo) Pod。
backend-svc.yml
---
apiVersion: v1
kind: Service
metadata:name: hello
spec:selector:app: hellotier: backendports:- protocol: TCPport: 80targetPort: http
...
這里的targetPort就是容器開放的80端口(http就是80端口)
配置文件中,你可以看到名為 hello
的 Service 將流量路由到包含 app: hello
和 tier: backend
標(biāo)簽的 Pod。
查看Service信息:
root@k8s-master:~# kubectl describe svc hello
Name: hello
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=hello,tier=backend
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.110.113.146
IPs: 10.110.113.146
Port: <unset> 80/TCP
TargetPort: http/TCP
Endpoints: 10.244.169.168:80,10.244.169.169:80,10.244.169.170:80
Session Affinity: None
Events: <none>
此時(shí),你已經(jīng)有了一個(gè)運(yùn)行著
hello
應(yīng)用的三個(gè)副本的backend
Deployment,你也有了 一個(gè) Service 用于路由網(wǎng)絡(luò)流量。不過,這個(gè)服務(wù)在集群外部無法訪問也無法解析。
創(chuàng)建前端
現(xiàn)在你已經(jīng)有了運(yùn)行中的后端應(yīng)用,你可以創(chuàng)建一個(gè)可在集群外部訪問的前端,并通過代理 前端的請求連接到后端。
前端使用被賦予后端 Service 的 DNS 名稱將請求發(fā)送到后端工作 Pods。這一 DNS 名稱為
hello
,就是Service的yml文件中name
字段的取值。
前端 Deployment 中的 Pods 運(yùn)行一個(gè) nginx 鏡像,這個(gè)已經(jīng)配置好的鏡像會(huì)將請求轉(zhuǎn)發(fā) 給后端的
hello
Service。
frontend-nginx.conf
(這個(gè)配置文件在前端鏡像里存在)
# Backend 是 nginx 的內(nèi)部標(biāo)識符,用于命名以下特定的 upstream
upstream Backend {# hello 是 Kubernetes 中的后端服務(wù)所使用的內(nèi)部 DNS 名稱server hello;
}
server {
listen 80;
location / {# 以下語句將流量通過代理方式轉(zhuǎn)發(fā)到名為 Backend 的上游proxy_pass http://Backend;
}
}
與后端類似,前端用包含一個(gè) Deployment 和一個(gè) Service。后端與前端服務(wù)之間的一個(gè) 重要區(qū)別是前端 Service 的配置文件包含了
type:NodePort
(這里官方文檔使用的是LoadBalancer,需要使用外部設(shè)備)
frontend-deploy.yml
---
apiVersion: apps/v1
kind: Deployment
metadata:name: frontend
spec:selector:matchLabels:app: hellotier: frontendtrack: stablereplicas: 1template:metadata:labels:app: hellotier: frontendtrack: stablespec:containers:- name: nginximage: "gcr.io/google-samples/hello-frontend:1.0"lifecycle:preStop:exec:command: ["/usr/sbin/nginx","-s","quit"]
...
frontend-svc.yml
---
apiVersion: v1
kind: Service
metadata:name: frontend
spec:selector:app: hellotier: frontendports:- protocol: "TCP"port: 80targetPort: 80type: NodePort
...
通過前端發(fā)送流量
查看前端Service信息:
root@k8s-master:~# kubectl describe svc frontend
Name: frontend
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=hello,tier=frontend
Type: NodePort
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.104.187.207
IPs: 10.104.187.207
Port: <unset> 80/TCP
TargetPort: 80/TCP
NodePort: <unset> 31649/TCP #這里31649就是集群外暴露的端口號
Endpoints: 10.244.169.171:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
查看集群節(jié)點(diǎn)IP:
root@k8s-master:~# kubectl get node -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
k8s-master Ready control-plane,master 679d v1.22.0 192.168.123.150 <none> Ubuntu 18.04.5 LTS 4.15.0-213-generic docker://20.10.0
k8s-node1 Ready <none> 679d v1.22.0 192.168.123.151 <none> Ubuntu 18.04.5 LTS 4.15.0-213-generic docker://20.10.0
k8s-node2 Ready <none> 679d v1.22.0 192.168.123.152 <none> Ubuntu 18.04.5 LTS 4.15.0-213-generic docker://20.10.0
任意選擇集群IP進(jìn)行訪問:
root@k8s-master:~# curl 192.168.123.150:31649
{"message":"Hello"}
root@k8s-master:~# curl 192.168.123.151:31649
{"message":"Hello"}
root@k8s-master:~# curl 192.168.123.152:31649
{"message":"Hello"}
就可以看到這樣的信息,同時(shí)外部也可以通過IP對集群進(jìn)行訪問