继续之前的移除Nacos迁移至K8s,现在把项目服务简单的部署到Kubernetes上面。
-
项目服务配置
- Kubernetes的ConfigMap有以下几种创建方式:
- kubectl create configmap special-config --from-literal=special.k1=v1 --from-literal=special.k2=v2
- kubectl create configmap special-config --from-file=special-config.properties
- kubectl apply -f special-config.yaml
- 这里使用第三种方式创建ConfigMap。
- 根据项目服务的需要新建common-config.yaml、service-config.yaml两个ConfigMap文件,文件内容见下方。
- kubectl apply -f common-config.yaml
- kubectl apply -f service-config.yaml
apiVersion: v1 kind: ConfigMap metadata:name: common-confignamespace: default data:common-config.yml: |spring:main:allow-bean-definition-overriding: truemvc:throw-exception-if-no-handler-found: truemessages:basename: i18n/messagesdatasource:driver-class-name: com.mysql.cj.jdbc.Drivertype: com.zaxxer.hikari.HikariDataSourcehikari:connection-timeout: 10000validation-timeout: 10000idle-timeout: 30000max-lifetime: 60000maximum-pool-size: 20minimum-idle: 10read-only: falseconnection-test-query: SELECT 1 FROM DUALserver:undertow:threads:io: 4worker: 32buffer-size: 1024direct-buffers: truemax-http-post-size: 10485760max-http-header-size: 16384compression:enabled: truemime-types:- application/json- application/xml- application/javascript- text/html- text/xml- text/plain- text/javascript- text/cssmin-response-size: 1024management:endpoints:enabled-by-default: trueweb:exposure:include: healthshutdown:enabled: truemybatis-plus:mapper-locations: classpath*:mapper/**/*.xmlconfiguration:log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
apiVersion: v1 kind: ConfigMap metadata:name: service-confignamespace: default data:service-config.yml: |spring:profiles:active: devapplication:name: demo-servicedatasource:url: jdbc:mysql://192.168.0.1:3306/demo-service?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8username: rootpassword: 123456server:port: 18080servlet:context-path: /demo-service
- Kubernetes的ConfigMap有以下几种创建方式:
-
项目服务打包镜像
- 项目打包:mvn clean -X -U package -DskipTests
- 创建Dockerfile文件,简单内容见下方
- 构建镜像:docker build -t swr.cn-east-3.myhuaweicloud.com/develop/demo-service:latest .
- 上传镜像:docker push swr.cn-east-3.myhuaweicloud.com/develop/demo-service:latest
FROM swr.cn-east-3.huaweicloud.com/develop/java:jdk1.8_jmxEXPOSE 18080 EXPOSE 18081ENV DEF_JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom -Duser.timezone=Asia/Shanghai -Dfile.encoding=utf-8 -Dhttps.protocols=TLSv1.2" ENV APP_JAR_PATH=app.jarADD target/*.jar /app.jarENTRYPOINT ["sh", "-c", "java ${DEF_JAVA_OPTS} -jar /app.jar"]
-
项目服务部署
- 创建管理应用Pod。
- 新建Deployment.yaml文件,简单内容见下方。
- 执行命令:kubectl apply -f Deployment.yaml
- 查看Pod状态:kubectl get pods
- 查看Pod日志:kubectl logs
apiVersion: apps/v1 kind: Deployment metadata:name: demo-service spec:replicas: 1selector:matchLabels:app: demo-servicetemplate:metadata:labels:app: demo-servicespec:imagePullSecrets:- name: imagesecretcontainers:- name: demo-serviceimage: swr.cn-east-3.myhuaweicloud.com/develop/demo-service:latestimagePullPolicy: Alwaysports:- containerPort: 18080
- 可以为动态变化的Pod提供稳定的访问入口,实现服务发现和负载均衡。
- 新建Service.yaml文件,简单内容见下方。
- 执行命令:kubectl apply -f Service.yaml
- 查看Service信息:kubectl get svc
- 访问项目服务:http://192.168.0.1:32749/demo-service/doc.html
apiVersion: v1 kind: Service metadata:name: demo-service spec:selector:app: demo-serviceports:- protocol: TCPport: 18080targetPort: 18080type: NodePort
- 创建管理应用Pod。