Kubernetes入门实验:deployment

作者注:本文仅为笔者学习记录,不具任何参考意义。

k8s deployment 实验。
注:本文为笔者实验记录,非教程,另会不定时更新。

环境

1
2
3
4
5
# kubectl get node
NAME STATUS ROLES AGE VERSION
edge-node Ready <none> 15m v1.17.0
edge-node2 Ready <none> 16m v1.17.0
ubuntu Ready master 67d v1.17.0

Deployment

技术总结

测试yaml文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# vim nginx-deployment.yaml

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3 # tells deployment to run 2 pods matching the template
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: latelee/lidch
ports:
- containerPort: 80

创建:

1
kubectl create -f nginx-deployment.yaml

查看:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# kubectl get deployments -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx-deployment 3/3 3 3 5m11s nginx latelee/lidch app=nginx

# kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-deployment-55677b6b95 3 3 3 5m41s

# kubectl get pod -l app=nginx
NAME READY STATUS RESTARTS AGE
nginx-deployment-55677b6b95-2khzn 1/1 Running 0 5m48s
nginx-deployment-55677b6b95-2v5kf 1/1 Running 0 5m48s
nginx-deployment-55677b6b95-vpwsn 1/1 Running 0 5m48s

# kubectl rollout status deployment nginx-deployment 或者 kubectl rollout status deployment/nginx-deployment
deployment "nginx-deployment" successfully rolled out

查看:

1
# kubectl describe deployment

暴露端口:

1
kubectl expose deployment nginx-deployment --port 88

TODO:测试未成功。

升级回滚测试

更新文件,再测试,将image: latelee/lidch 改为 image: latelee/lidch:1.1,再执行:

1
kubectl apply -f nginx-deployment.yaml

会自动升级镜像版本(旧的pod被删除,新的pod被创建,半个月后的PS:似乎此法不可行)。
另一个简单的更改镜像版本的命令:

1
kubectl set image deployment/nginx-deployment nginx=latelee/lidch

其中,nginx=latelee/lidch前者为容器名称,后者为镜像名称(及版本)。如果新版本镜像不存在,则自动拉取,耗时较长,反之则短。

验证:

1
# curl 10.244.4.28  // IP为pod的IP

deployment的升级,与rc的不太一样,似乎deployment简单些。

查看历史:

1
2
3
4
5
6
7
8
9
kubectl rollout history deployment/nginx-deployment
输出:
deployment.apps/nginx-deployment
REVISION CHANGE-CAUSE
1 <none>
2 <none>

查看某版本信息:
kubectl rollout history deployment nginx-deployment --revision=1

回滚到1版本:

1
kubectl rollout undo deployment/nginx-deployment --to-revision=1

此时会自动更新pod(旧的删除,新的创建)。
验证:

1
# curl 10.244.4.29  // IP为pod的IP

升级说明:
如果deployment只有一个,则在旧的销毁,新的创建过程中,服务是无法访问的。因此,才会指定多个副本。

自动扩缩容

手动扩容、缩容:

1
2
kubectl scale deployment nginx-deployment --replicas=10
kubectl scale deployment nginx-deployment --replicas=2

自动扩容、缩容:

1
kubectl autoscale deployment nginx-deployment --min=2 --max=15 --cpu-percent=70

再次执行,提示已存在:

1
Error from server (AlreadyExists): horizontalpodautoscalers.autoscaling "nginx-deployment" already exists

TODO 未解决。

问题

1
0/3 nodes are available: 1 node(s) had taints that the pod didn't tolerate, 2 node(s) didn't have free ports for the requested pod ports.

原因:pod已经占用节点端口,无法再启动。