首先我们需要搭建一个简单的SpringBoot应用:

引入dependency依赖

org.springframework.boot
spring-boot-starter-web
打包docker镜像的配置:
打包出来的镜像名称
org.springframework.boot
spring-boot-maven-plugin
2.2.5.RELEASE
com.spotify
docker-maven-plugin
1.0.0
${project.artifactId}
1.0.1
src/main/docker
/
${project.build.directory}
${project.build.finalName}.jar
接着是简单的controller和启动类:
@RestController
@RequestMapping(value = “/test”)
public class TestController {

@GetMapping(value = “/do-test”)
public String doTest(){
System.out.println(“this is a test”);
return “success”;
}
}

@SpringBootApplication
public class WebApplication {

public static void main(String[] args) {
SpringApplication.run(WebApplication.class);
}
}
编写Dockerfile的脚本:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
#将springboot-k8s-template.jar复制到容器内部 并且别名叫springboot-k8s-template-v1.jar
ADD springboot-k8s-template.jar springboot-k8s-template-v1.jar
#相当于在容器中用cmd命令执行jar包 指定外部配置文件
ENTRYPOINT [“java”,“-Djava.security.egd=file:/dev/./urandom”,“-jar”,“/springboot-k8s-template-v1.jar”]
然后进入到Dockerfile的目录底下,进行镜像的构建:
【idea @ Mac】>>>>>>docker build -t springboot-k8s-template:1.0 .
[+] Building 0.5s (7/7) FINISHED
=> [internal] load build definition from Dockerfile                                                                                                                                       0.0s
=> => transferring dockerfile: 419B                                                                                                                                                       0.0s
=> [internal] load .dockerignore                                                                                                                                                         0.0s
=> => transferring context: 2B                                                                                                                                                           0.0s
=> [internal] load metadata for docker.io/library/openjdk:8-jdk-alpine                                                                                                                   0.0s
=> [internal] load build context                                                                                                                                                         0.3s
=> => transferring context: 17.60MB                                                                                                                                                       0.3s
=> CACHED [1/2] FROM docker.io/library/openjdk:8-jdk-alpine                                                                                                                               0.0s
=> [2/2] ADD springboot-k8s-template.jar springboot-k8s-template-v1.jar                                                                                                                   0.1s
=> exporting to image                                                                                                                                                                     0.1s
=> => exporting layers                                                                                                                                                                   0.1s
=> => writing image sha256:86d02961c4fa5bb576c91e3ebf031a3d8b140ddbb451b9613a2c4d601ac4d853                                                                                               0.0s
=> => naming to docker.io/library/springboot-k8s-template:1.0                                                                                                                             0.0s

Use ‘docker scan’ to run Snyk tests against images to find vulnerabilities and learn how to fix them

【idea @ Mac】>>>>>>docker images | grep template
springboot-k8s-template                                   1.0                                                     86d02961c4fa   48 seconds ago   122MB
构建完成之后,将本地镜像打包发布到镜像仓库中,这里我是通过推送到阿里云镜像仓库的方式来处理的。



首先是登录到docker仓库,然后记录对应的tag信息,最终推送镜像。

$ docker login --username=[阿里云账户名称] registry.cn-qingdao.aliyuncs.com
$ docker tag [ImageId] registry.cn-qingdao.aliyuncs.com/idea_hub/idea_resp:[镜像版本号]
$ docker push registry.cn-qingdao.aliyuncs.com/idea_hub/idea_resp:[镜像版本号]
例如:
【idea @ Mac】>>>>>>docker images | grep config
qiyu-framework-k8s-config         1.0           6168639757e9   2 minutes ago   122MB
【idea @ Mac】>>>>>>docker tag 6168639757e9 registry.cn-qingdao.aliyuncs.com/idea_hub/idea_resp:qiyu-framework-k8s-config-1.0
【idea @ Mac】>>>>>>docker push registry.cn-qingdao.aliyuncs.com/idea_hub/idea_resp:qiyu-framework-k8s-config-1.0
The push refers to repository [registry.cn-qingdao.aliyuncs.com/idea_hub/idea_resp]
1ace00556b41: Pushed
ceaf9e1ebef5: Layer already exists
9b9b7f3d56a0: Layer already exists
f1b5933fe4b5: Layer already exists
qiyu-framework-k8s-config-1.0: digest: sha256:50c1a87484f6cbec699d65321fa5bbe70f5ad6da5a237e95ea87c7953a1c80da size: 1159
【idea @ Mac】>>>>>>

