镜像 | 版本 |
---|---|
postgres | 11.6 |
sonarqube | 9.0.0-community |
buxybox | 1.27.1 |
sonarqube7.8以上的版本都需要使用postgres为底层数据库
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: sonar-postgres
namespace: sonar
spec:
storageClassName: "managed-nfs-storage" #nfs动态供给存储类的名字,可以使用kubectl get sc 查看到;
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
kind: Deployment
apiVersion: apps/v1
metadata:
name: sonar-db
namespace: sonar
labels:
app: sonar-db
name: sonar-db
spec:
replicas: 1
selector:
matchLabels:
app: sonar-db
name: sonar-db
template:
metadata:
labels:
app: sonar-db
name: sonar-db
spec:
volumes:
- name: sonar-postgres
persistentVolumeClaim:
claimName: sonar-postgres
containers:
- name: postgres
image: 'postgres:11.6'
ports:
- containerPort: 5432
protocol: TCP
env:
- name: POSTGRES_DB
value: sonardb
- name: POSTGRES_USER #数据库的用户名
value: sonar
- name: POSTGRES_PASSWORD #数据库的密码
value: sonar
resources:
limits:
cpu: '1'
memory: 2Gi
requests:
cpu: 800m
memory: 2Gi
volumeMounts:
- name: sonar-postgres
mountPath: /var/lib/postgresql/data
imagePullPolicy: IfNotPresent
restartPolicy: Always
kind: Service
apiVersion: v1
metadata:
name: sonar-db
namespace: sonar
labels:
name: sonar-db
spec:
ports:
- name: sonardb
protocol: TCP
port: 5432
targetPort: 5432
selector:
name: sonar-db
type: ClusterIP
这里我是把上述的内容合到一个yaml文件里了
#创建sonar命名空间
[root@k8s-master sonar_workspace]# kubectl create ns sonar
namespace/sonar created
[root@k8s-master sonar_workspace]# kubectl apply -f postgres.yaml
deployment.apps/sonar-db created
persistentvolumeclaim/sonar-postgres created
service/sonar-db created
[root@k8s-master sonar_workspace]# kubectl get pod -n sonar
NAME READY STATUS RESTARTS AGE
sonar-db-545676f4c6-zvjw8 1/1 Running 0 4m26s
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: sonar-data
namespace: sonar
spec:
storageClassName: "managed-nfs-storage" #nfs动态供给存储类的名字,可以使用kubectl get sc 查看到;
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: sonar-extensions
namespace: sonar
spec:
storageClassName: "managed-nfs-storage" #nfs动态供给存储类的名字,可以使用kubectl get sc 查看到;
accessModes:
- ReadWriteMany
resources:
requests:
storage: 1Gi
ind: Deployment
apiVersion: apps/v1
metadata:
name: sonarqube
namespace: sonar
labels:
app: sonarqube
name: sonarqube
spec:
replicas: 1
selector:
matchLabels:
app: sonarqube
name: sonarqube
template:
metadata:
labels:
app: sonarqube
name: sonarqube
spec:
volumes:
- name: sonar-extensions
persistentVolumeClaim:
claimName: sonar-extensions
- name: sonar-data
persistentVolumeClaim:
claimName: sonar-data
initContainers:
- name: init-sysctl
image: 'busybox:1.27.1'
command: # elasticsearch用户拥有的内存权限太小,es启动会报错,此命令保证es正常启动
- sysctl
- '-w'
- vm.max_map_count=262144
ports:
- containerPort: 80
protocol: TCP
resources:
limits:
cpu: '1'
memory: 2Gi
requests:
cpu: 300m
memory: 2Gi
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
imagePullPolicy: IfNotPresent
securityContext: # 配置特权
privileged: true
containers:
- name: sonarqube
image: 'sonarqube:9.0.0-community'
ports:
- containerPort: 9000
protocol: TCP
env:
- name: SONARQUBE_JDBC_USERNAME
value: sonar
- name: SONARQUBE_JDBC_PASSWORD
value: sonar
- name: SONARQUBE_JDBC_URL #数据库的连接地址;格式为:jdbc:postgresql://数据库实例名称.命名空间:5432/数据库名称
value: 'jdbc:postgresql://sonar-db.sonar:5432/sonardb'
resources:
limits:
cpu: '1'
memory: 4Gi
requests:
cpu: 400m
memory: 1Gi
volumeMounts:
- name: sonar-extensions
mountPath: /opt/sonarqube/extensions
- name: sonar-data
mountPath: /opt/sonarqube/data
readinessProbe:
httpGet:
path: /
port: 9000
scheme: HTTP
initialDelaySeconds: 240
timeoutSeconds: 5
periodSeconds: 5
successThreshold: 1
failureThreshold: 3
imagePullPolicy: IfNotPresent
securityContext:
privileged: false
restartPolicy: Always
kind: Service
apiVersion: v1
metadata:
name: sonarqube
namespace: sonar
labels:
name: sonarqube
spec:
ports:
- name: sonarqube
protocol: TCP
port: 9000
targetPort: 9000
selector:
name: sonarqube
type: ClusterIP
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
namespace: sonar
name: sonarqube
annotations:
nginx.ingress.kubernetes.io/proxy-body-size: "1024m"
spec:
ingressClassName: "nginx"
rules:
- host: 'internal.sonarqube.com'
http:
paths:
- path: /
backend:
serviceName: sonarqube
servicePort: 9000
这里我是把上述的内容合到一个yaml文件里了
[root@k8s-master sonar_workspace]# kubectl apply -f sonarqube.yaml
deployment.apps/sonarqube created
persistentvolumeclaim/sonar-data created
persistentvolumeclaim/sonar-extensions created
service/sonarqube created
Warning: extensions/v1beta1 Ingress is deprecated in v1.14+, unavailable in v1.22+; use networking.k8s.io/v1 Ingress
ingress.extensions/sonarqube created
[root@k8s-master sonar_workspace]# kubectl get pod -n sonar
NAME READY STATUS RESTARTS AGE
sonar-db-545676f4c6-zvjw8 1/1 Running 0 7m46s
sonarqube-56cb8748b9-zmp97 1/1 Running 0 4m34s
上方菜单栏的【Administration】➡【Marketplace】➡【Plugins】
需要先同意霸王条款才能进行安装插件
搜索并下载中文插件 “Chinese Pack”
安装完插件之后需要重启一下sonarqube才能生效
上方菜单栏的【Administration】➡【System】➡【Restart Rerver】
重启完之后就是中文的了
以上就是sonarqube在k8s内的安装配置了;
补充:
登录sonar-db(postgres)容器查看sonardb数据库的数据
[root@k8s-master sonar_workspace]# kubectl get pod -n sonar
NAME READY STATUS RESTARTS AGE
sonar-db-545676f4c6-zvjw8 1/1 Running 0 175m
sonarqube-56cb8748b9-zmp97 1/1 Running 0 171m
[root@k8s-master sonar_workspace]# kubectl exec -it pod/sonar-db-545676f4c6-zvjw8 -n sonar sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
#切换到Linux用户postgres,并执行psql,使用sonar用户登录
# su - postgres
postgres@sonar-db-545676f4c6-zvjw8:~$ psql -d sonardb -U sonar
psql (11.6 (Debian 11.6-1.pgdg90+1))
Type "help" for help.
#切换到sonardb数据库
sonardb=# \c sonardb
You are now connected to database "sonardb" as user "sonar".
#查看库内的表
sonardb=# \dt
List of relations
Schema | Name | Type | Owner
--------+---------------------------+-------+-------
public | active_rule_parameters | table | sonar
public | active_rules | table | sonar
public | alm_pats | table | sonar
public | alm_settings | table | sonar
....
更多postgresql语句可查询下面这个连接:
https://www.sjkjc.com/postgresql/describe-table/