Kubernetes Springboot部署

环境

  • JDK11
  • Springboot 2.5
  • Ubuntu 20
  • K8S 1.22.2
  • 3 台机器(1Master + 2Nodes)

镜像制作

  1. 下载jdk11jdk-11.0.11_linux-x64_bin.tar.gz
  2. 基于springboot构建简单项目并达成jarhelloword-1.0.0.jar
  3. 编写Dockerfile文件build镜像
root@lean-master:~/docker# cat jdk11-image.Dockerfile 
#1.指定基础镜像,并且必须是第一条指令
FROM centos:7

#2.指明该镜像的作者和其电子邮件
MAINTAINER quan "quan@163.com"

RUN mkdir /usr/local/java
RUN mkdir /usr/local/webapp
# ADD 命令会解压
ADD jdk-11.0.11_linux-x64_bin.tar.gz /usr/local/java/
COPY helloword-1.0.0.jar /usr/local/webapp

ENV JAVA_HOME=/usr/local/java/jdk-11.0.11
ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH=$JAVA_HOME/bin:$PATH

# VOLUME 指定了临时文件目录为/tmp
# 其效果是在主机 /var/lib/docker 目录下创建了一个临时文件,并链接到容器的/tmp
VOLUME /tmp 
EXPOSE 8080


#容器启动时需要执行的命令
ENTRYPOINT ["java","-jar","/usr/local/webapp/helloword-1.0.0.jar","&"]

将以上上个文件放到一个目录下,在build镜像文件前,先验证下jar是可执行文件,如果没用问题开始执行:

root@lean-master:~/docker# docker build -t quan/demo:v1 . -f jdk11-image.Dockerfile 
Sending build context to Docker daemon  199.4MB
Step 1/12 : FROM centos:7
 ---> eeb6ee3f44bd
Step 2/12 : MAINTAINER quan "quan@163.com"
 ---> Using cache
 ---> d95385715450
Step 3/12 : RUN mkdir /usr/local/java
 ---> Using cache
 ---> fd2c58c00b30
Step 4/12 : RUN mkdir /usr/local/webapp
 ---> Using cache
 ---> 42bcd0709ce0
Step 5/12 : ADD jdk-11.0.11_linux-x64_bin.tar.gz /usr/local/java/
 ---> Using cache
 ---> d5cd97be443e
Step 6/12 : COPY helloword-1.0.0.jar /usr/local/webapp
 ---> Using cache
 ---> b35dced89349
Step 7/12 : ENV JAVA_HOME=/usr/local/java/jdk-11.0.11
 ---> Using cache
 ---> d60b72699b73
Step 8/12 : ENV CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
 ---> Using cache
 ---> 78764344f345
Step 9/12 : ENV PATH=$JAVA_HOME/bin:$PATH
 ---> Using cache
 ---> ae40c0d620d7
Step 10/12 : VOLUME /tmp
 ---> Using cache
 ---> b47488937b58
Step 11/12 : EXPOSE 8080
 ---> Using cache
 ---> 6715c9c8af90
Step 12/12 : ENTRYPOINT ["java","-jar","/usr/local/webapp/helloword-1.0.0.jar","&"]
 ---> Using cache
 ---> cba6100b46d5
Successfully built cba6100b46d5
Successfully tagged quan/demo:v1

验证镜像可以正常工作

#运行容器
root@lean-master:~/docker# docker run -d -P quan/demo:v1
22ef7fdfa52a53b70d6ceaacb052fee2582bb0b4f4856a0f1de6a3148412f132

#查看容器
root@lean-master:~/docker# docker ps | grep java
22ef7fdfa52a   quan/demo:v1                                        "java -jar /usr/loca…"   About a minute ago   Up About a minute   0.0.0.0:49153->8080/tcp, :::49153->8080/tcp   distracted_hellman
root@lean-master:~/docker# 

#检查应用
root@lean-master:~/docker#  curl -XGET 'http://127.0.0.1:49153/app/status'
Welcome you!

代表应用能成功启动,现在停止上面的容器,准备用k8s发布

root@lean-master:~/docker# docker rm 22ef7fdfa52a
Error response from daemon: You cannot remove a running container 22ef7fdfa52a53b70d6ceaacb052fee2582bb0b4f4856a0f1de6a3148412f132. Stop the container before attempting removal or force remove
root@lean-master:~/docker# docker rm -f 22ef7fdfa52a
22ef7fdfa52a
root@lean-master:~/docker# docker ps -a | grep java
edc437b22f65   66b9ce35a54b                                        "/bin/sh -c 'java -j…"   20 hours ago   Exited (0) 20 hours ago                                                 priceless_payne

如果镜像没有推送到远程仓库,需要在每个机器上制作上面的镜像

  1. 准备K8S部署文件
root@lean-master:~/deployment# kubectl create deployment demo --image=nginx -o yaml --dry-run > demo.yaml

root@lean-master:~/deployment# cat demo.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: demo
  name: demo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: demo
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: demo
    spec:
      containers:
      - image: quan/demo:v1
        name: demo
        resources: {}
status: {}

在生成的demo.yaml中根据自己的需求更新相关配置,这里就不作任何改动了

root@lean-master:~/deployment# kubectl apply -f demo.yaml 
deployment.apps/demo created
root@lean-master:~/deployment# kubectl get pods -n default -o wide
NAME                   READY   STATUS    RESTARTS   AGE    IP           NODE            NOMINATED NODE   READINESS GATES
demo-65ff75768-sqqwb   1/1     Running   0          2m1s   10.244.3.4   k8snode000002   <none>           <none>

该pod部署在了k8snode000002节点.

接下来暴露端口给外部访问

root@lean-master:~# kubectl get svc -o wide
NAME         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE    SELECTOR
kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   2d5h   <none>
#
#暴露端口
root@lean-master:~# kubectl expose deployment demo --port=8080 --target-port=8080 --type=NodePort
service/demo exposed

root@lean-master:~# kubectl get svc -o wide
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE    SELECTOR
demo         NodePort    10.97.252.210   <none>        8080:32026/TCP   8s     app=demo
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP          2d5h   <none>

Kubernetes Springboot部署
外部访问成功,现新增容器副本到2个:

# 添加副本
root@lean-master:~# kubectl scale deployment demo --replicas=2
deployment.apps/demo scaled
root@lean-master:~# kubectl get pods -n default -o wide
NAME                   READY   STATUS    RESTARTS   AGE   IP           NODE            NOMINATED NODE   READINESS GATES
demo-65ff75768-cdz7l   1/1     Running   0          25s   10.244.1.5   k8snode000001   <none>           <none>
demo-65ff75768-sqqwb   1/1     Running   0          40m   10.244.3.4   k8snode000002   <none>           <none>

应用部署到了2个节点k8snode000001k8snode000002


Ingress

通过部署ingress来通过域名访问

TODO
上一篇:Pandas数据分析——超好用的Groupby详解


下一篇:用虚拟机中的Ubuntu编译lean大神固件(1)-解决虚拟机命令行代理问题