请根据实际镜像信息替换示例中的[ImageId]和[镜像版本号]参数。

将镜像文件打包并且推送到镜像仓库之后,可以通过在yaml文件中编写对应的镜像地址,这样就能保证在镜像下载的时候能从仓库拉取出对应的镜像文件。

通常项目中我们会采用统一的yaml文件来进行pod节点的部署与构建。

yaml配置文件:

apiVersion: apps/v1 #kubectl api-versions 可以通过这条指令去看版本信息
kind: Deployment # 指定资源类别
metadata: #资源的一些元数据
name: springboot-k8s-template-deployment #deloyment的名称
labels:
app: springboot-k8s-template-deployment #标签
spec:
replicas: 2 #创建pod的个数
selector:
matchLabels:
app: springboot-k8s-template-deployment #满足标签为这个的时候相关的pod才能被调度到
template:
metadata:
labels:
app: springboot-k8s-template-v1
spec:
containers:
• name: springboot-k8s-template-v1
image: registry.cn-qingdao.aliyuncs.com/idea_hub/idea_resp:1.0
imagePullPolicy: IfNotPresent
ports:
• containerPort: 8080

由于阿里云的镜像仓库需要用户账号密码权限访问,所以这里我们可以尝试简单一些的策略,登录minikube的内部,提前下载好对应的阿里云镜像。

通过 minikube ssh 指令即可登录到minikube的内部:

采用docker pull指令即可下载对应资源:

docker@minikube:~$ docker pull registry.cn-qingdao.aliyuncs.com/idea_hub/idea_resp:springboot-k8s-template-1.0
springboot-k8s-template-1.0: Pulling from idea_hub/idea_resp
e7c96db7181b: Already exists
f910a506b6cb: Already exists
c2274a1a0e27: Already exists
d2fe98fe1e4e: Pull complete
Digest: sha256:dc1c9caa101df74159c1224ec4d7dcb01932aa8f4a117bba603ffcf35e91c60c
Status: Downloaded newer image for registry.cn-qingdao.aliyuncs.com/idea_hub/idea_resp:springboot-k8s-template-1.0
registry.cn-qingdao.aliyuncs.com/idea_hub/idea_resp:springboot-k8s-template-1.0
docker@minikube:~$

查看对应的镜像文件

镜像拉取策略可以对照官网的介绍来系统认识:

Images | Kubernetes

在yaml文件里我选用了IfNotPresent策略,这条策略能够保证当本地有镜像的时候优先选用本地,没有才选择网络拉取。

最后是找到相关的yaml文件进行pod的部署启动。

kubectl create -f ./k8s-springboot-template.yaml

此时通过 kubectl get pod 命令可以看到对应的pod节点:

springboot 读取k8s 配置 k8s springboot 开发_springboot 读取k8s 配置

最终需要暴露deployment服务:

【idea @ 拍了拍我的iterm2 说】>>>>>> kubectl expose deployment springboot-k8s-template-deployment --type=NodePort
service/springboot-k8s-template-deployment exposed
【idea @ 拍了拍我的iterm2 说】>>>>>> kubectl get pods
NAME                                                 READY   STATUS   RESTARTS   AGE
springboot-k8s-template-deployment-687f8bf86d-gqxcp   1/1     Running   0         7m50s
springboot-k8s-template-deployment-687f8bf86d-lcq5p   1/1     Running   0         7m50s