k8s部署微服务(springboot程序)

常规步骤:

  1. 项目打包(jar、war)-->可以采用一些工具git、maven、jenkins;
  2. 制作Dockerfile文件,生成镜像;
  3. kubectl create deployment nginx --image= 你的镜像;
  4. 你的springboot就部署好了,是以docker容器的方式运行在pod里面的;

一、先通过dockefile生成一个java环境的镜像

[root@k8snode /]# mkdir /data
[root@k8snode /]# cd /data/
[root@k8snode data]# ll
total 183836
-rw-r--r-- 1 root root 188244749 Feb 24 10:14 openjdk-18.0.1_linux-x64_bin.tar.gz
[root@k8snode jdk]# vim Dockerfile
From centos
MAINTAINER ZhaoLei
ADD openjdk-18.0.1_linux-x64_bin.tar.gz /usr/local/java
ENV JAVA_HOME /usr/local/java/jdk-18.0.1
ENV CLASSPATH $JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
ENV PATH $PATH:$JAVA_HOME/bin
CMD java -version
[root@k8snode jdk]# docker build -t jdk-18.0.1 .
[+] Building 60.0s (5/6)                                                                                                                                 
 => [internal] load build definition from Dockerfile                                                                                                0.0s
 => => transferring dockerfile: 276B                                                                                                                0.0s
 => [internal] load .dockerignore                                                                                                                   0.0s
 => => transferring context: 2B                                                                                                                     0.0s
 => ERROR [internal] load metadata for docker.io/library/centos:latest                                                                             30.0s
 => [internal] load build context                                                                                                                   0.0s
 => => transferring context: 59B                                                                                                                    0.0s
 => ERROR [1/2] FROM docker.io/library/centos:latest                                                                                               30.0s
 => => resolve docker.io/library/centos:latest                                                                                                     30.0s
------
ERROR: failed to solve: failed to solve with frontend dockerfile.v0: failed to build LLB: failed to load cache key: failed to do request: Head https://registry.docker-cn.com/v2/library/centos/manifests/latest: dial tcp 106.14.52.175:443: i/o timeout

报错,原因应该是加速器https://registry.docker-cn.com的问题,修改加速器:
[root@k8snode jdk]# vim /etc/docker/daemon.json
{
        "registry-mirrors": ["https://gg3gwnry.mirror.aliyuncs.com"],
        "exec-opts": ["native.cgroupdriver=systemd"]
}
保存退出
[root@k8snode jdk]# systemctl restart docker
[root@k8snode jdk]# docker build -t jdk-18.0.1 .
[+] Building 29.4s (6/7)                                                                                                                                 
 => [internal] load .dockerignore                                                                                                                  29.4s
 => => transferring context: 2B                                                                                                                     0.0s
 => [internal] load build definition from Dockerfile                                                                                                0.0s
 => => transferring dockerfile: 32B                                                                                                                 0.0s
 => [internal] load metadata for docker.io/library/centos:latest                                                                                   18.8s
 => [internal] load build context                                                                                                                   0.0s
 => => transferring context: 59B                                                                                                                    0.0s
 => [1/2] FROM docker.io/library/centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177                                     6.7s
 => => resolve docker.io/library/centos@sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177                                     0.0s
 => => sha256:a27fd8080b517143cbbbab9dfb7c8571c40d67d534bbdee55bd6c473f432b177 762B / 762B                                                          0.0s
 => => sha256:a1801b843b1bfaf77c501e7a6d3f709401a1e0c83863037fa3aab063a7fdb9dc 529B / 529B                                                          0.0s
 => => sha256:5d0da3dc976460b72c77d94c8a1ad043720b0416bfc16c52c45d4847e53fadb6 2.14kB / 2.14kB                                                      0.0s
 => => sha256:a1d0c75327776413fa0db9ed3adcdbadedc95a662eb1d360dad82bb913f8a1d1 83.52MB / 83.52MB                                                    3.7s
 => => extracting sha256:a1d0c75327776413fa0db9ed3adcdbadedc95a662eb1d360dad82bb913f8a1d1                                                           2.8s
 => [2/2] ADD openjdk-18.0.1_linux-x64_bin.tar.gz /usr/local/java                                                                                   2.5s
 => exporting to image                                                                                                                              1.3s
 => => exporting layers                                                                                                                             1.3s
 => => writing image sha256:7033ab12b31c9d8312b82c46b62123f3ad4bff33dd908717f89c1f25644bb649                                                        0.0s
 => => naming to docker.io/library/jdk-18.0.1                                                                                                       0.0s
