为了满足不同业务场景,k8s开发了Deployment、ReplicaSet、DaemonSet、StatefuleSet、Job 等多种 Controller
运行一个deployments,镜像为nginx,副本数为2
kubectl run nginx --image=nginx --replicas=2
kubectl get deployments.apps nginx
NAME READY UP-TO-DATE AVAILABLE AGE
nginx 2/2 2 2 59s
使用describe查看
kubectl describe deployments.apps nginx
Name: nginx
Namespace: default
CreationTimestamp: Tue, 14 Jul 2020 17:46:01 +0800
Labels: run=nginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: run=nginx
Replicas: 2 desired | 2 updated | 2 total | 2 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: run=nginx
Containers:
nginx:
Image: nginx
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-6db489d4b7 (2/2 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 100s deployment-controller Scaled up replica set nginx-6db489d4b7 to 2
这里告诉我们创建了一个 NewReplicaSet: nginx-6db489d4b7 (2/2 replicas created),证明deoloyments是通过replicaset管理pod
kubectl describe replicasets.apps nginx-6db489d4b7
Name: nginx-6db489d4b7
Namespace: default
Selector: pod-template-hash=6db489d4b7,run=nginx
Labels: pod-template-hash=6db489d4b7
run=nginx
Annotations: deployment.kubernetes.io/desired-replicas: 2
deployment.kubernetes.io/max-replicas: 3
deployment.kubernetes.io/revision: 1
Controlled By: Deployment/nginx
Replicas: 2 current / 2 desired
Pods Status: 2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: pod-template-hash=6db489d4b7
run=nginx
Containers:
nginx:
Image: nginx
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 3m56s replicaset-controller Created pod: nginx-6db489d4b7-ghzd9
Normal SuccessfulCreate 3m56s replicaset-controller Created pod: nginx-6db489d4b7-7f6jb
Controlled By: Deployment/nginx 这里表示是replicasets是由deployment、nginx创建
kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-6db489d4b7-7f6jb 1/1 Running 0 7m18s
nginx-6db489d4b7-ghzd9 1/1 Running 0 7m18s
kubectl describe pod nginx-6db489d4b7-7f6jb
Name: nginx-6db489d4b7-7f6jb
Namespace: default
Priority: 0
Node: node1/192.168.1.20
Start Time: Tue, 14 Jul 2020 17:46:01 +0800
Labels: pod-template-hash=6db489d4b7
run=nginx
Annotations: <none>
Status: Running
IP: 10.244.1.19
IPs:
IP: 10.244.1.19
Controlled By: ReplicaSet/nginx-6db489d4b7
Containers:
nginx:
Container ID: docker://277b9ca9091bd2690c0524ef8a74696aa7bfad8f3f4ccf56062c221277c00894
Image: nginx
Image ID: docker-pullable://nginx@sha256:a93c8a0b0974c967aebe868a186e5c205f4d3bcb5423a56559f2f9599074bbcd
Port: <none>
Host Port: <none>
State: Running
Started: Tue, 14 Jul 2020 17:46:12 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-hvq4p (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-hvq4p:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-hvq4p
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 7m26s default-scheduler Successfully assigned default/nginx-6db489d4b7-7f6jb to node1
Normal Pulling 7m26s kubelet, node1 Pulling image "nginx"
Normal Pulled 7m15s kubelet, node1 Successfully pulled image "nginx"
Normal Created 7m15s kubelet, node1 Created container nginx
Normal Started 7m15s kubelet, node1 Started container nginx
Controlled By: ReplicaSet/nginx-6db489d4b7 表示是由ReplicaSet/nginx-6db489d4b7创建pod
vim nginx.yml
apiVersion: apps/v1 ##文件格式版本
kind: Deployment ##资源类型
metadata: ##资源的元数据
labels: ##标签
app: nginx
name: nginx ##名称
spec: ##规格
replicas: 3 ##副本数
selector:
matchLabels:
app: nginx
template: ##pod模板
metadata: ##pod元数据
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx ##pod使用的镜像
name: nginx
运行yaml
kubectl apply -f nginx.yml
查看
kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-86c57db685-4579k 0/1 ContainerCreating 0 14s <none> node1 <none> <none>
nginx-86c57db685-psc7m 0/1 ContainerCreating 0 14s <none> node2 <none> <none>
nginx-86c57db685-ssz2p 0/1 ContainerCreating 0 14s <none> node1 <none> <none>
上个yaml文件中初始副本数为3个,在文件中直接修改副本数
vim nginx.yml
修改 replicas: 5
再次执行命令
kubectl apply -f nginx.yml
deployment.apps/nginx configured
查看
kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-86c57db685-4579k 1/1 Running 0 4m15s 10.244.1.21 node1 <none> <none>
nginx-86c57db685-8tv42 1/1 Running 0 47s 10.244.3.18 node2 <none> <none>
nginx-86c57db685-psc7m 1/1 Running 0 4m15s 10.244.3.17 node2 <none> <none>
nginx-86c57db685-ssz2p 1/1 Running 0 4m15s 10.244.1.20 node1 <none> <none>
nginx-86c57db685-wl4tr 1/1 Running 0 47s 10.244.3.19 node2 <none> <none>
缩减副本数为2个再次查看
vim nginx.yml
修改 replicas: 2
重新执行命令
kubectl apply -f nginx.yml
deployment.apps/nginx configured
查看
kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-86c57db685-4579k 1/1 Running 0 6m 10.244.1.21 node1 <none> <none>
nginx-86c57db685-ssz2p 1/1 Running 0 6m 10.244.1.20 node1 <none> <none>
当前有两个副本运行在node1上,当我们把node1关闭掉之后,在等待一段时间之后,kubernetes会检查到node1不可用,重新在node2上创建pod维持副本数
kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-86c57db685-4579k 1/1 Terminating 0 15m 10.244.1.21 node1 <none> <none>
nginx-86c57db685-7p6gz 1/1 Running 0 90s 10.244.3.20 node2 <none> <none>
nginx-86c57db685-8fxps 1/1 Running 0 90s 10.244.3.21 node2 <none> <none>
nginx-86c57db685-ssz2p 1/1 Terminating 0 15m 10.244.1.20 node1 <none> <none>
重新启动node1后,状态为Terminating的pod会被删除,但是已经运行的pod不会回到node1上
kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready master 5d23h v1.17.0
node1 Ready <none> 5d23h v1.17.0
node2 Ready <none> 5d21h v1.17.0
kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-86c57db685-7p6gz 1/1 Running 0 3m18s 10.244.3.20 node2 <none> <none>
nginx-86c57db685-8fxps 1/1 Running 0 3m18s 10.244.3.21 node2 <none> <none>
默认配置下,Scheduler会将pod调度到所有可用的Node上,如果需要控制pod位置则需要创建label的方式来控制
先删除之前的pod
kubectl delete -f nginx.yml
创建label
kubectl label nodes node1 disktype=ssd
查看label
kubectl get nodes --show-labels
node1 Ready <none> 5d23h v1.17.0 beta.kubernetes.io/arch=amd64,beta.kubernetes.io/os=linux,disktype=ssd,kubernetes.io/arch=amd64,kubernetes.io/hostname=node1,kubernetes.io/os=linux
修改yaml文件
在pod模块中加入nodeSelector并且把副本集数量添加到5
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
app: nginx
name: nginx
spec:
replicas: 5
selector:
matchLabels:
app: nginx
template:
metadata:
creationTimestamp: null
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
nodeSelector:
disktype: ssd
执行yml
kubectl apply -f nginx.yml
deployment.apps/nginx created
查看
kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-66fcf948c9-hxgdc 1/1 Running 0 85s 10.244.1.25 node1 <none> <none>
nginx-66fcf948c9-ls268 1/1 Running 0 85s 10.244.1.22 node1 <none> <none>
nginx-66fcf948c9-r74rv 1/1 Running 0 85s 10.244.1.24 node1 <none> <none>
nginx-66fcf948c9-rplwm 1/1 Running 0 85s 10.244.1.23 node1 <none> <none>
nginx-66fcf948c9-x6ck2 1/1 Running 0 85s 10.244.1.26 node1 <none> <none>
可以看到5个副本均在node1节点上运行