0.准备三个持久化磁盘

[root@m-xxx ~]# ifconfig | head -2| tail -1|awk 'print{$2}'
192.168.1.11
[root@m-xxx ~]# echo '/net/pet1 *(rw,no_root_squash)' >> /etc/export
[root@m-xxx ~]# echo '/net/pet2 *(rw,no_root_squash)' >> /etc/export
[root@m-xxx ~]# echo '/net/pet3 *(rw,no_root_squash)' >> /etc/export
[root@m-xxx ~]# systemcel restart nfs-server
[root@m-xxx ~]# shoumount -e 192.168.1.11 # 验证

1.创建三个持久卷(pv-a、pv-b、pv-c)

---
  apiVersion: v1
  kind: PersistentVolume
  metadata:
    name: pv-a|pv-b|pv-c
  spec:
    capacity:
      storage: 1Gi
    accessModes: 
    - ReadWriteOnce
    - ReadOnlyMany
   #persistentVolumeReclaimPolicy: Retain # 当声明被释放,pv将保留(不清理和删除)
    persistentVolumeReclaimPolicy: Recycle # 当声明被释放,空间将回收再利用
    nfs:
      server: 192.168.1.11
      path: /net/pet1 | /net/pet2| /net/pet3

2.创建headless Service,让pod彼此发现
原理: kubernetes允许客户通过DNS查找发现pod IP,默认情况下,执行服务的DNS查找时,DNS服务会返回集群ip(clusterIP),当clusterIP属性为none时,将返回pod IP而不是单个服务IP。所以客户会得到该服务旗下所有pod ip。
在这里插入图片描述

---
  apiVersion: v1
  kind: Service
  metadata:
    name: kubia-headless
  spec:
    clusterIP: None
    ports:
    - port: 80
      targetPort: 8080
    selector:
      app: kubia

3.创建StatefulSet应用

apiVersion: apps/v1beta1
kind: StatefulSet
metadata:
  name: kubia
spec:
  serviceName: kubia
  replicas: 2
  template:
    metadata:
      labels:
        app: kubia
    spec:
      containers:
      - name: kubia
        image: kubia-pet
        ports:
        - name: http
          containerPort: 8080
        volumeMounts:
        - name: data
          mountPath: /var/data
  volumeClaimTemplates:
  - metadata:
      name: data
    spec:
      resources:
        requests:
          storage: 1Mi
      accessModes:
      - ReadWriteOnce

4.新建centos Pod容器,并安装bind-utils作测试

4.1得到两个Pod ip

[root@centos /]# nslookup kubia-headless  
...
Name:   kubia-headless.default.svc.cluster.local
Address: 10.244.1.61
Name:   kubia-headless.default.svc.cluster.local
Address: 10.244.2.80

4.2开始两个Pod均无记录

[root@centos /]# curl 10.244.1.61:8080
You've hit kubia-0
Data stored on this pod: No data posted yet
[root@centos /]# curl 10.244.2.80:8080
You've hit kubia-1
Data stored on this pod: No data posted yet

4.3往kubia-0插入数据

[root@centos /]# curl -X POST -d "Hey there! This greeting was submitted to kubia-0." 10.244.1.61:8080

4.4再次验证,kubia-0与kubia-1独享各自状态

[root@centos /]# curl 10.244.1.61:8080
You've hit kubia-0
Data stored on this pod: Hey there! This greeting was submitted to kubia-0.
[root@centos /]# curl 10.244.2.80:8080
You've hit kubia-1
Data stored on this pod: No data posted yet

4.5删除kubia-0,再次查看重新调度kubia-0

[root@m-xxx~ ]# kubectl delete po kubia-0
[root@centos /]# curl 10.244.1.61:8080
You've hit kubia-0
Data stored on this pod: Hey there! This greeting was submitted to kubia-0.

状态正常

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