kubernetes(10):简单web应用tomcat+mysql样例

简单web应用tomcat+mysql

我们测试一个web+db的项目,这种架构都是先启动MySQL再启动tomcat的,注意顺序

1 准备镜像

1.1 下载官方的测试镜像和MySQL

docker pull kubeguide/tomcat-app:v1
docker pull mysql:5.7
[root@k8s-master ~]# docker images | grep -E "mysql|tomcat"
docker.io/mysql                         5.7                 e1e1680ac726        9 days ago          373.3 MB
docker.io/kubeguide/tomcat-app          v2                  00beaa1d956d        3 years ago         358.2 MB
docker.io/kubeguide/tomcat-app          v1                  a29e200a18e9        3 years ago         358.2 MB
[root@k8s-master ~]#

 

1.2 上传到私服

[root@k8s-master ~]# docker tag docker.io/kubeguide/tomcat-app:v1  192.168.0.136:5000/tomcat-app:v1
[root@k8s-master ~]# docker push 192.168.0.136:5000/tomcat-app:v1
The push refers to a repository [192.168.0.136:5000/tomcat-app]
fe9a890c4f24: Pushed 
5f70bf18a086: Pushed 
a072f755a133: Pushed 
6d0267f8a9fd: Pushed 
7bb92eb08c02: Pushed 
d8ba5f179687: Pushed 
2275023dea33: Pushed 
d490458a60cb: Pushed 
bb3e02b5a488: Pushed 
3b7a0c95e085: Pushed 
02adacdfda2f: Pushed 
d2c5e3a8d3d3: Pushed 
4dcab49015d4: Pushed 
v1: digest: sha256:565bb4e52ac67b4d37feed9ea4626b786f23e0871451587c7187683532a6188f size: 5719
[root@k8s-master ~]# docker tag docker.io/mysql:5.7 192.168.0.136:5000/mysql:5.7
[root@k8s-master ~]# docker push 192.168.0.136:5000/mysql:5.7
The push refers to a repository [192.168.0.136:5000/mysql]
2ebc9c2f59ec: Pushed 
de9c8788be4d: Pushed 
59f1d30f4003: Pushed 
8ea3faa6f944: Pushed 
bb1ef34119b2: Pushed 
65430c57aee2: Pushed 
1dd5f3e365cf: Pushed 
7f33ce1066af: Pushed 
9f77b78f01a7: Pushed 
f5741d086b76: Pushed 
8fa655db5360: Pushed 
5.7: digest: sha256:2bd4665d9c5ecad61f7ceff82f82e6470c4686b9ec0fd986b84012861506c722 size: 2621
[root@k8s-master ~]#

 

 

 

2 部署mysql 服务

2.1 创建一个mysql -deployment文件

apiVersion: extensions/v1beta1
kind: Deployment            #副本控制器Deployment
metadata:  
  name: mysql                          #Deployment的名称,全局唯一
spec:
  replicas: 1                          #Pod副本的期待数量
  template:                            #根据此模版创建Pod的副本(实例)
    metadata:
       labels:
         app: mysql                    #Pod副本拥有的标签,对应Deployment的selector
    spec:
       containers:                     #Pod内,定义容器
       - name: mysql                   #容器名称
         image: 192.168.0.136:5000/mysql:5.7              #Docker image
         ports:
         - containerPort: 3306         #容器应用监听的端口
         env:                          #注入容器内的环境变量
         - name: MYSQL_ROOT_PASSWORD   #这里设置root初始密码
           value: "123456"


#RC方式
apiVersion: v1
kind: ReplicationController            #副本控制器RC
metadata:  
  name: mysql                          #RC的名称,全局唯一
spec:
  replicas: 1                          #Pod副本的期待数量
  selector:
    app: mysql                         #符合目标的Pod拥有此标签
  template:                            #根据此模版创建Pod的副本(实例)
    metadata:
       labels:
         app: mysql                    #Pod副本拥有的标签,对应RC的selector
    spec:
       containers:                     #Pod内,定义容器
       - name: mysql                   #容器名称
         image: 192.168.0.136:5000/mysql:5.7              #Docker image
         ports:
         - containerPort: 3306         #容器应用监听的端口
         env:                          #注入容器内的环境变量
         - name: MYSQL_ROOT_PASSWORD   #这里设置root初始密码
           value: "123456"

 

2.2 创建mysql-deployment

kubectl create -f mysql-deploy.yaml

 

2.3 验证mysql-deployment

[root@k8s-master k8s]# kubectl get deployment 
NAME      DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
mysql     1         1         1            1           2m
[root@k8s-master k8s]# kubectl get rs -o wide  
NAME               DESIRED   CURRENT   READY     AGE       CONTAINER(S)   IMAGE(S)                       SELECTOR
mysql-1283186320   1         1         1         2m        mysql          192.168.0.136:5000/mysql:5.7   app=mysql,pod-template-hash=1283186320
[root@k8s-master k8s]# kubectl get pods -o wide
NAME                     READY     STATUS    RESTARTS   AGE       IP            NODE
mysql-1283186320-1pt32   1/1       Running   0          2m        172.16.73.2   k8s-node-1
[root@k8s-master k8s]# 

 

