中文 English

Installing minikube-dashboard + Auto-start and External Network Access

Published: 2024-04-30
k8s

1. Installing minikube-dashboard

Refer to the official documentation: https://minikube.sigs.k8s.io/docs/start/ Execute the command minikube dashboard

2. Installing the Minikube Background Service

If you want minikube to start only once under systemctl management and exit automatically after successful startup, you can modify the service file for systemctl to prevent automatic restart of minikube. This can be achieved by setting Restart=no, and ensuring the service is started manually only when needed.

Below is a basic systemctl service file example suitable for this scenario:

#minikube自启动脚本
#/etc/systemd/system/minikube.service

[Unit]
Description=Minikube Kubernetes
After=docker.service
Requires=docker.service

[Service]
Type=oneshot
User=root
RemainAfterExit=yes
Environment="HOME=/root"
Environment="MINIKUBE_HOME=/root/.minikube"
ExecStart=/usr/local/bin/minikube start --force

[Install]
WantedBy=multi-user.target

Explanation

Usage Steps

  1. Create the service file: Save the above content to /etc/systemd/system/minikube.service.

  2. Reload systemd configuration: Update systemd to recognize your new service or changes to the service file.

    sudo systemctl daemon-reload
    
  3. Start the service: Use systemctl to start the minikube service.

    sudo systemctl start minikube.service
    
  4. Check the status: Verify the service status to ensure there are no errors.

    sudo systemctl status minikube.service
    
  5. Enable the service: If you want Minikube to run automatically at system startup (execute once, then exit), you can enable the service.

    sudo systemctl enable minikube.service
    

With this configuration, Minikube will run once at system startup, initialize, and then exit, but will not restart automatically.

3. Installing the minikube-dashboard Background Service

#Minikube自启动并自动映射 dashboard 端口
#/etc/systemd/system/minikube-dashboard.service

[Unit]
Description=Auto-start Kubernetes Dashboard with port-forwarding
After=minikube.service
Requires=minikube.service
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
User=root
Environment="HOME=/root"
Environment="MINIKUBE_HOME=/root/.minikube"
ExecStartPre=kubectl wait --for=condition=ready pod --selector=k8s-app=kubernetes-dashboard -n kubernetes-dashboard --timeout=120s
ExecStart=/bin/bash -c 'kubectl port-forward --address 0.0.0.0 service/kubernetes-dashboard 8080:80 -n kubernetes-dashboard'
RemainAfterExit=no

[Install]
WantedBy=multi-user.target

*常规服务操作

#重载服务配置
sudo systemctl daemon-reload
#配置自启动
sudo systemctl enable minikube-dashboard.service
#手工启动服务
sudo systemctl start minikube-dashboard.service
#查看服务状态
sudo systemctl status minikube-dashboard.service