这篇文章上次修改于 583 天前,可能其部分内容已经发生变化,如有疑问可询问作者。

前期准备

当前用户目录下创建新的隐藏目录,该隐藏目录中用于存放配置文件和软件数据

cd ~
mkdir .k8sconfig && cd .k8sconfig
mkdir mongo

检查microk8s是否正在运行

microk8s status
# 回显如下
microk8s is running
high-availability: no
  datastore master nodes: 127.0.0.1:19001
  datastore standby nodes: none
addons:
  enabled:
    dashboard            # (core) The Kubernetes dashboard
    dns                  # (core) CoreDNS
    ha-cluster           # (core) Configure high availability on the current node
    helm                 # (core) Helm - the package manager for Kubernetes
    helm3                # (core) Helm 3 - the package manager for Kubernetes
    metrics-server       # (core) K8s Metrics Server for API access to service metrics
    rbac                 # (core) Role-Based Access Control for authorisation
  disabled:
    cert-manager         # (core) Cloud native certificate management
    community            # (core) The community addons repository
    gpu                  # (core) Automatic enablement of Nvidia CUDA
    host-access          # (core) Allow Pods connecting to Host services smoothly
    hostpath-storage     # (core) Storage class; allocates storage from host directory
    ingress              # (core) Ingress controller for external access
    kube-ovn             # (core) An advanced network fabric for Kubernetes
    mayastor             # (core) OpenEBS MayaStor
    metallb              # (core) Loadbalancer for your Kubernetes cluster
    minio                # (core) MinIO object storage
    observability        # (core) A lightweight observability stack for logs, traces and metrics
    prometheus           # (core) Prometheus operator for monitoring and logging
    registry             # (core) Private image registry exposed on localhost:32000
    storage              # (core) Alias to hostpath-storage add-on, deprecated

配置文件

准备配置文件,一共需要编写三份配置文件,分别为mongo-pv-pvc.yaml、mongo-configmap.yaml和mongo-deployment.yaml

vim mongo-pv-pvc.yaml
# 内容如下
apiVersion: v1
kind: PersistentVolume
metadata:
  name: mongo-pv
  labels:
    app: mongo-app
spec:
  storageClassName: standard
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/root/.k8sconfig/mongo"

---

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mongo-pvc
spec:
  storageClassName: standard
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi
  selector:
    matchLabels:
      app: mongo-app
vim mongo-configmap.yaml
# 配置如下
apiVersion: v1
kind: ConfigMap
metadata:
  name: mongodb-config
data:
  mongod.conf: |
    # mongodb configuration file
    storage:
      dbPath: /data/db
    net:
      bindIp: 0.0.0.0
      port: 27017
    security:
      authorization: enabled
vim mongo-deployment.yaml
# 配置如下
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mongodb
spec:
  selector:
    matchLabels:
      app: mongodb
  replicas: 1
  template:
    metadata:
      labels:
        app: mongodb
    spec:
      containers:
      - name: mongodb
        image: mongo:4.4.20
        ports:
        - containerPort: 27017
        volumeMounts:
        - name: mongo-persistent-storage
          mountPath: /data/db
        - name: mongo-config
          mountPath: /etc/mongo
      volumes:
      - name: mongo-persistent-storage
        persistentVolumeClaim:
          claimName: mongo-pvc
      - name: mongo-config
        configMap:
          name: mongodb-config
---
apiVersion: v1
kind: Service
metadata:
  name: mongo-service
spec:
  selector:
    app: mongodb
  ports:
  - name: mongo
    protocol: TCP
    port: 27017
    targetPort: 27017
  type: NodePort

执行配置文件

执行配置文件

microk8s kubectl apply -f mongo-pv-pvc.yaml
microk8s kubectl apply -f mongo-configmap.yaml
microk8s kubectl apply -f mongo-deployment.yaml

检查是否正确安装

microk8s kubectl get service
microk8s kubectl get configmap
microk8s kubectl get pv
microk8s kubectl get pvc
microk8s kubectl get pod
microk8s kubectl get deployment

全部状态均为启动后即启动完成
特别说明: 测试机CPU较老且配置较低,不支持MongoDB5.0+版本,故使用4.4.20版本,5.0+版本无法启动的可以尝试降级安装

microk8s kubectl logs <pod名称>

WARNING: MongoDB 5.0+ requires a CPU with AVX support, and your current system does not appear to have that!
# 如果回显该警告信息,意为当前CPU不支持AVX,只能尝试安装低于5.0版本的mongodb

端口映射

端口映射可以先采用brook端口转发来实现

vim /root/.brook/brookforward.sh
# 内容如下
#!/bin/bash
brook relay -f :27018 -t 10.152.183.105:27017 &

把这个命令加入systemd

vim /etc/systemd/system/brook.service

[Unit]
Description=brookforward
After=syslog.target
After=network.target

[Service]
Type=forking
ExecStart=/root/.brook/brookforward.sh

[Install]
WantedBy=multi-user.target

启动后效果

mongo 127.0.0.1:27018

可以连接成功即为搭建成功