K8s/docker部署运行oracle12c数据库

K8s/docker部署运行oracle12c数据库

简介

Oracle Database,又名Oracle RDBMS,或简称Oracle。是甲骨文公司的一款关系数据库管理系统。它是在数据库领域一直处于领先地位的产品。可以说Oracle数据库系统是目前世界上流行的关系数据库管理系统,系统可移植性好、使用方便、功能强,适用于各类大、中、小、微机环境。它是一种高效率、可靠性好的、适应高吞吐量的数据库方案。

k8s部署Oracle12c

下面是Oracle部署的定义代码,此代码由两部分组成,即Oracle部署的部署以及其代理服务。此处部署的Oracle数据库为oracle12c,镜像使用的是 dockerHub 官方提供的 truevoly/oracle-12c:latest。通过NodePort模式对外暴露了31521和38080这两个端口,并通过hostPath文件系统对Oracle的数据进行持久化。

修改k8s的nodeport类型默认端口范围为1-65535

修改k8s的nodeport类型端口范围provided port is not in the valid range. The range of valid ports is 30000-32767

[root@k8s-master01 oracle]# kubectl apply -f service.yaml 
The Service "oracle12c-nodeport" is invalid: spec.ports[0].nodePort: Invalid value: 38080: provided port is not in the valid range. The range of valid ports is 30000-32767
[root@k8s-master01 oracle]# vim /etc/kubernetes/manifests/kube-apiserver.yaml

修改k8s的nodeport类型默认端口范围为1-65535

找到 --service-cluster-ip-range 这一行,在这一行的下一行增加 如下内容

- --service-node-port-range=1-65535
kube-apiserver内容如下
[root@k8s-master01 oracle]# vim /etc/kubernetes/manifests/kube-apiserver.yaml
apiVersion: v1
kind: Pod
metadata:
annotations:
  kubeadm.kubernetes.io/kube-apiserver.advertise-address.endpoint: 172.25.42.235:6443
creationTimestamp: null
labels:
  component: kube-apiserver
  tier: control-plane
name: kube-apiserver
namespace: kube-system
spec:
containers:
- command:
  - kube-apiserver
  - --advertise-address=172.25.42.235
  - --allow-privileged=true
  - --authorization-mode=Node,RBAC
  - --client-ca-file=/etc/kubernetes/pki/ca.crt
  - --enable-admission-plugins=NodeRestriction
  - --enable-bootstrap-token-auth=true
  - --etcd-cafile=/etc/kubernetes/pki/etcd/ca.crt
  - --etcd-certfile=/etc/kubernetes/pki/apiserver-etcd-client.crt
  - --etcd-keyfile=/etc/kubernetes/pki/apiserver-etcd-client.key
  - --etcd-servers=https://127.0.0.1:2379
  - --insecure-port=0
  - --kubelet-client-certificate=/etc/kubernetes/pki/apiserver-kubelet-client.crt
  - --kubelet-client-key=/etc/kubernetes/pki/apiserver-kubelet-client.key
  - --kubelet-preferred-address-types=InternalIP,ExternalIP,Hostname
  - --proxy-client-cert-file=/etc/kubernetes/pki/front-proxy-client.crt
  - --proxy-client-key-file=/etc/kubernetes/pki/front-proxy-client.key
  - --requestheader-allowed-names=front-proxy-client
  - --requestheader-client-ca-file=/etc/kubernetes/pki/front-proxy-ca.crt
  - --requestheader-extra-headers-prefix=X-Remote-Extra-
  - --requestheader-group-headers=X-Remote-Group
  - --requestheader-username-headers=X-Remote-User
  - --secure-port=6443
  - --service-account-issuer=https://kubernetes.default.svc.cluster.local
  - --service-account-key-file=/etc/kubernetes/pki/sa.pub
  - --service-account-signing-key-file=/etc/kubernetes/pki/sa.key
  - --service-cluster-ip-range=10.96.0.0/12
  - --service-node-port-range=1-65535
  - --tls-cert-file=/etc/kubernetes/pki/apiserver.crt
  - --tls-private-key-file=/etc/kubernetes/pki/apiserver.key
  image: k8s.gcr.io/kube-apiserver:v1.21.0
  imagePullPolicy: IfNotPresent
