SpringBoot项目
新建springboot项目
@RestController
public class HelloWorldController {
@RequestMapping("/")
public String home() {
return "Hello Docker World";
}
}
构建镜像
前面说过怎么构建了
查看:
docker search hongdada|grep demo
hongdada/com.huishi.demo 0
docker方式启动
docker run -p 8080:8080 -t hongdada/com.huishi.demo:latest
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.1.8.RELEASE)
2019-10-08 09:03:06.690 INFO 1 --- [ main] com.huishi.demo.DemoApplication : Starting DemoApplication v0.0.1-SNAPSHOT on 55df098daeb9 with PID 1 (/app.jar started by root in /)
2019-10-08 09:03:06.697 INFO 1 --- [ main] com.huishi.demo.DemoApplication : No active profile set, falling back to default profiles: default
2019-10-08 09:03:10.166 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-10-08 09:03:10.267 INFO 1 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-10-08 09:03:10.268 INFO 1 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.24]
2019-10-08 09:03:10.584 INFO 1 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-10-08 09:03:10.584 INFO 1 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 3685 ms
2019-10-08 09:03:11.126 INFO 1 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-10-08 09:03:11.536 INFO 1 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2019-10-08 09:03:11.549 INFO 1 --- [ main] com.huishi.demo.DemoApplication : Started DemoApplication in 5.986 seconds (JVM running for 7.011)
查看:
$ curl 18.16.202.95:8080
Hello Docker World
删除:
docker stop container_id
docker rm container_id
集成Kubernetes
NodePort
k8s-springboot-demo.yaml
apiVersion: apps/v1beta2
kind: Deployment
metadata:
name: k8s-springboot-demo
labels:
app: k8s-springboot-demo
spec:
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
app: k8s-springboot-demo
template:
metadata:
labels:
app: k8s-springboot-demo
spec:
containers:
- name: k8s-springboot-demo
image: hongdada/com.huishi.demo:latest
ports:
- containerPort: 8080
protocol: TCP
livenessProbe:
httpGet:
path: /
port: 8080
initialDelaySeconds: 30
timeoutSeconds: 30
imagePullPolicy: IfNotPresent
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
---
apiVersion: v1
kind: Service
metadata:
name: k8s-springboot-demo
namespace: default
labels:
app: k8s-springboot-demo
spec:
ports:
- port: 8080
targetPort: 8080
selector:
app: k8s-springboot-demo
type: NodePort
创建deploy和service
[root@master demo]# kubectl apply -f k8s-springboot-demo.yaml
deployment.apps/k8s-springboot-demo created
service/k8s-springboot-demo created
[root@master demo]# kubectl get po,svc,deploy -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/curl-6bf6db5c4f-vhsqc 1/1 Running 11 74d 10.244.2.56 slaver2 <none> <none>
pod/k8s-springboot-demo-766c489688-jcxjz 1/1 Running 0 3m40s 10.244.2.57 slaver2 <none> <none>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/k8s-springboot-demo NodePort 10.105.149.77 <none> 8080:30516/TCP 3m40s app=k8s-springboot-demo
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 74d <none>
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.extensions/curl 1/1 1 1 74d curl radial/busyboxplus:curl run=curl
deployment.extensions/k8s-springboot-demo 1/1 1 1 3m40s k8s-springboot-demo hongdada/com.huishi.demo:latest app=k8s-springboot-demo
NodePort
方式,可以到任意一个节点的30516
端口查看
[root@master demo]# curl -i -X GET slaver2:30516
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 18
Date: Wed, 09 Oct 2019 01:30:20 GMT
Hello Docker World
[root@master demo]# curl -i -X GET master:30516
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 18
Date: Wed, 09 Oct 2019 01:30:33 GMT
Hello Docker World
[root@master demo]# curl -i -X GET slaver1:30516
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 18
Date: Wed, 09 Oct 2019 01:30:44 GMT
Hello Docker World
[root@master demo]# curl -i -X GET 18.16.202.163:30516
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 18
Date: Wed, 09 Oct 2019 01:30:59 GM
ClusterIP
修改k8s-springboot-demo.yaml
apiVersion: v1
kind: Service
metadata:
name: k8s-springboot-demo
namespace: default
labels:
app: k8s-springboot-demo
spec:
ports:
- port: 8080
targetPort: 8080
selector:
app: k8s-springboot-demo
type: ClusterIP
将service的type修改为ClusterIP
应用:
[root@master demo]# vim k8s-springboot-demo.yaml
[root@master demo]# kubectl apply -f k8s-springboot-demo.yaml
deployment.apps/k8s-springboot-demo unchanged
The Service "k8s-springboot-demo" is invalid: spec.ports[0].nodePort: Forbidden: may not be used when `type` is 'ClusterIP'
[root@master demo]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/k8s-springboot-demo NodePort 10.105.149.77 <none> 8080:30516/TCP 37m
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 74d
[root@master demo]# kubectl delete svc k8s-springboot-demo
service "k8s-springboot-demo" deleted
[root@master demo]# kubectl apply -f k8s-springboot-demo.yaml
deployment.apps/k8s-springboot-demo unchanged
service/k8s-springboot-demo created
[root@master demo]# kubectl get po,svc -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/curl-6bf6db5c4f-vhsqc 1/1 Running 11 74d 10.244.2.56 slaver2 <none> <none>
pod/k8s-springboot-demo-766c489688-jcxjz 1/1 Running 0 37m 10.244.2.57 slaver2 <none> <none>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/k8s-springboot-demo ClusterIP 10.111.95.86 <none> 8080/TCP 24s app=k8s-springboot-demo
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 74d <none>
ClusterIP方式不提供对外访问
如果实在要访问,可以使用kubectl proxy
,但是k8s官方不推荐这种方式。
LoadBalancer
将上述的服务删除,并设置为LoadBalancer方式
[root@master demo]# kubectl delete svc k8s-springboot-demo
service "k8s-springboot-demo" deleted
[root@master demo]# kubectl get po,svc,deploy
NAME READY STATUS RESTARTS AGE
pod/curl-6bf6db5c4f-vhsqc 1/1 Running 11 74d
pod/k8s-springboot-demo-766c489688-jcxjz 1/1 Running 0 16h
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 74d
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.extensions/curl 1/1 1 1 74d
deployment.extensions/k8s-springboot-demo 1/1 1 1 16h
命令方式修改
[root@master demo]# kubectl expose deploy k8s-springboot-demo --type=LoadBalancer
service/k8s-springboot-demo exposed
上述两种方式也可以使用kubectl expose
方式修改,比较快捷
yaml文件修改
修改k8s-springboot-demo.yaml
apiVersion: v1
kind: Service
metadata:
name: k8s-springboot-demo
namespace: default
labels:
app: k8s-springboot-demo
spec:
ports:
- port: 8080
targetPort: 8080
selector:
app: k8s-springboot-demo
type: LoadBalancer
将service的type修改为LoadBalancer
kubectl apply -f k8s-springboot-demo.yaml
查看:
[root@master demo]# kubectl get po,svc,deploy -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/curl-6bf6db5c4f-vhsqc 1/1 Running 11 74d 10.244.2.56 slaver2 <none> <none>
pod/k8s-springboot-demo-766c489688-jcxjz 1/1 Running 0 16h 10.244.2.57 slaver2 <none> <none>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/k8s-springboot-demo LoadBalancer 10.101.182.15 <pending> 8080:30852/TCP 2m44s app=k8s-springboot-demo
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 74d <none>
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.extensions/curl 1/1 1 1 74d curl radial/busyboxplus:curl run=curl
deployment.extensions/k8s-springboot-demo 1/1 1 1 16h k8s-springboot-demo hongdada/com.huishi.demo:latest app=k8s-springboot-demo
集群内部访问服务:
[root@master demo]# curl -i -X GET 10.101.182.15:8080
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 18
Date: Wed, 09 Oct 2019 01:41:21 GMT
Hello Docker World
集群外部访问服务:
[root@master demo]# curl -i -X GET master:30852
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 18
Date: Wed, 09 Oct 2019 01:39:23 GMT
Hello Docker World
[root@master demo]# curl -i -X GET slaver1:30852
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 18
Date: Wed, 09 Oct 2019 01:39:35 GMT
Hello Docker World
[root@master demo]# curl -i -X GET slaver2:30852
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 18
Date: Wed, 09 Oct 2019 01:39:39 GMT
Hello Docker World
[root@master demo]# curl -i -X GET 18.16.202.95:30852
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 18
Date: Wed, 09 Oct 2019 01:39:52 GMT
Hello Docker World
参考:
Spring Boot 项目转容器化 K8S 部署实用经验分享
K8s 集群使用 ConfigMap 优雅加载 Spring Boot 配置文件