[Cloud native | Learn Kubernetes from scratch] Twenty-one, kubernetes persistent storage

This article has been included in the column " Learn k8s from scratch "
Previous article: Detailed explanation of Service proxy kube-proxy components Click jump

insert image description here

Supplement to the previous article

Service service discovery: detailed explanation of coredns components

What is DNS?

The full name of DNS is Domain Name System, which is the phone book of the entire Internet. It can translate domain names that can be understood by people into IP addresses that can be understood by machines, so that Internet users no longer need to directly contact hard-to-read and Understand the IP address. The domain name system is very important in the current Internet, because the IP address of the server may change frequently. If there is no DNS, then once the IP address changes, the client of the current server cannot connect to the target server. If We provide an "alias" for the IP address and modify the relationship between the alias and the IP address when it changes, then we can ensure that the services provided by the cluster can be accessed by other clients relatively stably. DNS is actually a distributed tree-like naming system. It is like a decentralized distributed database that stores the mapping from domain names to IP addresses.

CoreDNS?

CoreDNS is actually a DNS service, and DNS is a common service discovery method, so many open source projects and engineers use CoreDNS to provide service discovery for clusters. Kubernetes uses CoreDNS to solve service discovery problems in clusters. As a CNCF (Cloud Native Computing Foundation) service, the implementation of CoreDNS is very simple.

验证 coredns 
#把 dig.tar.gz 上传到 xianchaonode2 和 xianchaonode1 机器上,手动解压: 
[root@k8snode2 ~]# docker load -i dig.tar.gz 
[root@k8snode1 ~]# docker load -i dig.tar.gz 
[root@xianchaomaster1 ~]# vim dig.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: dig
  namespace: default
spec:
  containers:
  - name: dig
    image: xianchao/dig:latest
    command:
      - sleep
      - "3600"
    imagePullPolicy: IfNotPresent
  restartPolicy: Always
#更新资源清单文件 
[root@k8smaster node]# kubectl apply -f dig.yaml 
pod/dig created

#查看默认名称空间的 kubernetes 服务 
[root@k8smaster node]# kubectl get svc | grep kubernetes 
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        16d
#解析 dns,如有以下返回说明 dns 安装成功 
[root@k8smaster node]# kubectl exec -it dig -- nslookup kubernetes 
Server:		10.96.0.10
Address:	10.96.0.10#53

Name:	kubernetes.default.svc.cluster.local
Address: 10.96.0.1
 
kubernetes.default.svc.cluster.local 
服务名.名称空间.默认后缀 不屑也会搜索到,因为默认/etc/resolv里面dns服务中会搜索default svc cluster.local这些后缀

在 k8s 中创建 service 之后,service 默认的 FQDN 是<service name>.<namespace>.svc.cluster.local,那么 k8s 集群内部的服务就可以通过 FQDN 访问

Kubernetes persistent storage

Quick understanding

We mentioned the data volume before: emptydir, it is local storage, if the pod restarts, the data does not exist, and the data needs to be stored persistently

For data persistent storage [pod restart, data still exists], there are two ways

  • nfs: network storage [stored through a server]

Detailed explanation of k8s persistent storage

Why do you need persistent storage in k8s?

Applications deployed in k8s run in the form of pod containers. If we deploy MySQL, Redis and other databases, we need to back up the data generated by these databases. Because a pod has a life cycle, if the pod does not mount the data volume, the data will disappear after the pod is deleted or restarted. If you want to retain the data for a long time, you need to use the pod data persistent storage.

k8s persistent storage: emptyDir

#查看 k8s 支持哪些存储 
[root@k8smaster node]# kubectl explain pods.spec.volumes 
KIND:     Pod
VERSION:  v1

RESOURCE: volumes <[]Object>

DESCRIPTION:
     List of volumes that can be mounted by containers belonging to the pod.
     More info: https://kubernetes.io/docs/concepts/storage/volumes

     Volume represents a named volume in a pod that may be accessed by any
     container in the pod.
FIELDS: 
 awsElasticBlockStore <Object> 
 azureDisk <Object> 
 azureFile <Object> 
 cephfs <Object> 
 cinder <Object> 
 configMap <Object> 
 csi <Object> 
 downwardAPI <Object> 
 emptyDir <Object> 
 ephemeral <Object> 
 fc <Object> 
 flexVolume <Object> 
 flocker <Object> 
 gcePersistentDisk <Object> 
 gitRepo <Object> 
 glusterfs <Object> 
 hostPath <Object> 
 iscsi <Object> 
 name <string> -required- 
 nfs <Object> 
 persistentVolumeClaim <Object> 
 photonPersistentDisk <Object> 
 portworxVolume <Object> 
 projected <Object> 
 quobyte <Object> 
 rbd <Object> 
 scaleIO <Object> 
 secret <Object> 
 storageos <Object> 
 vsphereVolume <Object>
