K8S三 K8S部署微服务应用

一 用k8s部署微服务应用

以我们之前用docker部署过的eureka应用为例,首先添加配置文件eureka-app-deployment.yaml用于创建Deployment

apiVersion: apps/v1
kind: Deployment
metadata:name: eureka-app-deployment # deployment名字labels:app: eureka-app
spec:replicas: 1 #副本个数selector:matchLabels:app: eureka-app template:metadata:labels:app: eureka-appspec:containers:- name: eureka-app #pod名字# 指定Docker Hub中的镜像地址image: zhuge666/microservice-eureka-server:0.0.1ports:- containerPort: 8761 # 容器内部端口

通过应用配置文件来创建一个Deployment部署

kubectl apply -f eureka-app-deployment.yaml

查看下deploy和pod信息

kubectl get deploy,pod

在这里插入图片描述
我们可以通过kubectl logs命令来查看应用的启动日志

kubectl logs -f  pod/eureka-app-deployment-7cd4b6f4d4-lkzs4

在这里插入图片描述
如果想要从外部访问应用,需要创建Service,添加配置文件eureka-app-service.yaml用于创建Service

apiVersion: v1
kind: Service
metadata:name: eureka-app-service
spec:type: NodePortselector:app: eureka-app #选择有该标签的pod容器ports:- name: httpprotocol: TCPport: 8761  #service的端口targetPort: 8761  #pod的端口,一般与pod内部容器的服务端口一致

通过应用配置文件来创建Service

kubectl apply -f eureka-app-service.yaml

查看服务Service信息

[macro@linux-local k8s]$ kubectl get services

在这里插入图片描述
此时就可以通过外网来访问了:http://192.168.65.160:30558/
在这里插入图片描述

二 用k8s部署电商项目微服务

以电商微服务里的product服务为例,我们来创建对应的deployment和service,做之前需要把商品服务做成镜像推到docker镜像仓库里去,参考之前的docker章节。

docker login
docker tag mall/tulingmall-product:0.0.1 zhuge666/tulingmall-product:0.0.1
docker push zhuge666/tulingmall-product:0.0.1

新增文件tulingmall-product-deployment.yaml,内容如下:

apiVersion: apps/v1
kind: Deployment
metadata:name: tulingmall-product-deploymentlabels:app: tulingmall-product
spec:replicas: 1selector:matchLabels:app: tulingmall-producttemplate:metadata:labels:app: tulingmall-productspec:containers:- name: tulingmall-productimage: zhuge666/tulingmall-product:0.0.1#imagePullPolicy: Always  # 1)Always 总是拉取镜像, 2)IfNotPresent(默认该值) 本地有则使用本地镜像,3)Never 只使用本地镜像,从不拉取,即使本地没有镜像ports:- containerPort: 8866env: # 环境- name: TZvalue: Asia/Shanghai- name: spring.cloud.nacos.config.server-addr # 指定配置中心nacosvalue: 192.168.65.42:8848- name: LOG_FILEvalue: /var/logsvolumeMounts:- mountPath: /var/logs #容器日志目录(挂载到下边的宿主机目录)name: log-volumevolumes:- name: log-volumehostPath:path: /mydata/k8s-app/tulingmall-product/logs #宿主机目录type: DirectoryOrCreatednsPolicy: Default  #继承Pod所在宿主机的DNS设置,使pod能访问外网

配置环境变量
在这里插入图片描述

执行如下命令创建商品服务的deployment:

kubectl apply -f tulingmall-product-deployment.yaml
# 查看
kubectl get all

新增文件tulingmall-product-service.yaml,内容如下:
(其实我们实际部署时不需要写这个文件,因为微服务里有fegin或Ribbion这种内部调用框架去互相调用服务;但是部署gateway时一定要配置对应的service.yaml文件,因为gateway项目是需要外网访问的)

apiVersion: v1
kind: Service
metadata:name: tulingmall-product-service
spec:type: NodePortselector:app: tulingmall-productports:- name: httpprotocol: TCPport: 8866targetPort: 8866

执行如下命令创建商品服务的service:

kubectl apply -f tulingmall-product-service.yaml

查看商品服务的对外暴露端口:
在这里插入图片描述
访问下查询商品的接口,如果有json数据返回,代表服务正常:

http://192.168.65.210:30911/pms/productInfo/1

在这里插入图片描述
用相同的方法部署下电商里的order,member,gateway,authcenter等服务,这里不一一详述了,附上每个服务k8s部署的yaml文件供大家参考。

