k8s基础实战之部署java项目

1 部署应用步骤

  • 制作镜像,镜像分类:基础镜像,中间件镜像,项目镜像.
  • 控制器管理pod 控制器分类: deployment,statefulset,daemonset,job&cronjob
  • pod数据持久化
  • service
  • 对外发布应用: service nodeport ingress

2 部署一个java项目

先打包
mvn clean package -DskipTests=true
Downloaded: https://repo.maven.apache.org/maven2/com/thoughtworks/xstream/xstream/1.4.9/xstream-1.4.9.jar (537 KB at 964.5 KB/sec)
[INFO] Packaging webapp
[INFO] Assembling webapp [ly-simple-tomcat] in [/root/tomcat-java-demo/target/ly-simple-tomcat-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Webapp assembled in [286 msecs]
[INFO] Building war: /root/tomcat-java-demo/target/ly-simple-tomcat-0.0.1-SNAPSHOT.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1:53.386s
[INFO] Finished at: Thu Jun 03 01:34:42 GMT 2021
[INFO] Final Memory: 25M/175M
[INFO] ----------------------------------------------------------------------

dockerfile构建镜像推送到镜像仓储
 cat Dockerfile
FROM lizhenliang/tomcat 
MAINTAINER devops
RUN rm -rf /usr/local/tomcat/webapps/*
ADD target/*.war /usr/local/tomcat/webapps/ROOT.war

docker build -t tomcat -f Dockerfile ./
docker tag tomcat 192.168.10.20:8081/javademo/javademo:latest
推送到harbor
[root@gitlab tomcat-java-demo]# docker push  192.168.10.20:8081/javademo/javademo:latest

如下图已传到harbor

k8s基础实战之部署java项目

部署java-demo 创建k8s连接 harbor的认证 kubectl create secret docker-registry dockerpullauth --docker-username=admin --docker-password=Harbor12345 --docker-server=192.168.10.20:8081 secret/dockerpullauth created 配置docker [root@k8s-node02 docker]# cat /etc/docker/daemon.json { "registry-mirrors": ["https://de378wx8.mirror.aliyuncs.com"], "insecure-registries":["192.168.10.20:8081"] } service docker force-reload [root@k8s-master01 java-demo]# cat java-demo-deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: labels: app: java-demo name: java-demo spec: replicas: 3 selector: matchLabels: app: java-demo strategy: {} template: metadata: creationTimestamp: null labels: app: java-demo spec: containers: - image: 192.168.10.20:8081/javademo/javademo:latest name: javademo 创建应用service [root@k8s-master01 java-demo]# kubectl expose deployment java-demo --port=80 --target-port=8080 --type=NodePort --dry-run -o yaml > service.yaml apiVersion: v1 kind: Service metadata: labels: app: java-demo name: java-demo spec: ports: - port: 80 protocol: TCP targetPort: 8080 nodePort: 30010 selector: app: java-demo type: NodePort 通过暴露出来的端口就可以访问了
k8s基础实战之部署java项目

配置ingress

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: tomcat-java-demo
  namespace: default
spec:
  rules:
    - host: java.devopstack.cn
      http:
        paths:
        - path: /
          backend:
            serviceName: java-demo
            servicePort: 80

 kubectl apply -f ingress.yaml 

ingress.extensions/tomcat-java-demo created

[root@k8s-master01 java-demo]# kubectl get ingress
NAME               CLASS    HOSTS                                                      ADDRESS   PORTS     AGE

tomcat-java-demo   <none>   java.devopstack.cn                                                   80        6s

[root@k8s-master01 java-demo]# kubectl describe ingress tomcat-java-demo 
Name:             tomcat-java-demo
Namespace:        default
Address:          
Default backend:  default-http-backend:80 (<error: endpoints "default-http-backend" not found>)
Rules:
  Host                Path  Backends
  ----                ----  --------
  java.devopstack.cn  
                      /   java-demo:80 (10.244.2.138:8080,10.244.2.139:8080,10.244.2.140:8080)
Annotations:          Events:
  Type                Reason  Age    From                      Message
  ----                ------  ----   ----                      -------
  Normal              CREATE  3m25s  nginx-ingress-controller  Ingress default/tomcat-java-demo
  Normal              CREATE  3m25s  nginx-ingress-controller  Ingress default/tomcat-java-demo

如下图
通过ingress配置的域名已生效

k8s基础实战之部署java项目
  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: