学海无涯
go,go,go

k8s部署java项目-配置sev和ingress导入数据库重新构建tomcat的镜像在滚动更新(四)

创建service和ingress
[root@k8s-master java]# kubectl apply -f service.yaml 
service/tomcat-java-demo created
[root@k8s-master java]# kubectl apply -f ingress.yaml 
ingress.extensions/tomcat-java-demo created

#检查
[root@k8s-master java]# kubectl get pods,svc,ing,deployment -n test -o wide
NAME                                    READY   STATUS    RESTARTS   AGE   IP                NODE        NOMINATED NODE   READINESS GATES
pod/tomcat-java-demo-76c84894d5-6xbrp   1/1     Running   0          39m   192.168.169.134   k8s-node2   <none>           <none>
pod/tomcat-java-demo-76c84894d5-7kbpc   1/1     Running   0          39m   192.168.107.202   k8s-node3   <none>           <none>
pod/tomcat-java-demo-76c84894d5-rkbxs   1/1     Running   0          39m   192.168.36.68     k8s-node1   <none>           <none>

NAME                       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE     SELECTOR
service/tomcat-java-demo   ClusterIP   10.0.0.163   <none>        80/TCP    4m13s   app=java-demo,project=www

NAME                                  HOSTS            ADDRESS   PORTS   AGE
ingress.extensions/tomcat-java-demo   java.ctnrs.com             80      4s

NAME                               READY   UP-TO-DATE   AVAILABLE   AGE   CONTAINERS   IMAGES                               SELECTOR
deployment.apps/tomcat-java-demo   3/3     3            3           39m   tomcat       192.168.2.194/project/java-demo:v1   app=java-demo,project=www

#部署ingress的负载均衡器
[root@k8s-master java]# kubectl get pod -n ingress-nginx
NAME                                        READY   STATUS              RESTARTS   AGE
nginx-ingress-controller-647bb4f5d8-hpsdj   0/1     ContainerCreating   0          77s


#配置windosf机器的解析,随便解析一个node节点即可,解析master节点无法访问
192.168.2.191 java.ctnrs.com

#浏览器访问测试

mysql的pod创建
[root@k8s-master java]# kubectl apply -f mysql.yaml 
service/mysql unchanged
statefulset.apps/db created

#J检查,创建成功
[root@k8s-master java]# kubectl get pods,svc -n test -o wide
NAME                                    READY   STATUS    RESTARTS   AGE   IP                NODE        NOMINATED NODE   READINESS GATES
pod/db-0                                1/1     Running   0          72s   192.168.36.69     k8s-node1   <none>           <none>
pod/tomcat-java-demo-76c84894d5-6xbrp   1/1     Running   0          67m   192.168.169.134   k8s-node2   <none>           <none>
pod/tomcat-java-demo-76c84894d5-7kbpc   1/1     Running   0          67m   192.168.107.202   k8s-node3   <none>           <none>
pod/tomcat-java-demo-76c84894d5-rkbxs   1/1     Running   0          67m   192.168.36.68     k8s-node1   <none>           <none>

NAME                       TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)    AGE     SELECTOR
service/mysql              ClusterIP   None         <none>        3306/TCP   4m59s   app=mysql-public
service/tomcat-java-demo   ClusterIP   10.0.0.163   <none>        80/TCP     31m     app=java-demo,project=www


#检查mysql的pv和pvc
[root@k8s-master java]# kubectl get pv,pvc
NAME                                                        CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS   CLAIM                  STORAGECLASS          REASON   AGE
persistentvolume/pvc-0ce1e471-b984-413e-a0d7-df2aa786e0c1   2Gi        RWX            Delete           Bound    test/mysql-data-db-0   managed-nfs-storage            2m4s

#登陆数据库认证
[root@k8s-master yaml]# kubectl exec -it  db-0 sh -n test

#上传数据库文件
[root@k8s-master java]# ls tables_ly_tomcat.sql 
tables_ly_tomcat.sql

