k8s中部署springcloud

时间:2022-02-03 23:49:31

安装和配置数据存储仓库MySQL

  • 1.MySQL简介
  • 2.MySQL特点
  • 3.安装和配置MySQL
  • 4.在MySQL数据库导入数据
  • 5.对MySQL数据库进行授权

1.MySQL简介

MySQL 是一款安全、跨平台、高效的,并与PHP、Java等主流编程语言紧密结合的数据库系统。该数据库系统是由瑞典的MySQL AB公司开发、发布并支持,由 MySQL的初始开发人员 David Axmark 和 Michael Monty Widenius 于 1995 年建立的。MySQL 的象征符号是一只名为 Sakila 的海豚,代表着 MySQL 数据库的速度、能力、精确和优秀本质。

MySQL logo:

k8s中部署springcloud

目前 MySQL 被广泛地应用在 Internet 上的中小型网站中。由于其体积小、速度快、总体拥有成本低,尤其是开放源码这一特点,使得很多公司都采用 MySQL 数据库以降低成本。



MySQL 数据库可以称得上是目前运行速度最快的SQL语言数据库之一。除了具有许多其他数据库所不具备的功能外,MySQL 数据库还是一种完全免费的产品,用户可以直接通过网络下载 MySQL 数据库,而不必支付任何费用。

2.MySQL特点

1) 功能强大

MySQL 中提供了多种数据库存储引擎,各引擎各有所长,适用于不同的应用场合,用户可以选择最合适的引擎以得到最高性能,可以处理每天访问量超过数亿的高强度的搜索 Web 站点。MySQL5 支持事务、视图、存储过程、触发器等。

2) 支持跨平台

MySQL 支持至少 20 种以上的开发平台,包括 Linux、Windows、FreeBSD 、IBMAIX、AIX、FreeBSD 等。这使得在任何平台下编写的程序都可以进行移植,而不需要对程序做任何的修改。

3) 运行速度快

高速是 MySQL 的显著特性。在 MySQL 中,使用了极快的 B 树磁盘表(MyISAM)和索引压缩;通过使用优化的单扫描多连接,能够极快地实现连接;SQL 函数使用高度优化的类库实现,运行速度极快。

4) 支持面向对象

PHP 支持混合编程方式。编程方式可分为纯粹面向对象、纯粹面向过程、面句对象与面向过程混合 3 种方式。

5) 安全性高

灵活和安全的权限与密码系统,允许基本主机的验证。连接到服务器时,所有的密码传输均采用加密形式,从而保证了密码的安全。

6) 成本低

MySQL 数据库是一种完全免费的产品,用户可以直接通过网络下载。

7) 支持各种开发语言

MySQL 为各种流行的程序设计语言提供支持,为它们提供了很多的 API 函数,包括 PHP、ASP.NET、Java、Eiffel、Python、Ruby、Tcl、C、C++、Perl 语言等。

8) 数据库存储容量大

MySQL 数据库的最大有效表尺寸通常是由操作系统对文件大小的限制决定的,而不是由 MySQL 内部限制决定的。InnoDB 存储引擎将 InnoDB 表保存在一个表空间内,该表空间可由数个文件创建,表空间的最大容量为 64TB,可以轻松处理拥有上千万条记录的大型数据库。

9) 支持强大的内置函数

PHP 中提供了大量内置函数,几乎涵盖了 Web 应用开发中的所有功能。它内置了数据库连接、文件上传等功能,MySQL 支持大量的扩展库,如 MySQLi 等,可以为快速开发 Web 应用提供便利。

3.安装MySQL

在192.168.40.130上操作:

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum install mysql-server -y 权限设置
chown mysql:mysql -R /var/lib/mysql 初始化MySQL
mysqld --initialize 启动MySQL
systemctl start mysqld 查看 MySQL 运行状态
systemctl status mysqld mysql安装成功后,默认的root用户密码为空,你可以使用以下命令来创建root用户的密码,密码设置成111111 mysqladmin -u root password "111111" 登陆数据库
mysql -uroot -p111111 创建数据库tb_order、tb_product、tb_stock
create database tb_product;
create database tb_stock;
create database tb_order; 4.在Mysql数据库导入数据
把相应的sql语句上传到mysql机器的root目录下,sql文件分别是order.sql、product.sql、stock.sql use tb_order
source /root/order.sql use tb_stock
source /root/stock.sql use tb_product
source /root/product.sql 5.对MySQL数据库授权
grant all on *.* to 'root'@'10.244.%.%' identified by '111111';
grant all on *.* to 'root'@'192.168.%.%' identified by '111111';
flush privileges; grant all on *.* to 'root'@'%' identified by '111111';
flush privileges;

