暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

原创|NO.A.0005|专题|——|云计算|K8S|Pod|节点亲和性|污点和污点容忍|Deployment|

Java BBS 2021-05-03
301

一、标题:

原创|NO.A.0005|专题|——|云计算|K8S|Pod概述和意义|Pod两种实现机制|Pod镜像拉取/重启策略/资源限制|Pod健康检查|Pod调度策略:创建Pod流程/影响Pod调度(资源限制和节点选择器)|节点亲和性|污点和污点容忍|controller Deployment概述和应用场景/发布|service概述/三种类型|controller statefulset部署有状态应用|controller daemonset部署守护进程|controller job和cronjob一次任务和定时任务|配置管理secret|配置管理configmap|

——>实验专题<——

六、k8s核心技术——pod

——>pod概述-流程概述<——

Pod实现机制-共享网络

Pod实现机制——共享存储

镜像拉取策略

Pod资源限制示例

Pod重启策略

Pod健康检查

[root@k8s-master ~]# kubectl get pods           #状态为running,java堆内存溢出,检测可能不能对外提供服务,通过容器检查可能不能检查出故障
NAME READY STATUS RESTARTS AGE
nginx-f89759699-p8tnx 1/1 Running 0 3d

创建Pod流程

Pod调度节点亲和性

Pod调度节点选择器

Pod调度-污点,污点容忍

[root@k8s-master ~]# kubectl describe node k8s-master |grep Taint  #查看污点
Taints: node-role.kubernetes.io/master:NoSchedule
[root@k8s-master ~]# kubectl describe node k8s-node1 |grep Taint
Taints: <none>
[root@k8s-master ~]# kubectl describe node k8s-node2 |grep Taint
Taints: <none>
[root@k8s-master ~]# kubectl create deployment web --image=nginx
deployment.apps/web created
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-f89759699-p8tnx 1/1 Running 0 3d
web-5dcb957ccc-4sbc6 0/1 ContainerCreating 0 7s
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-f89759699-p8tnx 1/1 Running 0 3d 10.244.1.2 k8s-node1 <none> <none>
web-5dcb957ccc-4sbc6 0/1 ContainerCreating 0 19s <none> k8s-node2 <none> <none>
[root@k8s-master ~]# kubectl scale deployment web --replicas=5
deployment.apps/web scaled
[root@k8s-master ~]# kubectl get pods -o wide #没有加污点,所以会加入到node1或者node2 上
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-f89759699-p8tnx 1/1 Running 0 3d 10.244.1.2 k8s-node1 <none> <none>
web-5dcb957ccc-4sbc6 1/1 Running 0 50s 10.244.2.2 k8s-node2 <none> <none>
web-5dcb957ccc-9mrhr 0/1 ContainerCreating 0 3s <none> k8s-node1 <none> <none>
web-5dcb957ccc-9qfjx 0/1 ContainerCreating 0 3s <none> k8s-node2 <none> <none>
web-5dcb957ccc-jtpwt 0/1 ContainerCreating 0 3s <none> k8s-node2 <none> <none>
web-5dcb957ccc-wkvm5 0/1 ContainerCreating 0 3s <none> k8s-node1 <none> <none>
[root@k8s-master ~]# kubectl delete deployment web #删掉刚才的创建
deployment.apps "web" deleted
[root@k8s-master ~]# kubectl get pods
[root@k8s-master ~]# kubectl taint node k8s-node1 env_role=yes:NoSchedule #加上污点
node/k8s-node1 tainted
[root@k8s-master ~]# kubectl describe node k8s-node1 |grep Taint #查看污点NoSchedule
Taints: env_role=yes:NoSchedule
#验证:
[root@k8s-master ~]# kubectl create deployment web --image=nginx
deployment.apps/web created
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-f89759699-p8tnx 1/1 Running 0 3d 10.244.1.2 k8s-node1 <none> <none>
web-5dcb957ccc-8dp8w 1/1 Running 0 12s 10.244.2.5 k8s-node2 <none> <none>
[root@k8s-master ~]# kubectl scale deployment web --replicas=5
deployment.apps/web scaled
[root@k8s-master ~]# kubectl get pods -o wide              #都调度到node2节点,因为node1不会被调度
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-f89759699-p8tnx 1/1 Running 0 3d 10.244.1.2 k8s-node1 <none> <none>
web-5dcb957ccc-88rvn 1/1 Running 0 3s 10.244.2.6 k8s-node2 <none> <none>
web-5dcb957ccc-8dp8w 1/1 Running 0 26s 10.244.2.5 k8s-node2 <none> <none>
web-5dcb957ccc-d4t6w 1/1 Running 0 3s 10.244.2.7 k8s-node2 <none> <none>
web-5dcb957ccc-f9jd7 1/1 Running 0 3s 10.244.2.9 k8s-node2 <none> <none>
web-5dcb957ccc-xrvfr 1/1 Running 0 3s 10.244.2.8 k8s-node2 <none> <none>
#删除污点
[root@k8s-master ~]# kubectl describe node k8s-node1 |grep Taint
Taints: env_role=yes:NoSchedule
[root@k8s-master ~]# kubectl taint node k8s-node1 env_role:NoSchedule- #注意:后面有个横杠
node/k8s-node1 untainted
[root@k8s-master ~]# kubectl describe node k8s-node1 |grep Taint
Taints: <none>

=================================END==================================

八、kubernetes核心技术-Controller控制器

——>controller流程概述<——

#1、使用deployment部署应用(yaml)
[root@k8s-master ~]# kubectl create deployment web --image=nginx #就可以部署,但是只能做测试环境
[root@k8s-master ~]# kubectl create deployment web --image=nginx --dry-run -o yaml #生成yaml文件
[root@k8s-master ~]# kubectl create deployment web --image=nginx --dry-run -o yaml > web.yaml #申城yaml文件并导出
[root@k8s-master ~]# vim web.yaml
spec: #通过这两条做匹配,selector和labels标签来做匹配
replicas: 1
selector:
matchLabels:
app: web
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: web
#2、通过yaml文件进行部署
[root@k8s-master ~]# kubectl apply -f web.yaml
deployment.apps/web configured
[root@k8s-master ~]# kubectl get pods #查看部署的应用
#3、对外发布部署
[root@k8s-master ~]# kubectl expose deployment web --port=80 --type=NodePort --target-port=80 --name=web1 -o yaml > web1.yaml
[root@k8s-master ~]# kubectl apply -f web1.yaml #部署
[root@k8s-master ~]# kubectl get pods,svc
#4、通过浏览器来访问验证部署是否验证成功
#应用升级回滚和弹性伸缩
#1、指定nginx版本
[root@k8s-master ~]# vim web.yaml
spec:
containers:
- image: nginx:1.14 #指定为1.14的版本
[root@k8s-master ~]# kubectl apply -f web.yaml
deployment.apps/web configured
[root@k8s-master ~]# kubectl get pods
web-66bf4959f5-dh6dm 0/1 ContainerCreating 0 23s
#2、升级nginx到1.15版本
[root@k8s-master ~]# kubectl set image deployment web nginx=nginx:1.15
deployment.apps/web image updated
[root@k8s-master ~]# kubectl get pods                     #在做升级的操作
NAME READY STATUS RESTARTS AGE
nginx-f89759699-4msbd 1/1 Running 0 16m
web-66bf4959f5-dh6dm 1/1 Running 0 3m34s
web-bbcf684cb-cj274 0/1 ContainerCreating 0 4s
#node节点可以看到镜像的版本1.15下载成功
[root@k8s-node1 ~]# docker images
nginx 1.15 53f3fd8007f7 21 months ago 109MB
#查看升级后的状态:显示successfully是成功的
[root@k8s-master ~]# kubectl rollout status deployment web
deployment "web" successfully rolled out
#3、把nginx1.15回滚到nginx1.14版本
[root@k8s-master ~]# kubectl rollout history deployment web
deployment.apps/web
REVISION CHANGE-CAUSE
1 <none>
2 <none> #nginx1.14版本
3 <none> #当前的版本:1.15版本
[root@k8s-master ~]# kubectl rollout undo deployment web #回滚到上一个版本
deployment.apps/web rolled back
[root@k8s-master ~]# kubectl rollout status deployment web #查看回滚的状态
Waiting for deployment "web" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "web" rollout to finish: 1 old replicas are pending termination...
deployment "web" successfully rolled out
#4、把nginx1.14回滚到指定的版本
[root@k8s-master ~]# kubectl rollout undo deployment web --to-revision=3
deployment.apps/web rolled back                          #回滚到第三个版本
[root@k8s-master ~]# kubectl rollout status deployment web
Waiting for deployment "web" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "web" rollout to finish: 1 old replicas are pending termination...
deployment "web" successfully rolled out
#5、弹性伸缩
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-f89759699-4msbd 1/1 Running 0 28m
web-bbcf684cb-w894j 1/1 Running 0 2m20s
[root@k8s-master ~]# kubectl scale deployment web --replicas=10 #创建10个
deployment.apps/web scaled
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-f89759699-4msbd 1/1 Running 0 31m
web-bbcf684cb-2g2wr 1/1 Running 0 2m54s
web-bbcf684cb-672j9 1/1 Running 0 2m54s
web-bbcf684cb-bjbrj 1/1 Running 0 2m54s
web-bbcf684cb-ctpv2 1/1 Running 0 2m54s
web-bbcf684cb-hr8j5 1/1 Running 0 2m54s
web-bbcf684cb-jbm8l 1/1 Running 0 2m54s
web-bbcf684cb-rn4pt 1/1 Running 0 2m54s
web-bbcf684cb-vr2pb 1/1 Running 0 2m54s
web-bbcf684cb-w894j 1/1 Running 0 5m50s
web-bbcf684cb-xgdfq 0/1 Running 0 2m54s

