目录
一、准备Java应用
二、创建docker镜像
三、上传docker镜像
四、创建minikube容器
如何将Java程序发布到Kubenetes,本文通过一个简单的例子来说明。
开发环境:
Windows 10 Home
Docker Toolbox
virtualbox
minikube
创建Maven工程,Java代码:
package com.test.server;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorMvcAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class, ErrorMvcAutoConfiguration.class })
public class App {
public static void main(String[] args) {
ConfigurableApplicationContext ctx = SpringApplication.run(App.class, args);
ctx.registerShutdownHook();
}
}
package com.test.server;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/")
@RestController
public class BaseController {
@GetMapping(path = { "index" })
public String doIndex() {
return "login";
}
@GetMapping("welcome.do")
public String doHeartbeat() {
return "welcome";
}
}
application.properties
spring.application.name=kube-hello
server.port=7001
pom.xml
4.0.0
com.test
kube-hello
0.0.1-SNAPSHOT
jar
kube-hello
http://maven.apache.org
UTF-8
org.springframework.boot
spring-boot-starter-parent
2.3.3.RELEASE
org.springframework.boot
spring-boot-starter-web
junit
junit
test
org.springframework.boot
spring-boot-maven-plugin
2.3.3.RELEASE
com.test.server.App
JAR
repackage
将当前目录切换到工程目录kube-hello,若使用spring-boot生成jar
mvn clean package spring-boot:repackage -f pom.xml -Dmaven.test.skip=true
在kube-hello目录下创建Dockerfile
# 基于Java8镜像
FROM java:8
# 在容器中创建目录,将本地文件夹挂在到容器目录
VOLUME /app
# 更改容器中的工作目录
WORKDIR /app
# 复制文件到容器
ADD ./target/kube-hello-0.0.1-SNAPSHOT.jar /app/kube-hello.jar
# 声明需要暴露的端口
EXPOSE 7080
# 配置容器启动后执行的命令
ENTRYPOINT ["java","-jar","/app/kube-hello.jar"]
启动Docker Quickstart Terminal后,执行如下命令:
创建images
$ docker build -t kube-hello:0.0.1 .
创建完成后可查询到创建的image
$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
kube-hello 0.0.1 f8f4169625c1 18 minutes ago 660MB
可将docker运行起来,来查看image是不断正确创建
运行image,并开放7080供外部访问:
docker run --name kube-hello-service -d -p 7080:7001 kube-hello:0.0.1
可通过如下命令查看到容器运行状态:
docker container ls -a
如何状态不正常,可通过如下命令查看应用日志:
docker logs
一旦容器正常运行,可进入到container实例中:
docker exec -it
sh
另外,需要在VirtulBox中找到Default实例,在其中的“端口转发”中设置对7080开放,这样在本机就可以通过如下URL访问应用了:
http://127.0.0.1:7080/index
为了让minikube能创建容器,需要上传docker镜像,上传时需要先登录
$ docker login
如果出现以下问题:
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username (XXXXX): XXXXX
Password:
Error response from daemon: Get https://registry-1.docker.io/v2/: dial tcp: lookup registry-1.docker.io on 10.0.2.3:53: server misbehaving
修改方法:
登录到docker所在的default虚拟机
sudo vi /etc/resolv.conf
将nameserver 修改为
nameserver 8.8.8.8
给docker镜像打标记:
$ docker tag f8f4169625c1 XXXXX/kube-hello:v1
将docker镜像上传:
$ docker push XXXXX/kube-hello:v1
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: kube-hello-deployment
spec:
selector:
matchLabels:
app: kube-hello-app
replicas: 2
minReadySeconds: 15
strategy:
type: RollingUpdate
rollingUpdate:
maxUnavailable: 1
maxSurge: 1
template:
metadata:
labels:
app: kube-hello-app
spec:
containers:
- image: XXXXX/kube-hello:v1
imagePullPolicy: Always
name: kube-hello
ports:
- containerPort: 7001
---
apiVersion: v1
kind: Service
metadata:
name: kube-hello-service
spec:
ports:
- port: 8001
targetPort: 7001
selector:
app: kube-hello-app
启动minikube
$ minkube start
创建容器在:
kubectl apply -f deployment.yaml
可通过如下命令查看创建的资源:
kubectl get pods --show-labels -o wide
kubectl get deployment --show-labels -o wide
kubectl get services --show-labels -o wide
比如:
$ kubectl get services --show-labels -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR LABELS
kube-hello-service ClusterIP 10.107.157.38001/TCP 2m app=kube-hello-app
使用minikube ssh登录后,执行如下命令就可以查看到结果
curl http://10.107.157.3:8001/index
如果创建的资源不再使用,可能如下命令删除:
kubectl delete pods --all
kubectl delete deployments --all
kubectl delete service --all
参考文档
Error response from daemon: Get https://registry-1.docker.io/v2/: dial tcp: lookup registry-1.docker.io on 192.168.65.1:53: no such host
Docker网络原则入门