重启 kubelet
systemctl daemon-reload
systemctl restart kubelet

配置 namespace

Namespace.yaml

apiVersion: v1
kind: Namespace
metadata:
name: database
应用 namespace 配置
[root@k8s-master01 oracle]# kubectl apply -f namespace.yml 
namespace/database unchanged
[root@k8s-master01 oracle]#
查看 namespace 是否创建
[root@k8s-master01 oracle]# kubectl get namespace
NAME             STATUS   AGE
database         Active   5d20h
default           Active   6d2h
ingress-nginx     Active   5d21h
kube-node-lease   Active   6d2h
kube-public       Active   6d2h
kube-system       Active   6d2h
nginx             Active   6d2h

配置 deployment

Deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
name: oracle12c
namespace: database
spec:
selector:
  matchLabels:
    app: oracle12c
replicas: 1 # tells deployment to run 2 pods matching the template
template:
  metadata:
    labels:
      app: oracle12c
  spec:
    containers:
    - name: oracle12c
      image: truevoly/oracle-12c
      volumeMounts:
      - mountPath: /u01/app/oracle
        name: data-app-volumes
      ports:
      - containerPort: 8080
      - containerPort: 1521
    volumes:
    - name: data-app-volumes
      hostPath:
        path: /data/oracle12c-data
        type: Directory
应用 deployment 配置
[root@k8s-master01 oracle]# kubectl apply -f deployment.yaml 
deployment.apps/oracle12c unchanged
[root@k8s-master01 oracle]#
查看 deployment 是否部署成功
[root@k8s-master01 oracle]# kubectl get po -n database -o wide
NAME                       READY   STATUS   RESTARTS   AGE   IP           NODE   NOMINATED NODE   READINESS GATES
oracle12c-ddd5f54b6-6z69g   1/1     Running   0         148m   10.244.0.62   node   <none>           <none>
[root@k8s-master01 oracle]#

配置 service 暴露 31521 和 38080 端口

apiVersion: v1
kind: Service
metadata:
namespace: database
labels:
  app: oracle12c
name: oracle12c-nodeport
spec:
externalTrafficPolicy: Cluster
type: NodePort
ports:
  - name: manager
    protocol: TCP
    port: 8080
    targetPort: 8080
    nodePort: 38080
  - name: connect
    protocol: TCP
    port: 1521
    targetPort: 1521
    nodePort: 31521
selector:
  app: oracle12c
应用 service 配置
[root@k8s-master01 oracle]# kubectl get service -n database -o wide
NAME                 TYPE       CLUSTER-IP       EXTERNAL-IP   PORT(S)                         AGE     SELECTOR
oracle12c-nodeport   NodePort   10.109.195.127   <none>        8080:38080/TCP,1521:31521/TCP   5d19h   app=oracle12c
[root@k8s-master01 oracle]#
验证服务是否可用

由于我们在宿主机上暴露了31521和38080端口,我们可以在宿主机上这样验证

[root@k8s-master01 oracle]# curl 172.25.42.235:38080
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML><HEAD>
<TITLE>401 Unauthorized</TITLE>
</HEAD><BODY><H1>Unauthorized</H1>
</BODY></HTML>
[root@k8s-master01 oracle]# telnet 172.25.42.235 31521
Trying 172.25.42.235...
Connected to 172.25.42.235.
Escape character is '^]'.
^C^CConnection closed by foreign host.
[root@k8s-master01 oracle]# ^C

看一下oracle12c启动日志

[root@k8s-master01 oracle]# kubectl logs -f oracle12c-5975cbb54-qmmrx -n database
Database not initialized. Initializing database.
Starting tnslsnr
Copying database files
1% complete
3% complete
11% complete
18% complete
26% complete
37% complete
Creating and starting Oracle instance
40% complete
45% complete
50% complete
55% complete
56% complete
60% complete
62% complete
Completing Database Creation
66% complete
70% complete
73% complete
85% complete
96% complete
100% complete
Look at the log file "/u01/app/oracle/cfgtoollogs/dbca/xe/xe.log" for further details.
Configuring Apex console
Database initialized. Please visit http://#containeer:8080/em http://#containeer:8080/apex for extra configuration if needed
Starting web management console

