k8s deployment(无状态服务)
【代码】k8s deployment(无状态服务)
·
创建nginx的描述文件
# 基于deployment创建3个pod,加上标签。
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx:latest
name: container-0
resources:
limits:
cpu: 100m
memory: 200Mi
requests:
cpu: 100m
memory: 200Mi
创建并记录版本
# 基于上述yaml描述文件创建pod
kubectl create -f nginx-pod.yml --record
Flag --record has been deprecated, --record will be removed in the future
deployment.apps/nginx created
# 方法2 基于命令行创建pod
kubectl create deployment redis --image=docker.io/library/nginx:latest
# 查看扩容后的pod结果
kubectl get pods -owide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-7b57694cbb-9r9rh 1/1 Running 0 84s 172.20.217.118 10.0.0.204 <none> <none>
nginx-7b57694cbb-l8x6g 1/1 Running 0 84s 172.20.139.166 10.0.0.203 <none> <none>
# 查看rs控制器的信息
kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx-7b57694cbb 2 2 2 70s
# 查看当前回滚历史记录值
kubectl rollout history deployment nginx
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 kubectl create --filename=ngninx-pod.yml --record=true
创建一个service
# 由于是测试环境,为了省事,这里没有使用名称空间
# 集群内,开启一个固定死的ip,svc的ip地址,负载均衡代理一组pod
apiVersion: v1
kind: Service
metadata:
name: svc-nginx # Service的名称 ,svc ip,dns
spec:
selector: # Label Selector,选择包含app=nginx标签的Pod
app: nginx
ports:
- name: service0
targetPort: 80 # Pod的端口
port: 80 # Service对外暴露的端口,也就是ClusterIP的port
protocol: TCP # 转发协议类型,支持TCP和UDP
# Service的类型
# 创建svc
kubectl create -f svc-test.yml
service/svc-nginx created
# 查看svc
kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.68.0.1 <none> 443/TCP 2d
svc-nginx ClusterIP 10.68.235.222 <none> 80/TCP 5s
# 查看svc详细信息
kubectl describe svc
# 测试访问svc查看当前nginx版本
curl -I 10.68.235.222
HTTP/1.1 200 OK
Server: nginx/1.21.5
模拟服务发版,修改nginx版本
# 查看当前回滚历史记录
kubectl rollout history deployment nginx
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 kubectl create --filename=ngninx-pod.yml --record=true
# 方法1 使用edit编辑,更新nginx版本(nginx:1.14.0)
kubectl edit deployments.apps nginx
# 修改如下
spec:
containers:
- image: nginx:1.14.0
imagePullPolicy: Always
name: container-0
resources:
limits:
# 方法2 命令行更新
kubectl set image deploy nginx container=nginx:0:1.14.0 --record
# 监测滚动更新状态
kubectl get pods -w
NAME READY STATUS RESTARTS AGE
nginx-65cbd4cb47-6l59c 0/1 ContainerCreating 0 10s
nginx-8c99db597-46pmb 1/1 Running 0 2m34s
nginx-8c99db597-xdvzm 1/1 Running 0 2m12s
nginx-65cbd4cb47-6l59c 1/1 Running 0 45s
nginx-8c99db597-46pmb 1/1 Terminating 0 3m10s
nginx-65cbd4cb47-7xg6z 0/1 Pending 0 0s
nginx-65cbd4cb47-7xg6z 0/1 Pending 0 0s
nginx-65cbd4cb47-7xg6z 0/1 Pending 0 0s
nginx-65cbd4cb47-7xg6z 0/1 ContainerCreating 0 0s
nginx-8c99db597-46pmb 0/1 Terminating 0 3m11s
nginx-8c99db597-46pmb 0/1 Terminating 0 3m11s
nginx-8c99db597-46pmb 0/1 Terminating 0 3m12s
nginx-8c99db597-46pmb 0/1 Terminating 0 3m12s
nginx-65cbd4cb47-7xg6z 1/1 Running 0 6s
nginx-8c99db597-xdvzm 1/1 Terminating 0 2m55s
nginx-8c99db597-xdvzm 1/1 Terminating 0 2m55s
nginx-8c99db597-xdvzm 1/1 Terminating 0 2m55s
nginx-65cbd4cb47-7xg6z 1/1 Running 0 11s
# 查看回滚状态
kubectl rollout status deployment nginx
deployment "nginx" successfully rolled out
# 查看重新生成的pod
kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-65cbd4cb47-6l59c 1/1 Running 0 73s
nginx-65cbd4cb47-7xg6z 1/1 Running 0 27s
# 测试访问(此时的nginx版本为1.14.0)
curl -I 10.68.235.222
HTTP/1.1 200 OK
Server: nginx/1.14.0
# 再次查看回滚历史记录
# 此时刚刚修改的nginx版本信息被记录了
kubectl rollout history deployment nginx
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 kubectl create --filename=ngninx-pod.yml --record=true
2 kubectl create --filename=ngninx-pod.yml --record=true
# 查看具体版本的,镜像信息(--revision=版本号)
kubectl rollout history deployment nginx --revision=3
# 方法1 回到上一个nginx版本nginx:latest
kubectl rollout undo deployment nginx
# 方法2 指定回滚到某个版本
kubectl rollout undo deployment nginx --to-revision=1
# 再次测试访问
curl -I 10.68.235.222
HTTP/1.1 200 OK
Server: nginx/1.21.5
拓展
# 同时查看k8s多个资源小技巧
kubectl get deployments.apps,pods,rs
# 获得yaml配置
# 创建deployment的命令,加上`--dry-run -o yaml`
# --dry-run代表这条命令不会实际在K8s执行,-o yaml是会将试运行结果以yaml的格式打印出来
kubectl create deployment nginx --image=nginx --dry-run -o yaml
动态、扩、缩容POD
# 扩容副本数为4
kubectl -n <namespace> scale --replicas=4 rs nginx-rs
# 缩容pod数改为2
kubectl -n <namespace> scale --replicas=2 rs nginx-rs
Node管理
# 禁止pod调度到该节点上
kubectl cordon <node name> --delete-emptydir-data --ignore-daemonsets
# 恢复pod正常调度
kubectl uncordon <node name>
# 驱逐该节点上的所有pod
kubectl drain <node name>
该命令会删除该节点上的所有Pod(DaemonSet除外),在其他node上重新启动它们,通常该节点需要维护时使用该命令。直接使用该命令会自动调用kubectl cordon <node>命令。当该节点维护完成,启动了kubelet后,再使用kubectl uncordon <node>即可将该节点添加到kubernetes集群中
更多推荐
已为社区贡献2条内容
所有评论(0)