1 springboot 配置k8s中的redis服务名

#tomcat访问端口
# 应用名称
spring.application.name=demo
# 应用服务Web访问端口
server.port=8089
server.env=test
#缓存关闭
spring.thymeleaf.cache=false
#可选配置
management.endpoints.enabled-by-default=true
management.endpoints.web.base-path=/actuator
#暴露Prometheus数据端点  /actuator/prometheus
management.endpoints.web.exposure.include=prometheus
#暴露的Prometheus数据中添加application label
management.metrics.tags.application=demo
# Redis 连接信息 主机是k8s服务名.命名空间
spring.redis.host=redis-leader-service.rz-dt
spring.redis.port=6379
spring.redis.password=

2 k8s springboot部署脚本 test.sh

echo "开始制作镜像..."
image_tag=`date +%y-%m-%d-%H-%M-%S` #_%H%M
echo "当前时间:$image_tag"
docker build -t k8s-springboot-demo:${image_tag} .
echo "制作镜像成功!"

echo "登录"
sudo docker login --username=aliyun0313112 rz-dt-image-server-registry-vpc.cn-shanghai.cr.aliyuncs.com --password=1234

echo "镜像最新版本提交"
docker tag k8s-springboot-demo:${image_tag} rz-dt-image-server-registry-vpc.cn-shanghai.cr.aliyuncs.com/rz-dt/k8s-springboot-demo:${image_tag}
docker push rz-dt-image-server-registry-vpc.cn-shanghai.cr.aliyuncs.com/rz-dt/k8s-springboot-demo:${image_tag}

echo "k8s一键部署"
export IMG_TAG=${image_tag}
envsubst < k8s-springboot-demo.yaml | kubectl --kubeconfig ~/.kube-rz/config apply -f -


3 k8s部署deployment+service配置文件

apiVersion: v1
kind: Service
metadata:
  name: k8s-springboot-demo
  namespace: rz-dt
  labels:
    app: k8s-springboot-demo
spec:
  type: NodePort
  ports:
    - port: 8089
      nodePort: 30091 #service对外开放端口
  selector:
    app: k8s-springboot-demo
---
apiVersion: apps/v1
kind: Deployment #对象类型
metadata:
  name: k8s-springboot-demo #名称
  namespace: rz-dt
  labels:
    app: k8s-springboot-demo #标注
spec:
  replicas: 1 #运行容器的副本数,修改这里可以快速修改分布式节点数量
  selector:
    matchLabels:
      app: k8s-springboot-demo
  template:
    metadata:
      labels:
        app: k8s-springboot-demo
    spec:
      containers: #docker容器的配置
        #- command:
        #    - java
        #    - '-Dserver.port=8055'
        #    - '-jar'
        #    - /usr/local/cenobitor/k8s-springboot-demo.jar
        - name: k8s-springboot-demo
          image: rz-dt-image-server-registry.cn-shanghai.cr.aliyuncs.com/rz-dt/k8s-springboot-demo:$IMG_TAG # pull镜像的地址 ip:prot/dir/images:tag
          imagePullPolicy: IfNotPresent #pull镜像时机,
          ports:
            - containerPort: 8089 #容器对外开放端口,需与springboot配置文件一致
      #从私有仓库拉取镜像凭证
      imagePullSecrets:
        - name: rz-dt-miyue

4 Dockefile 文件

#基础镜像,如果本地仓库没有,会从远程仓库拉取 openjdk:8
FROM openjdk:8
#暴露端口
EXPOSE 8089
#容器中创建目录
RUN mkdir -p /usr/local/cenobitor
#编译后的jar包copy到容器中创建到目录内12
COPY target/k8s-springboot-demo.jar /usr/local/cenobitor/k8s-springboot-demo.jar
#指定容器启动时要执行的命令
ENTRYPOINT ["java","-Dserver.port=8089","-jar","/usr/local/cenobitor/k8s-springboot-demo.jar"]

5 k8s里面服务截图

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