service


#services定义pod的访问规则
#1、防止pod失联
[root@k8s-master ~]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-f89759699-4msbd 1/1 Running 0 133m 10.244.1.5 k8s-node1 <none> <none>
web-bbcf684cb-2g2wr 1/1 Running 0 104m 10.244.2.13 k8s-node2 <none> <none>
web-bbcf684cb-672j9 1/1 Running 0 104m 10.244.1.11 k8s-node1 <none> <none>
web-bbcf684cb-bjbrj 1/1 Running 0 104m 10.244.2.14 k8s-node2 <none> <none>
web-bbcf684cb-ctpv2 1/1 Running 0 104m 10.244.2.15 k8s-node2 <none> <none>
web-bbcf684cb-hr8j5 1/1 Running 0 104m 10.244.1.9 k8s-node1 <none> <none>
web-bbcf684cb-jbm8l 1/1 Running 0 104m 10.244.1.12 k8s-node1 <none> <none>
web-bbcf684cb-rn4pt 1/1 Running 0 104m 10.244.1.10 k8s-node1 <none> <none>
web-bbcf684cb-vr2pb 1/1 Running 0 104m 10.244.2.16 k8s-node2 <none> <none>
web-bbcf684cb-w894j 1/1 Running 0 107m 10.244.1.8 k8s-node1 <none> <none>
web-bbcf684cb-xgdfq 1/1 Running 0 104m 10.244.2.12 k8s-node2 <none> <none>
#某一个服务想要访问到哪一个Pod,由servvices来负责,起到一个负载均衡的作用。
#定义Pod的负载均衡。
#services存在的意义:防止pod失联,pod负载均衡的作用
#2、通过yaml文件进行部署
#将sts.yaml上传到服务器;创建三个副本,三个pod
#查看pod,每个pod的名称为唯一的
[root@k8s-master ~]# vim sts.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
app: nginx
spec:
ports:
- port: 80
name: web
clusterIP: None
selector:
app: nginx


---


