初始化容器与边车容器实战
PS:昨天晚上学习了init container与sidecar的容器设计思路,所以今天设计了一个小实验实际操作一下,通过configmap挂载配置文件在上一篇博客
注意:
- 初始化容器与边车容器都不能共享主容器的存储,必须通过volumeMount的挂载才能实现
- 初始化容器执行完毕后会被终结(Terminated)
init container
实验目标:
通过初始化容器给nginx下载一个html网页,放在nginx的默认目录
废话少说,上代码
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
namespace: default
labels:
app: nginx
spec:
selector:
matchLabels:
app: nginx
replicas: 1
template:
metadata:
name: nginx-tem
labels:
app: nginx
spec:
initContainers:
- image: busybox
name: busybox
command:
- wget
- "-O"
- "/work/test.html"
- http://www.baidu.com
volumeMounts:
- name: work
mountPath: "/work"
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx-pod
livenessProbe:
exec:
command: ["/bin/sh","-c","ls /work/test.html"]
initialDelaySeconds: 5
periodSeconds: 3
volumeMounts:
- name: nginx-configmap
mountPath: /etc/nginx/nginx.conf
subPath: nginx.conf
volumeMounts:
- name: work
mountPath: "/usr/share/nginx/html"
ports:
- containerPort: 80
name: nginx-port
volumes:
- name: nginx-configmap
configMap:
name: nginx-configmap
items:
- key: nginx.conf
path: nginx.conf
volumes:
- name: work
emptyDir: {}
为了方便测试我们用命令通过nodeport方式暴露我们的service
kubectl create service nodeport nginx --tcp=80
测试访问
PS:这里是nodeport的形式,不同于hostport,nodeport方式所有的nodeip都可以实现访问,而hostport只能通过所在pod的nodeIP进行访问。
测试访问:
sidecar container
实验目标:
我们用一个sidecar容器去实时输出主业务容器的日志 上yaml文件
apiVersion: v1
kind: Pod
metadata:
name: app01
spec:
containers:
- name: app
image: busybox
args:
- /bin/sh
- -c
- >
i=0;
while true;
do
echo "$i:`date`" >> /var/log/date.log;
i=$((i+1))
sleep 1
done
volumeMounts:
- name: varlog
mountPath: /var/log
- name: sidecar
image: busybox
imagePullPolicy: IfNotPresent
args: [/bin/sh, -c,"tail -f -n+1 /var/log/date.log"]
volumeMounts:
- name: varlog
mountPath: /var/log
volumes:
- name: varlog
emptyDir: {}
测试访问
查看sidecar容器日志
|