《OpenShift 4.x HOL教程汇总》
说明:本文已经在OpenShift 4.11环境中验证
- 创建项目
$ USER_ID=YOUR-USER-ID
$ oc new-project ${USER_ID}-rocket
- 创建内容如下的mongodb-internal.yaml文件。
kind: Service
apiVersion: v1
metadata:
name: "mongodb-internal"
labels:
name: "mongodb"
annotations:
service.alpha.kubernetes.io/tolerate-unready-endpoints: "true"
spec:
clusterIP: None
ports:
- name: mongodb
port: 27017
selector:
name: "mongodb"
- 创建内容如下的mongodb.yaml文件。
kind: Service
apiVersion: v1
metadata:
name: "mongodb"
labels:
name: "mongodb"
spec:
ports:
- name: mongodb
port: 27017
selector:
name: "mongodb"
- 创建内容如下的mongodb-statefulset.yaml文件。
kind: StatefulSet
apiVersion: apps/v1
metadata:
name: "mongodb"
spec:
serviceName: "mongodb-internal"
replicas: 1
selector:
matchLabels:
name: mongodb
template:
metadata:
labels:
name: "mongodb"
spec:
containers:
- name: mongo-container
image: "registry.access.redhat.com/rhscl/mongodb-36-rhel7:latest"
ports:
- containerPort: 27017
args:
- "run-mongod-replication"
volumeMounts:
- name: mongo-data
mountPath: "/var/lib/mongodb/data"
env:
- name: MONGODB_DATABASE
value: "mongodb"
- name: MONGODB_USER
value: "mongodb_user"
- name: MONGODB_PASSWORD
value: "mongodb_password"
- name: MONGODB_ADMIN_PASSWORD
value: "mongodb_admin_password"
- name: MONGODB_REPLICA_NAME
value: "rs0"
- name: MONGODB_KEYFILE_VALUE
value: "12345678901234567890"
- name: MONGODB_SERVICE_NAME
value: "mongodb-internal"
readinessProbe:
exec:
command:
- stat
- /tmp/initialized
volumeClaimTemplates:
- metadata:
name: mongo-data
labels:
name: "mongodb"
spec:
accessModes: [ ReadWriteOnce ]
resources:
requests:
storage: "4Gi"
- 执行命令,根据以上3个文件创建相关资源。
$ oc create -f mongodb.yaml -n ${USER_ID}-rocket
$ oc create -f mongodb-internal.yaml -n ${USER_ID}-rocket
$ oc create -f mongodb-statefulset.yaml -n ${USER_ID}-rocket
- 查看pod和与其对应的pvc。
$ oc get pod -o wide -n ${USER_ID}-rocket
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mongodb-0 1/1 Running 0 6m27s 10.128.0.116 ip-10-0-131-103.us-east-2.compute.internal <none> <none>
$ oc get pod mongodb-0 -ojson -n ${USER_ID}-rocket | jq .spec.volumes[0]
{
"name": "mongo-data",
"persistentVolumeClaim": {
"claimName": "mongo-data-mongodb-0"
}
}
$ oc get pvc -n ${USER_ID}-rocket
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mongo-data-mongodb-0 Bound pvc-9526dd32-960d-46f3-9d93-2bc20218b605 4Gi RWO gp2 17s
- 删除现有 Pod,然后确认新生成的 Pod 还会使用原来的 PVC。
$ oc delete pod mongodb-0 -n ${USER_ID}-rocket
$ oc get pod -o wide -n ${USER_ID}-rocket
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
mongodb-0 1/1 Running 0 15s 10.128.0.134 ip-10-0-131-103.us-east-2.compute.internal <none> <none>
$ oc get pod mongodb-0 -ojson -n ${USER_ID}-rocket | jq .spec.volumes[0]
{
"name": "mongo-data",
"persistentVolumeClaim": {
"claimName": "mongo-data-mongodb-0"
}
}
- 执行命令,部署应用客户端。
$ oc new-app docker.io/rocketchat/rocket.chat:0.63.3 -e MONGO_URL="mongodb://mongodb_user:mongodb_password@mongodb:27017/mongodb?replicaSet=rs0" --as-deployment-config=true -n ${USER_ID}-rocket
- 生成route,然后查看rocketchat应用对应的route访问地址。
$ oc expose svc/rocketchat
$ oc get route rocketchat -o template --template '{{.spec.host}}' -n ${USER_ID}-rocket
rocketchat-0b56-rocket.apps.cluster-sten.sten.ilt.opentlc.com
- 然后用浏览器打开上面的route地址访问。在注册并登陆后,将显示以下应用界面。