如何绑定动态PV,以及K8S的简要流程

图片.png

创建PVC
nginx-storage-pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: nginx-spvc
  annotations:
    volume.beta.kubernetes.io/storage-class: "managed-nfs-storage"
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 100Mi

查看创建的PVC

[root@harbor opt]# kubectl get pvc
NAME                          STATUS    VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS          AGE
nginx-spvc                    Bound     pvc-ee6d9418-d803-482b-9722-6807c04ba7d0   100Mi      RWX            managed-nfs-storage   59m

创建Pod
nginx-storage-pod.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx-test-storage-pod
  labels:
    app: nginx-storage
spec:
  containers:
  - name: nginx-t
    image: nginx:alpine
    imagePullPolicy: IfNotPresent
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
    ports:
    - name: http
      containerPort: 80
  volumes:
  - name: html
    persistentVolumeClaim:
      claimName: nginx-spvc

查看创建的Pod

[root@harbor opt]# kubectl get pods -o wide
NAME                                      READY   STATUS    RESTARTS   AGE   IP           NODE     NOMINATED NODE   READINESS GATES
nginx-test-storage-pod                    2/2     Running   0          49m   10.42.1.11   harbor              

查看存储类中自动生成的目录

[root@harbor opt]# ll /opt/nfsdata/storage-data/
总用量 0
drwxrwxrwx 2 root root 24 12月 29 16:14 default-nginx-spvc-pvc-ee6d9418-d803-482b-9722-6807c04ba7d0
drwxrwxrwx 2 root root 28 11月 13 13:41 default-redis-data-redis-1-master-0-pvc-1bfa9da0-f6d9-4ef5-a7e2-89cee97f935f
drwxrwxrwx 2 root root  6 11月 13 13:39 default-test-claim-pvc-5d8e1747-c255-401c-9720-bbfb4dd9ba5f

进入相应目录,写入一个html文件
cd default-nginx-spvc-pvc-ee6d9418-d803-482b-9722-6807c04ba7d0
echo "

hell storageClass

" > index.html
创建service
nginx-storage-svc.yaml

apiVersion: v1
kind: Service
metadata:
  name: nginx-storage-svc
spec:
  selector:
    app: nginx-storage
  type: ClusterIP
  ports:
  - port: 80
    targetPort: 80

查看svc

[root@harbor opt]# kubectl get svc
NAME                TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)    AGE
nginx-storage-svc   ClusterIP   10.43.75.89            80/TCP     42m

查看SVC是否关联Pod

[root@harbor opt]# kubectl describe svc nginx-storage-svc
Name:              nginx-storage-svc
Namespace:         default
Labels:            
Annotations:       kubectl.kubernetes.io/last-applied-configuration:
                     {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"name":"nginx-storage-svc","namespace":"default"},"spec":{"ports":[{"port...
Selector:          app=nginx-storage
Type:              ClusterIP
IP:                10.43.75.89
Port:                80/TCP
TargetPort:        80/TCP
Endpoints:         10.42.1.11:80
Session Affinity:  None
Events:            

创建Ingress
nginx-storage-ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: nginx-storage-ingress
spec:
  rules:
  - host: nginx.storage.com
    http:
      paths:
      - backend:
          serviceName: nginx-storage-svc
          servicePort: 80

查看ingress

[root@harbor opt]# kubectl get ing
NAME                    CLASS    HOSTS               ADDRESS                     PORTS   AGE
nginx-storage-ingress      nginx.storage.com   172.16.20.58,172.16.20.64   80      33m

由于域名是自定义测试域名,windows中hosts需要添加解析
172.16.20.58 nginx.storage.com
最后浏览器访问


图片.png

你可能感兴趣的:(如何绑定动态PV,以及K8S的简要流程)