Cloud-native Kubernetes resource management, basic use of Namespace, Pod, Label, Deployment, and Service

Introduction to Resource Management

●The essence of Kubernetes is a cluster system, and users can deploy various services in the cluster. The so-called deployment service is actually running containers one by one in the Kubernetes cluster, and running the specified program in the container.
The smallest management unit of Kubernetes is a Pod rather than a container, so the container can only be placed in the Pod, and Kubernetes generally does not directly manage the Pod, but manages the Pod through the Pod Controller.
●After the Pod provides services, you need to consider how to access the services in the Pod. Kubernetes provides Service resources to implement this function.
●Of course, if the data of the program in the Pod needs to be persisted, Kubernetes also provides various storage systems.

Imperative object management: can only operate active objects, cannot audit, track, usage

Imperative object configuration: when the project is large, there are many configuration files, and the operation is troublesome

Namespace

  • Namespace is a very important resource in the kubernetes system, and its main function is to implement 多套系统的资源隔离or 多租户的资源隔离.

  • By default, all pods in a kubernetes cluster are mutually accessible. But in practice, you may not want to allow two Pods to access each other, so you can divide the two Pods into different Namespaces. Kubernetes can form logical "groups" by allocating resources within the cluster to different Namespaces to facilitate the isolated use and management of resources in different groups.

  • Different namespaces can be handed over to different tenants for management through the authorization mechanism of kubernetes, thus realizing multi-tenant resource isolation. At this time, it can also combine the resource quota mechanism of kubernetes to limit the resources that different tenants can occupy, such as CPU usage, memory usage, etc., to manage the resources available to tenants

 After the cluster is started, kubernetes will create several namespaces by default

kubectl get namespace #View
default: All objects with unspecified Namespace will be allocated in the default namespace.
kube-node-lease: Heartbeat maintenance between cluster nodes, introduced in v1.13.
kube-public: Resources in this namespace can be accessed by everyone (including unauthenticated users).
kube-system: All resources created by the kubernetes system are in this namespace

Pod

  • A Pod is the smallest unit of management in a kubernetes cluster. To run a program, it must be deployed in a container, and the container must exist in a Pod.

  • A Pod can be thought of as an encapsulation of a container, and one or more containers can exist in a Pod.

After kubernetes starts the cluster, each component in the cluster also runs in Pod mode, which can be viewed by the following command:

kubectl get pods -n kube-system

Create and run Pods, mainly through the Pod Controller

kubectl run (Pod的名称) [参数]  没有单独运行
# --image 指定Pod的镜像
# --port 指定端口
# --namespace 指定namespace
kubectl run nginx --image=nginx:1.17.1 --port=80 --namespace=dev
查询所有Pod的基本信息
kubectl get pods -n dev
查看Pod的详细信息
kubectl describe pod nginx -n dev
访问Nginx的Pod
kubectl get pods -n dev -o wide
curl 10.244.2.7:80  #ip加端口
删除Nginx的Pod
kubectl delete pod nginx -n dev

Imperative object configuration, create a new pod-nginx.yaml

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: dev
spec:
  containers:
  - image: nginx:1.17.1
    imagePullPolicy: IfNotPresent
    name: pod
    ports: 
    - name: nginx-port
      containerPort: 80
      protocol: TCP

Execute create and delete commands

kubectl create -f pod-nginx.yaml
kubectl delete -f pod-nginx.yaml

Label

Label is an important concept of kubernetes. Its role is to add identifiers to resources to distinguish and select them.

Features of Label:

  • A Label will be attached to various objects in the form of key/value key-value pairs, such as Node, Pod, Service, etc.
  • A resource object can define any number of Labels, and the same Label can be added to any number of resource objects.
  • Label is usually determined when the resource object is defined, of course, it can also be dynamically added or deleted after the object is created.
  • Multi-dimensional grouping of resources can be realized through Label, so as to manage resource allocation, scheduling, configuration and deployment flexibly and conveniently, such as version labels, environment labels, etc.

tag resources

kubectl label pod xxx key=value [-n 命名空间]
为Nginx的Pod打上标签
kubectl label pod nginx version=1.0 -n dev
更新资源的标签
kubectl label pod xxx key=value [-n 命名空间] --overwrite
kubectl label pod nginx version=2.0 -n dev --overwrite
显示Nginx的Pod的标签
kubectl get pod nginx -n dev --show-labels
筛选标签
kubectl get pod -l version=2.0 -n dev --show-labels
删除标签
kubectl label pod nginx version- -n dev

