Playing with k8s: Resource Management

1 Introduction to resource management

In Kubernetes, all content is abstracted into resources, and users need to manage Kubernetes by operating resources.

Kubernetes is essentially a cluster system. Users can deploy various services in the cluster. The so-called deployment service actually means running containers one by one in the kubernetes cluster and running specified programs in the containers.

The smallest management unit of kubernetes is pod rather than container, so containers can only be placed in pods. Kubernetes generally does not manage pods directly, but manages pods through the pod controller.

After the Pod can provide services, you must 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.

The core of learning kubernetes is to learn how to operate various resources such as Pods, Pod controllers, Services, and storage on the cluster.

2 Introduction to YAML language

YAML is a markup language similar to XML and JSON. It emphasizes being data-centric and not focusing on markup language. Therefore, the definition of YAML itself is relatively simple, and it is known as "a humanized data format language".

<heima>
    <age>15</age>
    <address>Beijing</address>
</heima>
heima:
  age: 15
  address: Beijing

The syntax of YAML is relatively simple, mainly including the following:

  • Case Sensitive
  • Use indentation to indicate hierarchical relationships
  • Tabs are not allowed for indentation, only spaces are allowed (lower version restrictions)
  • The number of spaces for indentation does not matter, as long as elements at the same level are left aligned
  • '#' indicates comment

YAML supports the following data types:

  • scalar: a single, indivisible value
  • Object: A collection of key-value pairs, also known as mapping/hash/dictionary
  • Array: A set of values ​​arranged in order, also called a sequence/list

# Scalar, refers to a simple value, string, Boolean value, integer, floating point number, Null, time, date # 1
Boolean type
c1: true (or True)
# 2 Integer type
c2: 234
# 3 Floating point Type
c3: 3.14
# 4 null type 
c4: ~ # Use ~ to represent null
# 5 Date type
c5: 2018-02-17 # The date must use ISO 8601 format, that is, yyyy-MM-dd
# 6 Time type
c6: 2018-02 -17T15:02:31+08:00 # The time uses the ISO 8601 format, the time and date are connected using T, and finally + is used to represent the time zone
# 7 String type
c7: heima # Simple writing method, write the value directly, if the string There are special characters in the middle, which must be wrapped in double quotes or single quotes 
c8: line1
    line2 # If there are too many strings, they can be split into multiple lines, and each line will be converted into a space.

#Object
# Form 1 (recommended):
heima:
  age: 15
  address: Beijing
# Form 2 (understood):
heima: {age: 15,address: Beijing}

# Array
# Form 1 (recommended):
address:
  - Shunyi
  - Changping  
# Form 2 (understood):
address: [Shunyi, Changping]

hint:   

1 Remember when writing yaml: add a space after it

2 If you need to put multiple pieces of yaml configuration in one file, use --- to separate them.

3 The following is a website that converts yaml to json. You can use it to verify whether yaml is written correctly.

https://www.json2yaml.com/convert-yaml-to-json

3 Resource management methods

  • Imperative object management: directly use commands to operate kubernetes resources
kubectl run nginx-pod --image=nginx:1.17.1 --port=80
  • Imperative object configuration: operate kubernetes resources through command configuration and configuration files
kubectl create/patch -f nginx-pod.yaml
  • Declarative object configuration: operate kubernetes resources through apply commands and configuration files
kubectl apply -f nginx-pod.yaml
type Operation object Applicable environment advantage shortcoming
Imperative object management object test Simple Can only operate active objects, cannot audit or track
Imperative object configuration document develop Can be audited and tracked When the project is large, there are many configuration files and the operation is cumbersome.
Declarative object configuration Table of contents develop Support directory operations Difficult to debug in unexpected situations

3.1 Imperative object management

kubectl command

kubectl is a command line tool for kubernetes clusters. It can manage the cluster itself and install and deploy containerized applications on the cluster. The syntax of the kubectl command is as follows:

kubectl [command] [type] [name] [flags]

comand: Specify the operation to be performed on the resource, such as create, get, delete

type: Specify resource type, such as deployment, pod, service

name: The name of the specified resource, the name is case sensitive

flags: Specify additional optional parameters

# 查看所有pod

kubectl get pod 
​
# 查看某个pod

kubectl get pod pod_name
​
# 查看某个pod,以yaml格式展示结果

kubectl get pod pod_name -o yaml

Resource Type

All content in kubernetes is abstracted into resources, which can be viewed through the following command:

kubectl api-resources

Frequently used resources include the following:

Resource classification Resource Name abbreviation resource role
Cluster level resources nodes no cluster components
namespaces ns Isolate Pod
pod resources pods after loading container
pod resource controller replicationcontrollers rc Control pod resources
replicasets rs Control pod resources
deployments deploy Control pod resources
daemonsets ds Control pod resources
jobs Control pod resources
cronjobs cj Control pod resources
horizontalpodautoscalers hpa Control pod resources
statefulsets sts Control pod resources
Service discovery resources services svc Unified pod external interface
ingress ing Unified pod external interface
Storage resources volumeattachments storage
persistentvolumes pv storage
persistentvolumeclaims pvc storage
Configure resources configmaps cm Configuration
secrets Configuration