apiVersion: apps/v1
kind: StatefulSet
metadata:
name: nginx-statefulset
namespace: default
spec:
serviceName: nginx
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
[root@k8s-master ~]# kubectl apply -f sts.yaml
service/nginx created
statefulset.apps/nginx-statefulset created
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-statefulset-0 1/1 Running 0 2m27s
nginx-statefulset-1 1/1 Running 0 119s
nginx-statefulset-2 1/1 Running 0 88s
#查看创建之后的services,services是无头的
[root@k8s-master ~]# kubectl get svc
nginx ClusterIP None <none> 80/TCP 4m43s
#None表示为无头的
#唯一个网络标识,如何约定的
#3、确保所有node运行在同一个pod中
#通过DaemonSet部署所有node运行在同一个pod当中
#将ds.yaml上传到服务器
[root@k8s-master ~]# cat ds.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: ds-test
labels:
app: filebeat
spec:
selector:
matchLabels:
app: filebeat
template:
metadata:
labels:
app: filebeat
spec:
containers:
- name: logs
image: nginx
ports:
- containerPort: 80
volumeMounts:
- name: varlog
mountPath: /tmp/log
volumes:
- name: varlog
hostPath:
path: /var/log
#删除所有的pod
[root@k8s-master ~]# kubectl delete statefulset --all
statefulset.apps "nginx-statefulset" deleted
[root@k8s-master ~]# kubectl delete svc nginx
service "nginx" deleted
[root@k8s-master ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 3d5h
[root@k8s-master ~]# kubectl delete pod --all #删除所有的pod
#执行ds.yaml文件
[root@k8s-master ~]# kubectl apply -f ds.yaml
daemonset.apps/ds-test created
[root@k8s-master ~]# kubectl get pods
ds-test-fv2kx 0/1 ContainerCreating 0 20s
ds-test-gzxlq 0/1 ContainerCreating 0 20s
#进入某一个pod里面,查看日志
[root@k8s-master ~]# kubectl exec -it ds-test-fv2kx bash  #进入pod里面
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
root@ds-test-fv2kx:/# ls /tmp/log/ #已经采集到日志数据
anaconda boot.log btmp cron dmesg.old grubby_prune_debug maillog pods secure tallylog vmware-vgauthsvc.log.0 wtmp
audit     boot.log-20210222  containers  dmesg  firewalld  lastlog         messages  rhsm  spooler  tuned     vmware-vmsvc.log    yum.log
#4、job(一次性任务)
[root@k8s-master ~]# kubectl get pods
NAME READY STATUS RESTARTS AGE
ds-test-fv2kx 1/1 Running 0 4m3s
ds-test-gzxlq 1/1 Running 0 4m3s
#创建一次性任务;将该文件job.yaml上传到服务器中
[root@k8s-master ~]# vim job.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
template:
spec:
containers:
- name: pi
image: perl
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never
backoffLimit: 4
[root@k8s-master ~]# kubectl create -f job.yaml
job.batch/pi created
[root@k8s-master ~]# kubectl get pods
pi-p4tlp 0/1 ContainerCreating 0 13s
[root@k8s-master ~]# kubectl get jobs #查看定时任务
NAME COMPLETIONS DURATION AGE
pi 1/1 2m8s 2m10s
[root@k8s-master ~]# kubectl get pods -o wide #查看pod属于哪个node节点
pi-p4tlp 0/1 running 0 64s <none> k8s-node1 <none> <none>
[root@k8s-node1 ~]# docker pull perl #可以查看到镜像下载状态
[root@k8s-master ~]# kubectl get pods #因为它是一次性任务,所以会显示Completed
pi-p4tlp 0/1 Completed 0 13m
[root@k8s-master ~]# kubectl logs pi-p4tlp #通过日志查看它的运算
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275901
[root@k8s-master ~]# kubectl delete -f job.yaml #删除掉job.yaml文件后,才会删除掉pods
job.batch "pi" deleted
[root@k8s-master ~]# kubectl get pods
#5、controller(定时任务)
#将cronjob.yaml上传到服务器
[root@k8s-master ~]# cat cronjob.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: hello
spec:
schedule: "*/1 * * * *" #定时任务的表达式
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
args:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
[root@k8s-master ~]# kubectl apply -f cronjob.yaml
cronjob.batch/hello created
[root@k8s-master ~]# kubectl get pods
hello-1614003120-5tmz6 0/1 ContainerCreating 0 10s
[root@k8s-master ~]# kubectl get cronjobs
NAME SCHEDULE SUSPEND ACTIVE LAST SCHEDULE AGE
hello */1 * * * * False 1 51s 107s
[root@k8s-master ~]# kubectl logs hello-1614003120-5tmz6
Mon Feb 22 14:12:51 UTC 2021
Hello from the Kubernetes cluster
[root@k8s-master ~]# kubectl get pods                    #每隔一段时间执行一次,历史的会变成completed
hello-1614003120-5tmz6 0/1 Completed 0 86s
hello-1614003180-2fppm 0/1 ContainerCreating 0 26s

controller


#对base64进行编码执行
[root@k8s-master ~]# echo -n 'admin' | base64
YWRtaW4=
#将下列文件上传到服务器
[root@k8s-master ~]# ls secret*
secret-var.yaml secret-vol.yaml secret.yaml
#1、创建Secret加密数据
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
  username: YWRtaW4=                                      #将该内容改为创建的base64编码
password: MWYyZDFlMmU2N2Rm
[root@k8s-master ~]# kubectl create -f secret.yaml
secret/mysecret created
[root@k8s-master ~]# kubectl get secret                  #创建完成一个secret编码数据的过程
NAME TYPE DATA AGE
default-token-686hn kubernetes.io/service-account-token 3 3d7h
mysecret Opaque 2 11s
#2、在pod中挂载编码
#2.1、以变量的形式把编码的数据挂载到pod容器中
[root@k8s-master ~]# vim secret-var.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: nginx
image: nginx
env:
- name: SECRET_USERNAME
        valueFrom:                                       #以变量的形式挂载
secretKeyRef:
name: mysecret
key: username
- name: SECRET_PASSWORD
valueFrom:
secretKeyRef:
name: mysecret
key: password
[root@k8s-master ~]# kubectl apply -f secret-var.yaml
pod/mypod created
[root@k8s-master ~]# kubectl get pods
mypod 1/1 Running 0 11s
[root@k8s-master ~]# kubectl exec -it mypod bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
root@mypod:/# echo $SECRET_USERNAME                     #echo输出username及密码,
admin
root@mypod:/# echo $SECRET_PASSWORD
1f2d1e2e67df
#2.2、通过volume形式挂载到pod中
[root@k8s-master ~]# vim secret-vol.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: nginx #名字一致
image: nginx
volumeMounts:
- name: foo
mountPath: "/etc/foo" #挂载地址
readOnly: true
volumes:
- name: foo
secret:
secretName: mysecret
[root@k8s-master ~]# kubectl delete -f secret-var.yaml    #删除挂载形式的配置
pod "mypod" deleted
[root@k8s-master ~]# kubectl apply -f secret-vol.yaml
pod/mypod created
[root@k8s-master ~]# kubectl get pods
mypod 0/1 Running 0 14s
[root@k8s-master ~]# kubectl exec -it mypod bash
root@mypod:/# ls /etc/foo/
password username
root@mypod:/# cat /etc/foo/username                      #在提示符上显示输出值
adminroot@mypod:/# cat /etc/foo/password

configMap:不加密数据

#将历史环境删除
[root@k8s-master ~]# kubectl delete secret --all
secret "default-token-686hn" deleted
secret "mysecret" deleted
[root@k8s-master ~]# kubectl delete Pod --all
pod "ds-test-fv2kx" deleted
pod "ds-test-gzxlq" deleted
pod "hello-1614010320-rh726" deleted
pod "hello-1614010380-mt6dc" deleted
pod "hello-1614010440-dcqzj" deleted
pod "mypod" deleted
pod "nginx-f89759699-gp9rr" deleted
#将配置文件上传到服务器
[root@k8s-master ~]# ls cm* config* myconfig* redis*
cm.yaml config-var.yaml myconfig.yaml redis.properties
#1、常见配置文件redis.properties
[root@k8s-master ~]# vim redis.properties
redis.host=127.0.0.1
redis.port=6379
redis.password=123456
#2、创建configmap
[root@k8s-master ~]# kubectl create configmap redis.config --from-file=redis.properties
configmap/redis.config created
[root@k8s-master ~]# kubectl get cm
NAME DATA AGE
redis.config 1 17s
[root@k8s-master ~]# kubectl describe cm redis.config     #查看它的详细信息
Name: redis.config
Namespace: default
Labels: <none>
Annotations: <none>


Data
====
redis.properties:
----
redis.host=127.0.0.1
redis.port=6379
redis.password=123456


Events: <none>
#3、以volume的形式挂载到pod里面
[root@k8s-master ~]# vim cm.yaml
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: busybox
image: busybox
command: [ "/bin/sh","-c","cat /etc/config/redis.properties" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: redis-config #configmap的名字
restartPolicy: Never
[root@k8s-master ~]# kubectl apply -f cm.yaml
pod/mypod created
[root@k8s-master ~]# kubectl get pods
mypod 0/1 ContainerCreating 0 7s
[root@k8s-master ~]# kubectl logs mypod                #查看挂载的值
redis.host=127.0.0.1
redis.port=6379
redis.password=123456
#4、以变量形式挂载
[root@k8s-master ~]# kubectl delete -f cm.yaml          #删除历史创建
pod "mypod" deleted
[root@k8s-master ~]# vim myconfig.yaml
apiVersion: v1
kind: ConfigMap
metadata:
name: myconfig
namespace: default
data:
special.level: info
special.type: hello
[root@k8s-master ~]# kubectl apply -f myconfig.yaml
configmap/myconfig created
[root@k8s-master ~]# kubectl get cm
NAME DATA AGE
myconfig 2 12s
redis.config 1 15m
[root@k8s-master ~]# vim config-var.yaml                 #创建挂载文件
apiVersion: v1
kind: Pod
metadata:
name: mypod
spec:
containers:
- name: busybox
image: busybox
command: [ "/bin/sh", "-c", "echo $(LEVEL) $(TYPE)" ]
env:
- name: LEVEL
valueFrom:
configMapKeyRef:
name: myconfig
key: special.level
- name: TYPE
valueFrom:
configMapKeyRef:
name: myconfig
key: special.type
restartPolicy: Never
[root@k8s-master ~]# kubectl apply -f config-var.yaml
pod/mypod created
[root@k8s-master ~]# kubectl get pods
mypod 0/1 Completed 0 40s
[root@k8s-master ~]# kubectl logs mypod                #查看挂载的值
info hello

END





声明:JavaBBS论坛主要用于IT技术专题的交流学习,为开源技术爱好者提供广泛、权威的技术资料。若您在技术专题有更好的想法或者建议,欢迎交流!!!



推荐阅读

Recommended reading

 







JavaBBS



Git











  






https://www.javabbs.cn/git






  JavaBBS大数据






→ 






https://www.javabbs.cn





/dsj





  JavaBBS云存储






→  https://www.javabbs.cn






/ycc







  JavaBBS数据库













  https://www.javabbs.cn






/sjk







  JavaBBS云计算













  https://www.javabbs.cn






/yjs







  JavaBBSIT.Log













https://www.javabbs.cn






/itl






  JavaBBSNginx













  https://www.javabbs.cn






/ngx






  JavaBBSzabbix













https://www.javabbs.cn






/zbx






  JavaBBSJavaSE













https://www.javabbs.cn






/jse







  JavaBBS社区文章













https://www.javabbs.cn






/bwz






  JavaBBS社区资料













https://www.javabbs.cn






/bzl






文章转载自Java BBS,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论