1. 程式人生 > >Kubernetes 多container組成的Pod

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節點

[[email protected] ~]# 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 [[email protected] ~]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

docker-compose方式

先來看看docker的docker-compose方式下是怎樣做的。

docker-compose.yml

[[email protected] 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
[[email protected] 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確認結果

[[email protected] 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
[[email protected] sonar]#
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

事前確認

事前確認:沒有pod

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

sonar.yml

[[email protected] 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"
[[email protected] 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