kubernetes云平台管理实战:tomcat + mysql(十二)

一、实验准备

1、文件结构与组成

1
2
[root@master tomcat_demo]# ls
mysql-rc.yml  mysql-svc.yml  tomcat-rc.yml  tomcat-svc.yml

2、具体内容

mysql-rc.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@master tomcat_demo]# cat mysql-rc.yml
apiVersion: v1
kind: ReplicationController
metadata:
  name: mysql
spec:
  replicas: 1
  selector:
    app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: 192.168.118.18:5000/mysql:5.7
          ports:
          - containerPort: 3306
          env:
          - name: MYSQL_ROOT_PASSWORD
            value: '123456'

mysql-svc.yml

1
2
3
4
5
6
7
8
9
10
11
[root@master tomcat_demo]# cat mysql-svc.yml
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
    - port: 3306
      targetPort: 3306
  selector:
    app: mysql

tomcat-rc.yml

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
[root@master tomcat_demo]#
[root@master tomcat_demo]# cat tomcat-rc.yml
apiVersion: v1
kind: ReplicationController
metadata:
  name: myweb
spec:
  replicas: 1
  selector:
    app: myweb
  template:
    metadata:
      labels:
        app: myweb
    spec:
      containers:
        - name: myweb
          image: 192.168.118.18:5000/tomcat-app:v2
          ports:
          - containerPort: 8080
          env:
          - name: MYSQL_SERVICE_HOST
            value: '10.254.39.137'
          - name: MYSQL_SERVICE_PORT
            value: '3306'

tomcat-svc.yml

1
2
3
4
5
6
7
8
9
10
11
12
[root@master tomcat_demo]# cat tomcat-svc.yml
apiVersion: v1
kind: Service
metadata:
  name: myweb
spec:
  type: NodePort
  ports:
    - port: 8080
      nodePort: 30008
  selector:
    app: myweb

镜像仓库

1
2
3
4
5
6
7
8
9
[root@master ~]# docker images
REPOSITORY                                                         TAG                 IMAGE ID            CREATED             SIZE
docker.io/registry                                                 latest              708bc6af7e5e        3 months ago        25.8
192.168.118.18:5000/pod-infrastructure                             latest              34d3450d733b        3 years ago         205 MB
docker.io/tianyebj/pod-infrastructure                              latest              34d3450d733b        3 years ago         205 MB
192.168.118.18:5000/mysql                                          5.7                 b7dc06006192        3 years ago         386 MB
docker.io/mysql                                                    5.7                 b7dc06006192        3 years ago         386 MB
192.168.118.18:5000/tomcat-app                                     v2                  00beaa1d956d        3 years ago         358 MB
docker.io/kubeguide/tomcat-app                                     v2                  00beaa1d956d        3 years ago         358 MB

二、操作演示过程

创建数据库容器

1
2
3
4
[root@k8s-master tomcat_demo]# kubectl create -f mysql-rc.yml
replicationcontroller "mysql" created
[root@k8s-master tomcat_demo]# kubectl create -f mysql-svc.yml
service "mysql" created

  web服务如何访问db服务?

1
2
3
4
5
6
7
8
[root@k8s-master tomcat_demo]# kubectl get svc
NAME         CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
kubernetes   10.254.0.1      <none>        443/TCP    6d
mysql        10.254.155.23   <none>        3306/TCP   4s
 
[root@k8s-master tomcat_demo]# vim tomcat-rc.yml
- name: MYSQL_SERVICE_HOST
            value: '10.254.39.137'

创建tomcat容器

1
2
3
4
5
[root@k8s-master tomcat_demo]# kubectl create -f tomcat-rc.yml
replicationcontroller "myweb" created
[root@k8s-master tomcat_demo]#
[root@k8s-master tomcat_demo]# kubectl create -f tomcat-svc.yml
service "myweb" created

三、测试访问

1
2
3
4
5
6
root@master ~]# kubectl get all
......
 
NAME                        READY     STATUS              RESTARTS   AGE
po/mysql-3qkf1              1/1       Running             0          15h
po/myweb-z2g3m              1/1       Running             0          15h

1、未添加数据前访问

2、添加数据库后访问

测试截图

 确认数据库有没有刚才添加的数据

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
[root@master ~]# kubectl exec -it mysql-3qkf1 bash
root@mysql-3qkf1:/# 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 7
Server version: 5.7.15 MySQL Community Server (GPL)
 
Copyright (c) 2000, 2016, 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.00 sec)
 
mysql> slectl * from T_USERS;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'slectl * from T_USERS' at line 1
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 | luoahong  | 1000000 |
+----+-----------+---------+
7 rows in set (0.00 sec)
 
mysql> exit
Bye

3、多个容器共享一个数据库

添加pod

1
2
3
4
5
6
7
8
9
10
[root@master ~]# kubectl scale rc myweb --replicas=3
replicationcontroller "myweb" scaled
[root@master ~]# kubectl get all
......
 
NAME             READY     STATUS    RESTARTS   AGE
po/mysql-3qkf1   1/1       Running   0          16h
po/myweb-286hg   1/1       Running   0          19s
po/myweb-vfr4v   1/1       Running   0          19s
po/myweb-z2g3m   1/1       Running   0          15h

访问截图

 

posted @   活的潇洒80  阅读(646)  评论(1编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
历史上的今天:
2019-05-20 深入浅出计算机组成原理学习笔记: 第九讲
2019-05-20 深入浅出计算机组成原理学习笔记:第十一讲
点击右上角即可分享
微信分享提示