2.4 连接MySQL测试

[root@k8s-master k8s]# mysql -uroot -p123456 -h172.16.73.2 
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.27 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.09 sec)

mysql>

 

 

2.5 创建mysql serveice文件

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  type: NodePort
  ports:
  - port: 3306
    nodePort: 30001
  selector:
    app: mysql

 

2.6 创建MySQL svc

[root@k8s-master k8s]# kubectl create -f mysql-svc.yaml
service "mysql" created

 

2.7 验证MySQL svc

两个node的30001端口都能连MySQL服务

[root@k8s-master k8s]# kubectl get svc  
NAME         CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
kubernetes   10.254.0.1     <none>        443/TCP          3d
mysql        10.254.58.59   <nodes>       3306:30001/TCP   6m
[root@k8s-master k8s]# kubectl get pods
NAME                     READY     STATUS    RESTARTS   AGE
mysql-1283186320-1pt32   1/1       Running   0          12m
[root@k8s-master k8s]#  
[root@k8s-master k8s]# mysql -uroot -p123456 -P30001 -h192.168.0.137
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 4
Server version: 5.7.27 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> ^DBye
[root@k8s-master k8s]# mysql -uroot -p123456 -P30001 -h192.168.0.138
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 5
Server version: 5.7.27 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>

 

 

3 部署web服务

3.1 创建一个tomcat-app-deployment文件

[root@k8s-master k8s]# cat tomcat-app-deployment.yaml 
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: tomcat-app
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: tomcat-app
    spec:
      containers:
      - name: tomcat-app
        image: 192.168.0.136:5000/tomcat-app:v1
        ports:
        - containerPort: 8080
        env:
        - name: MYSQL_SERVICE_HOST
          value: 'mysql' #这是一个坑,最好写IP
        - name: MYSQL_SERVICE_PORT
          value: '3306'

 

3.2 创建tomcat-app-deployment

[root@k8s-master k8s]# kubectl create -f tomcat-app-deployment.yaml 
deployment "tomcat-app" created

 

3.3 验证tomcat-app-deployment

[root@k8s-master k8s]# kubectl get deployment 
NAME         DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
mysql        1         1         1            1           32m
tomcat-app   3         3         3            3           6m
[root@k8s-master k8s]# kubectl get rs -o wide
NAME                    DESIRED   CURRENT   READY     AGE       CONTAINER(S)   IMAGE(S)                           SELECTOR
mysql-1283186320        1         1         1         32m       mysql          192.168.0.136:5000/mysql:5.7       app=mysql,pod-template-hash=1283186320
tomcat-app-3309240903   3         3         3         6m        tomcat-app     192.168.0.136:5000/tomcat-app:v1   app=tomcat-app,pod-template-hash=3309240903
[root@k8s-master k8s]# kubectl get pods -o wide
NAME                          READY     STATUS    RESTARTS   AGE       IP            NODE
mysql-1283186320-1pt32        1/1       Running   0          32m       172.16.73.2   k8s-node-1
tomcat-app-3309240903-338hl   1/1       Running   0          6m        172.16.47.2   k8s-node-2
tomcat-app-3309240903-xfqwv   1/1       Running   0          6m        172.16.47.3   k8s-node-2
tomcat-app-3309240903-xtqsk   1/1       Running   0          6m        172.16.73.3   k8s-node-1
[root@k8s-master k8s]#

 

 

3.4  测试tomcat-app-deployment服务

[root@k8s-master k8s]# curl -I 172.16.73.3:8080 
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 23 Aug 2019 07:58:42 GMT

[root@k8s-master k8s]# curl -I 172.16.47.2:8080
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 23 Aug 2019 07:58:53 GMT

[root@k8s-master k8s]# curl -I 172.16.47.2:8080/demo/
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=471FD0BD8660E49BF9A82E10FCB9AB47; Path=/demo/; HttpOnly
Content-Type: text/html;charset=utf-8
Transfer-Encoding: chunked
Date: Fri, 23 Aug 2019 08:00:39 GMT

[root@k8s-master k8s]#

 

3.5  创建tomcat-app service文件

[root@k8s-master k8s]# cat tomcat-app-svc.yaml 
apiVersion: v1
kind: Service
metadata:
  name: tomcat-app
spec:
  type: NodePort
  ports:
    - port: 8080
      name: myweb-svc
      nodePort: 30002
  selector:
    app: tomcat-app

 

 

3.6 创建tomcat-app service

#kubectl create -f tomcat-app-svc.yaml  
测试
[root@k8s-master k8s]# kubectl get svc -o wide
NAME         CLUSTER-IP       EXTERNAL-IP   PORT(S)          AGE       SELECTOR
kubernetes   10.254.0.1       <none>        443/TCP          3d        <none>
mysql        10.254.58.59     <nodes>       3306:30001/TCP   32m       app=mysql
tomcat-app   10.254.210.153   <nodes>       8080:30002/TCP   10s       app=tomcat-app
[root@k8s-master k8s]#

 


 

