1. deployment 控制器的介绍
-
Deployment的功能:
-
管理Pod和ReplicaSet
-
具有上线部署、副本设定、滚动升级、回滚等功能
-
提供声明式更新,例如只更新一个新的Image
-
-
应用场景:网站、API、微服务
2. deployment 使用流程
项目生命周期
3. deployment部署
-
部署
-
示例yaml配置
apiVersion: apps/v1 kind: Deployment metadata: name: web namespace: default spec: replicas: 3 # Pod副本预期数量 selector: matchLabels: app: web template: metadata: labels: app: web # Pod副本的标签 spec: containers: - name: web image: nginx:1.16
4. 案例
4.1 编写deployment的web.yaml文件
[root@k8s-master deployment]# vim web.yaml
[root@k8s-master deployment]# cat web.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web
namespace: default
spec:
replicas: 3 # Pod副本预期数量
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web # Pod副本的标签
spec:
containers:
- name: web
image: nginx:1.16
4.2 启动服务
[root@k8s-master deployment]# kubectl apply -f web.yaml
deployment.apps/web created
4.3 查看服务
[root@k8s-master deployment]# kubectl get pods
NAME READY STATUS RESTARTS AGE
web-548b65b8c6-4k8n7 1/1 Running 0 2m24s
web-548b65b8c6-7wvhj 1/1 Running 0 2m24s
web-548b65b8c6-vpmpg 1/1 Running 0 2m24s
[root@k8s-master deployment]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
web-548b65b8c6-4k8n7 1/1 Running 0 2m56s 10.244.36.96 k8s-node1 <none> <none>
web-548b65b8c6-7wvhj 1/1 Running 0 2m56s 10.244.169.159 k8s-node2 <none> <none>
web-548b65b8c6-vpmpg 1/1 Running 0 2m56s 10.244.36.95 k8s-node1 <none> <none>
4.4 deployment端口暴露
[root@k8s-master deployment]# kubectl expose deployment web --port=80 --target-port=80 --type=NodePort
service/web exposed
4.5 查询服务是否端口暴露
[root@k8s-master deployment]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 6d
probe-demo ClusterIP 10.104.161.168 <none> 80/TCP 2d
web NodePort 10.98.32.199 <none> 80:30082/TCP 14s