一、新建springboot项目
在pom.xml文件添加
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>0.4.3</version>
<configuration>
<imageName>${docker.image.prefix}/xxx</imageName>
<dockerDirectory>src/main/docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
注意:springboot自带的spring-boot-maven-plugin不要删,否则会找不到manifast文件。
二、建立自己的镜像:
Dockerfile
FROM java:8
VOLUME /tmp
ADD docker_study.jar docker_study.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/docker_study.jar"]
在服务器上安装docker和k8s,参考https://kuboard.cn/install/install-dashboard.html
并敲入
docker build -t xxx/docker_study .
注意:xxx是你的Docker ID,先在Docker hub上注册一个你的账号
三、使k8s所有机器都能pull你的镜像:
docker tag xxx/docker_study xxx/docker_study:v1
上传dockerhub(要带标签,且镜像Docker ID/镜像名的命名方式):
docker push xxx/docker_study:v1
在其他k8s(非master)上面pull下来这个镜像:
docker pull xxx/docker_study:v1
四、建立deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: hello-service
spec:
replicas: 1 # tells deployment to run 2 pods matching the template
selector:
matchLabels:
app: springbootdemo
template: # create pods using pod definition in this template
metadata:
labels:
app: springbootdemo
spec:
containers:
- name: hello-service
image: xxx/docker_study:v1
ports:
- containerPort: 8080
imagePullSecrets:
- name: xxx
执行:
kubectl apply -f deployment.yaml
五、建立service
apiVersion: v1
kind: Service
metadata:
name: test-service
namespace: default
spec:
selector:
app: springbootdemo
type: NodePort
ports:
- protocol: TCP
port: 8080
targetPort: 8080
nodePort: 30777
执行:
kubectl apply -f service.yaml
六、访问,成功!