k8s中部署springcloud
安装和配置数据存储仓库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:
⽬前 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 arch.rpm
rpm -arch.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下载jar包登陆数据库
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/ l
jdbc:mysql://192.168.40.130:3306/tb_stock?characterEncoding=utf-8
#变成⾃⼰的数据库地址
2)修改产品数据库
cat /root/microservic-test/product-service/product-service-biz/src/main/l
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/ l
url: jdbc:mysql://192.168.40.130:3306/tb_order?characterEncoding=utf-8
#变成⾃⼰的数据库地址
通过Maven编译、构建、打包源代码
修改源代码之后回到/root/microservic-test⽬录下执⾏如下命令:
mvn clean package -st.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 2015:53:22 CST 2021
[INFO] Final Memory: 92M/710M
[INFO] ------------------------------------------------------------------------
在k8s中部署Eureka组件
cat > /etc/docker/daemon.json <<EOF
{
"registry-mirrors":["rsbud4vc.mirror.aliyuncs","registry.docker-cn","docker.mirrors.ustc.edu","dockerhub.azk8s","163","qtid6917.mirror.aliyuncs "insecure-registries":["192.168.40.132","harbor"],
"exec-opts":["updriver=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镜像
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
#---
#apiVersion: extensions/v1beta1
#kind: Ingress
#metadata:
# name: eureka
# namespace: ms
# annotations: #注解信息
# kubernetes.io/ingress.class: "nginx"
#spec:
# rules:
# - host: s
# 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中部署⽹关Gateway服务
build Gateway docker 镜像
FROM java:8-jdk-alpine
RUN apk add -U tzdata && \
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
COPY ./target/gateway-service.jar ./
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
---
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服务镜像
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服务
#apiVersion: extensions/v1beta1
#kind: Ingress
#metadata:
# name: portal
# namespace: ms
#spec:
# rules:
# - host: s
# 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服务镜像
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服务
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服务镜像
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论