Kubernetes 多container组成的Pod

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

构成说明

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

No type IP OS
1 master 192.168.32.131 CENTOS7.2
2 etcd 192.168.32.131 CENTOS7.2
3 minion 192.168.32.132 CENTOS7.2
3 minion 192.168.32.133 CENTOS7.2
3 minion 192.168.32.134 CENTOS7.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 MB
docker.io/postgres                 latest              6f86882e145d        7 days ago          265.9 MB
[host133]
docker.io/sonarqube                latest              eea2f3093d50        Less than a second ago   790.9 MB
docker.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 MB
docker.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.yml
version: '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 -d
Creating network "sonar_default" with the default driver
Creating sonar_postgres_1
Creating 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/tcp
sonar_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.yml
apiVersion: v1
kind: Pod
metadata:
  name: sonar
  labels:
    app: web
spec:
  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.yml
pod "sonar" created
[root@host131 tmp]#
  • 1
  • 2
  • 3

确认pod的创建结果

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

[root@host131 tmp]#
[root@host131 tmp]# kubectl get pods
NAME      READY     STATUS              RESTARTS   AGE
sonar     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 pods
NAME      READY     STATUS    RESTARTS   AGE
sonar     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 wide
NAME      READY     STATUS              RESTARTS   AGE       NODE
sonar     2/2       Running             0          2m        host133
[root@host131 tmp]#
  • 1
  • 2
  • 3
  • 4

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

猜你喜欢

转载自www.cnblogs.com/firsttry/p/10194891.html