安装openjdk和maven

yum install java-1.8.0-openjdk  maven-3.0.5* -y

上传微服务源码包到k8s的

unzip microservic-test.zip
cd microservic-test

修改源代码,更改数据库连接地址

1)修改库存数据库
cat /root/microservic-test/stock-service/stock-service-biz/src/main/resources/ application-fat.yml jdbc:mysql://192.168.40.130:3306/tb_stock?characterEncoding=utf-8
#变成自己的数据库地址 2)修改产品数据库
cat /root/microservic-test/product-service/product-service-biz/src/main/resources/application-fat.yml jdbc:mysql://192.168.40.130:3306/tb_product?characterEncoding=utf-8
#变成自己的数据库地址 3)修改订单数据库
cat /root/microservic-test/order-service/order-service-biz/src/main/resources/ application-fat.yml url: jdbc:mysql://192.168.40.130:3306/tb_order?characterEncoding=utf-8
#变成自己的数据库地址

通过Maven编译、构建、打包源代码

修改源代码之后回到/root/microservic-test目录下执行如下命令:
mvn clean package -D maven.test.skip=true 看到如下说明编译打包已经成功了:
[INFO] simple-microservice ............................... SUCCESS [52.385s]
[INFO] basic-common ...................................... SUCCESS [0.001s]
[INFO] basic-common-core ................................. SUCCESS [6:11.156s]
[INFO] gateway-service ................................... SUCCESS [3:33.707s]
[INFO] eureka-service .................................... SUCCESS [12.075s]
[INFO] product-service ................................... SUCCESS [0.001s]
[INFO] product-service-api ............................... SUCCESS [0.271s]
[INFO] stock-service ..................................... SUCCESS [0.002s]
[INFO] stock-service-api ................................. SUCCESS [0.233s]
[INFO] product-service-biz ............................... SUCCESS [3.776s]
[INFO] stock-service-biz ................................. SUCCESS [0.332s]
[INFO] order-service ..................................... SUCCESS [0.000s]
[INFO] order-service-api ................................. SUCCESS [0.270s]
[INFO] order-service-biz ................................. SUCCESS [0.364s]
[INFO] basic-common-bom .................................. SUCCESS [0.000s]
[INFO] portal-service .................................... SUCCESS [0.738s]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11:13.305s
[INFO] Finished at: Wed Jan 20 15:53:22 CST 2021
[INFO] Final Memory: 92M/710M
[INFO] ------------------------------------------------------------------------

在k8s中部署Eureka组件

k8s中部署springcloudk8s中部署springcloud
cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors":["https://rsbud4vc.mirror.aliyuncs.com","https://registry.docker-cn.com","https://docker.mirrors.ustc.edu.cn","https://dockerhub.azk8s.cn","http://hub-mirror.c.163.com","http://qtid6917.mirror.aliyuncs.com"],
"insecure-registries":["192.168.40.132","harbor"],
"exec-opts":["native.cgroupdriver=systemd"],
"log-driver":"json-file",
"log-opts": {
"max-size": "100m"
},
"storage-driver":"overlay2",
"storage-opts": [
"overlay2.override_kernel_check=true"
]
}
EOF

修改docker镜像源及私有地址

创建拉取私有镜像仓库需要的secret

kubectl create ns ms && kubectl create secret docker-registry registry-pull-secret --docker-server=192.168.40.132 --docker-username=admin --docker-password=Harbor12345  -n ms

构建eureka docker镜像

k8s中部署springcloudk8s中部署springcloud
FROM java:8-jdk-alpine
RUN apk add -U tzdata && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY ./target/eureka-service.jar ./
COPY pinpoint /pinpoint
EXPOSE 8888
CMD java -jar -javaagent:/pinpoint/pinpoint-bootstrap-1.8.5.jar -Dpinpoint.agentId=${HOSTNAME} -Dpinpoint.applicationName=ms-eureka -Deureka.instance.hostname=${MY_POD_NAME}.eureka.ms /eureka-service.jar

Dockerfile

部署eureka