authcenter服务
tulingmall-authcenter-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:name: tulingmall-authcenter-deploymentlabels:app: tulingmall-authcenter
spec:replicas: 1selector:matchLabels:app: tulingmall-authcentertemplate:metadata:labels:app: tulingmall-authcenterspec:containers:- name: tulingmall-authcenterimage: zhuge666/tulingmall-authcenter:0.0.1imagePullPolicy: Alwaysports:- containerPort: 9999env: - name: TZvalue: Asia/Shanghai- name: spring.cloud.nacos.config.server-addrvalue: 192.168.65.42:8848- name: LOG_FILEvalue: /var/logsvolumeMounts:- mountPath: /var/logsname: log-volumevolumes:- name: log-volumehostPath:path: /mydata/k8s-app/tulingmall-authcenter/logstype: DirectoryOrCreatednsPolicy: Default  #继承Pod所在宿主机的DNS设置,使pod能访问外网

tulingmall-authcenter-service.yaml

apiVersion: v1
kind: Service
metadata:name: tulingmall-authcenter-service
spec:type: NodePortselector:app: tulingmall-authcenterports:- name: httpprotocol: TCPport: 9999targetPort: 9999

gateway服务
tulingmall-gateway-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:name: tulingmall-gateway-deploymentlabels:app: tulingmall-gateway
spec:replicas: 1selector:matchLabels:app: tulingmall-gatewaytemplate:metadata:labels:app: tulingmall-gatewayspec:containers:- name: tulingmall-gatewayimage: zhuge666/tulingmall-gateway:0.0.1imagePullPolicy: Alwaysports:- containerPort: 8888env:- name: TZvalue: Asia/Shanghai- name: spring.cloud.nacos.config.server-addrvalue: 192.168.65.42:8848- name: LOG_FILEvalue: /var/logsvolumeMounts:- mountPath: /var/logsname: log-volumevolumes:- name: log-volumehostPath:path: /mydata/k8s-app/tulingmall-gateway/logstype: DirectoryOrCreatednsPolicy: Default  #继承Pod所在宿主机的DNS设置,使pod能访问外网

tulingmall-gateway-service.yaml
(其他模块可以不写自己的service.yaml,gateway需要外网访问,所以一定要写service.yaml)

apiVersion: v1
kind: Service
metadata:name: tulingmall-gateway-service
spec:type: NodePortselector:app: tulingmall-gatewayports:- name: httpprotocol: TCPport: 8888targetPort: 8888

order服务
tulingmall-order-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:name: tulingmall-order-deploymentlabels:app: tulingmall-order
spec:replicas: 1selector:matchLabels:app: tulingmall-ordertemplate:metadata:labels:app: tulingmall-orderspec:containers:- name: tulingmall-orderimage: zhuge666/tulingmall-order:0.0.1imagePullPolicy: Alwaysports:- containerPort: 8844env:- name: TZvalue: Asia/Shanghai- name: spring.cloud.nacos.config.server-addrvalue: 192.168.65.42:8848- name: LOG_FILEvalue: /var/logs- name: JAVA_TOOL_OPTIONSvalue: -Xms1G -Xmx1G -Xmn512M -Xss512K -XX:MetaspaceSize=256M -XX:MaxMetaspaceSize=256M -javaagent:/agent/skywalking-agent.jar -DSW_AGENT_NAME=tulingmall-order -DSW_AGENT_COLLECTOR_BACKEND_SERVICES=192.168.65.204:11800volumeMounts:- mountPath: /var/logsname: log-volumevolumes:- name: log-volumehostPath:path: /mydata/k8s-app/tulingmall-order/logstype: DirectoryOrCreatednsPolicy: Default  #继承Pod所在宿主机的DNS设置,使pod能访问外网   

tulingmall-order-service.yaml

apiVersion: v1
kind: Service
metadata:name: tulingmall-order-service
spec:type: NodePortselector:app: tulingmall-orderports:- name: httpprotocol: TCPport: 8844targetPort: 8844

