41.第34章 k8s 资源对象

一.k8s中CRI、CSI、CNI表示什么含义?

CRI:运行时,runc,container runtion interface

CSI:存储接口,container storage interface

CNI:网络接口,container network interface

二.nfs卷

nfs 卷能将 NFS (网络文件系统) 挂载到你的 Pod 中。 不像 emptyDir 那样会在删除 Pod 的同时也会被删除,nfs 卷的内容在删除 Pod 时会被保存,卷只是被卸载。 这意味着 nfs 卷可以被预先填充数据,并且这些数据可以在 Pod 之间共享。

注意: 在使用 NFS 卷之前,你必须运行自己的 NFS 服务器并将目标 share 导出备用。

root@k8s-ha1:~# apt -y install nfs-server

root@k8s-ha1:~# mkdir /data/k8sdata

root@k8s-ha1:~# vim /etc/exports
/data/k8sdata *(rw,no_root_squash)
:wq

root@k8s-ha1:~# systemctl enable --now nfs-server


#如果k8s中pod使用NFS协议访问外部存储,那么一定要在宿主机安装nfs-common组件包,否则pod访问存储会特别慢
root@k8s-node1:~# apt -y install nfs-common

root@k8s-node2:~#  apt -y install nfs-common

root@k8s-node3:~# apt -y install nfs-common

root@k8s-node4:~# apt -y install nfs-common

root@k8s-node1:~# showmount -e 10.0.0.104
Export list for 10.0.0.104:
/data/k8sdata *

root@k8s-master1:/opt/n50/case4# kubectl delete -f deploy_hostPath.yml 
deployment.apps "nginx-deployment" deleted

root@k8s-master1:/opt/n50/case4# cd ..
root@k8s-master1:/opt/n50# cd case5
root@k8s-master1:/opt/n50/case5# ls
deploy_nfs2.yml  deploy_nfs.yml



root@k8s-master1:/opt/n50/case5# vim deploy_nfs.yml
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ng-deploy-80
  template:
    metadata:
      labels:
        app: ng-deploy-80
    spec:
      containers:
      - name: ng-deploy-80
        image: nginx 
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /usr/share/nginx/html/mysite
          name: my-nfs-volume-n50
      volumes:
      - name: my-nfs-volume-n50
        nfs:
          server: 10.0.0.104
          path: /data/k8sdata

---
apiVersion: v1
kind: Service
metadata:
  name: ng-deploy-80
spec:
  ports:
  - name: http
    port: 81
    targetPort: 80
    nodePort: 30016
    protocol: TCP
  type: NodePort
  selector:
app: ng-deploy-80
:wq

root@k8s-master1:/opt/n50/case5# kubectl apply -f deploy_nfs.yml 
deployment.apps/nginx-deployment created
service/ng-deploy-80 configured

root@k8s-master1:/opt/n50/case5# kubectl get pod
NAME                               READY   STATUS    RESTARTS   AGE
net-test1                          1/1     Running   7          4d22h
net-test2                          1/1     Running   7          4d22h
nginx-deployment-79b8c7fdc-dm68d   1/1     Running   0          20s

root@k8s-master1:/opt/n50/case5# kubectl exec -it nginx-deployment-79b8c7fdc-dm68d bash 
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx-deployment-79b8c7fdc-dm68d:/# df -Th
Filesystem               Type     Size  Used Avail Use% Mounted on
overlay                  overlay   92G  3.5G   84G   5% /
tmpfs                    tmpfs     64M     0   64M   0% /dev
tmpfs                    tmpfs    2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda1                ext4      92G  3.5G   84G   5% /etc/hosts
shm                      tmpfs     64M     0   64M   0% /dev/shm
10.0.0.104:/data/k8sdata nfs4      46G   52M   44G   1% /usr/share/nginx/html/mysite
tmpfs                    tmpfs    2.0G   12K  2.0G   1% /run/secrets/kubernetes.io/serviceaccount
tmpfs                    tmpfs    2.0G     0  2.0G   0% /proc/acpi
tmpfs                    tmpfs    2.0G     0  2.0G   0% /proc/scsi
tmpfs                    tmpfs    2.0G     0  2.0G   0% /sys/firmware

root@k8s-ha1:~# ls /data/k8sdata/
01.jpg

#一个pod挂载2个NFS存储
root@k8s-ha1:~# mkdir /data/neteagle


root@k8s-ha1:~# vim /etc/exports
/data/neteagle *(rw,no_root_squash)  
:wq

root@k8s-ha1:~# systemctl restart nfs-server


