Kubernetes 多container组成的Pod

在上一篇文章中我们使用kubectl run的方式来创建一个pod,但是pod中只含有一个container。我们知道pod中是可以包含多个container的,在这篇文章中,将会通过创建一个由sonarqube+postgresql组成的pod来演示在kubernetes中如何使用多个container来组成一个pod。

构成说明

演示程序使用下列的集群构成。

NotypeIPOS
1master192.168.32.131CENTOS7.2
2etcd192.168.32.131CENTOS7.2
3minion192.168.32.132CENTOS7.2
3minion192.168.32.133CENTOS7.2
3minion192.168.32.134CENTOS7.2

事前准备

事前将所需镜像提前pull下的各个minion节点

[root@host131 ~]# for h in host132 host133 host134> do> echo "[$h]"> ssh $h docker images |egrep ‘sonar|post‘> done[host132]docker.io/sonarqube latest eea2f3093d50 6 days ago 790.9 MBdocker.io/postgres latest 6f86882e145d 7 days ago 265.9 MB[host133]docker.io/sonarqube latest eea2f3093d50 Less than a second ago 790.9 MBdocker.io/postgres latest 6f86882e145d Less than a second ago 265.9 MB[host134]docker.io/sonarqube latest eea2f3093d50 Less than a second ago 790.9 MBdocker.io/postgres latest 6f86882e145d Less than a second ago 265.9 MB[root@host131 ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

docker-compose方式

先来看看docker的docker-compose方式下是怎样做的。

docker-compose.yml

[root@host132 sonar]# cat docker-compose.ymlversion: ‘2‘services: sonarqube: image: sonarqube ports: - "9000:9000"  links: - postgres:db  environment: - SONARQUBE_JDBC_URL=jdbc:postgresql://db:5432/sonar  postgres: image: postgres environment: - POSTGRES_USER=sonar  - POSTGRES_PASSWORD=sonar [root@host132 sonar]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18

docker-compose up启动

[root@host132 sonar]# docker-compose up -dCreating network "sonar_default" with the default driverCreating sonar_postgres_1Creating sonar_sonarqube_1[root@host132 sonar]#
  • 1
  • 2
  • 3
  • 4
  • 5

docker-compose ps确认结果

[root@host132 sonar]# docker-compose ps Name Command State Ports -----------------------------------------------------------------------------------sonar_postgres_1 /docker-entrypoint.sh postgres Up 5432/tcpsonar_sonarqube_1 ./bin/run.sh Up 0.0.0.0:9000->9000/tcp[root@host132 sonar]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

事前确认

事前确认:没有pod

[root@host131 ~]# kubectl get pods[root@host131 ~]#
  • 1
  • 2

sonar.yml

[root@host131 tmp]# cat sonarqube.ymlapiVersion: v1kind: Podmetadata: name: sonar labels: app: webspec: containers: - name: sonarqube image: sonarqube ports: - containerPort: 9000 env: - name: "SONARQUBE_JDBC_URL" value: "jdbc:postgresql://postgres:5432/sonar" - name: postgres image: postgres env: - name: "POSTGRES_USER" value: "sonar" name: "POSTGRES_PASSWORD" value: "sonar"[root@host131 tmp]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

创建pod

多个pod的方式,就只能使用create语句才能创建

[root@host131 tmp]# kubectl create -f sonarqube.ymlpod "sonar" created[root@host131 tmp]#
  • 1
  • 2
  • 3

确认pod的创建结果

确认状态发现正在创建之中, READY的状态显示0/2,表明此pod有2个container,现在还都没有ready。

[root@host131 tmp]#[root@host131 tmp]# kubectl get podsNAME READY STATUS RESTARTS AGEsonar 0/2 ContainerCreating 0 19s[root@host131 tmp]#
  • 1
  • 2
  • 3
  • 4
  • 5

稍等一会,再次确认已经是running状态,READY也显示为2/2,说明此pod中的2个container都已正常启动

[root@host131 tmp]# kubectl get podsNAME READY STATUS RESTARTS AGEsonar 2/2 Running 1 2m[root@host131 tmp]#
  • 1
  • 2
  • 3
  • 4

确认pod所在的node

我们有3个node,到底是在哪台node上运行这个pod呢

[root@host131 tmp]# kubectl get pods -o wideNAME READY STATUS RESTARTS AGE NODEsonar 2/2 Running 0 2m host133[root@host131 tmp]#
  • 1
  • 2
  • 3
  • 4

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://www.cnblogs.com/captainbed

相关文章