Imperative Object Configuration

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  namespace: dev
  labels:
    version: "3.0"
    env: "test"        
spec:
  containers:
  - image: nginx:1.17.1
    imagePullPolicy: IfNotPresent
    name: pod
    ports: 
    - name: nginx-port
      containerPort: 80
      protocol: TCP

Execute create and delete commands

kubectl create -f pod-nginx.yaml
kubectl delete -f pod-nginx.yaml

Deployment

  • In kubernetes, Pod is the smallest control unit, but kubernetes rarely controls Pod directly, usually through Pod controller .

  • The Pod controller is used for Pod management to ensure that the Pod resources meet the expected state. When the Pod resources fail, it will try to restart or rebuild the Pod, and the newly created IP address is different from the original one.

When a Deployment creates a pod, it will label the pod and select the pod to be managed through the label selector.

#创建2个pod管理nginx命名空间为dev
kubectl run nginx --image=nginx:1.17.1 --replicas=2 --port=80 --namespace dev
#查看
kubectl get deployment,pods -n dev
#查看nginx详细描述
kubectl describe deploy nginx -n dev
#查看标签
kubectl get pods -n dev --show-lables
#删除
kubectl delete deploy nginx -n dev

Imperative object configuration, create a deploy-nginx.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
  namespace: dev
#副本数  选择器
spec:
  replicas: 3
  selector:
    matchLabels:
      run: nginx
  #pod模板    
  template:
    metadata:
      labels:
        run: nginx
    spec:
      containers:
      - image: nginx:1.17.1
        name: nginx
        ports:
        - containerPort: 80
          protocol: TCP
#执行创建和删除命令
kubectl create -f deploy-nginx.yaml
kubectl delete -f deploy-nginx.yaml
#查看创建的Pod
kubectl get pods [-n 命名空间]
#查看名称为dev的namespace下通过deployment创建的3个Pod
kubectl get pods -n dev
#查看deployment的信息
kubectl get deployment [-n 命名空间]
kubectl get deployment -n dev
kubectl describe deployment nginx -n dev
#删除deployment
kubectl delete deployment nginx -n dev

Service

  • We have been able to use Deployment to create a group of Pods to provide services with high availability. Although each Pod will be assigned a separate Pod IP address, there are the following problems:

  • The IP of the Pod will change as the Pod is rebuilt.
  • The Pod's IP is only a virtual IP that is visible inside the cluster and cannot be accessed from outside

Service can be regarded as a group of pods of the same kind of external access interface. With the help of Service, applications can easily realize service discovery and load balancing

#暴露Service
kubectl expose deployment xxx --name=服务名 --type=ClusterIP --port=暴露的端口 --target-port=指向集群中的Pod的端口 [-n 命名空间]
# 新建时借助pod控制器找到对应的pod
#暴露名为test的namespace下的名为nginx的deployment,并设置服务名为svc-nginx1
kubectl expose deployment nginx --name=svc-nginx1 --type=ClusterIP --port=80 --target-port=80 -n test
#查看Service
kubectl get service -n test
#访问
curl 对应集群ip

Recreate the Service accessible outside the cluster

kubectl expose deployment xxx --name=服务名 --type=NodePort --port=暴露的端口 --target-port=指向集群中的Pod的端口 [-n 命名空间]
# 会产生一个外部也可以访问的Service
kubectl expose deploy nginx --name=svc-nginx2 --type=NodePort --port=80 --target-port=80 -n test
#查看名为test的命名空间的所有Service
kubectl get service -n test
#浏览器访问 查出来的ip与端口号
#删除服务
kubectl delete service svc-nginx1 -n test

Object configuration method, create a new svc-nginx.yaml

apiVersion: v1
kind: Service
metadata:
  name: svc-nginx
  namespace: dev
spec:
  clusterIP: 10.109.179.231
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
  selector:
    run: nginx
  type: ClusterIP
执行创建和删除命令
kubectl  create  -f  svc-nginx.yaml
kubectl  delete  -f  svc-nginx.yaml

Guess you like

Origin blog.csdn.net/weixin_52210557/article/details/123810185