PL/SQL procedure successfully completed.

Starting import from '/docker-entrypoint-initdb.d':
ls: cannot access /docker-entrypoint-initdb.d/*: No such file or directory
Import finished

Database ready to use. Enjoy! ;)

启动成功

我们会在挂载目录看到软连接product -> /u01/app/oracle-product 可能有问题

这个坏的软连接没有影响

[root@k8s-master01 oracle12c-data]# pwd
/data/oracle12c-data
[root@k8s-master01 oracle12c-data]# ll
总用量 0
drwxr-x---. 3 1001 1001  16 4月  19 09:55 admin
drwxr-x---. 3 1001 1001  16 4月  19 09:59 audit
drwxr-x---. 3 1001 1001  18 4月  19 09:54 cfgtoollogs
drwxr-x---. 2 1001 1001   6 4月  19 10:04 checkpoints
drwxr-xr-x. 2 1001 1001 104 4月  19 10:03 dbs
drwxrwxr-x. 4 1001 1001  34 4月  19 09:55 diag
drwxr-x---. 4 1001 1001  26 4月  19 09:57 fast_recovery_area
drwxr-x---. 3 1001 1001  16 4月  19 09:55 oradata
lrwxrwxrwx. 1 root root  23 4月  19 09:54 product -> /u01/app/oracle-product

​

重启oracle12c服务

[root@k8s-master01 oracle]# kubectl delete -f deployment.yaml 
deployment.apps "oracle12c" deleted
[root@k8s-master01 oracle]# kubectl get pod -n database -o wide
NAME                       READY   STATUS       RESTARTS   AGE   IP           NODE   NOMINATED NODE   READINESS GATES
oracle11g-c4dd46556-s22p8   1/1     Running       0         150m   10.244.0.65   node   <none>           <none>
oracle12c-ddd5f54b6-6z69g   1/1     Terminating   0         161m   10.244.0.62   node   <none>           <none>
[root@k8s-master01 oracle]# kubectl get pod -n database -o wide
NAME                       READY   STATUS   RESTARTS   AGE   IP           NODE   NOMINATED NODE   READINESS GATES
oracle11g-c4dd46556-s22p8   1/1     Running   0         150m   10.244.0.65   node   <none>           <none>

[root@k8s-master01 oracle]# kubectl apply -f deployment.yaml
deployment.apps/oracle12c created
[root@k8s-master01 oracle]#

查看启动日志

可以发现oracle12c在启动时,会自动判断数据文件是否存在

found files in /u01/app/oracle/oradata Using them instead of initial database

发现数据文件存在,则跳过数据库初始化步骤

[root@k8s-master01 oracle]# kubectl logs -f oracle12c-ddd5f54b6-9s4hf -n database
found files in /u01/app/oracle/oradata Using them instead of initial database
ORACLE instance started.

Total System Global Area 1610612736 bytes
Fixed Size    2924928 bytes
Variable Size  603983488 bytes
Database Buffers  989855744 bytes
Redo Buffers   13848576 bytes
Database mounted.
Database opened.
Starting web management console

PL/SQL procedure successfully completed.

[IMPORT] Not a first start, SKIPPING Import from Volume '/docker-entrypoint-initdb.d'
[IMPORT] If you want to enable import at any state - add 'IMPORT_FROM_VOLUME=true' variable

Database ready to use. Enjoy! ;)

访问地址:
IP地址:172.25.42.235
端口:31521
SID:xe
用户名:system
密码:oracle

管理地址:172.25.42.235:38080
用户名:system
密码:oracle

已上就是K8s/docker部署运行oracle12c数据库安装过程,觉得有用就给个赞吧

版权声明:本文为作者原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

原创文章,作者:老C,如若转载,请注明出处:https://www.code404.icu/342.html

发表评论

登录后才能评论