Kubernetes 使用 Pod 来管理容器,每个 Pod 可以包含一个或多个紧密关联的容器。 Pod 是一组紧密关联的容器集合,它们共享 PID、IPC、Network 和 UTS namespace,是 Kubernetes 调度的基本单位。Pod 内的多个容器共享网络和文件系统,可以通过进程间通信和文件共享这种简单高效的方式组合完成服务
一个pod的yaml文件
apiVersion: 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: string path: string
其中比较值得说明有这么几个点
{ "insecure-registries":["http://your-harbor-url"] }
然后重启docker
systemctl dadmon-reload systemctl restart docker
一个Deployment的yaml文件(限于篇幅,很多内容已省略)
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: string #Deployment名称 spec: replicas: 3 #目标副本数量 strategy: rollingUpdate: maxSurge: 1 #滚动升级时最大同时升级1个pod maxUnavailable: 1 #滚动升级时最大允许不可用的pod个数 template: metadata: labels: app: string #模板名称 sepc: #定义容器模板,该模板可以包含多个容器 containers: - name: string image: string ports: - name: http containerPort: 8080 #对service暴露端口
k8s如何滚动升级和回滚应用
进行滚动升级的时候先在yaml文件中更新镜像的版本,然后根据设置需求设置maxSurge、和maxUnavailable的值即可完成
k8s如何完成扩容和缩容
修改replicas的值后重新发布即可
apiVersion: v1 kind: Service matadata: #元数据 name: string #service的名称 namespace: string #命名空间 labels: #自定义标签属性列表 - name: string annotations: #自定义注解属性列表 - name: string spec: #详细描述 selector: [] #label selector配置,将选择具有label标签的Pod作为管理 范围 type: string #service的类型,指定service的访问方式,默认为clusterIp clusterIP: string #虚拟服务地址 sessionAffinity: string #是否支持session ports: #service需要暴露的端口列表 - name: string #端口名称 protocol: string #端口协议,支持TCP和UDP,默认TCP port: int #服务监听的端口号 targetPort: int #需要转发到后端Pod的端口号 nodePort: int #当type = NodePort时,指定映射到物理机的端口号 status: #当spce.type=LoadBalancer时,设置外部负载均衡器的地址 loadBalancer: #外部负载均衡器 ingress: #外部负载均衡器 ip: string #外部负载均衡器的Ip地址值 hostname: string #外部负载均衡器的主机名
deploy控制RS,RS控制Pod,这一整套,向外提供稳定可靠的Service。
分析
以下是分析的过程
首先,我们先从最小的调度单位pod开始。
我的k8s集群中目前有一个pod,它的name为mq-svc-5b96bf78d9-brpjw
[root@VM_0_17_centos ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
mq-svc-5b96bf78d9-brpjw 1/1 Running 0 51m
来看一下它的详情
[root@VM_0_17_centos ~]# kubectl describe pod mq-svc-5b96bf78d9-brpjw
Name: mq-svc-5b96bf78d9-brpjw
Namespace: default
Node: 10.0.0.17/10.0.0.17
Start Time: Fri, 17 Aug 2018 17:24:44 +0800
Labels: pod-template-hash=1652693485
qcloud-app=mq-svc
Annotations:
Status: Running
IP: 172.16.0.39
Controlled By: ReplicaSet/mq-svc-5b96bf78d9
Containers:
queue-mq:
Container ID: docker://700cdc55c111a413faaa8cabb8680009d2663701ccbe84b8a50ea6e6fe1d538c
Image: rabbitmq:management
Image ID: docker-pullable://rabbitmq@sha256:0b36ea1a8df9e53228aaeee277680de2cc97c7d675bc2d5dbe1cc9e3836a9d9f
Port:
Host Port:
State: Running
Started: Fri, 17 Aug 2018 17:24:49 +0800
Ready: True
Restart Count: 0
Limits:
cpu: 500m
memory: 1Gi
Requests:
cpu: 250m
memory: 256Mi
Environment:
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-vzhz4 (ro)
Conditions:
Type Status
Initialized True
Ready True
PodScheduled True
Volumes:
default-token-vzhz4:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-vzhz4
Optional: false
QoS Class: Burstable
Node-Selectors:
Tolerations:
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 51m default-scheduler Successfully assigned mq-svc-5b96bf78d9-brpjw to 10.0.0.17
Normal SuccessfulMountVolume 51m kubelet, 10.0.0.17 MountVolume.SetUp succeeded for volume "default-token-vzhz4"
Normal Pulling 51m kubelet, 10.0.0.17 pulling image "rabbitmq:management"
Normal Pulled 51m kubelet, 10.0.0.17 Successfully pulled image "rabbitmq:management"
Normal Created 51m kubelet, 10.0.0.17 Created container
Normal Started 51m kubelet, 10.0.0.17 Started container
其实有一个敏感的信息,pod是被一个名字为mq-svc-5b96bf78d9的ReplicaSet管理的,所以我们认为,RS是比Pod高一级别的专门用来管理pod的组件。一个RS会管理一批pod。
Controlled By: ReplicaSet/mq-svc-5b96bf78d9
而且在pod里面发生的实践也都是对容器的操作,比如拉取镜像、启动容器等等
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 51m default-scheduler Successfully assigned mq-svc-5b96bf78d9-brpjw to 10.0.0.17
Normal SuccessfulMountVolume 51m kubelet, 10.0.0.17 MountVolume.SetUp succeeded for volume "default-token-vzhz4"
Normal Pulling 51m kubelet, 10.0.0.17 pulling image "rabbitmq:management"
Normal Pulled 51m kubelet, 10.0.0.17 Successfully pulled image "rabbitmq:management"
Normal Created 51m kubelet, 10.0.0.17 Created container
Normal Started 51m kubelet, 10.0.0.17 Started container
接下来,我们就看看这个RS的详细情况
[root@VM_0_17_centos ~]# kubectl describe rs mq-svc-5b96bf78d9
Name: mq-svc-5b96bf78d9
Namespace: default
Selector: pod-template-hash=1652693485,qcloud-app=mq-svc
Labels: pod-template-hash=1652693485
qcloud-app=mq-svc
Annotations: deployment.changecourse=Updating
deployment.kubernetes.io/desired-replicas=1
deployment.kubernetes.io/max-replicas=2
deployment.kubernetes.io/revision=2
description=Service based on rabbitmq.
Controlled By: Deployment/mq-svc
Replicas: 1 current / 1 desired
Pods Status: 1 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: pod-template-hash=1652693485
qcloud-app=mq-svc
Containers:
queue-mq:
Image: rabbitmq:management
Port:
Host Port:
Limits:
cpu: 500m
memory: 1Gi
Requests:
cpu: 250m
memory: 256Mi
Environment:
Mounts:
Volumes:
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 50m replicaset-controller Created pod: mq-svc-5b96bf78d9-r8n8t
Normal SuccessfulCreate 49m replicaset-controller Created pod: mq-svc-5b96bf78d9-l4zj2
Normal SuccessfulCreate 49m replicaset-controller Created pod: mq-svc-5b96bf78d9-m8tmv
Normal SuccessfulDelete 49m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-m8tmv
Normal SuccessfulCreate 49m replicaset-controller Created pod: mq-svc-5b96bf78d9-r9wkj
Normal SuccessfulCreate 49m replicaset-controller Created pod: mq-svc-5b96bf78d9-8wzpq
Normal SuccessfulCreate 49m replicaset-controller Created pod: mq-svc-5b96bf78d9-d8gwc
Normal SuccessfulDelete 49m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-d8gwc
Normal SuccessfulDelete 49m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-8wzpq
Normal SuccessfulDelete 49m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-l4zj2
Normal SuccessfulDelete 49m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-r9wkj
Normal SuccessfulDelete 45m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-r8n8t
关键信息
Controlled By: Deployment/mq-svc
这个RS被名字为mq-svc的Deployment控制,这样看,Deployment是比RS高一级别用于管理RS的组件。
在RS级别上发生的事件,均是对pod的操作,创建pod,删除pod
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 50m replicaset-controller Created pod: mq-svc-5b96bf78d9-r8n8t
Normal SuccessfulCreate 49m replicaset-controller Created pod: mq-svc-5b96bf78d9-l4zj2
Normal SuccessfulCreate 49m replicaset-controller Created pod: mq-svc-5b96bf78d9-m8tmv
Normal SuccessfulDelete 49m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-m8tmv
Normal SuccessfulCreate 49m replicaset-controller Created pod: mq-svc-5b96bf78d9-r9wkj
Normal SuccessfulCreate 49m replicaset-controller Created pod: mq-svc-5b96bf78d9-8wzpq
Normal SuccessfulCreate 49m replicaset-controller Created pod: mq-svc-5b96bf78d9-d8gwc
Normal SuccessfulDelete 49m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-d8gwc
Normal SuccessfulDelete 49m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-8wzpq
Normal SuccessfulDelete 49m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-l4zj2
Normal SuccessfulDelete 49m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-r9wkj
Normal SuccessfulDelete 45m replicaset-controller Deleted pod: mq-svc-5b96bf78d9-r8n8t
接下来,我们来看看Delpoyment
[root@VM_0_17_centos ~]# kubectl describe deploy mq-svc
Name: mq-svc
Namespace: default
CreationTimestamp: Fri, 17 Aug 2018 17:21:13 +0800
Labels: qcloud-app=mq-svc
Annotations: deployment.changecourse=Updating
deployment.kubernetes.io/revision=2
description=Service based on rabbitmq.
Selector: qcloud-app=mq-svc
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 10
RollingUpdateStrategy: 0 max unavailable, 1 max surge
Pod Template:
Labels: qcloud-app=mq-svc
Containers:
queue-mq:
Image: rabbitmq:management
Port:
Host Port:
Limits:
cpu: 500m
memory: 1Gi
Requests:
cpu: 250m
memory: 256Mi
Environment:
Mounts:
Volumes:
Conditions:
Type Status Reason
---- ------ ------
Progressing True NewReplicaSetAvailable
Available True MinimumReplicasAvailable
OldReplicaSets:
NewReplicaSet: mq-svc-5b96bf78d9 (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 58m deployment-controller Scaled up replica set mq-svc-5b96bf78d9 to 2
Normal ScalingReplicaSet 57m deployment-controller Scaled up replica set mq-svc-5b96bf78d9 to 3
Normal ScalingReplicaSet 57m deployment-controller Scaled up replica set mq-svc-5b96bf78d9 to 4
Normal ScalingReplicaSet 57m deployment-controller Scaled down replica set mq-svc-5b96bf78d9 to 3
Normal ScalingReplicaSet 57m deployment-controller Scaled up replica set mq-svc-5b96bf78d9 to 6
Normal ScalingReplicaSet 57m deployment-controller Scaled down replica set mq-svc-5b96bf78d9 to 4
Normal ScalingReplicaSet 56m deployment-controller Scaled down replica set mq-svc-5b96bf78d9 to 2
Normal ScalingReplicaSet 53m deployment-controller Scaled down replica set mq-svc-5b96bf78d9 to 1
可以看出,在deployment级别上,不再受其他组件的控制,而他的状态的转变是作为API被调用而产生的。我们看到,在deployment级别上发生的事件一般是创建服务、滚动升级一个服务,或者是操作RS伸缩Pod集群。
最后,到这里也很明白了,service其实是在这一整套基础之上提供给外部的稳定的服务。