标签:k8s source 文件 template client 状态 实现 端口映射 node
1、搭建nfs存储[root@docker-k8s01 ~]# yum -y install nfs-utils
[root@docker-k8s01 ~]# mkdir /nfsdata/mysql -p
[root@docker-k8s01 ~]# cat /etc/exports
/nfsdata *(rw,sync,no_root_squash)
[root@docker-k8s01 ~]# systemctl restart nfs-server
[root@docker-k8s01 ~]# systemctl enable rpcbind
[root@docker-k8s01 ~]# systemctl enable nfs-server
[root@docker-k8s01 ~]# showmount -e
Export list for docker-k8s01:
/nfsdata *
2、创建PV
//编写pv的yaml文件
kind: PersistenVolume
apiVersion: v1
metadata:
name: mysql-pv
spec:
accessModes:
- ReadWriteOnce
capacity:
storage: 1Gi
persistentVolumeReclaimPolicy: Retain
storageClassName: nfs
nfs:
path: /nfsdata/mysql
server: 192.168.171.151
//执行yaml文件
[root@docker-k8s01 mysql]# kubectl apply -f mysql-pv.yaml
3、创建PVC
//编写PVC的yaml文件
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce //访问模式定义为只能以读写的方式挂载到单个节点
resources:
requests:
storage: 1Gi
storageClassName: nfs //这里指定关联的PV名称
[root@docker-k8s01 mysql]# kubectl apply -f mysql-pvc.yaml
4、确认pv及pvc的状态
//可以看到pv及pvc的状态都是Bound
[root@docker-k8s01 mysql]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
mysql-pv 1Gi RWO Retain Bound default/mysql-pvc nfs 6m21s
[root@docker-k8s01 mysql]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mysql-pvc Bound mysql-pv 1Gi RWO nfs 2m4s
5、创建pod+svc(service)
这个pod是提供的MySQL服务,并将其映射到宿主机,可以做和client端通信
//编写deployment及service的yaml文件
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.7
env: //配置变量,设置mysql的密码
- name: MYSQL_ROOT_PASSWORD
value: 123.com
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql //MySQL容器的数据都是存在这个目录的,要对这个目录做数据持久化
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pvc //指定pvc名称
---
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
type: NodePort
ports:
- port: 3306
targetPort: 31111
selector:
app: mysql
[root@docker-k8s01 mysql]# kubectl apply -f mysql-pod.yaml
//确定pod运行正常
[root@docker-k8s01 mysql]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
mysql-6d898f8bcb-fc7bb 1/1 Running 0 60s
6、进入MySQL数据库,添加测试数据
mysql> create database test;
mysql> use test
mysql> create table my_id(id int(11));
mysql> insert my_id values(1234);
mysql> select * from test.my_id; //确定数据添加成功
+------+
| id |
+------+
| 1234 |
+------+
7、手动删除节点上的容器,验证数据库内的数据是否还存在
//查看Pod运行在哪个节点
[root@docker-k8s01 mysql]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-6d898f8bcb-fc7bb 1/1 Running 0 7m6s 10.244.1.2 docker-k8s02 <none> <none>
[root@docker-k8s02 ~]# docker ps // 查看mysql容器的id
//删除mysql容器
[root@docker-k8s02 ~]# docker rm -f 7ba3f9d9ad35
//由于Deployment的保护策略,当删除容器后,它会根据pod的yaml文件生成一个新的容器,但新容器的ID号就变了
//回到master节点,登录到数据库,查看数据是否还存在
[root@docker-k8s01 mysql]# kubectl exec -it mysql-6d898f8bcb-fc7bb -- mysql -uroot -p123.com
mysql> select * from test.my_id;
+------+
| id |
+------+
| 1234 |
+------+
1 row in set (0.01 sec)
证明:当使用PV来做数据持久化的话,容器的新陈代谢并不会造成丢失数据的。
8、模拟MySQL容器所在的节点宕机,验证数据是否会丢失
//查看容器所在节点
[root@docker-k8s01 mysql]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mysql-6d898f8bcb-fc7bb 1/1 Running 0 13m 10.244.1.2 docker-k8s02 <none> <none>
//将docker-k8s02节点宕机,动态查看Pod的状态
//大概node01关机五分钟左右,可以看到pod就被转移到node02上运行了
[root@docker-k8s01 ~]# kubectl get pod -w
NAME READY STATUS RESTARTS AGE
mysql-6d898f8bcb-fc7bb 1/1 Running 0 22m
mysql-6d898f8bcb-fc7bb 1/1 Running 0 22m
mysql-6d898f8bcb-fc7bb 1/1 Terminating 0 27m
mysql-6d898f8bcb-2h47v 0/1 Pending 0 0s
mysql-6d898f8bcb-2h47v 0/1 Pending 0 0s
mysql-6d898f8bcb-2h47v 0/1 ContainerCreating 0 0s
mysql-6d898f8bcb-2h47v 1/1 Running 0 33s
9、client端访问MySQL数据库
因为我们在创建pod的yaml文件中,将其svc的端口映射类型是nodeport,所以,我们是可以通过访问群集内的任意主机的31111端口(yaml文件中自定义的端口)来登录到数据库的。
[root@docker-k8s01 ~]# mysql -uroot -p123.com -h 192.168.171.151 -P 31111
MySQL [(none)]> select * from test.my_id; #查看数据
+------+
| id |
+------+
| 1234 |
+------+
1 row in set (0.01 sec)
标签:k8s source 文件 template client 状态 实现 端口映射 node
原文地址:https://blog.51cto.com/14227204/2531301