文本描述如何在Kubernetes上运行一个Sonarqube和MySQL。

准备

kubernetes集群构成如下,这里是单机的试验环境。

1
2
3
kubectl get nodes
NAME      STATUS         AGE
cent0     Ready,master   1h

mysql的pod

mysql-rc.yaml

 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
27
apiVersion: v1
kind: ReplicationController            
metadata:
  name: mysql                          
spec:
  replicas: 1                          
  selector:
    app: mysql                          
  template:
    metadata:
      labels:
        app: mysql                      
    spec:
      containers:                       
      - name: mysql                     
        image: mysql:5.7                
        ports:
        - containerPort: 3306           
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: "123456"
        - name: MYSQL_DATABASE
          value: sonardb
        - name: MYSQL_USER
          value: sonar
        - name: MYSQL_PASSWORD
          value: sonar

创建RC

1
kubectl create -f mysql-rc.yaml

查看Pod的状态,确认其处于Running状态。

1
2
kubectl get pods -o wide | grep mysql
mysql-7wl9d   1/1       Running   0          12m       10.244.0.9   cent0

可以使用kubectl describe pod <pod-name>查看Pod的详细信息,其中在Events中可以看到Pod启动各个步骤相关的信息。

1
kubectl describe pod mysql-7wl9d

mysql的svc

mysql-svc.yaml

1
2
3
4
5
6
7
8
9
apiVersion: v1
kind: Service                       
metadata:
  name: mysql                       
spec:
  ports:
    - port: 3306
  selector:
    app: mysql

创建svc

1
kubectl create -f mysql-svc.yaml

确认svc运行状态

1
2
3
4
kubectl get svc
NAME         CLUSTER-IP    EXTERNAL-IP   PORT(S)    AGE
kubernetes   10.96.0.1     <none>        443/TCP    2h
mysql        10.98.249.5   <none>        3306/TCP   1m

sonar的pod

sonar-rc.yml

 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
apiVersion: v1
kind: ReplicationController
metadata:
 name: sonar
spec:
 replicas: 1
 selector:
   app: sonar
 template:
   metadata:
     labels:
       app: sonar
   spec:
     containers:
       - name: sonar
         image: sonarqube:6.2
         ports:
         - containerPort: 9000
         env:
         - name: SONARQUBE_JDBC_USERNAME
           value: sonar
         - name: SONARQUBE_JDBC_PASSWORD
           value: sonar
         - name: SONARQUBE_JDBC_URL
           value: "jdbc:mysql://mysql:3306/sonardb?useSSL=false&useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance"
1
kubectl create -f sonar-rc.yaml

查看Pod的状态,确认其处于Running状态。

1
2
kubectl get pods -o wide | grep sonar
sonar-5vhw8   1/1       Running   0          44s       10.244.0.10   cent0

sonar的svc

sonar-svc.yaml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
apiVersion: v1
kind: Service
metadata:
  name: sonar
spec:
  type: NodePort
  ports:
    - port: 9000
      nodePort: 30001
  selector:
    app: sonar
1
kubectl create -f sonar-svc.yaml

查看service状态

1
2
kubectl get svc | grep sonar
sonar        10.101.73.74   <nodes>       9000:30001/TCP   1m

最后以http://nodeIp:30001在浏览器中打开sonar。

1
2
3
4
http://192.168.61.100:30001/

user: admin
pwd: admin