6Pod的资源配置文件框架传参变量共享网络名称空间容器镜像拉取策略
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了6Pod的资源配置文件框架传参变量共享网络名称空间容器镜像拉取策略相关的知识,希望对你有一定的参考价值。
pod资源定义示例
[root@K8s-master01 ~]#mkdir manifeste
[root@K8s-master01 ~]#cd manifeste/
[root@K8s-master01 manifeste]#vim pod-demoapp.yaml
apiVersion: v1 #(kubectl explain pods显示所属的Version)
kind: Pod
metadata: #(kubectl api-resource 查看pods是名称空间级别的资源)
name: demoapp-pod
namespace: default
labels:
key1: value1
key2: value2
spec: # (期望终态)
containers:
- name: demoapp
image: ikubernetes/demoapp:v1.0
status: #(由控制器来实现填充)
一个最简单的pod资源的定义示例基本完成了,就可以提交给 apiserver来进行运行。
提交运行
如果使用create,需要加 --save-config=ture
如:[root@K8s-master01 manifeste]#kubectl create --save-config=true -f pod-demoapp.yaml
pod/demoapp-pod created
[root@K8s-master01 manifeste]#kubectl get pods
NAME READY STATUS RESTARTS AGE
client-12383 0/1 Error 0 167m
client-5251 1/1 Running 0 168m
demoapp-pod 1/1 Running 0 36s
查看详细信息
[root@K8s-master01 manifeste]#kubectl get pods demoapp-pod -o yaml
除了自己定义的,又额外出现了很多其他字段。
可选字段如果没定义,就会使用默认值来填充。
配置pod,可从以下方面进行定义
apiVersion: v1
kind: Pod
metadata:
name:
namespace:
labels:
key1: value1:
spec:
containers:
- name:
image:
- name:
image:
- name:
image:
securityContext:
startProbe:
livenessProbe:
readinessProbe:
command: []string
args: []string
resources:
reuqests:
cpu: 100m
memory: 512Mi
limits:
env:
- name:
value:
imagePullPolicy:
restartPolicy:
securityContext:
除了手动创建pod资源定义,还可以使用命令来生成pod框架简单修改就可以使用
生成Pod的资源配置文件框架
kubectl run demoapp-pod-002 --image=ikubernets/demoapp:v1.0 --port=80 --dry-run=client -o yaml
#--restart=Never 加上这个就相当于他是个裸pod,不受任何控制欲的控制
#--dry-run=client -o yaml 干跑,客户端看到很简洁,只是测试能不能创建,不真创建。使用yaml显示
基本pod框架,把这个框架保存下来
apiVersion: v1
kind: Pod
metadata:
creationTimestamp: null
labels:
run: demoapp-pod-002
name: demoapp-pod-002
spec:
containers:
- image: ikubernets/demoapp:v1.0
name: demoapp-pod-002
port:
- containerPort: 80
resources:
dnsPolicy: ClusterFirst
restartPolicy: Never
status:
保存框架并对框架进行编辑
kubectl run demoapp-pod-002 --image=ikubernets/demoapp:v1.0 --restart=Never --potr=80 --dry-run=client -o yaml > demoapp-pod-002.yaml
查看pods
[root@K8s-master01 ~]#kubectl get pods
NAME READY STATUS RESTARTS AGE
client-12383 0/1 Error 0 14h
client-5251 0/1 Error 0 14h
demoapp-pod 1/1 Running 1 (11m ago) 11h
打印pod完整的资源规范
[root@K8s-master01 ~]#kubectl get pods demoapp-pod -o yaml
[root@K8s-master01 ~]#kubectl get pods demoapp-pod -o json
[root@K8s-master01 ~]#kubectl get pods demoapp-pod -o wide
打印pod资源的详细状态
[root@K8s-master01 ~]#kubectl describe pods demoapp-pod
重点看Events字段(该资源对象身上发生的事件,出错情况下事件会提示)
定义容器镜像拉取策略:
[root@K8s-master01 ~]#kubectl get pods demoapp-pod -o yaml |less
1、imagePullPolicy: IfNotPresent
#在镜像标签不是latest时,当前节点上存在相关image时,就直接使用;否则就去pull image;在镜像标签是latest时,则无论如何都去pull image;
2、imagePullPolicy: Always #无论当前节点是否存在相关image,都要重新pull image
3、imagePullPolicy: Never #无论当前节点是否存在相关image,都不执行pull image
[root@K8s-master01 ~]#docker container ps
每个pod内部自动有一个pause容器
获取pod中容器应用的日志(-f 持续更新)(-c 指定容器)
[root@K8s-master01 ~]#kubectl logs demoapp-pod -f
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
对pod进行访问
[root@K8s-master01 ~]#curl 10.244.5.11
查看日志
[root@K8s-master01 ~]#kubectl logs demoapp-pod -f
* Running on http://0.0.0.0:80/ (Press CTRL+C to quit)
10.244.0.0 - - [10/Nov/2022 03:30:36] "GET / HTTP/1.1" 200 -
10.244.0.0 - - [10/Nov/2022 03:30:46] "GET / HTTP/1.1" 200 -
也可根据图形化页面的工具进行生成
使用Web UI: 资源配置生成向导
华为云CCE
阿里云ACK
定义容器字段(command:表示覆盖容器镜像默认运行的命令,改为自己要运行的命令)
(args:传递给自定义命令的参数。如果只定义args,没定义command,表示参数只传递给镜像内部里面的程序)
以上两者可单独使用,也可同时使用。如果要引用变 格式为:$(VAR NAME)
修改容器运行的命令和传递的参数
示例:
克隆镜像
[root@K8s-master01 ~]#git clone https://github.com/iKubernetes/Kubernetes_Advanced_Practical_2rd.git
[root@K8s-master01 ~]#ls
Kubernetes_Advanced_Practical_2rd
[root@K8s-master01 ~]#cd Kubernetes_Advanced_Practical_2rd/chapter4/
all-in-one.yaml pod-using-env.yaml
bbox-cap.yaml pod-using-hostnetwork.yaml
envoy.yaml pod-using-hostport.yaml
init-container-demo.yaml readiness-httpget-demo.yaml
lifecycle-demo.yaml resource-limits-demo.yaml
liveness-exec-demo.yaml resource-requests-demo.yaml
liveness-httpget-demo.yaml securitycontext-capabilities-demo.yaml
liveness-tcpsocket-demo.yaml securitycontext-runasuser-demo.yaml
memleak-demo.yaml securitycontext-sysctls-demo.yaml
pod-demo-with-cmd-and-args.yaml sidecar-container-demo.yaml
pod-demo.yaml startup-exec-demo.yaml
pod-resources-demo.yaml
访问默认运行的demoapp-pod
[root@K8s-master01 ~]#kubectl exec demoapp-pod -- ps aux
PID USER TIME COMMAND
1 root 0:01 python3 /usr/local/bin/demo.py
8 root 0:00 ps aux
查看demoapp-pod的默认端口
[root@K8s-master01 ~]#kubectl exec demoapp-pod -- netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
查看自定义后的demoapp-pod的yaml文件,自定义参数-p 8080,不在使用默认端口80
查看使用自定义命令的yaml文件
vim pod-demo-with-cmd-and-args.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-demo-with-cmd-and-args
namespace: default
spec:
containers:
- name: demo
image: ikubernetes/demoapp:v1.0
imagePullPolicy: IfNotPresent
command: [/bin/sh,-c]
args: [python3 /usr/local/bin/demo.py -p 8080]
使用自定义参数的yaml文件配置的pod
[root@K8s-master01 chapter4]#kubectl apply -f pod-demo-with-cmd-and-args.yaml
pod/pod-demo-with-cmd-and-args created
查看pod
[root@K8s-master01 chapter4]#kubectl get pods
pod-demo-with-cmd-and-args 1/1 Running 0 91s
查看端口
[root@K8s-master01 ~]#kubectl exec pod-demo-with-cmd-and-args -- netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN
注意:在command或args中引用的变量时,要使用"
NAME"
在pod上使用环境变量并传值
vim pod-using-env.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-using-env
namespace: default
spec:
containers:
- name: demo
image: ikubernetes/demoapp:v1.0
imagePullPolicy: IfNotPresent
env:
- name: HOST #向host变量
value: "127.0.0.1" #传127.0.0.1值
- name: PORT #向port变量
value: "8080" #传8080值
在容器上使用env,指定向demoapp:v1.0传值,指明其应用监听的默认的地址和端口。
如果没传值,默认将监听该主机所有可用地址的80端口
向容器传递环境变量
[root@K8s-master01 chapter4]#kubectl apply -f pod-using-env.yaml
pod/pod-using-env created
查看pods
[root@K8s-master01 chapter4]#kubectl get pods
pod-using-env 1/1 Running 0 27s
查看pod-using-env的端口
[root@K8s-master01 chapter4]#kubectl exec pod-using-env -- netstat -ntl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 127.0.0.1:8080 0.0.0.0:* LISTEN
打印该容器内的所有环境变量及值
[root@K8s-master01 chapter4]#kubectl exec pod-using-env -- printenv
期望pod直接共享宿主机的网络名称空间
vim pod-using-hostnetwork.yaml
apiVersion: v1
kind: Pod
metadata:
name: pod-using-hostnetwork
namespace: default
spec:
containers:
- name: demo
image: ikubernetes/demoapp:v1.0
imagePullPolicy: IfNotPresent
hostNetwork: true #pod级别
直接在pod级别共享所在节点的网络名称空间
[root@K8s-master01 chapter4]#kubectl apply -f pod-using-hostnetwork.yaml
pod/pod-using-hostnetwork created
[root@K8s-master01 chapter4]#kubectl get pods
pod-using-hostnetwork 1/1 Running 0 6s
查看IP
[root@K8s-master01 chapter4]#kubectl exec pod-using-hostnetwork -- ip a
以上是关于6Pod的资源配置文件框架传参变量共享网络名称空间容器镜像拉取策略的主要内容,如果未能解决你的问题,请参考以下文章