一、deployment说明
控制 Pod
,使 Pod
拥有多副本,自愈,扩缩容等能力
二、 创建 pod
的两种方式
1. 命令方式
create
帮助:
[root@master ~]# kubectl create deploy --help
Create a deployment with the specified name.
Aliases:
deployment, deploy
Examples:
# Create a deployment named my-dep that runs the busybox image
kubectl create deployment my-dep --image=busybox
# Create a deployment with a command
kubectl create deployment my-dep --image=busybox -- date
# Create a deployment named my-dep that runs the nginx image with 3 replicas
kubectl create deployment my-dep --image=nginx --replicas=3
# Create a deployment named my-dep that runs the busybox image and expose port 5701
kubectl create deployment my-dep --image=busybox --port=5701
Usage:
kubectl create deployment NAME --image=image -- [COMMAND] [args...] [options]
# 比较下面两个命令有何不同效果? pod 方式启动
kubectl run mynginx --image=nginx
# deployment方式启动
kubectl create deployment mynginx1 --image=harbor.top/public/nginx:1.25.1
#kubectl create deployment mytomcat --image=tomcat:8.5.68
# 自愈能力
# 查看deploy
[root@master yml]# kubectl get deploy
NAME READY UP-TO-DATE AVAILABLE AGE
mynginx1 1/1 1 1 19s
# 查看rs 可缩写为 kubectl get rs
[root@master yml]# kubectl get replicas
#名称 期望 当前 准备好
NAME DESIRED CURRENT READY AGE
mynginx1-57889d56f 1 1 1 48s
# 查看pod
[root@master yml]# kubectl get po
NAME READY STATUS RESTARTS AGE
mynginx1-57889d56f-62p9w 1/1 Running 0 55s
# 删除deploy
[root@master yml]# k delete deploy mynginx1
deployment.apps "my-dep" deleted
多副本
kubectl create deployment my-dep --image=harbor.top/public/nginx:1.25.1 --replicas=3
[root@master yml]# kubectl get po
NAME READY STATUS RESTARTS AGE
my-dep-6c57f9f8f8-dqhm5 1/1 Running 0 3m18s
my-dep-6c57f9f8f8-s7fkr 1/1 Running 0 3m18s
my-dep-6c57f9f8f8-xjb2k 1/1 Running 0 3m18s
[root@master yml]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-dep-6c57f9f8f8-dqhm5 1/1 Running 0 2m28s 10.244.2.11 node1 <none> <none>
my-dep-6c57f9f8f8-s7fkr 1/1 Running 0 2m28s 10.244.2.10 node1 <none> <none>
my-dep-6c57f9f8f8-xjb2k 1/1 Running 0 2m28s 10.244.1.12 node2 <none> <none>
2. yaml
清单方式 ( *.ymal
/ *.yml
)
--dry-run=client -o yaml
生成pods预期状态配置文件 1-deployment-nginx1.yml
kubectl create deployment mynginx1 --image=harbor.top/public/nginx:1.25.1 --replicas=3 --dry-run=client -o yaml > 1-deployment-nginx1.yml
1-deployment-nginx1.yml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: mynginx1
name: mynginx1
spec:
replicas: 3
selector:
matchLabels:
app: mynginx1
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: mynginx1
spec:
containers:
- image: harbor.top/public/nginx:1.25.1
name: nginx
resources: {}
status: {}
注释不用配置
修改名称为 mynginx2
apiVersion: apps/v1
kind: Deployment
metadata:
# creationTimestamp: null
labels:
app: mynginx2
name: mynginx2
spec:
replicas: 3 # 控制pod的数量
selector:
matchLabels: # 控制管理template.metedata.labels下的pod
app: mynginx2 # 要和template.metedata.labels.app的名称一致,才可以管理label名称为mynginx2的pod
# strategy: {}
template: # 以下为pod的信息
metadata:
# creationTimestamp: null
labels:
app: mynginx2 # pod的label名称
spec:
containers:
- image: harbor.top/public/nginx:1.25.1
name: nginx # 创建容器的名称
# resources: {}
#status: {}
创建服务
[root@master yml]# kubectl apply -f 1-deployment-nginx1.yml
deployment.apps/mynginx2 created
NAME READY UP-TO-DATE AVAILABLE AGE
my-dep 3/3 3 3 15m
mynginx2 3/3 3 3 61s
[root@master yml]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-dep-6c57f9f8f8-dqhm5 1/1 Running 0 15m 10.244.2.11 node1 <none> <none>
my-dep-6c57f9f8f8-s7fkr 1/1 Running 0 15m 10.244.2.10 node1 <none> <none>
my-dep-6c57f9f8f8-xjb2k 1/1 Running 0 15m 10.244.1.12 node2 <none> <none>
mynginx2-757b76df6d-4r28j 1/1 Running 0 99s 10.244.2.13 node1 <none> <none>
mynginx2-757b76df6d-qg59v 1/1 Running 0 99s 10.244.1.13 node2 <none> <none>
mynginx2-757b76df6d-sztvj 1/1 Running 0 99s 10.244.2.12 node1 <none> <none>
可以看到 mynginx2
生成了3个副本
集群内访问 pod
,
[root@master yml]# curl 10.244.2.13
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
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>
注意:此时集群外访问 pod
是不通的,因为没有暴露出端口
三、scale
扩缩容
1. 命令方式扩缩容
扩缩容到2个副本
kubectl scale --replicas=2 deployment/mynginx2
[root@master yml]# kubectl scale --replicas=2 deploy/mynginx2
deployment.apps/mynginx2 scaled
[root@master yml]# kubectl get po
NAME READY STATUS RESTARTS AGE
mynginx2-757b76df6d-qg59v 1/1 Running 0 59m
mynginx2-757b76df6d-rlzlc 1/1 Running 0 3s
2. 动态修改 deployment
方式
扩缩容到1个副本
kubectl edit deployment mynginx2
#修改 replicas
replicas: 1
保存后,pod
自动扩缩容到1个副本
查看结果
[root@master yml]# kubectl get po
NAME READY STATUS RESTARTS AGE
mynginx2-757b76df6d-qg59v 1/1 Running 0 63m
3、自愈&故障转移
● 停机
● 删除Pod
● 容器崩溃
●
# 查看当前pod
[root@master yml]# kubectl get po
NAME READY STATUS RESTARTS AGE
mynginx2-757b76df6d-qg59v 1/1 Running 0 63m
# 删除pod
[root@master yml]# kubectl delete po mynginx2-757b76df6d-qg59v
pod "mynginx2-757b76df6d-qg59v" deleted
# 可以看到pod的后缀已经不一样了,说明deployment服务,删除pod可以治愈
[root@master yml]# kubectl get po
NAME READY STATUS RESTARTS AGE
mynginx2-757b76df6d-h7hpw 1/1 Running 0 9s
四、set
滚动更新
滚动更新线上运行 deployment
下 pod
服务的镜像地址
[root@master yml]# docker pull nginx:1.16.1
# 镜像打标签
[root@master yml]# docker tag nginx:1.16.1 harbor.top/public/nginx:1.16.1
[root@master yml]# docker push harbor.top/public/nginx:1.16.1
# 更换容器镜像
# kubectl set image (-f FILENAME | TYPE NAME) CONTAINER_NAME_1=CONTAINER_IMAGE_1 ... CONTAINER_NAME_N=CONTAINER_IMAGE_N [options]
# 方式一:指定deploy名称更新 加--record=true 记录到历史中,这样history才可以看到历史
# kubectl set image deployment/metadata下的name值 containers下的容器name值=镜像地址:版本号 --record=true
[root@master yml]# kubectl set image deployment/mynginx2 nginx=harbor.top/public/nginx:1.16.1 --record=true
Flag --record has been deprecated, --record will be removed in the future
# 显示上线的状态
[root@master yml]# kubectl rollout status deployment/mynginx2
deployment "mynginx2" successfully rolled out
# 查看当前容器使用的镜像
[root@master yml]# kubectl get deployments.apps -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
mynginx2 3/3 3 3 23h nginx harbor.top/public/nginx:1.16.1 app=mynginx2
# 方式二:指定资源清单更新
[root@master yml]# kubectl set image -f 1-deployment-nginx1.yml nginx=harbor.top/public/nginx:1.25.1
deployment.apps/mynginx2 image updated
您在 /var/spool/mail/root 中有新邮件
[root@master yml]# kubectl rollout status -f 1-deployment-nginx1.yml
deployment "mynginx2" successfully rolled out
[root@master yml]# kubectl get deployments.apps -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
mynginx2 3/3 3 3 23h nginx harbor.top/public/nginx:1.25.1 app=mynginx2
更多参数参考文档:
https://www.cjavapy.com/article/2800/
命令方式修改 deployment
以下两种写法都可以修改 deployment
修改完保存后,就会自动生效
# 修改 kubectl edit deployment/mynginx2
# 修改 kubectl edit deploy/mynginx2
下载tomcat
[root@master yml]# docker pull tomcat
[root@master yml]# docker history tomcat:latest
IMAGE CREATED CREATED BY SIZE COMMENT
<missing> 26 hours ago /bin/sh -c #(nop) EXPOSE 8080 0B
<missing> 26 hours ago /bin/sh -c #(nop) ENV TOMCAT_VERSION=10.1.11 0B
[root@master yml]# docker tag tomcat:latest harbor.top/public/tomcat:10.1.11
[root@master yml]# docker push harbor.top/public/tomcat:10.1.11
五、rollout
版本回退
# kubectl rollout history (TYPE NAME | TYPE/NAME) [flags] [options]
# 历史记录
[root@master yml]# kubectl rollout history deploy/mynginx2
deployment.apps/mynginx2
REVISION CHANGE-CAUSE
3 kubectl set image deployment/mynginx2 nginx=harbor.top/public/nginx:1.16.1 --record=true
5 kubectl set image nginx=harbor.top/public/nginx:1.16.1 --filename=1-deployment-nginx1.yml --record=true
6 kubectl set image deploy/mynginx2 nginx=harbor.top/public/nginx:1.25.1 --record=true
# 查看某个历史详情
[root@master yml]# kubectl rollout history deployment mynginx2 --revision=5
deployment.apps/mynginx2 with revision #5
Pod Template:
Labels: app=mynginx2
pod-template-hash=57b4bd5d98
Annotations: kubernetes.io/change-cause: kubectl set image nginx=harbor.top/public/nginx:1.16.1 --filename=1-deployment-nginx1.yml --record=true
Containers:
nginx:
Image: harbor.top/public/nginx:1.16.1
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
# 回滚(回到上次)
kubectl rollout undo deployment/mynginx2
# 回滚(回到指定版本)
[root@master yml]# kubectl rollout undo deployment mynginx2 --to-revision=5
deployment.apps/mynginx2 rolled back
# 查看线上使用的镜像
[root@master yml]# kubectl get deployments.apps mynginx2 -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
mynginx2 3/3 3 3 23h nginx harbor.top/public/nginx:1.16.1 app=mynginx2
更多:
除了Deployment,k8s还有 StatefulSet 、DaemonSet 、Job 等 类型资源。我们都称为 工作负载。
有状态应用使用 StatefulSet 部署,无状态应用使用 Deployment 部署
https://kubernetes.io/zh/docs/concepts/workloads/controllers/
示例:
deployment.yaml文件
kind: Deployment
apiVersion: apps/v1
metadata:
name: core-api
namespace: default
labels:
k8s-app: core-api
spec:
replicas: 1
selector:
matchLabels:
k8s-app: core-api
template:
metadata:
name: core-api
labels:
k8s-app: core-api
spec:
nodeName: 192.168.0.42
containers:
- name: core-api
image: 'registry-inner.jiejuefangan1.cecloudcs.com/public/core_api:v1'
# IfNotPresent 仅本地没有镜像时才远程拉,Always 永远都是从远程拉,Never 永远只用本地镜像,本地没有则报错
imagePullPolicy: IfNotPresent
volumeMounts:
- name: config
mountPath: "/core-api/config" ## 容器里的目录 从根目标找
- name: image-store
mountPath: "/core-api/static" ## 容器里的目录
volumes:
# configMap 和 hostPath 都可以挂载配置文件,只能使用一种
- name: config
# 挂载配置文件 ConfigMap.yaml服务里name和core-api-config挂载到容器volumes下configMap.name绑定
configMap: # 挂载配置文件
name: api-admin-config
- name: config
hostPath:
# directory location on host
# 挂载配置文件 把宿主机path指向目录挂载到容器mountPath指向的目标
path: /root/haima/core-api/config #### 宿主机目录
type: DirectoryOrCreate
# emptyDir: {}
- name: image-store
hostPath:
# directory location on host
path: /root/haima/core-api/static #### 宿主机目录
type: DirectoryOrCreate
# emptyDir: {}
k8s将服务指定节点部署
https://blog.csdn.net/echo245/article/details/114445783
最后编辑:海马 更新时间:2024-08-03 21:41