使用 Service 把前端连接到后端

使用 Service 把前端连接到后端

如何创建前端(Frontend)微服务和后端(Backend)微服务。后端微服务是一个 hello 欢迎程序。 前端通过 nginx 和一个 Kubernetes 服务暴露后端所提供的服务。

  • 使用部署对象(Deployment object)创建并运行一个 hello 后端微服务
  • 使用一个 Service 对象将请求流量发送到后端微服务的多个副本
  • 同样使用一个 Deployment 对象创建并运行一个 nginx 前端微服务
  • 配置前端微服务将请求流量发送到后端微服务
  • 使用 type=NodePort 的 Service 对象将前端微服务暴露到集群外部

使用Depolyment创建后端

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

在这里插入图片描述

创建Service对象

将请求从前端发送到后端的关键是后端 Service。Service 创建一个固定 IP 和 DNS 解析名入口, 使得后端微服务总是可达。Service 使用 选择算符来寻找目标 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: hellotier: backend 标签的 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>

在这里插入图片描述

此时,你已经有了一个运行着 hello 应用的三个副本的 backend Deployment,你也有了 一个 Service 用于路由网络流量。不过,这个服务在集群外部无法访问也无法解析。

创建前端

现在你已经有了运行中的后端应用,你可以创建一个可在集群外部访问的前端,并通过代理 前端的请求连接到后端。

前端使用被赋予后端 Service 的 DNS 名称将请求发送到后端工作 Pods。这一 DNS 名称为 hello,就是Service的yml文件中 name 字段的取值。

前端 Deployment 中的 Pods 运行一个 nginx 镜像,这个已经配置好的镜像会将请求转发 给后端的 hello Service。

frontend-nginx.conf (这个配置文件在前端镜像里存在)

# Backend 是 nginx 的内部标识符,用于命名以下特定的 upstream
upstream Backend {# hello 是 Kubernetes 中的后端服务所使用的内部 DNS 名称server hello;
}
server {
listen 80;
location / {# 以下语句将流量通过代理方式转发到名为 Backend 的上游proxy_pass http://Backend;
}
}

与后端类似,前端用包含一个 Deployment 和一个 Service。后端与前端服务之间的一个 重要区别是前端 Service 的配置文件包含了 type:NodePort (这里官方文档使用的是LoadBalancer,需要使用外部设备)

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
...

在这里插入图片描述

通过前端发送流量

查看前端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>

查看集群节点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进行访问:

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"}

就可以看到这样的信息,同时外部也可以通过IP对集群进行访问

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/144483.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux下Samba服务安装及启用全攻略

Linux下Samba服务安装及启用全攻略 前言一、安装SSH Server二、安装Samba Server1.安装net-tool2.建立账号的samba3.windows通过Samba与linux共享文件4.使用远程工具登录Linux 总结 前言 提示&#xff1a;本文详解了在Linux系统下如何安装和启用Samba服务&#xff0c;涵盖了从…

postgresql14-模式的管理(三)

基本概念 postgresql成为数据库管理系统DBMS&#xff0c;在内存中以进程的形态运行起来成为一个实例&#xff0c;可管理多个database。 数据库databases&#xff1a;包含表、索引、视图、存储过程&#xff1b; 模式schema&#xff1a;多个对象组成一个模式&#xff0c;多个模…

linux安装visual studio code

下载 https://code.visualstudio.com/ 下载.deb文件 安装 假如文件被下载到了 /opt目录下 进入Opt目录&#xff0c;右键从当前目录打开终端。 输入下面的安装命令。 sudo apt-get install ./code_1.83.1-1696982868_amd64.deb 安装成功。 配置 打开 visual studio cod…

idea热加载,JRebel 插件是目前最好用的热加载插件,它支持 IDEA Ultimate 旗舰版、Community 社区版

1.如何安装 ① 点击 https://plugins.jetbrains.com/plugin/4441-jrebel-and-xrebel/versions 地址&#xff0c;下载 2022.4.1 版本。如下图所示&#xff1a; ② 打开 [Preference -> Plugins] 菜单&#xff0c;点击「Install Plugin from Disk…」按钮&#xff0c;选择刚下…

400 The plain HTTP request was sent to HTTPS port

接口请求发生问题&#xff1a; 解决方法&#xff1a; Nginx HTTP服务器的报错 “400 Bad Request: The plain HTTP request was sent to HTTPS port”&#xff0c;本文将讲解如何解决这个问题。简单从报错的字面意思上来看&#xff0c;是因为HTTP请求被发送到HTTPS端口&#x…

web开发初级工程师学习笔记

web开发初级工程师学习笔记 前端开发工具实验1 VS Code 初体验介绍 前端开发工具 实验1 VS Code 初体验 介绍 VS Code 环境提供的是一个可以在浏览器中使用原生 VS Code 编辑代码的程序。在该环境中&#xff0c;你可以使用到与本地安装近乎一致的 VS Code 程序来编辑代码文件…

整理MongoDB文档:身份验证

整理MongoDB文档:身份验证 个人博客&#xff0c;求关注。 文章概叙 本文主要讲MongoDB在单机状态下的账户配置。理解了MongoDB的语法&#xff0c;对于如何配置用户权限会知道怎么配置&#xff0c;但是请注意给谁配置什么权限才是最重要的。 最小权限原则 系统的每个程序或者…

IMX6ULL开发——第一个驱动程序

实现第一个应用程序&#xff1a;在IMX6ULL开发板上运行驱动程序hello_drv_test hello_drv_test #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> #include <string.h>/** ./hel…

Vue Router - 路由的使用、两种切换方式、两种传参方式、嵌套方式

目录 一、Vue Router 1.1、下载 1.2、基本使用 a&#xff09;引入 vue-router.js&#xff08;注意&#xff1a;要在 Vue.js 之后引入&#xff09;. b&#xff09;创建好路由规则 c&#xff09;注册到 Vue 实例中 d&#xff09;展示路由组件 1.3、切换路由的两种方式 1.…

postgresql14-表的管理(四)

表table 创建表 CREATE TABLE table_name --表名 (column_name data_type column_constraint, --字段名、字段类型、约束字段&#xff08;可选&#xff09;column_name data_type, --表级别约束字段...,table_constraint );CREATE TABLE emp1 --创建表 AS SELECT * FROM empl…

JavaWeb基础

B/S 和 C/S 的区别 B/S( Browser / Server)&#xff1a;即浏览器/服务器&#xff0c;访问的网站资源存放在服务器&#xff0c;无论服务器存放什么&#xff0c;客户端只需要安装一个浏览器&#xff0c;通过这个浏览器访问服务器的资源&#xff0c;B/S架构也有客户端&#xff0c…

【AIGC核心技术剖析】Hotshot-XL 一种 AI 文本转 GIF 模型(论文 + 代码:经过训练可与Stable Diffusion XL一起使用)

Hotshot-XL 是一种 AI 文本转 GIF 模型,经过训练可与Stable Diffusion XL一起使用。 Hotshot-XL 可以使用任何经过微调的 SDXL 模型生成 GIF。这意味着两件事: 您将能够使用您可能想要使用的任何现有或新微调的 SDXL 模型制作 GIF。 如果您想制作个性化主题的 GIF,您可以…