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

一、实验准备

1、文件结构与组成

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

2、具体内容

mysql-rc.yml

[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

[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

[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

[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

镜像仓库

[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

二、操作演示过程

创建数据库容器

[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服务?

[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容器

[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

三、测试访问

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、添加数据库后访问

测试截图

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

[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

[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 @ 2020-05-20 21:01  活的潇洒80  阅读(642)  评论(1编辑  收藏  举报