一个容器多个进程,一个pod多个容器
- 一个容器多个进程:在Docker的镜像编译脚本Dockerfile中带起多个进程,如下可以在contivNet.sh中启动多个进程
FROM 192.168.1.2:5001/world/centos7/ovs-2.6.4:1
COPY ./bin /contiv/bin/
COPY ./scripts /contiv/scripts/
ENTRYPOINT ["/contiv/scripts/contivNet.sh"]
- 一个pod多个容器,可以在yaml文件中如下设置,在containers 中定义两个容器
# each master and worker node in a Kubernetes cluster.
kind: DaemonSet
apiVersion: extensions/v1beta1
metadata:
name: contiv-netplugin-ovs
namespace: kube-system
labels:
k8s-app: contiv-netplugin
spec:
updateStrategy:
type: OnDelete
selector:
matchLabels:
k8s-app: contiv-netplugin
template:
metadata:
labels:
k8s-app: contiv-netplugin
annotations:
prometheus.io/scrape: 'true'
prometheus.io/port: '9004'
scheduler.alpha.kubernetes.io/critical-pod: ''
spec:
hostNetwork: true
hostPID: true
nodeSelector:
node-role.kubernetes.io/node: ""
node-network-driver: "ovs"
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
serviceAccountName: contiv-netplugin
containers:
- name: netplugin-exporter
image: 192.168.1.2:5001/contiv/exporter:0.1
env:
- name: CONTIV_ETCD
valueFrom:
configMapKeyRef:
name: contiv-config
key: contiv_etcd
- name: CONTIV_ROLE
value: 'ovs-netplugin'
volumeMounts:
- mountPath: /k8s_log/contiv
name: var-log-contiv-exporter
readOnly: false
- name: contiv-netplugin
image: 192.168.1.2:5001/contiv/netplugin:1.2.0_6.3
env:
- name: CONTIV_ROLE
value: netplugin
- name: CONTIV_NETPLUGIN_VLAN_UPLINKS
value: enp130s0f0
- name: CONTIV_NETPLUGIN_DRIVER
value: ovs
- name: CONTIV_NETPLUGIN_LOG_LEVEL
value: INFO
- name: CONTIV_NETPLUGIN_MODE
valueFrom:
configMapKeyRef:
name: contiv-config
key: contiv_mode
- name: CONTIV_NETPLUGIN_VTEP_IP
valueFrom:
可以在每个容器中启动一个进程,例如可以使用command启动进程:
command:
- /bin/sh
- -c
- /kubemark --morph=kubelet --name=$(NODE_NAME) --kubeconfig=/kubeconfig/kubelet-$(NODE_NAME).kubeconfig $(CONTENT_TYPE) --alsologtostderr --v=4
这样一个pod中启动了两个容器,每个容器启动了一个进程。
一个pod多个容器实例: 打印log的辅助容器
当容器的log输出到文件时,我们想用kubectl logs来查看容器日志是查看不到的。我们可以使用启动另一个容器的方式来实现。例如,开源cillium的项目tetragon,每个Pod启动两个容器。一个是用于业务处理的tetragon容器,它运行的log写入日志/var/run/cilium/tetragon/tetragon.log, 另一个容器export-stdout是把日志内容输出到标准输出。这里贴出daemonset的部分定义文件:
spec:
containers:
- args:
- /var/run/cilium/tetragon/tetragon.log
command:
- hubble-export-stdout
image: quay.io/cilium/hubble-export-stdout:v1.0.3
imagePullPolicy: IfNotPresent
name: export-stdout
resources: {}
securityContext: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts:
- mountPath: /var/run/cilium/tetragon
name: export-logs
- args:
- --config-dir=/etc/tetragon/tetragon.conf.d/
env:
- name: NODE_NAME
valueFrom:
fieldRef:
apiVersion: v1
fieldPath: spec.nodeName
image: quay.io/cilium/tetragon:v0.11.0
imagePullPolicy: IfNotPresent
livenessProbe:
exec:
command:
- tetra
- status
- --server-address
- localhost:54321
...
第一个容器export-stdout
的参数是/var/run/cilium/tetragon/tetragon.log,运行的命令是hubble-export-stdout,而这个命令是一个简单的脚本,内容如下:
#!/bin/sh
set -e
tail -q -F "$@" 2> /dev/null
而制作这个容器镜像的dockerfile内容如下:
# skopeo inspect --override-os linux docker://busybox:1.36.0-musl | jq -r .Digest
FROM docker.io/library/busybox:1.36.0-musl@sha256:b6252cc4d3a3a702284d828b89cf99d902fad4b00b4aebf2299aa15bfeae54bf as busybox
FROM scratch
# Use busybox statically compiled (musl) sh implementation
COPY --from=busybox /bin/sh /bin/sh
COPY --from=busybox /bin/tail /usr/bin/tail
COPY hubble-export-stdout /usr/local/bin/hubble-export-stdout
ENTRYPOINT ["/bin/sh", "/usr/local/bin/hubble-export-stdout"]
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现