创建一个deployment
[root@k8s-master ~]# cat nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx-test
template:
metadata:
labels:
app: nginx-test
spec:
containers:
- name: nginx
image: nginx:1.16.1
ports:
- containerPort: 80
[root@k8s-master ~]# kubectl apply -f nginx.yaml
[root@k8s-master ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 3/3 3 3 3h41m
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-5cdd4f799d-2jz9c 1/1 Running 0 3h41m
nginx-deployment-5cdd4f799d-bxszf 1/1 Running 0 3h41m
nginx-deployment-5cdd4f799d-drktr 1/1 Running 0 3h41m
[root@k8s-master ~]# kubectl get pods --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-deployment-5cdd4f799d-2jz9c 1/1 Running 0 3h43m app=nginx-test,pod-template-hash=5cdd4f799d
nginx-deployment-5cdd4f799d-bxszf 1/1 Running 0 3h43m app=nginx-test,pod-template-hash=5cdd4f799d
nginx-deployment-5cdd4f799d-drktr 1/1 Running 0 3h43m app=nginx-test,pod-template-hash=5cdd4f799d
[root@k8s-master ~]# kubectl get pods --show-labels -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
nginx-deployment-5cdd4f799d-2jz9c 1/1 Running 0 3h45m 10.244.1.18 k8s-node1 <none> <none> app=nginx-test,pod-template-hash=5cdd4f799d
nginx-deployment-5cdd4f799d-bxszf 1/1 Running 0 3h45m 10.244.1.17 k8s-node1 <none> <none> app=nginx-test,pod-template-hash=5cdd4f799d
nginx-deployment-5cdd4f799d-drktr 1/1 Running 0 3h45m 10.244.2.18 k8s-node2 <none> <none> app=nginx-test,pod-template-hash=5cdd4f799d
或者:kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=88 --type=NodePort --target-port=80 --name=nginx-service
创建service
[root@k8s-master ~]# cat service.yml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
app: nginx-test
spec:
ports:
- port: 88
targetPort: 80
selector:
app: nginx-test
[root@k8s-master ~]# kubectl apply -f service.yml
service/nginx-service created
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 5d5h
nginx-service ClusterIP 10.1.65.177 <none> 88/TCP 75s
访问服务
[root@k8s-master ~]# curl 10.1.65.177:88
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
暴露到外部访问
修改Service为NodePort类型
type: ClusterIP 将 ClusterIP 修改为NodePort
[root@k8s-master ~]# kubectl edit svc nginx-service
找到下面的行
spec:
clusterIP: 10.1.65.177
ports:
- port: 88
protocol: TCP
targetPort: 80
selector:
app: nginx-test
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}
修改为
spec:
clusterIP: 10.1.65.177
externalTrafficPolicy: Cluster
ports:
- nodePort: 30002
port: 88
protocol: TCP
targetPort: 80
selector:
app: nginx-test
sessionAffinity: None
type: NodePort
status:
loadBalancer: {}
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.0.0.1 <none> 443/TCP 5d5h
nginx-service NodePort 10.1.65.177 <none> 88:30002/TCP 13m
[root@k8s-master ~]# kubectl describe svc nginx-service
Name: nginx-service
Namespace: default
Labels: app=nginx-test
Annotations: Selector: app=nginx-test
Type: NodePort
IP: 10.1.65.177
Port: <unset> 88/TCP
TargetPort: 80/TCP
NodePort: <unset> 30002/TCP
Endpoints: 10.244.1.21:80,10.244.2.22:80,10.244.2.23:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
主机ip+端口访问
image.png
扩容nginx应用
[root@k8s-master ~]# kubectl scale deployment nginx-deployment --replicas 5
deployment.apps/nginx-deployment scaled
[root@k8s-master ~]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 5/5 5 5 4h12m
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-5cdd4f799d-2jz9c 1/1 Running 0 4h13m 10.244.1.18 k8s-node1 <none> <none>
nginx-deployment-5cdd4f799d-2qtps 1/1 Running 0 14s 10.244.2.19 k8s-node2 <none> <none>
nginx-deployment-5cdd4f799d-bxszf 1/1 Running 0 4h13m 10.244.1.17 k8s-node1 <none> <none>
nginx-deployment-5cdd4f799d-d8k2p 1/1 Running 0 14s 10.244.1.19 k8s-node1 <none> <none>
nginx-deployment-5cdd4f799d-drktr 1/1 Running 0 4h13m 10.244.2.18 k8s-node2 <none> <none>
[root@k8s-master ~]# kubectl describe pods nginx-deployment-5cdd4f799d-2qtps
Name: nginx-deployment-5cdd4f799d-2qtps
Namespace: default
Priority: 0
Node: k8s-node2/192.168.2.38
Start Time: Tue, 20 Oct 2020 15:19:44 +0800
Labels: app=nginx-test
pod-template-hash=5cdd4f799d
Annotations: <none>
Status: Running
IP: 10.244.2.19
IPs:
IP: 10.244.2.19
Controlled By: ReplicaSet/nginx-deployment-5cdd4f799d
Containers:
nginx:
Container ID: docker://761717fb01f0bb7f0e24d5eb1ae64d4a3f666119332082ef92611e81863b5881
Image: nginx:1.16.1
Image ID: docker-pullable://nginx@sha256:d20aa6d1cae56fd17cd458f4807e0de462caf2336f0b70b5eeb69fcaaf30dd9c
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Tue, 20 Oct 2020 15:19:45 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-cfllf (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-cfllf:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-cfllf
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 104s default-scheduler Successfully assigned default/nginx-deployment-5cdd4f799d-2qtps to k8s-node2
Normal Pulled 103s kubelet, k8s-node2 Container image "nginx:1.16.1" already present on machine
Normal Created 103s kubelet, k8s-node2 Created container nginx
Normal Started 103s kubelet, k8s-node2 Started container nginx
[root@k8s-master ~]# kubectl describe deployment nginx-deployment
Name: nginx-deployment
Namespace: default
CreationTimestamp: Tue, 20 Oct 2020 11:06:56 +0800
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx-test
Replicas: 5 desired | 5 updated | 5 total | 5 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx-test
Containers:
nginx:
Image: nginx:1.16.1
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Progressing True NewReplicaSetAvailable
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-deployment-5cdd4f799d (5/5 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 3m40s deployment-controller Scaled up replica set nginx-deployment-5cdd4f799d to 5
缩容nginx应用
[root@k8s-master ~]# kubectl scale deployment nginx-deployment --replicas 3
deployment.apps/nginx-deployment scaled
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-5cdd4f799d-2jz9c 1/1 Running 0 4h18m 10.244.1.18 k8s-node1 <none> <none>
nginx-deployment-5cdd4f799d-2qtps 1/1 Running 0 5m26s 10.244.2.19 k8s-node2 <none> <none>
nginx-deployment-5cdd4f799d-bxszf 0/1 Terminating 0 4h18m 10.244.1.17 k8s-node1 <none> <none>
nginx-deployment-5cdd4f799d-d8k2p 0/1 Terminating 0 5m26s 10.244.1.19 k8s-node1 <none> <none>
nginx-deployment-5cdd4f799d-drktr 1/1 Running 0 4h18m 10.244.2.18 k8s-node2 <none> <none>
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-5cdd4f799d-2jz9c 1/1 Running 0 4h19m 10.244.1.18 k8s-node1 <none> <none>
nginx-deployment-5cdd4f799d-2qtps 1/1 Running 0 6m36s 10.244.2.19 k8s-node2 <none> <none>
nginx-deployment-5cdd4f799d-drktr 1/1 Running 0 4h19m 10.244.2.18 k8s-node2 <none> <none>
[root@k8s-master ~]# kubectl describe deployment nginx-deployment
Name: nginx-deployment
Namespace: default
CreationTimestamp: Tue, 20 Oct 2020 11:06:56 +0800
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx-test
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx-test
Containers:
nginx:
Image: nginx:1.16.1
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Progressing True NewReplicaSetAvailable
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-deployment-5cdd4f799d (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 5m57s deployment-controller Scaled up replica set nginx-deployment-5cdd4f799d to 5
Normal ScalingReplicaSet 36s deployment-controller Scaled down replica set nginx-deployment-5cdd4f799d to 3
更新镜像
kubectl set image deployment/nginx-deployment nginx=nginx:1.18.0
or
kubectl edit deployment/nginx-deployment
[root@k8s-master ~]# kubectl edit deployment nginx-deployment
找到
spec:
containers:
- image: nginx:1.16.1
修改为
spec:
containers:
- image: nginx:1.18.0
[root@k8s-master ~]# kubectl edit deployment nginx-deployment
deployment.apps/nginx-deployment edited
[root@k8s-master ~]# kubectl rollout status deployment nginx-deployment #查看滚动更新过程
deployment "nginx-deployment" successfully rolled out
[root@k8s-master ~]# kubectl get pods -o wide #更新中
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-5cdd4f799d-2jz9c 1/1 Running 0 4h23m 10.244.1.18 k8s-node1 <none> <none>
nginx-deployment-5cdd4f799d-2qtps 1/1 Running 0 11m 10.244.2.19 k8s-node2 <none> <none>
nginx-deployment-5cdd4f799d-drktr 1/1 Running 0 4h23m 10.244.2.18 k8s-node2 <none> <none>
nginx-deployment-ffcfbb9f5-tb8r7 0/1 ContainerCreating 0 3s <none> k8s-node1 <none> <none>
[root@k8s-master ~]# kubectl get pods -o wide #更新完成
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-ffcfbb9f5-bbc86 1/1 Running 0 17s 10.244.2.20 k8s-node2 <none> <none>
nginx-deployment-ffcfbb9f5-r859g 1/1 Running 0 12s 10.244.2.21 k8s-node2 <none> <none>
nginx-deployment-ffcfbb9f5-tb8r7 1/1 Running 0 29s 10.244.1.20 k8s-node1 <none> <none>
Name: nginx-deployment
Namespace: default
CreationTimestamp: Tue, 20 Oct 2020 11:06:56 +0800
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 2
Selector: app=nginx-test
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx-test
Containers:
nginx:
Image: nginx:1.18.0
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-deployment-ffcfbb9f5 (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 16m deployment-controller Scaled up replica set nginx-deployment-5cdd4f799d to 5
Normal ScalingReplicaSet 10m deployment-controller Scaled down replica set nginx-deployment-5cdd4f799d to 3
Normal ScalingReplicaSet 5m6s deployment-controller Scaled up replica set nginx-deployment-ffcfbb9f5 to 1
Normal ScalingReplicaSet 4m54s deployment-controller Scaled down replica set nginx-deployment-5cdd4f799d to 2
Normal ScalingReplicaSet 4m54s deployment-controller Scaled up replica set nginx-deployment-ffcfbb9f5 to 2
Normal ScalingReplicaSet 4m49s deployment-controller Scaled down replica set nginx-deployment-5cdd4f799d to 1
Normal ScalingReplicaSet 4m49s deployment-controller Scaled up replica set nginx-deployment-ffcfbb9f5 to 3
Normal ScalingReplicaSet 4m48s deployment-controller Scaled down replica set nginx-deployment-5cdd4f799d to 0
[root@k8s-master ~]# kubectl describe pods nginx-deployment-ffcfbb9f5-bbc86 #更新完成后,查看镜像版本
Name: nginx-deployment-ffcfbb9f5-bbc86
Namespace: default
Priority: 0
Node: k8s-node2/192.168.2.38
Start Time: Tue, 20 Oct 2020 15:31:04 +0800
Labels: app=nginx-test
pod-template-hash=ffcfbb9f5
Annotations: <none>
Status: Running
IP: 10.244.2.20
IPs:
IP: 10.244.2.20
Controlled By: ReplicaSet/nginx-deployment-ffcfbb9f5
Containers:
nginx:
Container ID: docker://79a6cc30a58a7418ad1949ee044671aff2959861d28b11a740690ca919d263b7
Image: nginx:1.18.0
Image ID: docker-pullable://nginx@sha256:6e4fc428f9f25f1914e43dc2e75ff3be574141734509111282a3a050a420d94c
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Tue, 20 Oct 2020 15:31:09 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-cfllf (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-cfllf:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-cfllf
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 5m45s default-scheduler Successfully assigned default/nginx-deployment-ffcfbb9f5-bbc86 to k8s-node2
Normal Pulling 5m45s kubelet, k8s-node2 Pulling image "nginx:1.18.0"
Normal Pulled 5m40s kubelet, k8s-node2 Successfully pulled image "nginx:1.18.0"
Normal Created 5m40s kubelet, k8s-node2 Created container nginx
Normal Started 5m40s kubelet, k8s-node2 Started container nginx
回滚
[root@k8s-master ~]# kubectl rollout undo deployment/nginx-deployment
deployment.apps/nginx-deployment rolled back
[root@k8s-master ~]# kubectl rollout status deployment nginx-deployment
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 2 out of 3 new replicas have been updated...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "nginx-deployment" rollout to finish: 1 old replicas are pending termination...
deployment "nginx-deployment" successfully rolled out
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-deployment-5cdd4f799d-bfnrh 1/1 Running 0 24s 10.244.2.22 k8s-node2 <none> <none>
nginx-deployment-5cdd4f799d-fj2m9 1/1 Running 0 25s 10.244.1.21 k8s-node1 <none> <none>
nginx-deployment-5cdd4f799d-p49rq 1/1 Running 0 22s 10.244.2.23 k8s-node2 <none> <none>
[root@k8s-master ~]# kubectl describe pods nginx-deployment-5cdd4f799d-bfnrh
Name: nginx-deployment-5cdd4f799d-bfnrh
Namespace: default
Priority: 0
Node: k8s-node2/192.168.2.38
Start Time: Tue, 20 Oct 2020 15:47:55 +0800
Labels: app=nginx-test
pod-template-hash=5cdd4f799d
Annotations: <none>
Status: Running
IP: 10.244.2.22
IPs:
IP: 10.244.2.22
Controlled By: ReplicaSet/nginx-deployment-5cdd4f799d
Containers:
nginx:
Container ID: docker://a0f2d26b02dfd64d3ecb2d0880089a5affd655c3b87152d5c1b505a8c550314f
Image: nginx:1.16.1
Image ID: docker-pullable://nginx@sha256:d20aa6d1cae56fd17cd458f4807e0de462caf2336f0b70b5eeb69fcaaf30dd9c
Port: 80/TCP
Host Port: 0/TCP
State: Running
Started: Tue, 20 Oct 2020 15:47:56 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-cfllf (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-cfllf:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-cfllf
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 <unknown> default-scheduler Successfully assigned default/nginx-deployment-5cdd4f799d-bfnrh to k8s-node2
Normal Pulled 113s kubelet, k8s-node2 Container image "nginx:1.16.1" already present on machine
Normal Created 113s kubelet, k8s-node2 Created container nginx
Normal Started 113s kubelet, k8s-node2 Started container nginx
回滚到指定版本
[root@k8s-master ~]# kubectl rollout history deployment/nginx-deployment #查看历史版本
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 <none>
2 <none>
[root@k8s-master ~]# kubectl rollout undo deployment/nginx-deployment --to-revision=2 ## 可以使用 --revision参数指定
[root@k8s-master ~]# kubectl rollout pause deployment/nginx-deployment ## 暂停 deployment 的更新
record参数
--record参数可以记录命令,我们可以很方便的查看每次 revision 的变化
[root@k8s-master ~]# kubectl apply -f nginx.yaml --record
deployment.apps/nginx-deployment created
[root@k8s-master ~]# kubectl rollout history deployment/nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 kubectl apply --filename=nginx.yaml --record=true
[root@k8s-master ~]# kubectl set image deployment/nginx-deployment nginx=nginx:1.12.0 --record
deployment.apps/nginx-deployment image updated
[root@k8s-master ~]# kubectl rollout history deployment/nginx-deployment
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
3 <none>
4 <none>
5 kubectl set image deployment/nginx-deployment nginx=nginx:1.12.0 --record=true
删除应用
[root@k8s-master ~]# kubectl delete deployment/nginx-deployment
deployment.apps "nginx-deployment" deleted
[root@k8s-master ~]# kubectl delete svc/nginx-service
service "nginx-service" deleted
故障排查
kubectl describe TYPE NAME_PREFIX #查看描述
kubectl logs pod-name #查看pod的日志
kubectl exec -it pod-name bash #进入pod内部
网友评论