![b61cf972845fa818eede0cb996a0a8c8.gif](https://i-blog.csdnimg.cn/blog_migrate/54fb3fabd08276e984b22bb7184ea83c.gif)
今天我们学习创建Deployment。之前,我们只是学习如何创建一个POD实例,一旦POD出现故障,服务就会因此宕掉。因此,kubernetes提供了Deployment 的概念,借助kubernetes去管理一组POD 的副本,也就是副本集,这样就可以保证一定数量的副本一直可用,不会因为一个POD 挂掉导致整个服务宕掉。首先,我们可以这样定义Deployment:
![8bf23c1bcb10ba6ae3ac3c53713ca711.png](https://i-blog.csdnimg.cn/blog_migrate/f86155646038c016e9824fbd07e00036.png)
创建YAML文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
该Depoloyment创建了一个ReplicaSet,负责启动三个nginx Pods,在该例中:
-创建名为nginx-deployment (由 .metadata.name 字段标明)的 Deployment。
-该Deployment 创建三个(由 replicas 字段标明)Pod 副本。
-selector 字段定义Deployment 如何查找要管理的Pods。在这里,你只需选择在Pod 模板中定义的标签(app: nginx)。不过,更复杂的选择规则是也可能的,只要Pod 模板本身满足所给规则即可。
-template 字段包含以下子字段:
Pod 被使用 labels 字段打上 app: nginx 标签。
Pod 模板规约(即 .template.spec 字段)指示Pods 运行一个nginx 容器, 该容器运行版本为1.14.2 的nginx Docker Hub镜像。
创建一个容器并使用name 字段将其命名为 nginx。
![8bf23c1bcb10ba6ae3ac3c53713ca711.png](https://i-blog.csdnimg.cn/blog_migrate/f86155646038c016e9824fbd07e00036.png)
创建Deployment
[root@vms20 pod]# kubectl apply -f deployment.yaml
deployment.apps/nginx-deployment created
[root@vms20 pod]#
[root@vms20 pod]# kubectl get deployments.apps
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 70s
![8bf23c1bcb10ba6ae3ac3c53713ca711.png](https://i-blog.csdnimg.cn/blog_migrate/f86155646038c016e9824fbd07e00036.png)
查看标签与通过标签寻找Pod
[root@vms20 pod]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
foobar 1/1 Running 1 98d name=cpu-utilizer,run=foobar
front-end 1/1 Running 1 99d name=cpu-utilizer,run=front-end
nginx-deployment-6b474476c4-8vctt 1/1 Running 0 108s app=nginx,pod-template-hash=6b474476c4
nginx-deployment-6b474476c4-qkj2h 1/1 Running 0 108s app=nginx,pod-template-hash=6b474476c4
nginx-deployment-6b474476c4-x8stz 1/1 Running 0 108s app=nginx,pod-template-hash=6b474476c4
static-web 1/1 Running 0 28m app=web
webserver-5dc5bd6bf4-dvb25 1/1 Running 1 99d app=webserver,pod-template-hash=5dc5bd6bf4
[root@vms20 pod]#
[root@vms20 pod]# kubectl get pods -l app=nginx
NAME READY STATUS RESTARTS AGE
nginx-deployment-6b474476c4-8vctt 1/1 Running 0 2m18s
nginx-deployment-6b474476c4-qkj2h 1/1 Running 0 2m18s
nginx-deployment-6b474476c4-x8stz 1/1 Running 0 2m18s
![8bf23c1bcb10ba6ae3ac3c53713ca711.png](https://i-blog.csdnimg.cn/blog_migrate/f86155646038c016e9824fbd07e00036.png)
Deployment的创建过程
Deployment 作为replicaset-controller的管理者,RC会创建Pod。Pod自身会下载镜像并启动镜像:
[root@vms20 pod]# kubectl describe rs nginx-deployment
...
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 4m1s replicaset-controller Created pod: nginx-deployment-6b474476c4-x8stz
Normal SuccessfulCreate 4m1s replicaset-controller Created pod: nginx-deployment-6b474476c4-qkj2h
Normal SuccessfulCreate 4m1s replicaset-controller Created pod: nginx-deployment-6b474476c4-8vctt
[root@vms20 pod]# kubectl describe pod nginx-deployment-6b474476c4-x8stz
...
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled default-scheduler Successfully assigned default/nginx-deployment-6b474476c4-x8stz to vms22.rhce.cc
Normal Pulled 6m58s kubelet, vms22.rhce.cc Container image "nginx:1.14.2" already present on machine
Normal Created 6m58s kubelet, vms22.rhce.cc Created container nginx
Normal Started 6m57s kubelet, vms22.rhce.cc Started container nginx
![8bf23c1bcb10ba6ae3ac3c53713ca711.png](https://i-blog.csdnimg.cn/blog_migrate/f86155646038c016e9824fbd07e00036.png)
创建Service提供对外访问的接口
piVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
app: nginx
spec:
ports:
- port: 88
targetPort: 80
selector:
app: nginx
创建service
[root@vms20 pod]# kubectl apply -f nginx.yaml
service/nginx-service created
查看service(访问Pod是有负载均衡的)
[root@vms20 pod]# kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-service ClusterIP 10.102.87.171 88/TCP 12s
[root@vms20 pod]# curl 10.102.87.171:88
Welcome to nginx!
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
Commercial support is available at
Thank you for using nginx.
对service的描述
[root@vms20 pod]# kubectl describe service nginx-service
Name: nginx-service
Namespace: default
Labels: app=nginx
Annotations: Selector: app=nginx
Type: ClusterIP
IP: 10.102.87.171
Port: 88/TCP
TargetPort: 80/TCP
Endpoints: 10.244.7.13:80,10.244.7.14:80,10.244.7.15:80
Session Affinity: None
Events:
![8bf23c1bcb10ba6ae3ac3c53713ca711.png](https://i-blog.csdnimg.cn/blog_migrate/f86155646038c016e9824fbd07e00036.png)
附上一个具体的YAML解释文件
# yaml格式的pod定义文件完整内容:
apiVersion: v1 #必选,版本号,例如v1
kind: Pod #必选,Pod
metadata: #必选,元数据
name: string #必选,Pod名称
namespace: string #必选,Pod所属的命名空间
labels: #自定义标签
- name: string #自定义标签名字
annotations: #自定义注释列表
- name: string
spec: #必选,Pod中容器的详细定义
containers: #必选,Pod中容器列表
- name: string #必选,容器名称
image: string #必选,容器的镜像名称
imagePullPolicy: [Always | Never | IfNotPresent] #获取镜像的策略 Alawys表示下载镜像 IfnotPresent表示优先使用本地镜像,否则下载镜像,Nerver表示仅使用本地镜像
command: [string] #容器的启动命令列表,如不指定,使用打包时使用的启动命令
args: [string] #容器的启动命令参数列表
workingDir: string #容器的工作目录
volumeMounts: #挂载到容器内部的存储卷配置
- name: string #引用pod定义的共享存储卷的名称,需用volumes[]部分定义的的卷名
mountPath: string #存储卷在容器内mount的绝对路径,应少于512字符
readOnly: boolean #是否为只读模式
ports: #需要暴露的端口库号列表
- name: string #端口号名称
containerPort: int #容器需要监听的端口号
hostPort: int #容器所在主机需要监听的端口号,默认与Container相同
protocol: string #端口协议,支持TCP和UDP,默认TCP
env: #容器运行前需设置的环境变量列表
- name: string #环境变量名称
value: string #环境变量的值
resources: #资源限制和请求的设置
limits: #资源限制的设置
cpu: string #Cpu的限制,单位为core数,将用于docker run --cpu-shares参数
memory: string #内存限制,单位可以为Mib/Gib,将用于docker run --memory参数
requests: #资源请求的设置
cpu: string #Cpu请求,容器启动的初始可用数量
memory: string #内存清楚,容器启动的初始可用数量
livenessProbe: #对Pod内个容器健康检查的设置,当探测无响应几次后将自动重启该容器,检查方法有exec、httpGet和tcpSocket,对一个容器只需设置其中一种方法即可
exec: #对Pod容器内检查方式设置为exec方式
command: [string] #exec方式需要制定的命令或脚本
httpGet: #对Pod内个容器健康检查方法设置为HttpGet,需要制定Path、port
path: string
port: number
host: string
scheme: string
HttpHeaders:
- name: string
value: string
tcpSocket: #对Pod内个容器健康检查方式设置为tcpSocket方式
port: number
initialDelaySeconds: 0 #容器启动完成后首次探测的时间,单位为秒
timeoutSeconds: 0 #对容器健康检查探测等待响应的超时时间,单位秒,默认1秒
periodSeconds: 0 #对容器监控检查的定期探测时间设置,单位秒,默认10秒一次
successThreshold: 0
failureThreshold: 0
securityContext:
privileged:false
restartPolicy: [Always | Never | OnFailure]#Pod的重启策略,Always表示一旦不管以何种方式终止运行,kubelet都将重启,OnFailure表示只有Pod以非0退出码退出才重启,Nerver表示不再重启该Pod
nodeSelector: obeject #设置NodeSelector表示将该Pod调度到包含这个label的node上,以key:value的格式指定
imagePullSecrets: #Pull镜像时使用的secret名称,以key:secretkey格式指定
- name: string
hostNetwork:false #是否使用主机网络模式,默认为false,如果设置为true,表示使用宿主机网络
volumes: #在该pod上定义共享存储卷列表
- name: string #共享存储卷名称 (volumes类型有很多种)
emptyDir: {} #类型为emtyDir的存储卷,与Pod同生命周期的一个临时目录。为空值
hostPath: string #类型为hostPath的存储卷,表示挂载Pod所在宿主机的目录
path: string #Pod所在宿主机的目录,将被用于同期中mount的目录
secret: #类型为secret的存储卷,挂载集群与定义的secre对象到容器内部
scretname: string
items:
- key: string
path: string
configMap: #类型为configMap的存储卷,挂载预定义的configMap对象到容器内部
name: string
items:
- key:
![491a4d49b206afe4addb82ad70a0a287.png](https://i-blog.csdnimg.cn/blog_migrate/d4078630dc675799979104e56c31376f.png)