06 k8s部署java应用 简易版本


项目迁移至K8S流程

  1. 制作镜像
  2. 控制器管理POD
  3. 暴露应用
  4. 对外发布
  5. 日志监控

制作镜像

获取 javademo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1. 安装Git
[root@k8s-master2 opt]# yum install git

2. 下载项目
[root@k8s-master2 opt]# cd /opt/
[root@k8s-master2 opt]# git clone https://github.com/lizhenliang/tomcat-java-demo.git

[root@k8s-master2 tomcat-java-demo]# ls -l
total 32
drwxr-xr-x 2 root root 4096 Nov 20 16:33 db # 数据库sql文件
-rw-r--r-- 1 root root 148 Nov 20 16:33 Dockerfile # 构建镜像
-rw-r--r-- 1 root root 11357 Nov 20 16:33 LICENSE
-rw-r--r-- 1 root root 1930 Nov 20 16:33 pom.xml # maven构建环境
-rw-r--r-- 1 root root 270 Nov 20 16:33 README.md
drwxr-xr-x 3 root root 4096 Nov 20 16:33 src # 源码目录

安装 mariadb 数据库

1
2
3
4
[root@k8s-master2 tomcat-java-demo]# yum -y install mariadb mariadb-server
[root@k8s-master2 tomcat-java-demo]# systemctl start mariadb
[root@k8s-master2 tomcat-java-demo]# systemctl enable mariadb
[root@k8s-master2 tomcat-java-demo]# mysql_secure_installation

导入sql文件

1
2
3
4
[root@k8s-master2 tomcat-java-demo]# mysql -uroot -p
MariaDB [(none)]> use test
Database changed
MariaDB [test]> source /opt/tomcat-java-demo/db/tables_ly_tomcat.sql

测试访问

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 授权访问用户
MariaDB [test]> grant all on test.* to 'test'@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)

# 在node2上测试
[root@k8s-node2 ~]# yum install mysql
[root@k8s-node2 ~]# mysql -h172.17.70.252 -utest -p

MariaDB [(none)]> use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| user |
+----------------+
1 row in set (0.00 sec)

修改连库配置

1
2
3
4
5
6
[root@k8s-master2 resources]# vim /opt/tomcat-java-demo/src/main/resources/application.yml 

datasource:
url: jdbc:mysql://172.17.70.252:3306/test?characterEncoding=utf-8
username: test
password: 123456

编译源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@k8s-master2 tomcat-java-demo]# yum search openjdk
[root@k8s-master2 tomcat-java-demo]# yum install java-1.8.0-openjdk maven

# mvn 编译
[root@k8s-master2 tomcat-java-demo]# mvn clean package -Dmaven.test.skip=true
...
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.882s
[INFO] Finished at: Wed Nov 20 17:08:13 CST 2019
[INFO] Final Memory: 22M/95M
[INFO] ------------------------------------------------------------------------

# 查看生成的war包
[root@k8s-master2 tomcat-java-demo]# ls
db Dockerfile LICENSE pom.xml README.md src target
[root@k8s-master2 tomcat-java-demo]# cd target/
[root@k8s-master2 target]# ls -l *.war
-rw-r--r-- 1 root root 18265402 Nov 20 17:08 ly-simple-tomcat-0.0.1-SNAPSHOT.war

构建项目镜像

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 引用了老师的tomcat镜像运行环境
[root@k8s-master2 tomcat-java-demo]# vim Dockerfile