#拷贝数据库文件到mysql的pod容器
kubectl cp /yaml/tables_ly_tomcat.sql  db-0:/ -n test

#登陆数据库导入数据
[root@k8s-master yaml]# kubectl exec -it  db-0 sh -n test
# mysql -uroot -p123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.28 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

#导入数据
mysql> source /tables_ly_tomcat.sql
Query OK, 1 row affected, 1 warning (0.00 sec)

Database changed
Query OK, 0 rows affected (0.05 sec)

Query OK, 0 rows affected (0.00 sec)

#确定mysql的链接名称
#登陆mysql的pod
[root@k8s-master ~]# kubectl exec -it db-0 sh -n test
#查看hosts文件确定
# cat /etc/hosts
# Kubernetes-managed hosts file.
127.0.0.1	localhost
::1	localhost ip6-localhost ip6-loopback
fe00::0	ip6-localnet
fe00::0	ip6-mcastprefix
fe00::1	ip6-allnodes
fe00::2	ip6-allrouters
10.244.2.3	db-0.mysql.test.svc.cluster.local	db-0
#在另外一个pod拿名称进行测试
[root@k8s-master yaml]# kubectl exec -it busybox sh
#进行ping测试
/ # ping db-0.mysql.test.svc.cluster.local
PING db-0.mysql.test.svc.cluster.local (10.244.2.3): 56 data bytes
64 bytes from 10.244.2.3: seq=0 ttl=62 time=0.810 ms
64 bytes from 10.244.2.3: seq=1 ttl=62 time=0.674 ms
64 bytes from 10.244.2.3: seq=2 ttl=62 time=0.608 ms
64 bytes from 10.244.2.3: seq=3 ttl=62 time=0.659 ms
64 bytes from 10.244.2.3: seq=4 ttl=62 time=0.563 ms

#修改tomcat容器的链接地址
jdbc:mysql://db-0.mysql.test.svc.cluster.local:3306/test?characterEncoding=utf-8
db-0.mysql.test.svc.cluster.local :pod的名称.mysql的名称.命名空间的名字.svc的名称.域的名称
cat >/tools/tomcat-java-demo-master/src/main/resources/application.yml<<'EOF'
server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://db-0.mysql.test.svc.cluster.local:3306/test?characterEncoding=utf-8
    username: root
    password: 123456
    driver-class-name: com.mysql.jdbc.Driver
  freemarker:
    allow-request-override: false
    cache: true
    check-template-location: true
    charset: UTF-8
    content-type: text/html; charset=utf-8
    expose-request-attributes: false
    expose-session-attributes: false
    expose-spring-macro-helpers: false
    suffix: .ftl
    template-loader-path:
      - classpath:/templates/
EOF

在次构建
[root@k8s-harbor-nfs tomcat-java-demo-master]# /usr/local/maven/bin/mvn clean package
[INFO] Scanning for projects...
[INFO] 
[INFO] ------------------------------------------------------------------------
[INFO] Building ly-simple-tomcat 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-clean-plugin:3.0.0:clean (default-clean) @ ly-simple-tomcat ---
[INFO] Deleting /tools/tomcat-java-demo-master/target
[INFO] 
[INFO] --- maven-resources-plugin:3.0.1:resources (default-resources) @ ly-simple-tomcat ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 1 resource
[INFO] Copying 136 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ ly-simple-tomcat ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 7 source files to /tools/tomcat-java-demo-master/target/classes
[INFO] 
[INFO] --- maven-resources-plugin:3.0.1:testResources (default-testResources) @ ly-simple-tomcat ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /tools/tomcat-java-demo-master/src/test/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ ly-simple-tomcat ---
[INFO] No sources to compile
[INFO] 
[INFO] --- maven-surefire-plugin:2.21.0:test (default-test) @ ly-simple-tomcat ---
[INFO] No tests to run.
[INFO] 
[INFO] --- maven-war-plugin:3.1.0:war (default-war) @ ly-simple-tomcat ---
[INFO] Packaging webapp
[INFO] Assembling webapp [ly-simple-tomcat] in [/tools/tomcat-java-demo-master/target/ly-simple-tomcat-0.0.1-SNAPSHOT]
[INFO] Processing war project
[INFO] Webapp assembled in [280 msecs]
[INFO] Building war: /tools/tomcat-java-demo-master/target/ly-simple-tomcat-0.0.1-SNAPSHOT.war
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 9.558 s
[INFO] Finished at: 2019-11-16T14:07:54+08:00
[INFO] Final Memory: 30M/268M
[INFO] ------------------------------------------------------------------------