k8s中部署springcloudk8s中部署springcloud
#---
#apiVersion: extensions/v1beta1
#kind: Ingress
#metadata:
# name: eureka
# namespace: ms
# annotations: #注解信息
# kubernetes.io/ingress.class: "nginx"
#spec:
# rules:
# - host: eureka.ctnrs.com
# http:
# paths:
# - path: /
# backend:
# serviceName: eureka
# servicePort: 8888
---
apiVersion: v1
kind: Service
metadata:
name: eureka
namespace: ms
spec:
clusterIP: None
ports:
- port: 8888
name: eureka
selector:
project: ms
app: eureka --- apiVersion: apps/v1
kind: StatefulSet
metadata:
name: eureka
namespace: ms
spec:
replicas: 3
selector:
matchLabels:
project: ms
app: eureka
serviceName: "eureka"
template:
metadata:
labels:
project: ms
app: eureka
spec:
imagePullSecrets:
- name: registry-pull-secret
containers:
- name: eureka
image: 172.17.166.217/kubenetes/eureka:v4
ports:
- protocol: TCP
containerPort: 8888
env:
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
resources:
requests:
cpu: 0.5
memory: 256Mi
limits:
cpu: 1
memory: 1Gi
readinessProbe:
tcpSocket:
port: 8888
initialDelaySeconds: 60
periodSeconds: 10
livenessProbe:
tcpSocket:
port: 8888
initialDelaySeconds: 60
periodSeconds: 10

eureka.yaml

k8s中部署springcloud

在k8s中部署网关Gateway服务

build Gateway docker 镜像

k8s中部署springcloudk8s中部署springcloud
FROM java:8-jdk-alpine
RUN apk add -U tzdata && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY ./target/gateway-service.jar ./
COPY pinpoint /pinpoint
EXPOSE 9999
CMD java -jar -javaagent:/pinpoint/pinpoint-bootstrap-1.8.5.jar -Dpinpoint.agentId=${HOSTNAME} -Dpinpoint.applicationName=ms-gateway /gateway-service.jar

Dockerfile

k8s部署Gateway

k8s中部署springcloudk8s中部署springcloud
---
apiVersion: v1
kind: Service
metadata:
name: gateway
namespace: ms
spec:
ports:
- port: 9999
name: gateway
selector:
project: ms
app: gateway
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: gateway
namespace: ms
spec:
replicas: 2
selector:
matchLabels:
project: ms
app: gateway
template:
metadata:
labels:
project: ms
app: gateway
spec:
imagePullSecrets:
- name: registry-pull-secret
containers:
- name: gateway
image: 172.17.166.217/kubenetes/gateway:v1
# imagePullPolicy: Always
ports:
- protocol: TCP
containerPort: 9999
resources:
requests:
cpu: 0.5
memory: 256Mi
limits:
cpu: 1
memory: 1Gi
readinessProbe:
tcpSocket:
port: 9999
initialDelaySeconds: 60
periodSeconds: 10
livenessProbe:
tcpSocket:
port: 9999
initialDelaySeconds: 60
periodSeconds: 10

gateway.yaml

在k8s中部署其他微服务

构建前端portal服务镜像

k8s中部署springcloudk8s中部署springcloud
FROM java:8-jdk-alpine
RUN apk add -U tzdata && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY ./target/portal-service.jar ./
COPY pinpoint /pinpoint
EXPOSE 8080
CMD java -jar -javaagent:/pinpoint/pinpoint-bootstrap-1.8.5.jar -Dpinpoint.agentId=${HOSTNAME} -Dpinpoint.applicationName=ms-protal /portal-service.jar

Dockerfile

k8s部署portal服务

k8s中部署springcloudk8s中部署springcloud
#apiVersion: extensions/v1beta1
#kind: Ingress
#metadata:
# name: portal
# namespace: ms
#spec:
# rules:
# - host: portal.ctnrs.com
# http:
# paths:
# - path: /
# backend:
# serviceName: portal
# servicePort: 8080
---
apiVersion: v1
kind: Service
metadata:
name: portal
namespace: ms
spec:
ports:
- port: 8080
name: portal
selector:
project: ms
app: portal
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: portal
namespace: ms
spec:
replicas: 2
selector:
matchLabels:
project: ms
app: portal
template:
metadata:
labels:
project: ms
app: portal
spec:
imagePullSecrets:
- name: registry-pull-secret
containers:
- name: portal
image: 172.17.166.217/kubenetes/portal:v1
#imagePullPolicy: IfNotPreset
ports:
- protocol: TCP
containerPort: 8080
resources:
limits:
cpu: 1
memory: 1Gi
readinessProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 60
periodSeconds: 10
livenessProbe:
tcpSocket:
port: 8080
initialDelaySeconds: 60
periodSeconds: 10

portal.yaml

构建product服务镜像

