使用kubernetes创建一个双containers的pod

本文介绍如何使用kubernetes创建一个双containers的pod。首先确保docker的kubernetes已经加载:

如上图所示,docker自带一个kubernetes的单节点群集,把它启动就好。启动后可以看到kubernetes开始工作:

创建一个文件two.yaml,内容如下:

apiVersion: v1
kind: Pod
metadata:
  name: two-containers
spec:

  restartPolicy: Never

  volumes:
  - name: shared-data
    emptyDir: {}

  containers:

  - name: nginx-container
    image: nginx
    volumeMounts:
    - name: shared-data
      mountPath: /usr/share/nginx/html

  - name: debian-container
    image: debian
    volumeMounts:
    - name: shared-data
      mountPath: /pod-data
    command: ["/bin/sh"]
    args: ["-c", "echo Hello from the debian container > /pod-data/index.html"]

注意上面的pod里面有两个containers。第一个container叫做nginx,第二个叫做debian。并且两个containers共享一个存储位置,叫做shared-data,但是在各自container里面挂装到不同的位置。此外,在debian这个container里面,会往shared-data里面加入index.html,内容是Hello from the debian container

上面的配置架构图如下:

通过上面的yaml文件创建所需pod:

$ kubectl apply -f two.yaml
pod "two-containers" created

进入pod的其中一个container:

$ kubectl exec -it two-containers -c nginx-container -- /bin/bash
root@two-containers:/#

在这个nginx的container里面安装curlps命令:

$ apt-get update
$ apt-get install curl procps

安装完成如下:

使用ps命令查看nginx进程:

使用curl命令访问本地nginx服务:

$ curl localhost

可以看到相关输出:

上面的Hello from the debian container来自debian这个container对shared-data里面index.html的数据操作。

参考文档:

My Github Page: https://github.com/liweinan

Powered by Jekyll and Theme by solid

If you have any question want to ask or find bugs regarding with my blog posts, please report it here:
https://github.com/liweinan/liweinan.github.io/issues