一、创建对象
编写deployment.yaml文件,内容如下:
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 create -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 98s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 1/1 1 1 98s
[root@master object_manage]#
三、更新对象
编辑deployment.yaml文件,修改副本数为3,设置nginx镜像的tag为1.17.3,如下:
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 replace -f deployment.yaml
namespace/dev replaced
deployment.apps/nginx replaced
[root@master object_manage]#
此时使用如下命令查看,可以发现pod数量已经更新为3个,此外nginx的镜像的tag也更新为1.17.3
[root@master object_manage]# kubectl get -f deployment.yaml -o wide
NAME STATUS AGE
namespace/dev Active 5m52s
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/nginx 3/3 3 3 5m52s nginx nginx:1.17.3 run=nginx
[root@master object_manage]#
四、删除对象
使用如下命令即可删除
[root@master object_manage]# kubectl delete -f deployment.yaml
namespace "dev" deleted
deployment.apps "nginx" deleted
[root@master object_manage]#