3.7  测试tomcat

[root@k8s-master k8s]# kubectl get pods -o wide  
NAME                          READY     STATUS    RESTARTS   AGE       IP            NODE
mysql-1283186320-1pt32        1/1       Running   0          1h        172.16.73.2   k8s-node-1
tomcat-app-3401974344-mjskq   1/1       Running   0          27m       172.16.73.4   k8s-node-1
[root@k8s-master k8s]# curl -I 172.16.73.4  
curl: (7) Failed connect to 172.16.73.4:80; 拒绝连接
[root@k8s-master k8s]# curl -I 172.16.73.4:8080
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 23 Aug 2019 08:34:59 GMT

 

 

3.8  测试tomcat-APP项目-DNS错误

 

 

Error:com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

https://blog.51cto.com/14423403/2417097

 

我们先把副本设成一个解决问题

[root@k8s-master ~]# kubectl get pods -o wide    
NAME                          READY     STATUS    RESTARTS   AGE       IP            NODE
mysql-1283186320-1pt32        1/1       Running   0          1h        172.16.73.2   k8s-node-1
tomcat-app-3401974344-mjskq   1/1       Running   0          29m       172.16.73.4   k8s-node-1
[root@k8s-master ~]# kubectl exec -it tomcat-app-3401974344-mjskq /bin/bash
root@tomcat-app-3401974344-mjskq:/usr/local/tomcat# echo "172.16.73.2  mysql" >> /etc/hosts           
root@tomcat-app-3401974344-mjskq:/usr/local/tomcat#

 

然后重新刷新192.168.0.137:30002发现访问正常了。

 

 k8s好像网络还需要添加个DNS服务,暂时跳过

3.9 测试tomcat-APP项目

 

 

 

 

登陆数据库查看

 

[root@k8s-master ~]# mysql -uroot -p123456 -P30001 -h192.168.0.137              
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 12
Server version: 5.7.27 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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| HPE_APP            |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> use HPE_APP
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
mysql> show tables;
+-------------------+
| Tables_in_HPE_APP |
+-------------------+
| T_USERS           |
+-------------------+
1 row in set (0.01 sec)

mysql> select * from T_USERS;
+----+-----------+-------+
| ID | USER_NAME | LEVEL |
+----+-----------+-------+
|  1 | me        | 100   |
|  2 | our team  | 100   |
|  3 | HPE       | 100   |
|  4 | teacher   | 100   |
|  5 | docker    | 100   |
|  6 | google    | 100   |
|  7 | test      | 100   |
+----+-----------+-------+
7 rows in set (0.01 sec)

mysql>

 

 

3.10修改tomcat-app-deployment文件

把MySQL地址写死

[root@k8s-master ~]# kubectl get pods -o wide    
NAME                          READY     STATUS    RESTARTS   AGE       IP            NODE
mysql-1283186320-1pt32        1/1       Running   0          1h        172.16.73.2   k8s-node-1
tomcat-app-3752985145-xw2bg   1/1       Running   0          1m        172.16.47.2   k8s-node-2
[root@k8s-master ~]#
[root@k8s-master k8s]# cat tomcat-app-deployment.yaml 
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: tomcat-app
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: tomcat-app
    spec:
      containers:
      - name: tomcat-app
        image: 192.168.0.136:5000/tomcat-app:v1
        ports:
        - containerPort: 8080
        env:
        - name: MYSQL_SERVICE_HOST
          value: '172.16.73.2'
        - name: MYSQL_SERVICE_PORT
          value: '3306'

 

3.11扩容tomcat-app

kubectl scale deployment tomcat-app --replicas=5

[root@k8s-master k8s]# kubectl get pods -o wide 
NAME                          READY     STATUS    RESTARTS   AGE       IP            NODE
mysql-1283186320-1pt32        1/1       Running   0          1h        172.16.73.2   k8s-node-1
tomcat-app-3752985145-95w4t   1/1       Running   0          8m        172.16.73.3   k8s-node-1
tomcat-app-3752985145-gg3d6   1/1       Running   0          8m        172.16.73.4   k8s-node-1
tomcat-app-3752985145-l25dd   1/1       Running   0          8m        172.16.47.4   k8s-node-2
tomcat-app-3752985145-pjz1r   1/1       Running   0          8m        172.16.47.3   k8s-node-2
tomcat-app-3752985145-xw2bg   1/1       Running   0          9m        172.16.47.2   k8s-node-2
[root@k8s-master k8s]#

 

3.12再次测试tomcat-APP项目

 

 

 

4  存在的问题

1、pod的DNS不能解析,只能写死MySQL的IP地址,而pod的ip地址是很容易变化的。

2、MySQL没有持久化,pod的数据是临时的,如果删除pod或者重启deployment,数据会丢失。

 

posted on 2019-08-26 10:34  光阴8023  阅读(2042)  评论(0编辑  收藏  举报