如何在tomcat下应用部署日志_如何在kubernete集群上部署springboot应用
1.打包springboot鏡像
2.在kubernete上發(fā)布鏡像
3.測試
在之前的文章中,我講了使用kubeadm從0到1搭建kubernete集群,今天我們來聊一下如何在這套k8s集群上部署springboot應(yīng)用。首先說明一下,我們這次集群的網(wǎng)絡(luò)使用flannel網(wǎng)絡(luò),master節(jié)點啟動命令如下:
kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version=v1.17.3 --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address 192.168.59.132上面的參數(shù)?--pod-network-cidr=10.244.0.0/16
就是指明集群使用flannel網(wǎng)絡(luò),切記,不然部署springboot后,容器訪問外面網(wǎng)絡(luò)會有問題。
再來看一下這次實驗使用的集群環(huán)境,如下圖,我在個人筆記本上安裝了mysql數(shù)據(jù)庫,ip地址是192.168.59.1,同時安裝了2個vmware虛擬機,ip地址分別是192.168.59.132和192.168.59.138,kubernete集群就部署在這2個虛機上,192.168.59.132部署著master節(jié)點,192.168.59.138部署著worker1節(jié)點
這次使用的springboot源碼地址:
https://github.com/jinjunzhu/spring-boot-mybatis1.打包springboot鏡像
1.打包springboot鏡像
這兒遇到問題可以查看我之前的文章Linux下docker制作springboot應(yīng)用鏡像
編譯jar包
mvn clean -Dmaven.test.skip=true install編譯后上傳到虛機,編寫DockerFile文件
FROM openjdk:8-jdk-alpineVOLUME /tmpADD spring-boot-mybatis-1.0-SNAPSHOT.jar springboot-mybatis.jarENTRYPOINT ["java","-jar","springboot-mybatis.jar"]執(zhí)行打包鏡像命令,這里我打包的版本號是1.2,每次修改代碼后都要升級版本號,不然上傳dockerhub不會更新
docker build -t zjj2006forever/springboot-mybatis:1.2 .之后下面2個命令登錄dockerhub并且上傳
docker logindocker push zjj2006forever/springboot-mybatis:1.2[root@master k8s]# docker push zjj2006forever/springboot-mybatis:1.2The push refers to a repository [docker.io/zjj2006forever/springboot-mybatis]a3ce9cf52624: Pushed ceaf9e1ebef5: Pushed 9b9b7f3d56a0: Pushed f1b5933fe4b5: Pushed 1.0: digest: sha256:8d37516e910515b0e336ecc29befaa0f9240ebd92a3c22af8b90846476644420 size: 11592.在kubernete上發(fā)布鏡像
編寫yaml文件,如下:
apiVersion: apps/v1kind: Deploymentmetadata: name: springboot-mybatis-deploymentspec: selector: matchLabels: app: springboot-mybatis replicas: 2 template: metadata: labels: app: springboot-mybatis spec: containers: - name: spingboot-mybatis imagePullPolicy: IfNotPresent #imagePullPolicy: Never image: zjj2006forever/springboot-mybatis:1.2 ports: - containerPort: 8300上面的yaml文件中,我指定了鏡像名稱是我剛剛打包的鏡像:image: zjj2006forever/springboot-mybatis:1.2,副本數(shù)量是2(replicas: 2),
這樣部署的時候會生成2個pod,如果提供服務(wù)的時候如果一個pod掛了,集群會再創(chuàng)建一個新的出來,一直保持有2個pod。
kubectl?apply?-f?springboot-mybatis.yaml輸出下面提示后我們看一下pod狀態(tài)
deployment.apps/springboot-mybatis-deployment created
[root@master k8s]# kubectl get pods --all-namespacesNAMESPACE NAME READY STATUS RESTARTS AGEdefault springboot-mybatis-deployment-9f5b78b76-2qtfg 1/1 Running 0 9sdefault springboot-mybatis-deployment-9f5b78b76-tzf4x 1/1 Running 0 9skube-system coredns-9d85f5447-7k44c 1/1 Running 0 30mkube-system coredns-9d85f5447-sh24j 1/1 Running 0 30mkube-system etcd-master 1/1 Running 0 30mkube-system kube-apiserver-master 1/1 Running 0 30mkube-system kube-controller-manager-master 1/1 Running 0 30mkube-system kube-flannel-ds-amd64-rd842 1/1 Running 0 16mkube-system kube-flannel-ds-amd64-tdkwx 1/1 Running 0 16mkube-system kube-proxy-d6qqg 1/1 Running 0 25mkube-system kube-proxy-pm7cc 1/1 Running 0 30mkube-system kube-scheduler-master 1/1 Running 0 30m再看一下pod啟動日志
[root@master k8s]# kubectl logs -f springboot-mybatis-deployment-9f5b78b76-2qtfg . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.6.RELEASE)2020-06-13 08:25:15,002 [main] [INFO] boot.Application - Starting Application v1.0-SNAPSHOT on springboot-mybatis-deployment-9f5b78b76-2qtfg with PID 1 (/springboot-mybatis.jar started by root in /)2020-06-13 08:25:15,006 [main] [INFO] boot.Application - The following profiles are active: dev2020-06-13 08:25:20,545 [main] [INFO] org.springframework.context.support.PostProcessorRegistrationDelegate$BeanPostProcessorChecker - Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$c636a19f] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)2020-06-13 08:25:21,688 [main] [INFO] org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8083 (http)2020-06-13 08:25:21,723 [main] [INFO] org.apache.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-8083"]2020-06-13 08:25:21,839 [main] [INFO] org.apache.catalina.core.StandardService - Starting service [Tomcat]2020-06-13 08:25:21,840 [main] [INFO] org.apache.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.21]2020-06-13 08:25:22,260 [main] [INFO] org.apache.catalina.core.ContainerBase.[Tomcat].[localhost].[/] - Initializing Spring embedded WebApplicationContext2020-06-13 08:25:22,261 [main] [INFO] org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 6985 ms2020-06-13 08:25:25,220 [main] [INFO] springfox.documentation.spring.web.PropertySourcedRequestMappingHandlerMapping - Mapped URL path [/v2/api-docs] onto method [public org.springframework.http.ResponseEntity springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)]2020-06-13 08:25:25,631 [main] [INFO] org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor - Initializing ExecutorService 'applicationTaskExecutor'2020-06-13 08:25:27,595 [main] [INFO] springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper - Context refreshed2020-06-13 08:25:27,710 [main] [INFO] springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper - Found 1 custom documentation plugin(s)2020-06-13 08:25:27,857 [main] [INFO] springfox.documentation.spring.web.scanners.ApiListingReferenceScanner - Scanning for api listing references2020-06-13 08:25:28,755 [main] [INFO] org.apache.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-8083"]2020-06-13 08:25:28,956 [main] [INFO] org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 8083 (http) with context path ''2020-06-13 08:25:29,253 [main] [INFO] org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat initialized with port(s): 18082 (http)2020-06-13 08:25:29,255 [main] [INFO] org.apache.coyote.http11.Http11NioProtocol - Initializing ProtocolHandler ["http-nio-127.0.0.1-18082"]2020-06-13 08:25:29,289 [main] [INFO] org.apache.catalina.core.StandardService - Starting service [Tomcat]2020-06-13 08:25:29,289 [main] [INFO] org.apache.catalina.core.StandardEngine - Starting Servlet engine: [Apache Tomcat/9.0.21]2020-06-13 08:25:29,304 [main] [INFO] org.apache.catalina.core.ContainerBase.[Tomcat-1].[localhost].[/] - Initializing Spring embedded WebApplicationContext2020-06-13 08:25:29,305 [main] [INFO] org.springframework.web.context.ContextLoader - Root WebApplicationContext: initialization completed in 336 ms2020-06-13 08:25:29,389 [main] [INFO] org.springframework.boot.actuate.endpoint.web.EndpointLinksResolver - Exposing 16 endpoint(s) beneath base path '/actuator'2020-06-13 08:25:29,659 [main] [INFO] org.apache.coyote.http11.Http11NioProtocol - Starting ProtocolHandler ["http-nio-127.0.0.1-18082"]2020-06-13 08:25:29,692 [main] [INFO] org.springframework.boot.web.embedded.tomcat.TomcatWebServer - Tomcat started on port(s): 18082 (http) with context path ''2020-06-13 08:25:29,696 [main] [INFO] boot.Application - Started Application in 17.382 seconds (JVM running for 21.29)上面的結(jié)果可以看到我剛剛發(fā)布的2個springboot的pod啟動成功了,我們再看下其中一個pod的狀態(tài),執(zhí)行如下命令
kubectl describe pod springboot-mybatis-deployment-9f5b78b76-2qtfg[root@master k8s]# kubectl describe pod springboot-mybatis-deployment-9f5b78b76-2qtfgName: springboot-mybatis-deployment-9f5b78b76-2qtfgNamespace: defaultPriority: 0Node: worker1/192.168.59.138Start Time: Sat, 13 Jun 2020 04:25:04 -0400Labels: app=springboot-mybatis pod-template-hash=9f5b78b76Annotations: <none>Status: RunningIP: 10.244.1.2IPs: IP: 10.244.1.2Controlled By: ReplicaSet/springboot-mybatis-deployment-9f5b78b76Containers: spingboot-mybatis: Container ID: docker://f3b0b7d1e2b9da0970e23134bbece26e4eb4fb0416e303119a0c4a34b954b666 Image: zjj2006forever/springboot-mybatis:1.2 Image ID: docker-pullable://zjj2006forever/springboot-mybatis@sha256:0e6f78256ab70ed5244042abc2a5bd5154a193fb9f97f05a3061e8334d387986 Port: 8300/TCP Host Port: 0/TCP State: Running Started: Sat, 13 Jun 2020 04:25:08 -0400 Ready: True Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-cjp27 (ro)Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: default-token-cjp27: Type: Secret (a volume populated by a Secret) SecretName: default-token-cjp27 Optional: falseQoS Class: BestEffortNode-Selectors: <none>Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300sEvents: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 3m53s default-scheduler Successfully assigned default/springboot-mybatis-deployment-9f5b78b76-2qtfg to worker1 Normal Pulled 3m50s kubelet, worker1 Container image "zjj2006forever/springboot-mybatis:1.2" already present on machine Normal Created 3m50s kubelet, worker1 Created container spingboot-mybatis Normal Started 3m49s kubelet, worker1 Started container spingboot-mybatis上面的輸出看到這個pod被調(diào)度到了worker1節(jié)點上,同時從上面的Events可以看到pod的啟動過程。
3.測試
先讓我們看一下我們剛剛部署的2個pod
[root@master k8s]# kubectl get pod -l app -o wideNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATESspringboot-mybatis-deployment-5b78f66997-2hhdq 1/1 Running 0 7m23s 10.244.1.4 worker1 <none> <none>springboot-mybatis-deployment-5b78f66997-k87dx 1/1 Running 0 6m42s 10.244.1.5 worker1 <none> <none>上面的輸出結(jié)果中可以看到,這2個pod的ip地址是10.244.1.4和10.244.1.5,這是由flannel網(wǎng)絡(luò)插件分配的。
接著我們進(jìn)入容器內(nèi)部看一下網(wǎng)絡(luò)連通性
kubectl exec -it springboot-mybatis-deployment-5b78f66997-2hhdq -- /bin/sh我們分別ping集群的主192.168.59.132,從節(jié)點192.168.59.138和mysql數(shù)據(jù)庫地址192.168.59.1,發(fā)現(xiàn)都是正常返回數(shù)據(jù)包的
我們用curl看一下健康檢查的地址,返回如下
[root@worker1 ~]# curl http://10.244.1.4:18082/actuator/health{"status":"UP","details":{"db":{"status":"UP","details":{"firstDataSource":{"status":"UP","details":{"database":"MySQL","hello":1}},"secondDataSource":{"status":"UP","details":{"database":"MySQL","hello":1}}}},"diskSpace":{"status":"UP","details":{"total":18238930944,"free":10361712640,"threshold":10485760}}}}我們用瀏覽器輸入地址http://10.244.1.4:8083/employee/jinjunzhu,返回505
這時在容器里面查看日志,輸出見下面截圖
cd /app/jar/logs/springboot-mybatis-deployment-5b78f66997-2hhdq/applogtail -f catalina.out這個原因很明顯,mysql沒有對這個ip訪問授權(quán),執(zhí)行一下授權(quán)命令:
//對ip地址192.168.59.132授權(quán)grant all privileges on *.* to 'root'@'192.168.59.132' identified by '123456';//是授權(quán)立即生效flush privileges;?再次訪問上面的url,成功,見下圖
?pod中的日志如下:
總結(jié)
以上是生活随笔為你收集整理的如何在tomcat下应用部署日志_如何在kubernete集群上部署springboot应用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 台式机怎么连接wi 连接台式机至无线网络
- 下一篇: 主板不支持u盘启动怎么办 怎样解决主板无