一、创建对象
编辑deployment.yaml文件,内容如下:
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: dev
spec:
replicas: 3
selector:
matchLabels:
run: nginx
template:
metadata:
labels:
run: nginx
spec:
containers:
- image: nginx:1.17.3
name: nginx
ports:
- containerPort: 80
protocol: TCP
使用如下命令创建对象
[root@master object_manage]# kubectl apply -f deployment.yaml
namespace/dev created
deployment.apps/nginx created
[root@master object_manage]#
二、查看对象
使用如下命令即可查看创建的对象
[root@master object_manage]# kubectl get -f deployment.yaml
NAME STATUS AGE
namespace/dev Active 55s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 3/3 3 3 55s
[root@master object_manage]#
三、更新对象
更新deployment.yaml文件,将副本数修改为1,同时将nginx的镜像的tag降为1.17.1,如下:
apiVersion: v1
kind: Namespace
metadata:
name: dev
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: dev
spec:
replicas: 1
selector:
matchLabels:
run: nginx
template:
metadata:
labels:
run: nginx
spec:
containers:
- image: nginx:1.17.1
name: nginx
ports:
- containerPort: 80
protocol: TCP
然后使用如下命令更新
[root@master object_manage]# kubectl apply -f deployment.yaml
namespace/dev unchanged
deployment.apps/nginx configured
[root@master object_manage]#
这里注意,如果yaml文件没有修改,apply命令也是可以执行的,只是不会做任何操作,如下:
[root@master object_manage]# kubectl apply -f deployment.yaml
namespace/dev unchanged
deployment.apps/nginx unchanged
[root@master object_manage]#
四、删除对象
使用如下命令即可完成对象删除
[root@master object_manage]# kubectl delete -f deployment.yaml
namespace "dev" deleted
deployment.apps "nginx" deleted
[root@master object_manage]#