【原文链接】
因为kubernetes在v1.18.0版本之后废弃了–replicas 参数,因此这里就不涉及命令行方式创建deployment的了,而推荐使用yaml的方式
一、使用配置文件创建deployment
1.1 编写配置文件
如下,创建deployment-nginx.yaml文件,编辑内容如下,即创建一个dev命名空间,并在dev命名空间中创建三个nginx的pod,并且创建一个deployment控制器
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.1
name: nginx
ports:
- containerPort: 80
protocol: TCP
1.2 创建资源
使用如下命令创建资源
[root@master demo]# kubectl apply -f deployment-nginx.yaml
namespace/dev created
deployment.apps/nginx created
[root@master demo]#
1.3 查看创建的资源
如下,同时查看deployment和pod
[root@master demo]# kubectl get deployment,pod -n dev
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 3/3 3 3 68s
NAME READY STATUS RESTARTS AGE
pod/nginx-66ffc897cf-787xt 1/1 Running 0 68s
pod/nginx-66ffc897cf-cxxjs 1/1 Running 0 68s
pod/nginx-66ffc897cf-k88qt 1/1 Running 0 68s
[root@master demo]#
1.4 删除其中一个pod
如下删除其中一个pod
[root@master demo]# kubectl delete pod/nginx-66ffc897cf-787xt -n dev
pod "nginx-66ffc897cf-787xt" deleted
[root@master demo]#
再次查看deployment和pod,如下,可以发现此时还是三个pod,仔细观察可以发现,上面的删除操作确实成功了,只不过这里又增加了一个新的pod,这就是deployment控制器在发挥作用,即当一个pod被删除后,deployment控制器会自动的创建一个新的pod来满足要求
[root@master demo]# kubectl get deployment,pod -n dev
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 3/3 3 3 3m21s
NAME READY STATUS RESTARTS AGE
pod/nginx-66ffc897cf-cxxjs 1/1 Running 0 3m21s
pod/nginx-66ffc897cf-k88qt 1/1 Running 0 3m21s
pod/nginx-66ffc897cf-ztpbm 1/1 Running 0 38s
[root@master demo]#
1.5 删除资源
使用如下命令删除资源
[root@master demo]# kubectl delete -f deployment-nginx.yaml
namespace "dev" deleted
deployment.apps "nginx" deleted
[root@master demo]#