Kubernetes部署java web demo(tomcat+mysql)

参考《Kubernetes 权威指南(第二版)》第一章中的例子,部署一个 Java 应用,访问 MySql 数据库。

原书中的代码有一些坑,记录一下。

过程中遇到的bug主要是mysql连接错误。

  • 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.

  • Error:com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server

参考https://stackoverflow.com/questions/49204339/mysql-communications-link-failure-in-kubernetes-sample

以下是我的代码。

# mysql-rc.yaml
apiVersion: v1
kind: ReplicationController   # 副本控制器RC
metadata: 
  name: mysql                 # RC的名称,全局唯一
spec: 
  replicas: 1                 # 副本的期待数量
  selector:
    app: mysql                # 符合目标的Pod拥有此标签
  template:                   # 根据此模板创建Pod的副本(实例)
    metadata: 
      labels: 
        app: mysql            # Pod副本拥有的标签,对应RC的Selector
    spec: 
      containers:             # Pod内容器的定义部分
      - name: mysql           # 容器的名称
        image: hub.c.163.com/library/mysql:5.5          # 容器对应的Docker Image
        ports:
        - containerPort: 3306 # 容器应用监听的端口号
        env: 
        - name: MYSQL_ROOT_PASSWORD
          value: '123456'
# msyql-svc.yaml
apiVersion: v1
kind: Service         # 表明是Kubernetes Service
metadata:
  name: mysql         # Service的全局唯一名称
spec:
  type: NodePort
  ports:
  - port: 3306        # Service提供服务的端口号
  selector:           # Service对应的Pod拥有这里定义的标签
    app: mysql
# myweb-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: myweb
spec:
  replicas: 1
  selector:
    app: myweb
  template:
    metadata:
      labels:
        app: myweb
    spec:
      containers:
      - name: myweb
        image: kubeguide/tomcat-app:v1
        ports:
        - containerPort: 8080
# myweb-svc.yaml
apiVersion: v1
kind: Service
metadata: 
  name: myweb
spec:
  type: LoadBalancer  
  ports:
    - port: 80
      targetPort: 8080
  selector:
    app: myweb

上面用的是LoadBalancer的方式暴露服务,如果没配过LB的朋友可以用NodePort的方式,myweb-svc.yaml如下

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

创建pod和svc

kubectl create -f myweb-svc.yaml
kubectl create -f mysql-svc.yaml
kubectl create -f mysql-rc.yaml
kubectl create -f myweb-rc.yaml 

查看pod和svc。由于mysql镜像下载需要一点时间,所以pod的状态还可能是ContainerCreating,过一会儿就能更新为running了。

root@R740-2-1:~/myspace/workspace/java_web_test# kubectl get pods
NAME                         READY   STATUS    RESTARTS   AGE
mysql-gsq4l                  1/1     Running   0          3h
myweb-ftnlb                  1/1     Running   0          3h
root@R740-2-1:~/myspace/workspace/java_web_test# kubectl get svc
NAME         TYPE           CLUSTER-IP      EXTERNAL-IP       PORT(S)          AGE
mysql        NodePort       10.68.64.227    <none>            3306:29929/TCP   3h
myweb        LoadBalancer   10.68.205.217   192.168.199.245   80:31457/TCP     24m

可以看到我这里用LB的方式创建的myweb的外部IP是192.168.199.245,直接访问http://192.168.199.245/demo

如果myweb-svc配置的是NodePort的方式,可以用master的IP:30001的方式访问tomcat,比如我这里是http://192.168.199.183:30001/demo/。

Kubernetes部署java web demo(tomcat+mysql)_第1张图片

可以通过add按钮添加一条记录,点击submit之后就数据就被添加到MySQL数据库中了。
Kubernetes部署java web demo(tomcat+mysql)_第2张图片

在运行了mysql服务的node节点上登录数据库,查看数据。

root@R740-1-3:~# docker ps | grep mysql
6d5b9c339326        hub.c.163.com/library/mysql                                            "docker-entrypoint.s…"   2 hours ago         Up 2 hours                              k8s_mysql_mysql-gsq4l_default_3f2e0bae-a4b4-4df9-be43-3c853c6840c1_0
8717c683fc18        mirrorgooglecontainers/pause-amd64:3.1                                 "/pause"                 2 hours ago         Up 2 hours                              k8s_POD_mysql-gsq4l_default_3f2e0bae-a4b4-4df9-be43-3c853c6840c1_0
root@R740-1-3:~# docker exec -it 6d5b9c339326 bash
root@mysql-gsq4l:/# mysql -u root -p
Enter password: 
mysql> use HPE_APP
Database changed
mysql> show tables;
+-------------------+
| Tables_in_HPE_APP |
+-------------------+
| T_USERS           |
+-------------------+
1 row in set (0.00 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 | Wenxin Xie | 100   |
+----+------------+-------+
7 rows in set (0.00 sec)

你可能感兴趣的:(k8s)