利用kubernetes部署微服务项目

著作:行癫 <盗版必究> ------ ## 一:环境准备 #### 1.kubernetes集群环境 集群环境检查 ```shell [root@master ~]# kubectl get node NAME STATUS ROLES AGE VERSION master Ready control-plane,master 11d v1.23.1 node-1 Ready 11d v1.23.1 node-2 Ready 11d v1.23.1 node-3 Ready 11d v1.23.1 ``` #### 2.harbor环境 harbor环境检查 image-20220508222722564 ## 二:项目准备 #### 1.项目包 #### 2.项目端口准备 | 服务 | 内部端口 | 外部端口 | | :---------------------: | :------: | -------- | | tensquare_eureka_server | 10086 | 30020 | | tensquare_zuul | 10020 | 30021 | | tensquare_admin_service | 9001 | 30024 | | tensquare_gathering | 9002 | 30022 | | mysql | 3306 | 30023 | ## 三:项目部署 #### 1.eureka部署 application.yml文件修改 ``` spring: application: name: EUREKA-HA --- #单机配置 server: port: 10086 eureka: instance: hostname: localhost client: register-with-eureka: false fetch-registry: false service-url: defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/ #defaultZone: http://pod主机名称.service名称:端口/eureka/ ``` Dockerfile创建: ```shell [root@nfs-harbor jdk]# ls Dockerfile tensquare_eureka_server-1.0-SNAPSHOT.jar jdk-8u211-linux-x64.tar.gz [root@nfs-harbor jdk]# cat Dockerfile FROM xingdian MAINTAINER "xingdian" ADD jdk-8u211-linux-x64.tar.gz /usr/local/ RUN mv /usr/local/jdk1.8.0_211 /usr/local/java ENV JAVA_HOME /usr/local/java/ ENV PATH $PATH:$JAVA_HOME/bin COPY tensquare_eureka_server-1.0-SNAPSHOT.jar /usr/local EXPOSE 10086 CMD java -jar /usr/local/tensquare_eureka_server-1.0-SNAPSHOT.jar ``` 镜像构建: ```shell [root@nfs-harbor jdk]# docker build -t eureka:v2022.1 . ``` 上传到镜像仓库: ```shell [root@nfs-harbor jdk]# docker tag eureka:v2022.1 10.0.0.230/xingdian/eureka:v2022.1 [root@nfs-harbor jdk]# docker push 10.0.0.230/xingdian/eureka:v2022.1 ``` 仓库验证: image-20220508224930884 #### 2.tensquare_zuul部署 Dockerfile创建: ```shell [root@nfs-harbor jdk]# cat Dockerfile FROM xingdian MAINTAINER "xingdian" ADD jdk-8u211-linux-x64.tar.gz /usr/local/ RUN mv /usr/local/jdk1.8.0_211 /usr/local/java ENV JAVA_HOME /usr/local/java/ ENV PATH $PATH:$JAVA_HOME/bin COPY tensquare_zuul-1.0-SNAPSHOT.jar /usr/local EXPOSE 10020 CMD java -jar /usr/local/tensquare_zuul-1.0-SNAPSHOT.jar ``` 镜像构建: ```shell [root@nfs-harbor jdk]# docker build -t zuul:v2022.1 . ``` 镜像上传: ```shell [root@nfs-harbor jdk]# docker tag zuul:v2022.1 10.0.0.230/xingdian/zuul:v2022.1 [root@nfs-harbor jdk]# docker push 10.0.0.230/xingdian/zuul:v2022.1 ``` 仓库验证: image-20220508230055752 注意: ​ 在构建之前使用vim修改源码jar包,修改的内容如下(文件:application.yml): ```yml server: port: 10020 # 端口 # 基本服务信息 spring: application: name: tensquare-zuul # 服务ID # Eureka配置 eureka: client: service-url: #defaultZone: http://192.168.66.103:10086/eureka,http://192.168.66.104:10086/eureka # Eureka访问地址 #tensquare_eureka_server地址和端口(修改) defaultZone: http://10.0.0.220:30020/eureka instance: prefer-ip-address: true # 修改ribbon的超时时间 ribbon: ConnectTimeout: 1500 # 连接超时时间,默认500ms ReadTimeout: 3000 # 请求超时时间,默认1000ms # 修改hystrix的熔断超时时间 hystrix: command: default: execution: isolation: thread: timeoutInMillisecond: 2000 # 熔断超时时长,默认1000ms # 网关路由配置 zuul: routes: admin: path: /admin/** serviceId: tensquare-admin-service gathering: path: /gathering/** serviceId: tensquare-gathering # jwt参数 jwt: config: key: itcast ttl: 1800000 ``` #### 3.mysql部署 镜像获取(使用官方镜像): ```shell [root@nfs-harbor mysql]# docker pull mysql:5.7.38 ``` 镜像上传: ```shell [root@nfs-harbor mysql]# docker tag mysql:5.7.38 10.0.0.230/xingdian/mysql:v1 [root@nfs-harbor mysql]# docker push 10.0.0.230/xingdian/mysql:v1 ``` #### 4.admin_service部署 Dockerfile创建: ```shell [root@nfs-harbor jdk]# cat Dockerfile FROM xingdian MAINTAINER "xingdian" ADD jdk-8u211-linux-x64.tar.gz /usr/local/ RUN mv /usr/local/jdk1.8.0_211 /usr/local/java ENV JAVA_HOME /usr/local/java/ ENV PATH $PATH:$JAVA_HOME/bin COPY tensquare_admin_service-1.0-SNAPSHOT.jar /usr/local EXPOSE 9001 CMD java -jar /usr/local/tensquare_admin_service-1.0-SNAPSHOT.jar ``` 镜像构建: ```shell [root@nfs-harbor jdk]# docker build -t admin_service:v2022.1 . ``` 镜像上传: ```shell [root@nfs-harbor jdk]# docker tag admin_service:v2022.1 10.0.0.230/xingdian/admin_service:v2022.1 [root@nfs-harbor jdk]# docker push 10.0.0.230/xingdian/admin_service:v2022.1 ``` 注意: ​ 在构建之前使用vim修改源码jar包,修改的内容如下(文件:application.yml): ```yml spring: application: name: tensquare-admin-service #指定服务名 datasource: driverClassName: com.mysql.jdbc.Driver #数据库地址(修改) url: jdbc:mysql://10.0.0.220:30023/tensquare_user?characterEncoding=UTF8&useSSL=false #数据库账户名(修改) username: root #数据库账户密码(修改) password: mysql jpa: database: mysql show-sql: true #Eureka配置 eureka: client: service-url: #defaultZone: http://192.168.66.103:10086/eureka,http://192.168.66.104:10086/eureka ##tensquare_eureka_server地址和端口(修改) defaultZone: http://10.0.0.220:30020/eureka instance: lease-renewal-interval-in-seconds: 5 # 每隔5秒发送一次心跳 lease-expiration-duration-in-seconds: 10 # 10秒不发送就过期 prefer-ip-address: true # jwt参数 jwt: config: key: itcast ttl: 1800000 ``` #### 5.gathering部署 Dockerfile创建: ```shell [root@nfs-harbor jdk]# cat Dockerfile FROM xingdian MAINTAINER "xingdian" ADD jdk-8u211-linux-x64.tar.gz /usr/local/ RUN mv /usr/local/jdk1.8.0_211 /usr/local/java ENV JAVA_HOME /usr/local/java/ ENV PATH $PATH:$JAVA_HOME/bin COPY tensquare_gathering-1.0-SNAPSHOT.jar /usr/local CMD java -jar /usr/local/tensquare_gathering-1.0-SNAPSHOT.jar ``` 镜像构建: ```shell [root@nfs-harbor jdk]# docker build -t gathering:v2022.1 . ``` 镜像上传: ```shell [root@nfs-harbor jdk]# docker tag gathering:v2022.1 10.0.0.230/xingdian/gathering:v2022.1 [root@nfs-harbor jdk]# docker push 10.0.0.230/xingdian/gathering:v2022.1 ``` 仓库验证: image-20220508233621370 注意: ```yml server: port: 9002 spring: application: name: tensquare-gathering #指定服务名 datasource: driverClassName: com.mysql.jdbc.Driver #数据库地址(修改) url: jdbc:mysql://10.0.0.220:30023/tensquare_gathering?characterEncoding=UTF8&useSSL=false #数据库地址(修改) username: root #数据库账户密码(修改) password: mysql jpa: database: mysql show-sql: true #Eureka客户端配置 eureka: client: service-url: #defaultZone: http://192.168.66.103:10086/eureka,http://192.168.66.104:10086/eureka #tensquare_eureka_server地址和端口(修改) defaultZone: http://10.0.0.220:30020/eureka instance: lease-renewal-interval-in-seconds: 5 # 每隔5秒发送一次心跳 lease-expiration-duration-in-seconds: 10 # 10秒不发送就过期 prefer-ip-address: true ``` ## 四:kubernetes集群部署 #### 1.所有镜像验证 ![image-20220508233955412](%E5%88%A9%E7%94%A8kubernetes%E9%83%A8%E7%BD%B2%E5%BE%AE%E6%9C%8D%E5%8A%A1%E9%A1%B9%E7%9B%AE.assets/image-20220508233955412.png) #### 2.部署eureka Eureka之Deployment创建: ```shell [root@master xingdian]# cat Eureka.yaml apiVersion: apps/v1 kind: Deployment metadata: name: eureka-deployment labels: app: eureka spec: replicas: 1 selector: matchLabels: app: eureka template: metadata: labels: app: eureka spec: containers: - name: nginx image: 10.0.0.230/xingdian/eureka:v2022.1 ports: - containerPort: 10086 --- apiVersion: v1 kind: Service metadata: name: eureka-service labels: app: eureka spec: type: NodePort ports: - port: 10086 name: eureka targetPort: 10086 nodePort: 30020 selector: app: eureka ``` 创建: ```shell [root@master xingdian]# kubectl create -f Eureka.yaml deployment.apps/eureka-deployment created service/eureka-service created ``` 验证: ```shell [root@master xingdian]# kubectl get pod NAME READY STATUS RESTARTS AGE eureka-deployment-69c575d95-hx8s6 1/1 Running 0 2m20s [root@master xingdian]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE eureka-service NodePort 10.107.243.240 10086:30020/TCP 2m22s ``` image-20220508235409218 #### 3.部署zuul zuul之Deployment创建: ```shell [root@master xingdian]# cat Zuul.yaml apiVersion: apps/v1 kind: Deployment metadata: name: zuul-deployment labels: app: zuul spec: replicas: 1 selector: matchLabels: app: zuul template: metadata: labels: app: zuul spec: containers: - name: zuul image: 10.0.0.230/xingdian/zuul:v2022.1 ports: - containerPort: 10020 --- apiVersion: v1 kind: Service metadata: name: zuul-service labels: app: zuul spec: type: NodePort ports: - port: 10020 name: zuul targetPort: 10086 nodePort: 30021 selector: app: zuul ``` 创建: ```shell [root@master xingdian]# kubectl create -f Zuul.yaml ``` 验证: ```shell [root@master xingdian]# kubectl get pod NAME READY STATUS RESTARTS AGE eureka-deployment-69c575d95-hx8s6 1/1 Running 0 7m42s zuul-deployment-6d76647cf9-6rmdj 1/1 Running 0 10s [root@master xingdian]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE eureka-service NodePort 10.107.243.240 10086:30020/TCP 7m37s kubernetes ClusterIP 10.96.0.1 443/TCP 11d zuul-service NodePort 10.103.35.255 10020:30021/TCP 5s ``` 验证是否加入注册中心: ![image-20220508235634459](%E5%88%A9%E7%94%A8kubernetes%E9%83%A8%E7%BD%B2%E5%BE%AE%E6%9C%8D%E5%8A%A1%E9%A1%B9%E7%9B%AE.assets/image-20220508235634459.png) #### 4.mysql部署 mysql之rc和svc创建: ```shell [root@master mysql]# cat mysql-svc.yaml apiVersion: v1 kind: Service metadata: name: mysql-svc labels: name: mysql-svc spec: type: NodePort ports: - port: 3306 protocol: TCP targetPort: 3306 name: http nodePort: 30023 selector: name: mysql-pod [root@master mysql]# cat mysql-rc.yaml apiVersion: v1 kind: ReplicationController metadata: name: mysql-rc labels: name: mysql-rc spec: replicas: 1 selector: name: mysql-pod template: metadata: labels: name: mysql-pod spec: containers: - name: mysql image: 10.0.0.230/xingdian/mysql:v1 imagePullPolicy: IfNotPresent ports: - containerPort: 3306 env: - name: MYSQL_ROOT_PASSWORD value: "mysql" ``` 创建: ```shell [root@master mysql]# kubectl create -f mysql-rc.yaml replicationcontroller/mysql-rc created [root@master mysql]# kubectl create -f mysql-svc.yaml service/mysql-svc created ``` 验证: ```shell [root@master mysql]# kubectl get pod NAME READY STATUS RESTARTS AGE eureka-deployment-69c575d95-hx8s6 1/1 Running 0 29m mysql-rc-sbdcl 1/1 Running 0 8m41s zuul-deployment-6d76647cf9-gpsms 1/1 Running 0 21m [root@master mysql]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE eureka-service NodePort 10.107.243.240 10086:30020/TCP 29m kubernetes ClusterIP 10.96.0.1 443/TCP 11d mysql-svc NodePort 10.98.4.62 3306:30023/TCP 9m1s zuul-service NodePort 10.103.35.255 10020:30021/TCP 22m ``` 数据库创建: ```shell [root@nfs-harbor ~]# mysql -u root -pmysql -h 10.0.0.220 -P 30023 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.38 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> create database tensquare_user charset=utf8; Query OK, 1 row affected (0.00 sec) MySQL [(none)]> create database tensquare_gathering charset=utf8; Query OK, 1 row affected (0.01 sec) MySQL [(none)]> exit Bye ``` 数据导入: ```shell [root@nfs-harbor ~]# mysql -u root -pmysql -h 10.0.0.220 -P 30023 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.38 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> source /var/ftp/share/tensquare_user.sql MySQL [tensquare_user]> source /var/ftp/share/tensquare_gathering.sql MySQL [tensquare_gathering]> exit Bye ``` 验证: ```shell [root@nfs-harbor ~]# mysql -u root -pmysql -h 10.0.0.220 -P 30023 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.38 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> show databases; +---------------------+ | Database | +---------------------+ | information_schema | | mysql | | performance_schema | | sys | | tensquare_gathering | | tensquare_user | +---------------------+ 6 rows in set (0.00 sec) MySQL [(none)]> use tensquare_gathering Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MySQL [tensquare_gathering]> show tables; +-------------------------------+ | Tables_in_tensquare_gathering | +-------------------------------+ | tb_city | | tb_gathering | +-------------------------------+ 2 rows in set (0.00 sec) MySQL [tensquare_gathering]> use tensquare_user Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Database changed MySQL [tensquare_user]> show tables; +--------------------------+ | Tables_in_tensquare_user | +--------------------------+ | tb_admin | +--------------------------+ 1 row in set (0.01 sec) ``` #### 5.admin_service部署 admin_service之Deployment创建: ```shell [root@master xingdian]# cat Admin-service.yaml apiVersion: apps/v1 kind: Deployment metadata: name: admin-deployment labels: app: admin spec: replicas: 1 selector: matchLabels: app: admin template: metadata: labels: app: admin spec: containers: - name: admin image: 10.0.0.230/xingdian/admin_service:v2022.1 ports: - containerPort: 9001 --- apiVersion: v1 kind: Service metadata: name: admin-service labels: app: admin spec: type: NodePort ports: - port: 9001 name: admin targetPort: 9001 nodePort: 30024 selector: app: admin ``` 创建: ```shell [root@master xingdian]# kubectl create -f Admin-service.yaml deployment.apps/admin-deployment created service/admin-service created ``` 验证: ```shell [root@master xingdian]# kubectl get pod NAME READY STATUS RESTARTS AGE admin-deployment-54c5664d69-l2lbc 1/1 Running 0 23s eureka-deployment-69c575d95-mrj66 1/1 Running 0 47m mysql-rc-zgxk4 1/1 Running 0 7m23s zuul-deployment-6d76647cf9-gpsms 1/1 Running 0 39m [root@master xingdian]# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE admin-service NodePort 10.101.251.47 9001:30024/TCP 6s eureka-service NodePort 10.107.243.240 10086:30020/TCP 47m kubernetes ClusterIP 10.96.0.1 443/TCP 11d mysql-svc NodePort 10.98.4.62 3306:30023/TCP 26m zuul-service NodePort 10.103.35.255 10020:30021/TCP 39m ``` 注册中心验证: image-20220509013257937 #### 6.gathering部署 gathering之Deployment创建: ```shell [root@master xingdian]# cat Gathering.yaml apiVersion: apps/v1 kind: Deployment metadata: name: gathering-deployment labels: app: gathering spec: replicas: 1 selector: matchLabels: app: gathering template: metadata: labels: app: gathering spec: containers: - name: nginx image: 10.0.0.230/xingdian/gathering:v2022.1 ports: - containerPort: 9002 --- apiVersion: v1 kind: Service metadata: name: gathering-service labels: app: gathering spec: type: NodePort ports: - port: 9002 name: gathering targetPort: 9002 nodePort: 30022 selector: app: gathering ``` 创建: ```shell [root@master xingdian]# kubectl create -f Gathering.yaml deployment.apps/gathering-deployment created service/gathering-service created ``` 验证: ```shell [root@master xingdian]# kubectl get pod NAME READY STATUS RESTARTS AGE admin-deployment-54c5664d69-2tqlw 1/1 Running 0 33s eureka-deployment-69c575d95-xzx9t 1/1 Running 0 13m gathering-deployment-6fcdd5d5-wbsxt 1/1 Running 0 27s mysql-rc-zgxk4 1/1 Running 0 28m zuul-deployment-6d76647cf9-jkm7f 1/1 Running 0 12m ``` 注册中心验证: image-20220509005823566 #### 7.浏览器测试API接口 ![image-20220509015035488](%E5%88%A9%E7%94%A8kubernetes%E9%83%A8%E7%BD%B2%E5%BE%AE%E6%9C%8D%E5%8A%A1%E9%A1%B9%E7%9B%AE.assets/image-20220509015035488.png)