preface

K8s can be stored in many ways. This article focuses on ConfigMap and persistent storage – NFS. This is also the common storage method for K8S. It’s hard to explain, so I’ll use Nginx as an example, because Nginx needs both. So let me just draw a little picture here.

This series uses three servers, one of which is networked and two of which is not. The K8S cluster cannot be connected to the network.

Nginx storage type analysis

When using nginx, there are two main directories of concern:

The/etc/nginx/conf. D. and/usr/share/nginx/HTML

One is the configuration file directory and the other is the static resource directory. In production, ConfigMap is used to store configuration files, and NFS is used to store static resources persistently.

The configuration file

├ ─ ─ the/etc/nginx/conf. D ├ ─ ─ the default. The conf ├ ─ ─ a.c onf ├ ─ ─ biggest onf └ ─ ─ Arthur c. onfCopy the code

Static resource

├ ─ ─ the/usr/share/nginx/HTML ├ ─ ─ index. The HTML ├ ─ ─ a.h HTML ├ ─ ─ b.h HTML └ ─ ─ c. HTMLCopy the code

ConfigMap

Creating a working directory

mkdir -p /mldong/k8s/nginx
Copy the code

Go to the working directory

cd /mldong/k8s/nginx
Copy the code

Use YAML to define the mode

Define a single

cat <<EOF > /mldong/k8s/nginx/nginx-cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-cm
  namespace: mldong-test
data:
  a.conf: |-
    server {
      listen       80;
      server_name  a.mldong.com;
      location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
      }
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
        root   /usr/share/nginx/html;
      }
    }
EOF

Copy the code

Define multiple

cat <<EOF > /mldong/k8s/nginx/nginx-cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-cm
  namespace: mldong-test
data:
  a.conf: |-
    server {
      listen       80;
      server_name  a.mldong.com;
      location / {
        root   /usr/share/nginx/html/a;
        index  index.html index.htm;
      }
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
        root   /usr/share/nginx/html;
      }
    }
  b.conf: |-
    server {
      listen       80;
      server_name  b.mldong.com;
      location / {
        root   /usr/share/nginx/html/b;
        index  index.html index.htm;
      }
      error_page   500 502 503 504  /50x.html;
      location = /50x.html {
        root   /usr/share/nginx/html;
      }
    }
EOF

Copy the code

Create a command

kubectl apply -f nginx_cm.yaml 
Copy the code

View all configMaps in the namespace

kubectl get configmap -n mldong-test
Copy the code

View details about the specified ConfigMap

kubectl describe configmap nginx-cm -n mldong-test
Copy the code

ConfigMap===>kind

nginx-cm===>metadata.name

mldong-test===>metadata.namespace

Delete the defined ConfigMap

kubectl delete -f nginx_cm.yaml 
Copy the code

Use external file definitions

Create a.conf and b.C onf in the current directory

a.conf

cat <<EOF > /mldong/k8s/nginx/a.conf
server {
    listen       80;
    server_name  a.mldong.com;
    location / {
        root   /usr/share/nginx/html/a;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    	root   /usr/share/nginx/html;
    }
}
EOF

Copy the code

b.conf

cat <<EOF > /mldong/k8s/nginx/b.conf
server {
    listen       80;
    server_name  b.mldong.com;
    location / {
        root   /usr/share/nginx/html/b;
        index  index.html index.htm;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
    	root   /usr/share/nginx/html;
    }
}
EOF

Copy the code

Create a command

Create a single

kubectl create configmap nginx-cm --from-file=a.conf -n mldong-test
Copy the code

Create multiple

kubectl create configmap nginx-cm --from-file=a.conf --from-file=b.conf -n mldong-test
Copy the code

By default, the file name is key. If you want to change the file name, for example:

kubectl create configmap nginx-cm --from-file=xxxx=a.conf -n mldong-test
Copy the code

Example Delete the created ConfigMap

kubectl delete configmap nginx-cm -n mldong-test
Copy the code

View all configMaps in the namespace

kubectl get configmap -n mldong-test
Copy the code

View details about the specified ConfigMap

kubectl describe configmap nginx-cm -n mldong-test
Copy the code

ConfigMap===>kind

nginx-cm===>metadata.name

mldong-test===>metadata.namespace

Persistent storage

NFS is used for persistent storage here, and NAS is called on Ali Cloud. If you don’t have one, open it first. – There’s a free version.

Open the nas

Search the nas

Creation Process

The only thing you need to pay attention to is choosing the right area.

Viewing Configuration Details

mount

Copy the mount command and execute it on the small A server

sudo mount -t nfs -o vers=3,nolock,proto=tcp,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport 9fdd94bf87-wkf12.cn-zhangjiakou.nas.aliyuncs.com:/ /mnt
Copy the code

/ MNT indicates the local directory to be mounted. If the directory does not exist, you need to create it yourself. You can change it to another directory.

View the mounting status

df -h | grep aliyun
Copy the code

Cancel the mount

umount /mnt
Copy the code

Memory card

Ali Cloud cluster storage plug-in has two Flexvolume and CSI. CSI is officially recommended. After selecting CSI, the cluster will install the plug-in by default.

Before starting, it is recommended to review the documentation completely.

The storage plug-in is installed by default

Verify the installation

  • Run the following command to display a list of pods in the Running state.
kubectl get pod -n kube-system | grep csi-plugin
Copy the code
  • Run the following command to print a list of pods in the Running state.
kubectl get pod -n kube-system | grep csi-provisioner
Copy the code

Begin to use

