Kubernetes学习笔记-项目简单部署
继续之前的移除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-config namespace: default data: common-config.yml: | spring: main: allow-bean-definition-overriding: true mvc: throw-exception-if-no-handler-found: true messages: basename: i18n/messages datasource: driver-class-name: com.mysql.cj.jdbc.Driver type: com.zaxxer.hikari.HikariDataSource hikari: connection-timeout: 10000 validation-timeout: 10000 idle-timeout: 30000 max-lifetime: 60000 maximum-pool-size: 20 minimum-idle: 10 read-only: false connection-test-query: SELECT 1 FROM DUAL server: undertow: threads: io: 4 worker: 32 buffer-size: 1024 direct-buffers: true max-http-post-size: 10485760 max-http-header-size: 16384 compression: enabled: true mime-types: - application/json - application/xml - application/javascript - text/html - text/xml - text/plain - text/javascript - text/css min-response-size: 1024 management: endpoints: enabled-by-default: true web: exposure: include: health shutdown: enabled: true mybatis-plus: mapper-locations: classpath*:mapper/**/*.xml configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
apiVersion: v1 kind: ConfigMap metadata: name: service-config namespace: default data: service-config.yml: | spring: profiles: active: dev application: name: demo-service datasource: url: jdbc:mysql://192.168.0.1:3306/demo-service?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8 username: root password: 123456 server: port: 18080 servlet: 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_jmx EXPOSE 18080 EXPOSE 18081 ENV 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.jar ADD target/*.jar /app.jar ENTRYPOINT ["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: 1 selector: matchLabels: app: demo-service template: metadata: labels: app: demo-service spec: imagePullSecrets: - name: imagesecret containers: - name: demo-service image: swr.cn-east-3.myhuaweicloud.com/develop/demo-service:latest imagePullPolicy: Always ports: - 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-service ports: - protocol: TCP port: 18080 targetPort: 18080 type: NodePort
- 创建管理应用Pod。