k8s实现tomcat+mysql容器之间相互访问(九)
tomcat+mysql容器之间相互访问
在企业很多环境中,通常是容器之间相互配合访问的
在k8s中可以通过vip来实现容器之间的相互访问
先准备数据库的yaml文件,在准备应用的yaml文件,应用连接数据库时使用clusterip的方式去连接
环境用到的配置文件
[root@k8s-master tomcat]# tree .
.
├── docker-mysql-5.7.tar.gz
├── mysql-rc.yml
├── mysql-svc.yml
├── tomcat-app-v2.tar.gz
├── tomcat-rc.yml
└── tomcat-svc.yml
0 directories, 6 files
1.准备tomcat mysql容器
1)导入容器
[root@k8s-master tomcat]# docker load -i docker-mysql-5.7.tar.gz
[root@k8s-master tomcat]# docker load -i tomcat-app-v2.tar.gz
2)打标签
[root@k8s-master tomcat]# docker tag docker.io/mysql:5.7 192.168.81.240/k8s/mysql:5.7
[root@k8s-master tomcat]# docker tag docker.io/kubeguide/tomcat-app:v2 192.168.81.240/k8s/tomcat-app:v2
3)推送至harbor
[root@k8s-master tomcat]# docker push 192.168.81.240/k8s/mysql:5.7
[root@k8s-master tomcat]# docker push 192.168.81.240/k8s/tomcat-app:v2
2.准备mysql yaml文件
2.1.mysql rc文件
[root@k8s-master tomcat]# vim mysql-rc.yml
apiVersion: v1 #api版本
kind: ReplicationController #资源类型为rc
metadata:
name: mysql #资源名称是mysql
spec:
replicas: 1 #pod数量为1
selector:
app: mysql #标签为mysql
template:
metadata:
labels:
app: mysql #关联rc的标签
spec:
containers:
- name: mysql #容器的名称
image: 192.168.81.240/k8s/mysql:5.7 #镜像地址
ports:
- containerPort: 3306 #端口
env:
- name: MYSQL_ROOT_PASSWORD
value: '123456' #初始密码
2.2.mysql svc文件
[root@k8s-master tomcat]# vim mysql-svc.yml
apiVersion: v1 #api版本
kind: Service #资源类型
metadata:
name: mysql #资源名称
spec:
ports:
- port: 3306 #虚拟ip端口
targetPort: 3306 #pod端口
selector:
app: mysql #关联pod标签
2.3.创建mysql
1)启动rc资源并调用pod
[root@k8s-master tomcat]# kubectl create -f mysql-rc.yml
replicationcontroller "mysql" created
[root@k8s-master tomcat]# kubectl get all -o wide
NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR
rc/mysql 1 1 1 3s mysql 192.168.81.240/k8s/mysql:5.7 app=mysql
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
svc/kubernetes 10.254.0.1 <none> 443/TCP 14d <none>
NAME READY STATUS RESTARTS AGE IP NODE
po/mysql-hvnsc 1/1 Running 0 3s 172.16.48.2 192.168.81.230
会发现pod已经启动
2)启动svc,观察svc是否关联rc的标签
[root@k8s-master tomcat]# kubectl create -f mysql-svc.yml
service "mysql" created
[root@k8s-master tomcat]# kubectl get all -o wide
NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR
rc/mysql 1 1 1 23s mysql 192.168.81.240/k8s/mysql:5.7 app=mysql
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
svc/kubernetes 10.254.0.1 <none> 443/TCP 14d <none>
svc/mysql 10.254.17.96 <none> 3306/TCP 2s app=mysql
NAME READY STATUS RESTARTS AGE IP NODE
po/mysql-hvnsc 1/1 Running 0 23s 172.16.48.2 192.168.81.230
3.准备tomcat yaml文件
3.1.tomcat rc文件
[root@k8s-master tomcat]# vim tomcat-rc.yml
apiVersion: v1 #api版本
kind: ReplicationController #资源类型
metadata:
name: mytomcat #资源名称
spec:
replicas: 1 #pod数量
selector:
app: mytomcat #rc资源的标签
template:
metadata:
labels:
app: mytomcat #pod资源的标签,关联9行的rc标签
spec:
containers:
- name: mytomcat #容器名
image: 192.168.81.240/k8s/tomcat-app:v2 #镜像地址
ports:
- containerPort: 8080 #端口
env:
- name: MYSQL_SERVICE_HOST
value: '10.254.17.96' #mysql的vip地址,写之前先查看mysql svc的vip地址
- name: MYSQL_SERVICE_PORT
value: '3306' #mysql端口
查svc的cluster ip地址,写到yaml配置文件的第17行
[root@k8s-master tomcat]# kubectl get svc mysql -o wide
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
mysql 10.254.17.96 <none> 3306/TCP 8m app=mysql
3.2.tomcat svc文件
[root@k8s-master tomcat]# vim tomcat-svc.yml
apiVersion: v1 #api版本
kind: Service #资源类型
metadata:
name: mytomcat #资源名称
spec:
type: NodePort #端口映射
ports:
- port: 8080 #虚拟ip端口
nodePort: 30008 #节点映射端口
selector:
app: mytomcat #pod标签
3.3.创建tomcat
1)启动rc
[root@k8s-master tomcat]# kubectl create -f tomcat-rc.yml
replicationcontroller "mytomcat" created
[root@k8s-master tomcat]# kubectl get rc mytomcat -o wide
NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR
mytomcat 1 1 0 7s mytomcat 192.168.81.240/k8s/tomcat-app:v2 app=mytomcat
2)启动svc
[root@k8s-master tomcat]# kubectl create -f tomcat-svc.yml
service "mytomcat" created
[root@k8s-master tomcat]# kubectl get svc mytomcat -o wide
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
mytomcat 10.254.234.244 <nodes> 8080:30008/TCP 10s app=mytomcat
[root@k8s-master tomcat]#
4.访问资源
查看资源状态
[root@k8s-master tomcat]# kubectl get all -o wide
NAME DESIRED CURRENT READY AGE CONTAINER(S) IMAGE(S) SELECTOR
rc/mysql 1 1 1 16m mysql 192.168.81.240/k8s/mysql:5.7 app=mysql
rc/mytomcat 1 1 1 2m mytomcat 192.168.81.240/k8s/tomcat-app:v2 app=mytomcat
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
svc/kubernetes 10.254.0.1 <none> 443/TCP 14d <none>
svc/mynginx 10.254.36.92 <nodes> 80:9907/TCP 3d app=nginx
svc/mysql 10.254.17.96 <none> 3306/TCP 16m app=mysql
svc/mytomcat 10.254.234.244 <nodes> 8080:30008/TCP 1m app=mytomcat
svc/myweb2 10.254.250.66 <nodes> 80:3000/TCP 4d app=myweb
NAME READY STATUS RESTARTS AGE IP NODE
po/mysql-hvnsc 1/1 Running 0 16m 172.16.48.2 192.168.81.230
po/mytomcat-7rv0n 1/1 Running 0 2m 172.16.19.3 192.168.81.220
4.1.tomcat首页
浏览器访问:http://192.168.81.220:30008/
会看到tomcat首页
4.2.demo站点
浏览器访问:http://192.168.81.220:30008/demo/
点击add新增一个
4.3.进入mysql容器查看数据数据
[root@k8s-master tomcat]# kubectl exec -it mysql-hvnsc /bin/bash
root@mysql-hvnsc:/# mysql -uroot -p123456 mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| HPE_APP |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> use HPE_APP
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 | jiangxiaolong | 10000 |
+----+---------------+-------+
7 rows in set (0.01 sec)
5.持久化demo站点
如果pod节点死掉,rc会再启动一个,但是数据会丢失,因此我们采用pv+pvc的方式实现持久化存储
数据库一定要做数据卷存储
5.1.安装nfs
1)安装nfs
[root@k8s-master ~]# yum -y install nfs-utils
2)配置nfs
[root@k8s-master ~]# cat /etc/exports
/data2 192.168.81.0/24(rw,sync,no_root_squash,no_all_squash)
3)启动nfs
[root@k8s-master ~]# systemctl restart nfs
[root@k8s-master ~]# systemctl restart rpcbind
5.2.创建pv
1)准备pv yaml文件
[root@k8s-master tomcat]# vim mysql_pv.yaml
apiVersion: v1 #api版本
kind: PersistentVolume #资源类型
metadata:
name: tom-mysql-pv #资源名
labels:
type: tom-mysql-pv #类型
spec:
capacity:
storage: 10Gi #大小
accessModes:
- ReadWriteMany #多个节点可读可写
persistentVolumeReclaimPolicy: Recycle #支持回收
nfs:
path: "/data2/tom_mysql_pv" #nfs路径
server: 192.168.81.210 #nfs服务器地址
readOnly: false #是否可读
[root@k8s-master tomcat]# kubectl create -f mysql_pv.yaml
persistentvolume "tom-mysql-pv" created
5.3创建pvc
[root@k8s-master tomcat]# vim mysql_pvc.yaml
apiVersion: v1 #apiserver
kind: PersistentVolumeClaim #资源类型
metadata:
name: tom-mysql-pvc #资源名称
spec:
accessModes:
- ReadWriteMany #多个节点可读写
resources:
requests:
storage: 5Gi #空间
selector:
matchLabels:
pv: tom-mysql-pv #标签
[root@k8s-master tomcat]# kubectl create -f mysql_pvc.yaml
persistentvolumeclaim "tom-mysql-pvc" created
5.4.查看pv和pvc
[root@k8s-master tomcat]# kubectl get pv tom-mysql-pv
NAME CAPACITY ACCESSMODES RECLAIMPOLICY STATUS CLAIM REASON AGE
tom-mysql-pv 10Gi RWX Recycle Bound default/tom-mysql-pvc 1m
[root@k8s-master tomcat]# kubectl get pvc tom-mysql-pvc
NAME STATUS VOLUME CAPACITY ACCESSMODES AGE
tom-mysql-pvc Bound tom-mysql-pv 10Gi RWX 1m
pv和pvc相互关联
5.5.应用挂载
1)修改rc配置文件
[root@k8s-master tomcat]# vim 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.81.240/k8s/mysql:5.7
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: '123456'
volumeMounts: #定义卷
- name: tom-mysql-data #卷名称
mountPath: /var/lib/mysql #挂载点
volumes:
- name: tom-mysql-data #卷名称
persistentVolumeClaim:
claimName: tom-mysql-pvc #pvc名称
2)更新
[root@k8s-master tomcat]# kubectl apply -f mysql-rc.yml
replicationcontroller "mysql" configured
3)创建nfs目录
[root@k8s-master tomcat]# mkdir /data2/tom_mysql_pv
不挂载之前pod死了再启动数据就丢失了
挂载之后及时删了pod再开启也不会丢失数据
nfs目录
目录 返回
首页