访问测试页面:
[root@k8s-master nfs-sc]# curl 10.244.1.6 wo shi web-0 [root@k8s-master nfs-sc]# curl 10.244.2.8 wo shi web-1 [root@k8s-master nfs-sc]# curl 10.244.0.6 wo shi web-2
删除pod,重新生成pod以改变它们的IP,测试看看nginx的首页文件还能否访问到:
可以看到,pod的IP如何改变不影响它对外提供服务,因为volume存储是不变的,
[root@k8s-master nfs-sc]# k delete pod web-0 web-1 web-2 -n web pod "web-0" deleted pod "web-1" deleted pod "web-2" deleted [root@k8s-master nfs-sc]# k get po -n web -owide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES web-0 1/1 Running 0 6s 10.244.0.7 k8s-master <none> <none> web-1 1/1 Running 0 5s 10.244.2.9 k8s-node2 <none> <none> web-2 1/1 Running 0 3s 10.244.1.7 k8s-node1 <none> <none> [root@k8s-master nfs-sc]# curl 10.244.0.7 wo shi web-0 [root@k8s-master nfs-sc]# curl 10.244.2.9 wo shi web-1 [root@k8s-master nfs-sc]# curl 10.244.1.7 wo shi web-2
二,StateFulSet方式部署MySQL
建立相关namespace,名字叫database:
cat > mysql-sts-ns.yaml <<EOF apiVersion: v1 kind: Namespace metadata: name: database EOF
部署MySQL的清单文件:
cat > mysql-sts.yaml <<EOF apiVersion: apps/v1 kind: StatefulSet metadata: name: mysql namespace: database spec: selector: matchLabels: app: mysql #必须匹配 .spec.template.metadata.labels serviceName: "mysql" #声明它属于哪个Headless Service. replicas: 3 #副本数 template: metadata: labels: app: mysql # 必须配置 .spec.selector.matchLabels spec: terminationGracePeriodSeconds: 10 containers: - name: mysql image: mysql:5.7.23 ports: - containerPort: 3306 name: mysql env: - name: MYSQL_ROOT_PASSWORD value: "123456" volumeMounts: - name: mysql-pvc mountPath: /var/lib/mysql volumeClaimTemplates: #可看作pvc的模板 - metadata: name: mysql-pvc spec: accessModes: [ "ReadWriteOnce" ] storageClassName: "managed-nfs-storage" #存储类名,改为集群中已存在的 resources: requests: storage: 1Gi EOF
部署完成后,查看pod的大体信息:
[root@k8s-master ~]# k get pod,pv,pvc -n database -owide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES pod/mysql-0 1/1 Running 0 38m 10.244.2.10 k8s-node2 <none> <none> pod/mysql-1 1/1 Running 0 38m 10.244.0.8 k8s-master <none> <none> pod/mysql-2 1/1 Running 0 37m 10.244.0.9 k8s-master <none> <none> NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE VOLUMEMODE persistentvolume/pvc-46e7893f-3b66-4899-ae13-f1400eb1ff7f 1Gi RWO Delete Bound database/mysql-pvc-mysql-2 managed-nfs-storage 37m Filesystem persistentvolume/pvc-96748d04-0e1c-4b70-8c93-baf6a1d857d3 1Gi RWO Delete Bound database/mysql-pvc-mysql-1 managed-nfs-storage 38m Filesystem persistentvolume/pvc-a7b73ac0-d881-4c96-9fdb-e8d71f60d848 1Gi RWO Delete Bound web/nginx-pvc-web-1 managed-nfs-storage 4h40m Filesystem persistentvolume/pvc-b939521e-1e4b-4ba4-92b7-94f96cdba9a1 1Gi RWO Delete Bound database/mysql-pvc-mysql-0 managed-nfs-storage 38m Filesystem persistentvolume/pvc-dabd8050-77f6-4655-8434-dac2f9a077eb 1Gi RWO Delete Bound web/nginx-pvc-web-2 managed-nfs-storage 4h40m Filesystem persistentvolume/pvc-eb159490-353c-4aea-9412-10056a29911d 1Gi RWO Delete Bound web/nginx-pvc-web-0 managed-nfs-storage 4h40m Filesystem NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE VOLUMEMODE persistentvolumeclaim/mysql-pvc-mysql-0 Bound pvc-b939521e-1e4b-4ba4-92b7-94f96cdba9a1 1Gi RWO managed-nfs-storage 38m Filesystem persistentvolumeclaim/mysql-pvc-mysql-1 Bound pvc-96748d04-0e1c-4b70-8c93-baf6a1d857d3 1Gi RWO managed-nfs-storage 38m Filesystem persistentvolumeclaim/mysql-pvc-mysql-2 Bound pvc-46e7893f-3b66-4899-ae13-f1400eb1ff7f 1Gi RWO managed-nfs-storage 37m Filesystem
测试:
首先安装一哈MySQL的客户端:
yum install mariadb -y
登录进数据库后,依次按pod名称建立相应的数据库名称以作标识(本例就做了一个):
账号是root,密码是123456
[root@k8s-master ~]# mysql -uroot -p -h10.244.2.10 Enter password: Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.23 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> create database mysql-0; 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 '-0' at line 1 MySQL [(none)]> create database mysql0; Query OK, 1 row affected (0.01 sec)
删除MySQL的相关pod,等待重建pod完成后,再次进入数据库查看前面建立的数据库标识是否存在:
可以看到,删除原pod后,重新建立了pod,pod的IP改变了,但登录进去后,标识用的数据库还是存在的。
[root@k8s-master ~]# k delete pod mysql-0 mysql-1 mysql-2 -n database pod "mysql-0" deleted pod "mysql-1" deleted pod "mysql-2" deleted [root@k8s-master ~]# k get po -n database -owide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES mysql-0 1/1 Running 0 49s 10.244.2.11 k8s-node2 <none> <none> mysql-1 1/1 Running 0 40s 10.244.0.10 k8s-master <none> <none> mysql-2 1/1 Running 0 38s 10.244.0.11 k8s-master <none> <none> [root@k8s-master ~]# mysql -uroot -p123456 -h10.244.2.11 Welcome to the MariaDB monitor. Commands end with ; or \g. Your MySQL connection id is 2 Server version: 5.7.23 MySQL Community Server (GPL) Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MySQL [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | mysql0 | | performance_schema | | sys | +--------------------+ 5 rows in set (0.03 sec)
总结:
StateFulSet控制器的两个显著特点,一,按顺序创建,pod名称包含有自增数字,这使得它的域名是可以固定的。二,由StateFulSet控制器通过StorageClass控制器来控制volume持久存储卷和pod的名称一一对应,从而使得pod更加的稳定。
其实,说人话就是StateFulSet控制器使得pod能够形成一组pod,这一组pod能够进行一定的序列化。因此,StateFulSet控制器常常使用在比较复杂的需要一定序列化特点的集群搭建方面,比如,MySQL主从复制集群,redis集群的部署基础都是StateFulSet控制器。