簡單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,數據會丟失。