#修改版本号重新构建,必须更改版本号不然滚动更新会失败
[root@k8s-harbor-nfs tomcat-java-demo-master]# docker build -t 192.168.2.194/project/java-demo:v6 .
Sending build context to Docker daemon  44.08MB
Step 1/4 : FROM lizhenliang/tomcat
 ---> 143035d83fdc
Step 2/4 : LABEL maintainer www.ctnrs.com
 ---> Using cache
 ---> f6759c7fc94d
Step 3/4 : RUN rm -rf /usr/local/tomcat/webapps/*
 ---> Using cache
 ---> 323a9a4cf37c
Step 4/4 : ADD target/*.war /usr/local/tomcat/webapps/ROOT.war
 ---> 032f9b84b9ca
Successfully built 032f9b84b9ca
Successfully tagged 192.168.2.194/project/java-demo:v3

#在次把镜像推送到仓库
[root@k8s-harbor-nfs tomcat-java-demo-master]# docker push 192.168.2.194/project/java-demo:v6
The push refers to repository [192.168.2.194/project/java-demo]
4f22b3a47014: Pushed 
131a5858f30c: Layer already exists 
ceead5ca823f: Layer already exists 
2353c173a26a: Layer already exists 
071d8bd76517: Layer already exists 
v2: digest: sha256:28d00dd92d8f21c6d4991dff91486b68afe05a87d0a8223db22de59dafb62070 size: 1371
#修改deployment.yaml的文件滚动更新tomcat的pod
cat >deployment.yaml<<'EOF'
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcat-java-demo
  namespace: test
spec:
  replicas: 3
  selector:
    matchLabels:
      project: www
      app: java-demo
  template:
    metadata:
      labels:
        project: www
        app: java-demo
    spec:
      imagePullSecrets:
      - name: registry-pull-secret
      containers:
      - name: tomcat
        image: 192.168.2.194/project/java-demo:v6
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 8080
          name: web
          protocol: TCP
        resources:
          requests:
            cpu: 0.5
            memory: 1Gi
          limits:
            cpu: 1
            memory: 2Gi
        livenessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 60
          timeoutSeconds: 20
        readinessProbe:
          httpGet:
            path: /
            port: 8080
          initialDelaySeconds: 60
          timeoutSeconds: 20
EOF

#滚动更新
[root@k8s-master yaml]# kubectl apply -f deployment.yaml 
Warning: kubectl apply should be used on resource created by either kubectl create --save-config or kubectl apply
deployment.apps/tomcat-java-demo configured、

#检查
[root@k8s-master yaml]# kubectl get pod -n test
NAME                                READY   STATUS    RESTARTS   AGE
db-0                                1/1     Running   0          14h
tomcat-java-demo-76c84894d5-4zqdk   1/1     Running   0          15h
tomcat-java-demo-76c84894d5-bp4w6   1/1     Running   0          15h
tomcat-java-demo-76c84894d5-dxs4b   1/1     Running   0          15h
tomcat-java-demo-9648b64b6-6vggs    0/1     Running   0          42s
正在滚动更新

#滚动更新完添加美女进行测试
赞(0) 打赏
未经允许不得转载:YYQ运维技术博客_运维的工作学习之路 » k8s部署java项目-配置sev和ingress导入数据库重新构建tomcat的镜像在滚动更新(四)
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

运维devops

联系我们关于本博客

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