[root@k8snode jdk]# docker images
REPOSITORY                                                        TAG       IMAGE ID       CREATED         SIZE
jdk-18.0.1                                                        latest    7033ab12b31c   6 minutes ago   555MB

二、通过java镜像构建项目镜像

先上传jar包

[root@k8snode data]# ll
total 26532
-rw-r--r-- 1 root root 27168283 Jan  7 23:15 38-springboot-k8s-1.0.0.jar
drwxr-xr-x 3 root root       85 Feb 24 10:51 jdk

然后构建dockerfile

[root@k8snode data]# vim dockerfile
FROM jdk-18.0.1
MAINTAINER cat
ADD 38-springboot-k8s-1.0.0.jar /opt
RUN chmod +x /opt/38-springboot-k8s-1.0.0.jar
CMD java -jar /opt/38-springboot-k8s-1.0.0.jar

构建镜像

[root@k8snode data]# docker build -t 38-springboot-k8s-1.0.0-jar .
[+] Building 0.7s (8/8) FINISHED                                                                                                                         
 => [internal] load build definition from dockerfile                                                                                                0.0s
 => => transferring dockerfile: 200B                                                                                                                0.0s
 => [internal] load .dockerignore                                                                                                                   0.0s
 => => transferring context: 2B                                                                                                                     0.0s
 => [internal] load metadata for docker.io/library/jdk-18.0.1:latest                                                                                0.0s
 => [internal] load build context                                                                                                                   0.2s
 => => transferring context: 27.17MB                                                                                                                0.1s
 => [1/3] FROM docker.io/library/jdk-18.0.1                                                                                                         0.0s
 => => resolve docker.io/library/jdk-18.0.1:latest                                                                                                  0.0s
 => [2/3] ADD 38-springboot-k8s-1.0.0.jar /opt                                                                                                      0.1s
 => [3/3] RUN chmod +x /opt/38-springboot-k8s-1.0.0.jar                                                                                             0.3s
 => exporting to image                                                                                                                              0.2s
 => => exporting layers                                                                                                                             0.2s
 => => writing image sha256:2de5db41605104417e6932f8a85f1f542e6290af5ec6154fd90730e2562f9bac                                                        0.0s
 => => naming to docker.io/library/38-springboot-k8s-1.0.0-jar                                                                                      0.0s
[root@k8snode data]# docker images
REPOSITORY                                                        TAG       IMAGE ID       CREATED         SIZE
38-springboot-k8s-1.0.0-jar                                       latest    2de5db416051   6 seconds ago   609MB
jdk-18.0.1                                                        latest    7033ab12b31c   4 days ago      555MB

三、空运行测试(可通过此方式生成yaml文件,然后直接kubectl apply -f *.yaml部署也可,省着自己写yaml文件了)

1.空运行测试并输出yaml打印到屏幕
[root@k8smaster data]# kubectl create deployment springboot-k8s --image=38-springboot-k8s-1.0.0.jar --dry-run -o yaml 
W0301 15:07:17.840208    8275 helpers.go:598] --dry-run is deprecated and can be replaced with --dry-run=client.
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: springboot-k8s
  name: springboot-k8s
spec:
  replicas: 1
  selector:
    matchLabels:
      app: springboot-k8s
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: springboot-k8s
    spec:
      containers:
      - image: 38-springboot-k8s-1.0.0.jar
        name: 38-springboot-k8s-1-0-0-jar-7rgt2
        resources: {}
status: {}


2.空运行测试并将输出追加到deploy.yaml
[root@k8smaster data]# kubectl create deployment springboot-k8s --image=38-springboot-k8s-1.0.0.jar --dry-run -o yaml > deploy.yaml
W0301 15:05:52.825938    7574 helpers.go:598] --dry-run is deprecated and can be replaced with --dry-run=client.
[root@k8smaster data]# cat deploy.yaml 
apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: springboot-k8s
  name: springboot-k8s
spec:
  replicas: 1
  selector:
    matchLabels:
      app: springboot-k8s
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: springboot-k8s
    spec:
      containers:
      - image: 38-springboot-k8s-1.0.0.jar
        name: 38-springboot-k8s-1-0-0-jar-pm68t
        resources: {}