​Operation

Kubernetes allows a variety of operations on resources. You can view detailed operation commands through --help

kubectl --help

Commonly used operations include the following:

Command classification Order translate Command function
basic commands create create Create a resource
edit edit Edit a resource
get Obtain Get a resource
patch renew update a resource
delete delete Delete a resource
explain explain Display resource documents
Run and debug run run Run a specified image in the cluster
expose exposed Expose resources as Service
describe describe Show resource internal information
logs log Output the logs of the container in the pod
attach twine Enter a running container
exec implement Execute a command in the container
cp copy Copy files inside and outside the Pod
rollout first display Manage the release of resources
scale scale Expand (shrink) the number of Pods
autoscale auto-adjust Automatically adjust the number of Pods
Advanced commands apply rc Configure resources through files
label Label Update tags on resources
Other commands cluster-info Cluster information Show cluster information
version Version Display the current Server and Client versions

The following is a simple demonstration of the use of the command using the creation and deletion of a namespace/pod:

# 创建一个namespace
[root@master ~]# kubectl create namespace dev
namespace/dev created
​
# 获取namespace
[root@master ~]# kubectl get ns
NAME              STATUS   AGE
default           Active   21h
dev               Active   21s
kube-node-lease   Active   21h
kube-public       Active   21h
kube-system       Active   21h
​
# 在此namespace下创建并运行一个nginx的Pod

[root@master ~]# kubectl run pod --image=nginx:latest -n dev
kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead.
deployment.apps/pod created
​
# 查看新创建的pod

[root@master ~]# kubectl get pod -n dev
NAME  READY   STATUS    RESTARTS   AGE
pod   1/1     Running   0          21s
​
# 删除指定的pod

[root@master ~]# kubectl delete pod pod-864f9875b9-pcw7x
pod "pod" deleted
​
# 删除指定的namespace

[root@master ~]# kubectl delete ns dev
namespace "dev" deleted

3.2 Imperative object configuration

Imperative object configuration is to use commands and configuration files to operate kubernetes resources.

1) Create an nginxpod.yaml with the following content:

apiVersion: v1
kind: Namespace
metadata:
  name: dev
​
---
​
apiVersion: v1
kind: Pod
metadata:
  name: nginxpod
  namespace: dev
spec:
  containers:
  - name: nginx-containers
    image: nginx:latest

2) Execute the create command to create resources:

[root@master ~]# kubectl create -f nginxpod.yaml
namespace/dev created
pod/nginxpod created

At this time, it was found that two resource objects were created, namely namespace and pod.

3) Execute the get command to view resources:

[root@master ~]#  kubectl get -f nginxpod.yaml
NAME            STATUS   AGE
namespace/dev   Active   18s
​
NAME            READY   STATUS    RESTARTS   AGE
pod/nginxpod    1/1     Running   0          17s

This displays the information of the two resource objects

4)执行delete命令,删除资源:

[root@master ~]# kubectl delete -f nginxpod.yaml
namespace "dev" deleted
pod "nginxpod" deleted

此时发现两个资源对象被删除了

总结:

命令式对象配置的方式操作资源,可以简单的认为:命令  +  yaml配置文件(里面是命令需要的各种参数)

3.3 声明式对象配置

声明式对象配置跟命令式对象配置很相似,但是它只有一个命令apply。

# 首先执行一次kubectl apply -f yaml文件,发现创建了资源
[root@master ~]#  kubectl apply -f nginxpod.yaml
namespace/dev created
pod/nginxpod created
​
# 再次执行一次kubectl apply -f yaml文件,发现说资源没有变动
[root@master ~]#  kubectl apply -f nginxpod.yaml
namespace/dev unchanged
pod/nginxpod unchanged

总结:

其实声明式对象配置就是使用apply描述一个资源最终的状态(在yaml中定义状态)

使用apply操作资源:
        如果资源不存在,就创建,相当于 kubectl create
        如果资源已存在,就更新,相当于 kubectl patch

扩展:kubectl可以在node节点上运行吗 ?

kubectl的运行是需要进行配置的,它的配置文件是$HOME/.kube,如果想要在node节点运行此命令,需要将master上的.kube文件复制到node节点上,即在master节点上执行下面操作:

scp  -r  HOME/.kube   node1: HOME/

使用推荐: 三种方式应该怎么用 ?

创建/更新资源 使用声明式对象配置 kubectl apply -f XXX.yaml

删除资源 使用命令式对象配置 kubectl delete -f XXX.yaml

查询资源 使用命令式对象管理 kubectl get(describe) 资源名称

Guess you like

Origin blog.csdn.net/duansamve/article/details/131407006