member服务
tulingmall-member-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:name: tulingmall-member-deploymentlabels:app: tulingmall-member
spec:replicas: 1selector:matchLabels:app: tulingmall-membertemplate:metadata:labels:app: tulingmall-memberspec:containers:- name: tulingmall-memberimage: zhuge666/tulingmall-member:0.0.1imagePullPolicy: Alwaysports:- containerPort: 8877env:- name: TZvalue: Asia/Shanghai- name: spring.cloud.nacos.config.server-addrvalue: 192.168.65.42:8848- name: LOG_FILEvalue: /var/logs- name: JAVA_TOOL_OPTIONSvalue: -Xmx1g -Xms1g -XX:MaxMetaspaceSize=256m -javaagent:/agent/skywalking-agent.jar -DSW_AGENT_NAME=tulingmall-member -DSW_AGENT_COLLECTOR_BACKEND_SERVICES=192.168.65.204:11800volumeMounts:- mountPath: /var/logsname: log-volumevolumes:- name: log-volumehostPath:path: /mydata/k8s-app/tulingmall-member/logstype: DirectoryOrCreatednsPolicy: Default  #继承Pod所在宿主机的DNS设置,使pod能访问外网

tulingmall-member-service.yaml

apiVersion: v1
kind: Service
metadata:name: tulingmall-member-service
spec:type: NodePortselector:app: tulingmall-memberports:- name: httpprotocol: TCPport: 8877targetPort: 8877

创建网关的Ingress(相当于Nginx)
最后,我们来创建网关服务的Ingress,创建文件tulingmall-gateway-ingress.yaml,内容如下:

apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:name: tulingmall-gateway-ingress
spec:rules:- host: gateway.tuling.com  #转发域名http:paths:- path: /backend:serviceName: tulingmall-gateway-serviceservicePort: 8888  #service的端口

执行如下命令生效规则:

kubectl apply -f tulingmall-gateway-ingress.yaml

查看生效的ingress规则:

kubectl get ing

在这里插入图片描述
在访问机器配置host,win10客户机在目录:C:\Windows\System32\drivers\etc,在host里增加如下host(ingress部署的机器ip对应访问的域名)

192.168.65.203  gateway.tuling.com
或者
192.168.65.210  gateway.tuling.com

配置完后直接在客户机用域名请求下网关:
在这里插入图片描述

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

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

相关文章

【C++】CentOS环境搭建-升级CMAKE

【C】CentOS环境搭建-升级CMAKE CMAKE报错CMake 3.12 or higher is required. You are running version 2.8.12.2升级步骤1.移除当前的cmake2.安装必要的构建工具和库3.下载最新的cmake源码并解压5.编译和安装6.验证安装 CMAKE报错CMake 3.12 or higher is required. You are r…

MySQL存储引擎详解

存储引擎 MySQL体系结构 连接层:与客户端连接,权限校验、连接池服务层:SQL接口和解析、查询优化、缓存、函数引擎层:索引、存储引擎存储层:系统文件、日志(Redo、Undo等) 存储引擎介绍 不同的…

免费远程控制软件哪个好用

免费远程控制软件哪个好用 在现今高度信息化的社会,远程控制软件已成为许多用户进行远程办公、技术支持和教育培训的重要工具。市面上有许多免费的远程控制软件,但哪款才是最好用的呢?本文将为您介绍几款热门的免费远程控制软件,…

Matlab: ode45解微分方程——以弹簧振子模型为例

简介: 在科学和工程中,我们经常遇到描述事物变化的微分方程。这些方程可以帮助我们理解从行星运动到药物在体内的扩散等各种现象。但是,很多微分方程非常复杂,手动求解几乎不可能。这时,我们就可以使用像 ode45这样的…

【iOS】frame与bounds区别

文章目录 前言framebounds两者区别size的区别总结 前言 在学习响应者链的过程中用到了frame与bounds的混用,这两个属性经常出现在我们的开发中,特别撰写一篇博客分析区别 首先,我们来看一下iOS特有的坐标系,在iOS坐标系中以左上…

【debug】如何使用pycharm对代码调试

