“This is the 27th day of my participation in the Gwen Challenge in November. See details of the event: The Last Gwen Challenge in 2021”

With volume and namespace in mind, let’s start by sharing what a service is

What is the volume

Docker volume is a data volume

In K8S, volume is a shared directory in pod that can be accessed by multiple containers, essentially the same as docker

A volume is defined on top of a POD, so the life cycle of a volume is the same as that of a POD

A volume can be mounted to a specific file directory by multiple containers in the POD. If a container fails, volume is not affected. That is, data in a volume is not lost

We can use volume:

  • Specify the type and content of the volume in the PODspec.volumesfield
  • We need to map volume to the container, which we can usespec.containers.volumeMounts field

K8S supports the following types of volumes:

  • awsElasticBlockStore
  • azureFile
  • cephfs
  • emptyDir
  • hostPath
  • configMap
  • Fc (Fibre Channel)
  • . . , etc.

If you look at the documentation up here, each volume type is explained until I start rolling

Take a quick look at the emptyDir volume type

EmptyDir as the name implies, emptyDir is created when a POD is created and is empty, and the volume will remain as long as the POD is running

However, when the pod is removed from the node, the emptyDir is permanently removed with it

With emptyDir we can write like this

EmptyDir volume storage is dependent on the storage medium of our node, for example, disk, SSD or network storage, etc

Take a look at the hostPath type

A volume of the hostPath type allows the corresponding container to access the directory specified on the current host

For the hostPath type, we use it as sparingly as possible. There is an official warning

For example, if we need to run a container that will access a docker system directory, we can use the /var/lib/docker directory as a volume of type hostPath

However, once the pod leaves the host, the data in the hostPath will not be permanently deleted, but the data will not migrate with the POD to another host

With hostPath we can write this

For volumes, we can share a wave of higher-order usage, as well as specific principles

namespace

A namespace is a namespace

In most cases, namespaces are used to implement resource isolation among multiple users. Resource objects in a cluster are allocated to different namespaces to form logical groups.

This allows different groups to be managed in groups while sharing resources that use the entire cluster

In the initial K8S state, K8S will have three namespaces

  • default

The default namespace of a namespaceless object

In K8S, when the cluster is started, a default namespace is created. If rc, service, pod are not specified, these resources are created in the default namespace by the system

  • kube-system

The namespace of the object created by the K8S system

  • kube-public

Is a namespace created automatically by K8S and readable by all users

For namespace shared clusters we can see here: click I enter namespace

We can use namespaces like this:

  • Creating a NameSpace
apiVersion: v1
kind: NameSpace
metadata:
  name: myns
--------------------
apiVersion: v1
kind: Pod
metadata:
  name: busybox
  namespace: myns
spec:
 containers:
 - image: busybox
   command:
   - ls 
   name: busybox
Copy the code

As shown above, we create a namespace named myns, create a POD, and specify the namespace as myns

We can view namespaces like this:

kubectl get namesapces

We can also create namespaces directly by command

kubectl create namespace myns

Create a POD and specify the namespace myns. Once created, we can view the POD resources in the specified namespace

Kubectl get Pods --namespace= namespace

Today is here, learning, if there is a deviation, please correct

Welcome to like, follow and favorites

Friends, your support and encouragement, I insist on sharing, improve the quality of the power

All right, that’s it for this time

Technology is open, our mentality, should be more open. Embrace change, live in the sun, and strive to move forward.

I am Nezha, welcome to like, see you next time ~