Kubernetes 的资源管理方式
命令式对象配置
- 命令式对象配置就是通过命令配置和配置文件去操作 Kubernetes 的资源。
- 命令式对象配置的方式操作资源,可以简单的认为:命令 + yaml 配置文件(里面是命令需要的各种参数)。
① 创建一个 nginxpod.yaml 文件,内容如下:
bash
[root@k8s-master ~]# cat nginxpod.yaml
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: v1
kind: Pod
metadata:
name: nginxpod
namespace: dev
spec:
containers:
- name: nginx-container
image: nginx:1.17.1
- ② 执行 create 命令,创建资源:
bash
[root@k8s-master ~]# kubectl create -f nginxpod.yaml
namespace/dev created
pod/nginxpod created
③ 执行 get 命令,查看资源
bash
[root@k8s-master ~]# kubectl get -f nginxpod.yaml
NAME STATUS AGE
namespace/dev Active 21s
NAME READY STATUS RESTARTS AGE
pod/nginxpod 1/1 Running 0 21s
[root@k8s-master ~]# kubectl get ns dev
NAME STATUS AGE
dev Active 2m17s
[root@k8s-master ~]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
nginxpod 1/1 Running 0 2m30s
- ④ 执行 delete 命令,删除资源:
bash
[root@k8s-master ~]# kubectl delete -f nginxpod.yaml
namespace "dev" deleted
pod "nginxpod" deleted
[root@k8s-master ~]# kubectl get pod -n dev
No resources found in dev namespace.
声明式对象配置
- 声明式对象配置:通过 apply 命令和配置文件去操作 Kubernetes 的资源。
- 声明式对象配置和命令式对象配置类似,只不过它只有一个 apply 命令。
- apply 命令相当于 create 命令和 patch 命令。
① 创建一个 nginxpod.yaml 文件,内容如下:
bash
cat nginxpod.yaml
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: v1
kind: Pod
metadata:
name: nginxpod
namespace: dev
spec:
containers:
- name: nginx-container
image: nginx:1.17.1
- ② 执行 apply 命令:
bash
[root@k8s-master ~]# kubectl apply -f nginxpod.yaml
namespace/dev created
pod/nginxpod created
如果修改nginxpod.yaml配置文件,再次执行apply命令会怎么样?
将nginx版本修改为.2:
bash
[root@k8s-master ~]# cat nginxpod.yaml
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: v1
kind: Pod
metadata:
name: nginxpod
namespace: dev
spec:
containers:
- name: nginx-container
image: nginx:1.17.2
再次执行apply命令:
bash
[root@k8s-master ~]# kubectl apply -f nginxpod.yaml
namespace/dev unchanged
pod/nginxpod configured
[root@k8s-master ~]# kubectl describe pod nginxpod -n dev
Name: nginxpod
Namespace: dev
Priority: 0
Node: k8s-node2/192.168.58.233
Start Time: Sun, 29 Dec 2024 03:56:41 -0500
Labels: <none>
Annotations: cni.projectcalico.org/containerID: c87b253bcf420d60dba6e035480b7e0855e646b632db39197ad60b5b1c225b11
cni.projectcalico.org/podIP: 10.244.169.133/32
cni.projectcalico.org/podIPs: 10.244.169.133/32
Status: Running
IP: 10.244.169.133
IPs:
IP: 10.244.169.133
Containers:
nginx-container:
Container ID: docker://b721c324545b25ce9199adcbb9c89300666e3fd5c23d8b762d2daf766c134292
Image: nginx:1.17.2
Image ID: docker-pullable://nginx@sha256:b4b9b3eee194703fc2fa8afa5b7510c77ae70cfba567af1376a573a967c03dbb
Port: <none>
Host Port: <none>
State: Running
Started: Sun, 29 Dec 2024 03:56:43 -0500
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-vptkw (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-vptkw:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 2m57s default-scheduler Successfully assigned dev/nginxpod to k8s-node2
Normal Pulled 2m55s kubelet Container image "nginx:1.17.1" already present on machine
Normal Created 2m55s kubelet Created container nginx-container
Normal Started 2m55s kubelet Started container nginx-container
Normal Killing 53s kubelet Container nginx-container definition changed, will be restarted
Normal Pulling 53s kubelet Pulling image "nginx:1.17.2"
可见nginx版本会进行升级!