后续会将所有debug中遇到的知识放入,建议关注收藏 本站友情链接: 基本理论专栏(当前更新好的debug所有内容都在这里) 【debug】报错解决方法(CondaHTTPError:HTTP 000 connection failed for url&#xff…

【回溯 状态压缩 深度优先】37. 解数独

本文涉及知识点 回溯 状态压缩 深度优先 LeetCode37. 解数独 编写一个程序,通过填充空格来解决数独问题。 数独的解法需 遵循如下规则: 数字 1-9 在每一行只能出现一次。 数字 1-9 在每一列只能出现一次。 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只…

[C++核心编程-06]----C++类和对象之对象模型和this指针

🎩 欢迎来到技术探索的奇幻世界👨‍💻 📜 个人主页:一伦明悦-CSDN博客 ✍🏻 作者简介: C软件开发、Python机器学习爱好者 🗣️ 互动与支持:💬评论 &…

Microsoft 365 for Mac v16.84 office365全套办公软件

Microsoft 365 for Mac是一款功能丰富的办公软件套件,为Mac用户提供了丰富的功能和工具,提高了工作效率和协作能力。Microsoft 365 for Mac是一款专为Mac用户设计的订阅式办公软件套件,旨在提高生产力和效率。 Microsoft 365 for Mac v16.84正…

ubantu安装docker以及docker-compose

ubantu安装docker以及docker-compose 安装docker1、从官方存储库中安装Docker2、启动Docker服务3、验证 安装docker compose使用docker部署服务1、需要再opt文件夹下创建以下文件夹,/opt文件夹目录说明2、可将已备份对应文件夹拷至对应文件夹下3、在/opt/compose目录…

霍金《时间简史 A Brief History of Time》书后索引(A--D)

图源:Wikipedia INDEX A Abacus Absolute position Absolute time Absolute zero Acceleration Age of the universe Air resistance Albrecht, Andreas Alpha Centauri Alpher, Ralph Anthropic principle Antigravity Antiparticles Aristotle Arrows of time …

基于Vant UI的微信小程序开发(随时更新的写手)

基于Vant UI的微信小程序开发✨ (一)悬浮浮动1、效果图:只要无脑引用样式就可以了2、页面代码3、js代码4、样式代码 (二)底部跳转1、效果图:点击我要发布跳转到发布的页面2、js代码3、页面代码4、app.json代…

我觉得POC应该贴近实际

今天我看到一位老师给我一份测试数据。 这是三个国产数据库。算是分布式的。其中有两个和我比较熟悉,但是这个数据看上去并不好。看上去第一个黄色的数据库数据是这里最好的了。但是即使如此,我相信大部分做数据库的人都知道。MySQL和PostgreSQL平时拿出…

Spark Streaming笔记总结(保姆级)

万字长文警告!!! 目录 一、离线计算与流式计算 1.1 离线计算 1.1.1 离线计算的特点 1.1.2 离线计算的应用场景 1.1.3 离线计算代表技术 1.2 流式计算 1.2.1 流式计算的特点 1.2.2 流式计算的应用场景 1.2.3 流式计算的代表技术 二…

kernel32.dll丢失要如何解决?电脑kernel32.dll文件下载方法

kernel32.dll丢失要怎么解决才好?其实针对这个问题还是有很多种的解决方法的,只要你明白了kernel32.dll的作用,了解kernel32.dll,那么就可以有很多种方法去解决,下面一起来看看吧。 一.了解kernel32.dll文件 kernel32…

6个超TM好用的神仙App推荐!

1. AI文本视频生成工具——Jurilu Jurilu 是一款功能强大的 AI 文本视频生成器,允许用户快速将文本内容转换成极具吸引力的视频。它的使用非常简单:只需要输入文字,选择想要的样式和模板,Jurilu 就会自动将文字转换成生动的视频。…

Vue项目npm install certificate has expired报错解决方法

1.Vue项目 npm install 安装依赖突然报错: npm ERR! code CERT_HAS_EXPIRED npm ERR! errno CERT_HAS_EXPIRED npm ERR! request to https://registry.npm.taobao.org/zrender/download/zrender-4.3.0.tgz failed, reason: certificate has expired npm ERR! A com…

一个可以同时使用USB和WIFI传输文件到电脑的软件

双轨快传 结合USB2.0和WIFI6技术,通过1000Mbps网口实现每秒高达150MB的传输速率(理论上可达40MB/s通过USB和110MB/s通过WIFI)。 使用 模式 支持普通模式和Root模式,Root模式可访问~/Android/data/与/data/data/目录下的文件。 …

ETL-kettle数据转换及组件使用详解

目录 一、txt文本转换成excel 1、新建、转换 2、构建流程图 3、配置数据流图中的各个组件 3.1、配置文件文本输入组件 3.2、 配置Excel输出组件 4、保存执行 二、excel转换成mysql (1)在MySQL数据库中创建数据库,这个根据自身情况。我…

一文了解spring的aop知识

推荐工具 objectlog 对于重要的一些数据,我们需要记录一条记录的所有版本变化过程,做到持续追踪,为后续问题追踪提供思路。objectlog工具是一个记录单个对象属性变化的日志工具,工具采用spring切面和mybatis拦截器相关技术编写了api依赖包&a…