FROM lizhenliang/tomcat
LABEL maintainer www.ctnrs.com
RUN rm -rf /usr/local/tomcat/webapps/*
ADD target/*.war /usr/local/tomcat/webapps/ROOT.war

# 构建镜像
[root@k8s-master2 tomcat-java-demo]# docker build -t 172.17.70.252/project/java-demo .
[root@k8s-master2 tomcat-java-demo]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
172.17.70.252/project/java-demo latest f656fe851ad7 24 seconds ago 406M

# 推送到镜像仓库
[root@k8s-master2 tomcat-java-demo]# docker login 172.17.70.252
[root@k8s-master2 tomcat-java-demo]# docker push 172.17.70.252/project/java-demo

控制器管理 POD

deployment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# web 无状态应用 deployment
[root@k8s-master1 opt]# mkdir -p /opt/java-demo
[root@k8s-master1 opt]# cd /opt/java-demo/

# 生成yaml文件
[root@k8s-master1 java-demo]# kubectl create deployment java-demo --help
[root@k8s-master1 java-demo]# kubectl create deployment java-demo --image 172.17.70.252/project/java-demo:latest --dry-run -o yaml > deploy.yaml

[root@k8s-master1 java-demo]# vim deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: java-demo
name: java-demo
spec:
replicas: 3
selector:
matchLabels:
app: java-demo
template:
metadata:
labels:
app: java-demo
spec:
containers:
- image: 172.17.70.252/project/java-demo:latest
name: java-demo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 创建 pod 
[root@k8s-master1 java-demo]# kubectl apply -f deploy.yaml
deployment.apps/java-demo created

[root@k8s-master1 java-demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
java-demo-7746bb968c-4drq9 1/1 Running 0 12s
java-demo-7746bb968c-54gtv 1/1 Running 0 12s
java-demo-7746bb968c-jjtzr 1/1 Running 0 12s

# 查看日志
[root@k8s-master1 java-demo]# kubectl logs java-demo-7746bb968c-4drq9

# 删除掉一个pod 会重新创建
[root@k8s-master1 java-demo]# kubectl delete pod java-demo-7746bb968c-jjtzr
pod "java-demo-7746bb968c-jjtzr" deleted

[root@k8s-master1 java-demo]# kubectl get pods
NAME READY STATUS RESTARTS AGE
java-demo-7746bb968c-4drq9 1/1 Running 0 2m23s
java-demo-7746bb968c-54gtv 1/1 Running 0 2m23s
java-demo-7746bb968c-s7824 1/1 Running 0 19s

service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# service 暴露应用 
[root@k8s-master1 java-demo]# kubectl expose --help

Usage:
kubectl expose (-f FILENAME | TYPE NAME) [--port=port] [--protocol=TCP|UDP|SCTP] [--target-port=number-or-name]
[--name=name] [--external-ip=external-ip-of-service] [--type=type] [options]

# --port=80 service端口,集群内部使用
# --target-prot=8080 容器服务端口,tomcat是8080
# --type=NodePort 生成随机端口,集群外部访问

[root@k8s-master1 java-demo]# kubectl expose deployment java-demo --port=80 --target-port=8080 --type=NodePort --dry-run -o yaml > svc.yaml

[root@k8s-master1 java-demo]# vim svc.yaml

apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: java-demo
name: java-demo
spec:
ports:
- port: 80
protocol: TCP
targetPort: 8080
selector:
app: java-demo
type: NodePort
status:
loadBalancer: {}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 创建 service
[root@k8s-master1 java-demo]# kubectl apply -f svc.yaml
service/java-demo created

# 查看
[root@k8s-master1 java-demo]# kubectl get pods,svc
NAME READY STATUS RESTARTS AGE
pod/java-demo-7746bb968c-4drq9 1/1 Running 0 10m
pod/java-demo-7746bb968c-54gtv 1/1 Running 0 10m
pod/java-demo-7746bb968c-s7824 1/1 Running 0 8m9s

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/java-demo NodePort 10.0.0.72 <none> 80:30999/TCP 15s
service/kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 7d6h

测试访问

1
2
3
# nodeIP + 端口访问
http://39.106.168.181:30999/
http://39.106.100.108:30999/
1
2
3
4
5
6
7
8
9
10
# 测试添加数据 然后 分别查看

MariaDB [test]> select * from user;
+----+--------+-----+------+
| id | name | age | sex |
+----+--------+-----+------+
| 1 | 小乔 | 28 | F |
| 2 | 大乔 | 29 | M |
+----+--------+-----+------+
2 rows in set (0.00 sec)