0、强制删除
[root@master01 ~]# kubectl run --image=nginx:1.22 nginx1
pod/nginx1 created
[root@master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx1 0/1 ContainerCreating 0 1s
[root@master01 ~]# kubectl delete pod nginx1 --force --grace-period=0
warning: Immediate deletion does not wait for confirmation that the running resource has been terminated. The resource may continue to run on the cluster indefinitely.
pod "nginx1" force deleted
一、项目的发布方式:
应用升级以及新旧业务切换,这个过程当中如何保证对外的服务正常是一个非常重要的问题。
1.1、三种发布方式:
1、蓝绿发布
把服务器分为蓝组和绿组,先停其中的一部分,先停蓝组,绿组依然对外提供服务,等蓝组更新维护完毕,上线之后,再把绿组关闭维护,可以做业务更新和发布过程的对外服务不受影响。
过去成本比较高。
负载均衡+高可用
特点:一旦出现问题,影响范围比较大
发布策略也比较简单
用户无感知,平滑过渡
缺点:需要大量的后台服务器以作为支持,成本比较高的。
2、金丝雀发布(灰度发布):(例如体验服)
Deployment控制器可以通过自定义控制的方式实现金丝雀发布。
分两组:
pod1-------1.12-----1.18–测试服—192.168.168.81:30000
pod2-------1.12-----1.18–正式服—192.168.168.81:31000
自动化控制的要求很高,整个系统的稳定性比蓝绿的稳定性比蓝绿发布要高。影响范围发布。
副本3个-----3个副本+测试版本(暂停点)-----1个正式,其余两个滚动。
3、滚动发布
部署时间比较慢,但是比较节约资源。发布的策略比较复杂。
1.2、金丝雀的发布流程:
副本3个-----3个副本+测试版本(暂停点)-----测试版没问题,取消暂停--------1个正式,其余两个滚动。
[root@master01 ~]# kubectl create deployment nginx1 --image=nginx:1.12 --replicas=3
deployment.apps/nginx1 created
[root@master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx1-f7c44b649-2jbnb 1/1 Running 0 3s
nginx1-f7c44b649-chcsz 1/1 Running 0 3s
nginx1-f7c44b649-kkdx8 1/1 Running 0 3s
[root@master01 ~]# kubectl set image deployment/nginx1 nginx=nginx:1.22 && kubectl rollout pause deployment nginx1
deployment.apps/nginx1 image updated
deployment.apps/nginx1 paused
[root@master01 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx1-654cb56c4-xg2fb 1/1 Running 0 2s
nginx1-f7c44b649-2jbnb 1/1 Running 0 2m27s
nginx1-f7c44b649-chcsz 1/1 Running 0 2m27s
nginx1-f7c44b649-kkdx8 1/1 Running 0 2m27s
[root@master01 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx1-654cb56c4-xg2fb 1/1 Running 0 10s 10.244.1.44 node01 <none> <none>
nginx1-f7c44b649-2jbnb 1/1 Running 0 2m35s 10.244.2.41 node02 <none> <none>
nginx1-f7c44b649-chcsz 1/1 Running 0 2m35s 10.244.1.43 node01 <none> <none>
nginx1-f7c44b649-kkdx8 1/1 Running 0 2m35s 10.244.2.42 node02 <none> <none>
[root@master01 ~]# curl -I 10.244.1.44
[root@master01 ~]# kubectl rollout resume deployment/nginx1
[root@master01 ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx1-654cb56c4-829qg 1/1 Running 0 19s 10.244.2.44 node02 <none> <none>
nginx1-654cb56c4-czmkk 1/1 Running 0 21s 10.244.2.43 node02 <none> <none>
nginx1-654cb56c4-xg2fb 1/1 Running 0 2m5s 10.244.1.44 node01 <none> <none>
[root@master01 ~]# curl -I 10.244.1.44
1.3、声明式的管理方式:
YAML文件实现:
YAML文件:
deployment:部署方式
service的部署方式
[root@master01 ~]# cd /opt/
[root@master01 opt]# mkdir k8s-yaml/
[root@master01 opt]# cd k8s-yaml/
[root@master01 k8s-yaml]# ls
[root@master01 k8s-yaml]# vim nginx-deploy.yaml
#api的标签
apiVersion: apps/v1
kind: Deployment
#定义资源的类型/角色 kind的类型:deployment Job Ingress sevice Deamonset
metadata:
#定义创建资源的元数据的信息,资源的名称--pod的名称,命名空间,pod的标签
name: nginx1
# namespace:
labels:
app: nginx1
#标签名:app=nginx1 上下文要一致
spec:
#顶格写属于全局配置,spec配置deployment资源需要的参数属性,副本数,匹配的标签以及>容器的重启策略。
replicas: 3
#定义该资源的pod的数量
selector:
matchLabels:
#selector定义标签的选择器 match:定义匹配的标签,这里需要上下文要保持一致
app: nginx1
#都是定义容器和pod的元数据
template:
#定义业务模板,定义了3个模板,所有的pod的属性都会和template的设置保持一致。
metadata:
labels:
app: nginx1
#这里也需要和上下文保持一致
spec:
#定义pod内的容器
containers:
- name: nginx
image: nginx:1.22
#--image=nginx:1.22
ports:
- containerPort: 8080
##如果nginx的端口默认的就是80.ports可以不写,写了也无法改变容器内的端口
##如果默认端口不是80,那就需要加上ports申明非默认端口。
##dockerfile起来的nginx端口就是80。
pod和容器是分开也是合并的,镜像定义容器,pod不能改变镜像
[root@master01 k8s-yaml]# kubectl apply -f nginx-deploy.yaml
[root@master01 k8s-yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx1-654cb56c4-bsphh 1/1 Running 0 32s
nginx1-654cb56c4-crrwp 1/1 Running 0 32s
nginx1-654cb56c4-h6gwp 1/1 Running 0 32s
[root@master01 k8s-yaml]# kubectl apply -f nginx-deploy.yaml ##重新识别配置文件
[root@master01 k8s-yaml]# kubectl delete -f nginx-deploy.yaml ##删除资源对象
------------------以上通过yaml文件拉取镜像------------------------
-----------------以下通过yaml文件配置网络---------------------------------
[root@master01 k8s-yaml]# vim nginx-service.yaml
#定义api的标签
apiVersion: v1
kind: Service
metadata:
#定义service的名称和标签
name: nginx1-svc
labels:
app1: nginx1
spec:
#定义service的资源参数,端口映射,类型,匹配的标签名(和哪个资源对象进行匹配)
type: NodePort
ports:
- port: 80
#service的端口
targetPort: 80
#容器内的端口
nodePort: 30000
selector:
app: nginx1
[root@master01 k8s-yaml]# kubectl apply -f nginx-service.yaml
service/nginx1-svc created
[root@master01 k8s-yaml]# kubectl get pod -o wide ##查看容器ip、所在节点信息
[root@master01 k8s-yaml]# kubectl get svc ##查看service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 2d21h
nginx1-svc NodePort 10.96.2.252 <none> 80:30000/TCP 7m11s
[root@master01 k8s-yaml]# kubectl explain deployment
[root@master01 k8s-yaml]# kubectl explain service
[root@master01 k8s-yaml]# kubectl explain pod
#主机端口,直接让主机端口和容器端口做映射(只能在yaml文件当中表示)
[root@master01 k8s-yaml]# vim nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx-pod
spec:
#声明容器的参数:
containers:
- name: nginx
image: nginx:1.22
ports:
- containerPort: 80
hostPort: 80 #主机端口,直接让主机端口和容器端口做映射(只能在yaml文件当中表示)
[root@master01 k8s-yaml]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
centos-pod 0/1 Completed 0 10m 10.244.2.55 node02 <none> <none>
nginx-pod 1/1 Running 0 110s 10.244.1.50 node01 <none> <none>
nginx1-654cb56c4-bsphh 1/1 Running 0 151m 10.244.2.47 node02 <none> <none>
nginx1-654cb56c4-crrwp 1/1 Running 0 151m 10.244.2.46 node02 <none> <none>
nginx1-654cb56c4-h6gwp 1/1 Running 0 151m 10.244.1.47 node01 <none> <none>
[root@master01 k8s-yaml]# kubectl exec -it nginx-pod bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx-pod:/# echo 123 > /usr/share/nginx/html/index.html
root@nginx-pod:/# exit
exit
[root@master01 k8s-yaml]# curl 192.168.168.81
curl: (7) Failed connect to 192.168.168.81:80; 拒绝连接
[root@master01 k8s-yaml]# curl 192.168.168.82 ##只能curl--node01---nginx-pod
123
[root@master01 k8s-yaml]# kubectl exec -it nginx1-654cb56c4-bsphh bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx1-654cb56c4-bsphh:/# echo 456 > /usr/share/nginx/html/index.html
root@nginx1-654cb56c4-bsphh:/# exit
exit
[root@master01 k8s-yaml]# kubectl exec -it nginx1-654cb56c4-crrwp bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx1-654cb56c4-crrwp:/# echo 477 > /usr/share/nginx/html/index.html
root@nginx1-654cb56c4-crrwp:/# exit
exit
[root@master01 k8s-yaml]# kubectl exec -it nginx1-654cb56c4-h6gwp bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@nginx1-654cb56c4-h6gwp:/# echo 488 > /usr/share/nginx/html/index.html
root@nginx1-654cb56c4-h6gwp:/# exit
exit
[root@master01 k8s-yaml]# curl 192.168.168.81
curl: (7) Failed connect to 192.168.168.81:80; 拒绝连接
[root@master01 k8s-yaml]# curl 192.168.168.81:30000
477
[root@master01 k8s-yaml]# curl 192.168.168.81:30000
488
[root@master01 k8s-yaml]# curl 192.168.168.81:30000
456
[root@master01 k8s-yaml]# curl 192.168.168.82:30000
477
[root@master01 k8s-yaml]# curl 192.168.168.82:30000
488
[root@master01 k8s-yaml]# curl 192.168.168.82:30000
456
[root@master01 k8s-yaml]# vim nginx-deploy.yaml
#api的标签
apiVersion: apps/v1
kind: Deployment
#定义资源的类型/角色 kind的类型:deployment Job Ingress sevice Deamonset
metadata:
#定义创建资源的元数据的信息,资源的名称--pod的名称,命名空间,pod的标签
name: nginx1
# namespaces:
labels:
app: nginx1
#标签名:app=nginx1 上下文要一致
spec:
#顶格写属于全局配置,spec配置deployment资源需要的参数属性,副本数,匹配的标签以及>容器的重启策略。
replicas: 3
#定义该资源的pod的数量
selector:
matchLabels:
#selector定义标签的选择器 match:定义匹配的标签,这里需要上下文要保持一致
app: nginx1
#都是定义容器和pod的元数据
template:
#定义业务模板,定义了3个模板,所有的pod的属性都会和template的设置保持一致。
metadata:
labels:
app: nginx1
#这里也需要和上下文保持一致
spec:
#定义pod内的容器
containers:
- name: nginx
image: nginx:1.22
#--image=nginx:1.22
ports:
- containerPort: 80
hostPort: 80
[root@master01 k8s-yaml]# kubectl apply -f nginx-deploy.yaml
[root@master01 k8s-yaml]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx1-b554bf8c5-46zls 1/1 Running 0 21s 10.244.1.51 node01 <none> <none>
nginx1-b554bf8c5-l26mw 1/1 Running 0 21s 10.244.2.56 node02 <none> <none>
nginx1-b554bf8c5-tj844 0/1 Pending 0 21s <none> <none> <none> <none>
[root@master01 k8s-yaml]# kubectl logs -f nginx1-b554bf8c5-tj844
[root@master01 k8s-yaml]# kubectl describe pod nginx1-b554bf8c5-tj844
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Warning FailedScheduling 61s (x2 over 61s) default-scheduler 0/3 nodes are available: 1 node(s) had taint {node-role.kubernetes.io/master: }, that the pod didn't tolerate, 2 node(s) didn't have free ports for the requested pod ports. ##端口只有一个80,端口占用
[root@node01 ~]# netstat -antp | grep 80
tcp 0 0 10.96.0.1:49980 10.96.0.1:443 ESTABLISHED 4759/flanneld
--------------node02--80端口被占用,起了一个nginx容器,但是可以访问----------
[root@master01 k8s-yaml]# curl 192.168.168.82
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
[root@master01 k8s-yaml]# curl 192.168.168.83
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
单节点访问,用于内部访问,占用宿主机的80端口,而且不能做负载均衡
删除各种pod、容器
[root@master01 k8s-yaml]# kubectl delete svc nginx1-svc
service "nginx1-svc" deleted
[root@master01 k8s-yaml]# kubectl delete -f nginx-pod.yaml ##删除nginx-pod.yaml文件未改动创建的pod
pod "nginx-pod" deleted
[root@master01 k8s-yaml]# kubectl delete pod centos-pod ##删除nginx-pod创建的pod
pod "centos-pod" deleted
[root@master01 k8s-yaml]# kubectl delete deployments.apps nginx1 ##删除nginx-deploy.yaml创建的pod
deployment.apps "nginx1" deleted
二、容器的重启策略和传参:
restartPolicy:
Always:只要启动失败,就会一直重启容器
Never:从不重启 docker的默认模式
Onfailure:只有容器异常退出时,才会重启。返回码非0时才会对容器进行重启。
如果是deployment,那么容器只能是Always,默认是Always,可以不写。
基于pod的yaml,三种类型都可以。
pod内的传参:
command和arges
在yaml文件当中command和args只能存在一个
args可以给command传参
Always:一直重启
apiVersion: v1
kind: Pod
metadata:
name: centos-pod
labels:
app: nginx-pod
spec:
#声明容器的参数:
containers:
- name: centos
image: centos:7
ports:
- containerPort: 80
hostPort: 80
#主机端口,直接让主机端口和容器端口做映射(只能在yaml文件当中表示)
restartPolicy: Always
[root@master01 k8s-yaml]# kubectl apply -f nginx-pod.yaml --force
pod/centos-pod configured
[root@master01 k8s-yaml]# kubectl delete -f nginx-pod.yaml
[root@master01 k8s-yaml]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
centos-pod 0/1 Completed 3 (一直在重启) 61s 10.244.2.59 node02 <none> <none>
Never:从不重启,docker的默认模式
apiVersion: v1
kind: Pod
metadata:
name: centos-pod
labels:
app: nginx-pod
spec:
#声明容器的参数:
containers:
- name: centos
image: centos:7
ports:
- containerPort: 80
hostPort: 80
#主机端口,直接让主机端口和容器端口做映射(只能在yaml文件当中表示)
restartPolicy: Never
[root@master01 k8s-yaml]# kubectl apply -f nginx-pod.yaml
pod/centos-pod created
[root@master01 k8s-yaml]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
centos-pod 0/1 Completed 0 (从不重启) 40s 10.244.2.60 node02 <none> <none>
删除pod
[root@master01 k8s-yaml]# kubectl delete -f nginx-pod.yaml
pod "centos-pod" deleted
第二种:
[root@master01 k8s-yaml]# vim nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: centos-pod
labels:
app: nginx-pod
spec:
#声明容器的参数:
containers:
- name: centos
image: centos:7
#主机端口,直接让主机端口和容器端口做映射(只能在yaml文件当中表示)
restartPolicy: Never
[root@master01 k8s-yaml]# kubectl apply -f nginx-pod.yaml
pod/centos-pod created
[root@master01 k8s-yaml]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
centos-pod 0/1 Completed 0 (从不重启) 10s 10.244.2.61 node02 <none> <none>
pod容器执行命令
[root@master01 k8s-yaml]# vim nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: centos-pod
labels:
app: nginx-pod
spec:
#声明容器的参数:
containers:
- name: centos
image: centos:7
args:
- /bin/bash
- -c
- while true; do sleep 3600; done ##
#主机端口,直接让主机端口和容器端口做映射(只能在yaml文件当中表示)
# restartPolicy: Always
[root@master01 k8s-yaml]# kubectl apply -f nginx-pod.yaml --force
pod/centos-pod configured
apiVersion: v1
kind: Pod
metadata:
name: centos-pod
labels:
app: nginx-pod
spec:
#声明容器的参数:
containers:
- name: centos
image: centos:7
args:
["/bin/bash","-c"," while true; do sleep 3600; done"]
#主机端口,直接让主机端口和容器端口做映射(只能在yaml文件当中表示)
# restartPolicy: Always
~
args命令格式
[root@master01 k8s-yaml]# vim nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: centos-pod
labels:
app: nginx-pod
spec:
#声明容器的参数:
containers:
- name: centos
image: centos:7
args:
- /bin/bash
- -c
- while true; do sleep 3600; done
#结果为真的时候,后台休眠3600s
[root@master01 k8s-yaml]# kubectl apply -f nginx-pod.yaml --force ##强制执行
[root@master01 k8s-yaml]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
centos-pod 1/1 Running 0 22s 10.244.2.62 node02 <none> <none>
[root@master01 k8s-yaml]# kubectl delete -f nginx-pod.yaml
pod "centos-pod" deleted
第二种写法:
[root@master01 k8s-yaml]# vim nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: centos-pod
labels:
app: nginx-pod
spec:
#声明容器的参数:
containers:
- name: centos
image: centos:7
args: ["/bin/bash","-c"," while true; do sleep 3600; done"] ##永远为真,死循环
#结果为真的时候,后台休眠3600s
[root@master01 k8s-yaml]# kubectl apply -f nginx-pod.yaml --force ##强制执行,pod的yaml会提示出现提示重复,提示内容没有发送变化,需要加上--force强制执行
[root@master01 k8s-yaml]# kubectl apply -f nginx-pod.yaml
The Pod "centos-pod" is invalid: spec: Forbidden: pod updates may not change fields other than `spec.containers[*].image`, `spec.initContainers[*].image`, `spec.activeDeadlineSeconds` or `spec.tolerations` (only additions to existing tolerations)
core.PodSpec{
args输出多个命令—/bin/bash多个命令必须要加/bin/bash -c 都需要
[root@master01 k8s-yaml]# vim nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: centos-pod
labels:
app: nginx-pod
spec:
#声明容器的参数:
containers:
- name: centos
image: centos:7
args: ["/bin/bash","-c","touch /opt/123.txt; echo 123 > /opt/123.txt; sleep 3600"]
#多个命令必须要加/bin/bash
#-c输出命令的格式
#多个命令必须要加/bin/bash -c
/bin/bash多个命令必须要加/bin/bash -c 都需要
#后台休眠3600s
[root@master01 k8s-yaml]# kubectl apply -f nginx-pod.yaml --force
pod/centos-pod created
[root@master01 k8s-yaml]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
centos-pod 1/1 Running 0 4s 10.244.2.64 node02 <none> <none>
[root@master01 k8s-yaml]# kubectl exec -it centos-pod bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
[root@centos-pod /]# cd /opt/
[root@centos-pod opt]# ls
123.txt
[root@centos-pod opt]# cat 123.txt
123
command输出多个命令—/bin/bash多个命令必须要加/bin/bash -c 都需要
[root@master01 k8s-yaml]# vim nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: centos-pod
labels:
app: nginx-pod
spec:
#声明容器的参数:
containers:
- name: centos
image: centos:7 command: ["/bin/bash","-c","touch /opt/123.txt; echo 456 > /opt/123.txt; sleep 3600"]
#多个命令必须要加/bin/bash
#-c输出命令的格式
#多个命令必须要加/bin/bash -c
#结果为真的时候,后台休眠3600s
# restartPolicy: Never
[root@master01 k8s-yaml]# kubectl apply -f nginx-pod.yaml --force
[root@master01 k8s-yaml]# kubectl exec -it centos-pod bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
[root@centos-pod /]# cd /opt/
[root@centos-pod opt]# ls
123.txt
[root@centos-pod opt]# cat 123.txt
456
command命令
[root@master01 k8s-yaml]# vim nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: centos-pod
labels:
app: nginx-pod
spec:
#声明容器的参数:
containers:
- name: centos
image: centos:7
command: ["touch /opt/123.txt; echo 456 > /opt/123.txt; sleep 3600"]
#多个命令必须要加/bin/bash
#-c输出命令的格式
#多个命令必须要加/bin/bash -c
#结果为真的时候,后台休眠3600s
[root@master01 k8s-yaml]# vim nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: centos-pod
labels:
app: nginx-pod
spec:
#声明容器的参数:
containers:
- name: centos
image: centos:7
command: ["touch /opt/123.txt; echo 456 > /opt/123.txt; sleep 3600"]
#多个命令必须要加/bin/bash
#-c输出命令的格式
#多个命令必须要加/bin/bash -c
#结果为真的时候,后台休眠3600s
以上不能执行
[root@master01 k8s-yaml]# kubectl apply -f nginx-pod.yaml --force
pod/centos-pod configured
[root@master01 k8s-yaml]# kubectl exec -it centos-pod bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
error: unable to upgrade connection: container not found ("centos")
command args混合使用就是传参
[root@master01 k8s-yaml]# vim nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: centos-pod
labels:
app: nginx-pod
spec:
#声明容器的参数:
containers:
- name: centos
image: centos:7
command: ["echo"]
args: ["hello world"]
[root@master01 k8s-yaml]# kubectl apply -f nginx-pod.yaml --force
pod/centos-pod configured
[root@master01 k8s-yaml]# kubectl describe pod centos-pod
[root@master01 k8s-yaml]# kubectl logs -f centos-pod
hello world
nginx—command args混合使用就是传参
[root@master01 k8s-yaml]# vim nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx-pod
spec:
#声明容器的参数:
containers:
- name: nginx
image: nginx:1.22
command: ["echo"]
args: ["hello world"]
#多个命令必须要加/bin/bash
#-c输出命令的格式
#多个命令必须要加/bin/bash -c
##command和args如果不需要传参只能存在一个
不论是args还是command都会覆盖容器的标准输出(CMD或者是entrypoint)
[root@master01 k8s-yaml]# kubectl apply -f nginx-pod.yaml
pod/nginx-pod created
[root@master01 k8s-yaml]# kubectl describe pod nginx-pod
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 16s default-scheduler Successfully assigned default/nginx-pod to node01
Normal Pulled 14s (x2 over 15s) kubelet Container image "nginx:1.22" already present on machine
Normal Created 14s (x2 over 15s) kubelet Created container nginx
Normal Started 14s (x2 over 15s) kubelet Started container nginx
Warning BackOff 12s (x2 over 13s) kubelet Back-off restarting failed container
[root@master01 k8s-yaml]# kubectl logs -f nginx-pod
hello world
##CMD的命令被command: ["echo"];args: ["hello world"]覆盖
command和args如果不需要传参只能存在一个
#不论是args还是command都会覆盖容器的标准输出(CMD或者entrypoint)
OnFailure重启方式
[root@master01 k8s-yaml]# vim nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx-pod
spec:
#声明容器的参数:
containers:
- name: nginx
image: nginx:1.22
command: ["echo"]
args: ["hello world"]
#多个命令必须要加/bin/bash
#-c输出命令的格式
#多个命令必须要加/bin/bash -c
#结果为真的时候,后台休眠3600s
restartPolicy: OnFailure
[root@master01 k8s-yaml]# kubectl apply -f nginx-pod.yaml --force
pod/nginx-pod configured
[root@master01 k8s-yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
centos-pod 0/1 CrashLoopBackOff 9 21m
nginx-pod 0/1 Completed 0 88s
restartPolicy: OnFailure
[root@master01 k8s-yaml]# vim nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx-pod
spec:
#声明容器的参数:
containers:
- name: nginx
image: nginx:1.22
command: ["touch /ofods/123.txt;"]
#多个命令必须要加/bin/bash
#-c输出命令的格式
#多个命令必须要加/bin/bash -c
#结果为真的时候,后台休眠3600s
restartPolicy: OnFailure
[root@master01 k8s-yaml]# kubectl apply -f nginx-pod.yaml --force
pod/nginx-pod configured
[root@master01 k8s-yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
centos-pod 0/1 CrashLoopBackOff 10 27m
nginx-pod 0/1 CrashLoopBackOff 1 7s
导出deployment模板
[root@master01 k8s-yaml]# kubectl create deployment nginx-de --image=nginx:1.22 --replicas=3 ##创建
[root@master01 k8s-yaml]# kubectl get deployments.apps nginx-de -o yaml > /opt/nginx-de.yaml ##导出模板
1 k8s-yaml]# vim nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx-pod
spec:
#声明容器的参数:
containers:
- name: nginx
image: nginx:1.22
command: [“touch /ofods/123.txt;”]
#多个命令必须要加/bin/bash
#-c输出命令的格式
#多个命令必须要加/bin/bash -c
#结果为真的时候,后台休眠3600s
restartPolicy: OnFailure
[root@master01 k8s-yaml]# kubectl apply -f nginx-pod.yaml --force
pod/nginx-pod configured
[root@master01 k8s-yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
centos-pod 0/1 CrashLoopBackOff 10 27m
nginx-pod 0/1 CrashLoopBackOff 1 7s
#### 导出deployment模板
[root@master01 k8s-yaml]# kubectl create deployment nginx-de --image=nginx:1.22 --replicas=3 ##创建
[root@master01 k8s-yaml]# kubectl get deployments.apps nginx-de -o yaml > /opt/nginx-de.yaml ##导出模板