k8s中部署springcloudk8s中部署springcloud
FROM java:8-jdk-alpine
RUN apk add -U tzdata && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY ./target/product-service-biz.jar ./
COPY pinpoint /pinpoint
EXPOSE 8010
CMD java -jar -javaagent:/pinpoint/pinpoint-bootstrap-1.8.5.jar -Dpinpoint.agentId=${HOSTNAME} -Dpinpoint.applicationName=ms-product /product-service-biz.jar

Dockerfile

k8s部署product服务

k8s中部署springcloudk8s中部署springcloud
apiVersion: apps/v1
kind: Deployment
metadata:
name: product
namespace: ms
spec:
replicas: 1
selector:
matchLabels:
project: ms
app: product
template:
metadata:
labels:
project: ms
app: product
spec:
imagePullSecrets:
- name: registry-pull-secret
containers:
- name: product
image: 172.17.166.217/kubenetes/product:v5
imagePullPolicy: Always
ports:
- protocol: TCP
containerPort: 8010
resources:
limits:
cpu: 1
memory: 1Gi
readinessProbe:
tcpSocket:
port: 8010
initialDelaySeconds: 60
periodSeconds: 10
livenessProbe:
tcpSocket:
port: 8010
initialDelaySeconds: 60
periodSeconds: 10
~

product.yaml

构建stock服务镜像

k8s中部署springcloudk8s中部署springcloud
FROM java:8-jdk-alpine
RUN apk add -U tzdata && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY ./target/stock-service-biz.jar ./
COPY pinpoint /pinpoint
EXPOSE 8030
CMD java -jar -javaagent:/pinpoint/pinpoint-bootstrap-1.8.5.jar -Dpinpoint.agentId=${HOSTNAME} -Dpinpoint.applicationName=ms-stock /stock-service-biz.jar

Dockerfile

k8s发布stock服务

k8s中部署springcloudk8s中部署springcloud
apiVersion: apps/v1
kind: Deployment
metadata:
name: stock
namespace: ms
spec:
replicas: 1
selector:
matchLabels:
project: ms
app: stock
template:
metadata:
labels:
project: ms
app: stock
spec:
imagePullSecrets:
- name: registry-pull-secret
containers:
- name: stock
image: 172.17.166.217/kubenetes/stock:v5
#imagePullPolicy: Always
ports:
- protocol: TCP
containerPort: 8030
resources:
limits:
cpu: 1
memory: 1Gi
readinessProbe:
tcpSocket:
port: 8030
initialDelaySeconds: 60
periodSeconds: 10
livenessProbe:
tcpSocket:
port: 8030
initialDelaySeconds: 60
periodSeconds: 10

stock.yaml

构建order服务镜像

k8s中部署springcloudk8s中部署springcloud
FROM java:8-jdk-alpine
RUN apk add -U tzdata && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY ./target/order-service-biz.jar ./
COPY pinpoint /pinpoint
EXPOSE 8020
CMD java -jar -javaagent:/pinpoint/pinpoint-bootstrap-1.8.5.jar -Dpinpoint.agentId=${HOSTNAME} -Dpinpoint.applicationName=ms-order /order-service-biz.jar

Dockerfile

k8s发布order服务

k8s中部署springcloudk8s中部署springcloud
apiVersion: apps/v1
kind: Deployment
metadata:
name: order
namespace: ms
spec:
replicas: 1
selector:
matchLabels:
project: ms
app: order
template:
metadata:
labels:
project: ms
app: order
spec:
imagePullSecrets:
- name: registry-pull-secret
containers:
- name: order
image: 172.17.166.217/kubenetes/order:v5
#imagePullPolicy: Always
ports:
- protocol: TCP
containerPort: 8020
readinessProbe:
tcpSocket:
port: 8020
initialDelaySeconds: 60
periodSeconds: 10
livenessProbe:
tcpSocket:
port: 8020
initialDelaySeconds: 60
periodSeconds: 10

order.yaml

微服务的扩容和缩容

1.扩容

修改yaml文件里的replicas数量,如原来是2,可以修改成3,然后通过kubectl apply 重新更新yaml即可

2.缩容

修改yaml文件里的replicas数量,如原来是3,可以修改成2,然后通过kubectl apply 重新更新yaml即可

3.发布流程

开发提交代码到gitlab->触发自动构建(通过mvn打包代码)->把代码打包成镜像->把镜像上传到私有镜像仓库>把新的镜像更新到对应服务的yaml文件里->然后kubectl apply更新yaml文件->发布服务