status: {}

四、部署应用(命令或yaml文件部署)

1.编译yaml文件增加imagePullPolicy,防止默认从中心仓库拉取镜像
[root@k8smaster data]# vim deploy.yaml 
。。。。。。
    spec:
      containers:
      - image: 38-springboot-k8s-1.0.0-jar
        name: 38-springboot-k8s-1-0-0-jar-5wlr5
        imagePullPolicy: Never #不从源仓库拉取
        resources: {}
status: {}

2.部署应用
[root@k8smaster data]# kubectl apply -f deploy.yaml 
deployment.apps/springboot-k8s created
[root@k8smaster data]# kubectl get deploy
NAME             READY   UP-TO-DATE   AVAILABLE   AGE
nginx            1/1     1            1           13d
springboot-k8s   1/1     1            1           13s
tomcat           1/1     1            1           7d
[root@k8smaster data]# kubectl get pod
NAME                              READY   STATUS    RESTARTS        AGE
nginx-85b98978db-ptctw            1/1     Running   1 (7d20h ago)   12d
springboot-k8s-6fbf8d59f4-2przf   1/1     Running   0               17s
tomcat-655b94657b-x5ppq           1/1     Running   1 (7d20h ago)   7d

3.查看pod详细信息
[root@k8smaster data]# kubectl describe pod springboot-k8s-6fbf8d59f4-2przf
Name:         springboot-k8s-6fbf8d59f4-2przf
Namespace:    default
Priority:     0
Node:         k8snode/192.168.137.21
Start Time:   Tue, 21 Feb 2023 18:59:31 +0800
Labels:       app=springboot-k8s
              pod-template-hash=6fbf8d59f4
Annotations:  <none>
Status:       Running
IP:           10.244.1.17
IPs:
  IP:           10.244.1.17

4.查看pod日志
[root@k8smaster data]# kubectl logs springboot-k8s-6fbf8d59f4-2przf

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.1.RELEASE)

2023-02-21 10:59:32.682  INFO 1 --- [           main] com.bjpowernode.Application              : Starting Application v1.0.0 on springboot-k8s-6fbf8d59f4-2przf with PID 1 (/opt/38-springboot-k8s-1.0.0.jar started by root in /)
2023-02-21 10:59:32.685  INFO 1 --- [           main] com.bjpowernode.Application              : No active profile set, falling back to default profiles: default
2023-02-21 10:59:33.627  INFO 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-02-21 10:59:33.634  INFO 1 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-02-21 10:59:33.634  INFO 1 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.36]
2023-02-21 10:59:33.710  WARN 1 --- [           main] o.a.tomcat.util.scan.StandardJarScanner  : Failed to scan [jar:file:/opt/38-springboot-k8s-1.0.0.jar!/BOOT-INF/lib/spring-webmvc-5.2.7.RELEASE.jar!/] from classloader hierarchy

5.暴露服务端口
[root@k8smaster data]# kubectl expose deployment springboot-k8s --port=8080 --type=NodePort
service/springboot-k8s exposed
[root@k8smaster data]# kubectl get service
NAME             TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)          AGE
kubernetes       ClusterIP   10.96.0.1       <none>        443/TCP          13d
nginx            NodePort    10.108.5.189    <none>        80:32675/TCP     9d
springboot-k8s   NodePort    10.106.94.242   <none>        8080:31419/TCP   7s
tomcat           NodePort    10.102.47.228   <none>        8080:30584/TCP   6d

五、页面访问

 

 容器服务可查看到是运行在node上:

[root@k8snode data]# docker ps -a |grep spring
8cd29b7f33e9   aaa4f688b277                                         "/bin/sh -c 'java -j…"   6 minutes ago    Up 6 minutes                            k8s_38-springboot-k8s-1-0-0-jar-pm68t_springboot-k8s-6fbf8d59f4-2przf_default_bf21d8ab-8e50-45c5-b701-6550cb0f54f5_0
518c437bcd11   registry.aliyuncs.com/google_containers/pause:3.6    "/pause"                 6 minutes ago    Up 6 minutes                            k8s_POD_springboot-k8s-6fbf8d59f4-2przf_default_bf21d8ab-8e50-45c5-b701-6550cb0f54f5_0

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值