常用的如下: 
emptyDir 
hostPath 
nfs 
persistentVolumeClaim 
glusterfs 
cephfs 
configMap 
secret 
 
我们想要使用存储卷,需要经历如下步骤 
1、定义 pod 的 volume,这个 volume 指明它要关联到哪个存储上的 
2、在容器中要使用 volumemounts 挂载对应的存储 
 
经过以上两步才能正确的使用存储卷 
 
emptyDir 类型的 Volume 是在 Pod 分配到 Node 上时被创建,Kubernetes 会在 Node 上自动分配一个目录,因此无需指定宿主机 Node 上对应的目录文件。这个目录的初始内容为空,当 Pod 从 Node 上移除时,emptyDir中的数据会被永久删除。emptyDir Volume主要用于某些应用程序无需永久保存的临时目录,多个容器的共享目录等。 
 
#创建一个 pod,挂载临时目录 emptyDir 
 
Emptydir 的官方网址: https://kubernetes.io/docs/concepts/storage/volumes#emptydir 

[root@k8smaster ~]# mkdir cjh
[root@k8smaster ~]# cd cjh
[root@k8smaster cjh]# vim emptydir.yaml 
apiVersion: v1
kind: Pod 
metadata: 
  name: pod-empty
spec: 
  containers:
  - name: container-empty
    image: nginx
    imagePullPolicy: IfNotPresent
    volumeMounts: 
    - mountPath: /cache			#把cache-volume这个挂载到容器里的根cache目录下
      name: cache-volume
  volumes:
  - emptyDir:
     {
    
    }
    name: cache-volume
#- emptyDir: {} 也可以这么写 表示是临时的
#更新资源清单文件 
[root@k8smaster cjh]# kubectl apply -f emptydir.yaml 
pod/pod-empty created 
 
查看本机临时目录存在的位置
#查看 pod 调度到哪个节点 
[root@k8smaster cjh]# kubectl get pods -o wide | grep empty 
pod-empty               1/1     Running   0          1s    10.244.2.7   k8snode    <none>           <none>

#查看 pod 的 uid 
[root@k8smaster cjh]# kubectl get pods pod-empty -o yaml | grep uid 		#pod以yaml文件格式输出找到uid
  uid: c2b30d56-73b2-4d8d-9011-c39abd51b065
 
#登录到 xianchaonode1 上 
[root@k8snode ~]# tree /var/lib/kubelet/pods/c2b30d56-73b2-4d8d-9011-c39abd51b065
/var/lib/kubelet/pods/c2b30d56-73b2-4d8d-9011-c39abd51b065
├── containers
│   └── container-empty
│       └── 20e34c53
├── etc-hosts
├── plugins
│   └── kubernetes.io~empty-dir
│       ├── cache-volume
│       │   └── ready
│       └── wrapped_default-token-788ff
│           └── ready
└── volumes
    ├── kubernetes.io~empty-dir
    │   └── cache-volume
    └── kubernetes.io~secret
        └── default-token-788ff
            ├── ca.crt -> ..data/ca.crt
            ├── namespace -> ..data/namespace
            └── token -> ..data/token

11 directories, 7 files
 
由上可知,临时目录在本地的
/var/lib/kubelet/pods/c2b30d56-73b2-4d8d-9011-c39abd51b065/volumes/kubernetes.io~empty-dir/cache-volume/

#测试
[root@k8smaster cjh]# kubectl exec -it pod-empty -- /bin/sh
# cd cache  
# ls
[root@k8snode cache-volume]# echo "wowowowowow" > test.txt
# ls
test.txt
[root@k8smaster cjh]# kubectl delete pods pod-empty
pod "pod-empty" deleted

There are three types of persistent storage, we will explain in detail in the next article!

write at the end

It is not easy to create, if you think the content is helpful to you, please give me a three-link follow to support me! If there are any mistakes, please point them out in the comments and I will change them in time!
The series that is currently being updated: learn k8s from scratch.
Thank you for watching. The article is mixed with personal understanding. If there is any error, please contact me and point it out~

Guess you like

Origin blog.csdn.net/qq_45400861/article/details/126903668