root@k8s-node1:~# showmount -e 10.0.0.104
Export list for 10.0.0.104:
/data/neteagle *
/data/k8sdata  *


root@k8s-master1:/opt/n50/case5# vim deploy_nfs.yml
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ng-deploy-80
  template:
    metadata:
      labels:
        app: ng-deploy-80
    spec:
      containers:
      - name: ng-deploy-80
        image: nginx 
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /usr/share/nginx/html/mysite
          name: my-nfs-volume-n50
        - mountPath: /usr/share/nginx/html/neteagle
          name: my-nfs-volume-neteagle
      volumes:
      - name: my-nfs-volume-n50
        nfs:
          server: 10.0.0.104
          path: /data/k8sdata
      - name: my-nfs-volume-neteagle
        nfs:
          server: 10.0.0.104
          path: /data/neteagle

---
apiVersion: v1
kind: Service
metadata:
  name: ng-deploy-80
spec:
  ports:
  - name: http
    port: 81
    targetPort: 80
    nodePort: 30016
    protocol: TCP
  type: NodePort
  selector:
app: ng-deploy-80
:wq



root@k8s-master1:/opt/n50/case5# kubectl apply -f deploy_nfs.yml 
deployment.apps/nginx-deployment created
service/ng-deploy-80 unchanged

root@k8s-master1:/opt/n50/case5# kubectl get pod
NAME                                READY   STATUS    RESTARTS   AGE
net-test1                           1/1     Running   7          4d22h
net-test2                           1/1     Running   7          4d22h
nginx-deployment-6c6467645b-9pb2q   1/1     Running   0          42s
root@k8s-master1:/opt/n50/case5# kubectl exec -it nginx-deployment-6c6467645b-9pb2q bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx-deployment-6c6467645b-9pb2q:/# df -Th
Filesystem                Type     Size  Used Avail Use% Mounted on
overlay                   overlay   92G  3.5G   84G   5% /
tmpfs                     tmpfs     64M     0   64M   0% /dev
tmpfs                     tmpfs    2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda1                 ext4      92G  3.5G   84G   5% /etc/hosts
shm                       tmpfs     64M     0   64M   0% /dev/shm
10.0.0.104:/data/neteagle nfs4      46G   52M   44G   1% /usr/share/nginx/html/neteagle
tmpfs                     tmpfs    2.0G   12K  2.0G   1% /run/secrets/kubernetes.io/serviceaccount
10.0.0.104:/data/k8sdata  nfs4      46G   52M   44G   1% /usr/share/nginx/html/mysite
tmpfs                     tmpfs    2.0G     0  2.0G   0% /proc/acpi
tmpfs                     tmpfs    2.0G     0  2.0G   0% /proc/scsi
tmpfs                     tmpfs    2.0G     0  2.0G   0% /sys/firmware

root@k8s-ha1:~# ls /data/neteagle/
02.jpg

#一个pod里启动2个容器,分别挂载2个NFS存储
root@k8s-master1:/opt/n50/case5# vim deploy_nfs.yml
#apiVersion: extensions/v1beta1
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: ng-deploy-80
  template:
    metadata:
      labels:
        app: ng-deploy-80
    spec:
      containers:
      - name: ng-deploy-80
        image: nginx 
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /usr/share/nginx/html/mysite
          name: my-nfs-volume-n50
        - mountPath: /usr/share/nginx/html/neteagle
          name: my-nfs-volume-neteagle


      - name: tomcat-container
        image: tomcat
        ports:
        - containerPort: 8080
        volumeMounts:
        - mountPath: /usr/share/nginx/html/mysite
          name: my-nfs-volume-n50
        - mountPath: /usr/share/nginx/html/neteagle
          name: my-nfs-volume-neteagle

      volumes:
      - name: my-nfs-volume-n50
        nfs:
          server: 10.0.0.104
          path: /data/k8sdata
      - name: my-nfs-volume-neteagle
        nfs:
          server: 10.0.0.104
          path: /data/neteagle

---
apiVersion: v1
kind: Service
metadata:
  name: ng-deploy-80
spec:
  ports:
  - name: http
    port: 81
    targetPort: 80
    nodePort: 30016
    protocol: TCP
  type: NodePort
  selector:
app: ng-deploy-80
:wq

root@k8s-master1:/opt/n50/case5# kubectl delete -f deploy_nfs.yml 
deployment.apps "nginx-deployment" deleted
service "ng-deploy-80" deleted


root@k8s-master1:/opt/n50/case5# kubectl apply -f deploy_nfs.yml 
deployment.apps/nginx-deployment created
service/ng-deploy-80 created