This paper first uses NAS static storage volume practice, mainly for fear of getting too much at a time, there will be some chaos. Try it out for yourself, the yamL of this document is flawed. If you can’t, you can create it using the console and learn yamL.

Example Create a NAS static storage volume

nginx-pv.yaml

cat <<EOF > /mldong/k8s/nginx/nginx-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nginx-pv
  labels:
    alicloud-pvname: nginx-pv
spec:
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  csi:
    driver: nasplugin.csi.alibabacloud.com
    volumeHandle: nginx-pv
    volumeAttributes:
      server: "9fdd94bf87-wfq72.cn-zhangjiakou.nas.aliyuncs.com"
      path: "/"
      vers: "3"
  storageClassName: nas
EOF

Copy the code

instructions

  • driver: Driver type. In this example, the value isnasplugin.csi.alibabacloud.com, indicates that the aliyun NAS CSI plug-in is used.
  • volumeHandle: Sets the PV name.
  • server: NAS mount point.
  • path: Mounts subdirectories.
  • vers: Indicates the VERSION of the NFS protocol to which the NAS data volume is attached. V3 is recommended. Speed NAS only supports V3.

Note: PV is shared by all namespaces, so there is no need to add a namespace

Create the PV
kubectl apply -f nginx-pv.yaml
Copy the code
Check the PV
kubectl get pv
Copy the code

Available-> successful, Released-> failed

Viewing PV Details
kubectl describe pv
Copy the code
Delete the PV
kubectl delete -f nginx-pv.yaml
Copy the code

nginx-pvc.yaml

Note: PVC has namespaces, so add namespaces

cat <<EOF > /mldong/k8s/nginx/nginx-pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  annotations:
    pv.kubernetes.io/bind-completed: 'yes'
    pv.kubernetes.io/bound-by-controller: 'yes'
  finalizers:
    - kubernetes.io/pvc-protection
  name: nginx-pvc
  namespace: mldong-test
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
  selector:
    matchLabels:
      alicloud-pvname: nginx-pv
  storageClassName: nas
  volumeMode: Filesystem
  volumeName: nginx-pv
EOF

Copy the code

Here the main alicloud-pvname is the PV metadata.name

Create a PVC
kubectl apply -f nginx-pvc.yaml
Copy the code
Check the PVC
kubectl get pvc -n mldong-test
Copy the code

Bound-> success, Pending-> failure

View PVC details
kubectl describe pvc -n mldong-test
Copy the code
Delete the PVC
kubectl delete -f nginx-pvc.yaml
Copy the code

Began to mount

K8s volumes are defined first and then mounted. We have just defined two types of coupons, ConfigMap and PVC.

cat <<EOF > /mldong/k8s/nginx/nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-pod
  namespace: mldong-test
spec:
  selector:
    matchLabels:
      app: nginx-pod
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx-pod
    spec:
      containers:
        - name: nginx
          image: registry-vpc.cn-zhangjiakou.aliyuncs.com/mldong/java/nginx:latest
          imagePullPolicy: IfNotPresent
          ports:
            - containerPort: 80
              name: port
              protocol: TCP
          volumeMounts:
            - name: nginx-pvc
              mountPath: "/usr/share/nginx/html"
            - name: nginx-cm
              mountPath: "/etc/nginx/conf.d"
      volumes:
        - name: nginx-pvc
          persistentVolumeClaim: 
            claimName: nginx-pvc
        - name: nginx-cm
          configMap:
            name: nginx-cm
EOF

Copy the code

Description:

  • volumeMount.name= = >volumes.name
  • volumeMount.mountPath==> Container directory
  • persistentVolumeClaim.claimName==> Name of the DEFINED PVC
  • configMap.name==> Name of the configMap to be defined
  • ---For file segmentation, K8S will help handle it

Create a pod

kubectl apply -f nginx-deployment.yaml
Copy the code

Check the pod

kubectl get pods -n mldong-test
Copy the code

Small A server creates files

├ ─ ─ / MNT ├ ─ ─ a └ ─ ─ index. The HTML └ ─ ─ b └ ─ ─ index. The HTMLCopy the code

Into the container

kubectl exec -it pod-name -n mldong-test --  bash
Copy the code

Check whether the configuration file exists

ls /etc/nginx/conf.d/
Copy the code

Check whether static resources exist

ls /usr/share/nginx/html
Copy the code

Create a Service

file

cat <<EOF > /mldong/k8s/nginx/nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx-nodeport
  namespace: mldong-test
spec:
  type: NodePort
  ports:
  - port: 80
    targetPort: 80
    nodePort: 32180 
  selector:
    app: nginx-pod
EOF

Copy the code

command

kubectl apply -f nginx-service.yaml
Copy the code

Look at the Service

kubectl get Service -n mldong-test
Copy the code

Access the service

Accessing Node 1
curl worker1:32180
Copy the code
Accessing Node 2
curl worker2:32180
Copy the code
Access domain name A
curl -H 'Host: a.mldong.com' worker1:32180
Copy the code
Accessing domain name B
curl -H 'Host: b.mldong.com' worker1:32180
Copy the code

summary

This article uses Nginx as an example to show you how to use ConfigMap and persistent storage, although it is cloud-based, the basic flow is similar.

  • Determine the storage type of the service to be deployed

  • If it is ConfigMap

    • Define the configuration
  • If the pv

    • Define the pv
    • Definition of PVC
  • Define the POD mount

  • Define service To publish a service

Related articles

Walk you through K8S – cluster creation and Hello World