root@k8s-master1:/opt/n50/case5# kubectl get pod
NAME                               READY   STATUS              RESTARTS   AGE
net-test1                          1/1     Running             7          4d23h
net-test2                          1/1     Running             7          4d23h
nginx-deployment-dd459d98f-mtmjp   0/2     ContainerCreating   0          75s

root@k8s-master1:/opt/n50/case5# kubectl describe pod nginx-deployment-dd459d98f-mtmjp
Events:
  Type    Reason     Age   From               Message
  ----    ------     ----  ----               -------
  Normal  Scheduled  101s  default-scheduler  Successfully assigned default/nginx-deployment-dd459d98f-mtmjp to k8s-node3.example.local
  Normal  Pulling    100s  kubelet            Pulling image "nginx"
  Normal  Pulled     84s   kubelet            Successfully pulled image "nginx" in 15.754643179s
  Normal  Created    84s   kubelet            Created container ng-deploy-80
  Normal  Started    84s   kubelet            Started container ng-deploy-80
  Normal  Pulling    84s   kubelet            Pulling image "tomcat"
  Normal  Pulled     4s    kubelet            Successfully pulled image "tomcat" in 1m19.875822582s
  Normal  Created    4s    kubelet            Created container tomcat-container
  Normal  Started    4s    kubelet            Started container tomcat-container

root@k8s-master1:/opt/n50/case5# kubectl get pod
NAME                               READY   STATUS    RESTARTS   AGE
net-test1                          1/1     Running   7          4d23h
net-test2                          1/1     Running   7          4d23h
nginx-deployment-dd459d98f-mtmjp   2/2     Running   0          106s


root@k8s-master1:/opt/n50/case5# kubectl exec -it nginx-deployment-dd459d98f-mtmjp bash -n default
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
Defaulting container name to ng-deploy-80.
Use 'kubectl describe pod/nginx-deployment-dd459d98f-mtmjp -n default' to see all of the containers in this pod.
root@nginx-deployment-dd459d98f-mtmjp:/# df -Th
Filesystem                Type     Size  Used Avail Use% Mounted on
overlay                   overlay   92G  4.2G   83G   5% /
tmpfs                     tmpfs     64M     0   64M   0% /dev
tmpfs                     tmpfs    2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda1                 ext4      92G  4.2G   83G   5% /etc/hosts
shm                       tmpfs     64M     0   64M   0% /dev/shm
10.0.0.104:/data/neteagle nfs4      46G   52M   44G   1% /usr/share/nginx/html/neteagle
tmpfs                     tmpfs    2.0G   12K  2.0G   1% /run/secrets/kubernetes.io/serviceaccount
10.0.0.104:/data/k8sdata  nfs4      46G   52M   44G   1% /usr/share/nginx/html/mysite
tmpfs                     tmpfs    2.0G     0  2.0G   0% /proc/acpi
tmpfs                     tmpfs    2.0G     0  2.0G   0% /proc/scsi
tmpfs                     tmpfs    2.0G     0  2.0G   0% /sys/firmware
root@nginx-deployment-dd459d98f-mtmjp:/# exit
exit


root@k8s-master1:/opt/n50/case5# kubectl exec -it nginx-deployment-dd459d98f-mtmjp -c tomcat-container bash -n default
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx-deployment-dd459d98f-mtmjp:/usr/local/tomcat# df -Th
Filesystem                Type     Size  Used Avail Use% Mounted on
overlay                   overlay   92G  4.2G   83G   5% /
tmpfs                     tmpfs     64M     0   64M   0% /dev
tmpfs                     tmpfs    2.0G     0  2.0G   0% /sys/fs/cgroup
/dev/sda1                 ext4      92G  4.2G   83G   5% /etc/hosts
shm                       tmpfs     64M     0   64M   0% /dev/shm
tmpfs                     tmpfs    2.0G   12K  2.0G   1% /run/secrets/kubernetes.io/serviceaccount
10.0.0.104:/data/k8sdata  nfs4      46G   52M   44G   1% /usr/share/nginx/html/mysite
10.0.0.104:/data/neteagle nfs4      46G   52M   44G   1% /usr/share/nginx/html/neteagle
tmpfs                     tmpfs    2.0G     0  2.0G   0% /proc/acpi
tmpfs                     tmpfs    2.0G     0  2.0G   0% /proc/scsi
tmpfs                     tmpfs    2.0G     0  2.0G   0% /sys/firmware
root@nginx-deployment-dd459d98f-mtmjp:/usr/local/tomcat# exit
exit
posted @ 2021-05-10 17:40  网络之鹰  阅读(588)  评论(0编辑  收藏  举报