设定 Microk8s 为固定IP,解决重开机 Hyper-V IP 浮动问题

工作上用到 Microk8s,因架设在 Hyper-V 上,当电脑重新开机後 Hyper-V IP会浮动,导致 microk8s 指令无法正常运作,因此想到了使用固定IP来解决这个问题。

在 Windows 10 先安装好以下工具:

  • MicroK8s
  • Multipass

使用 Microk8s 架设 Kubernetes

安装Kubernetes

microk8s install

执行完後

Launched: microk8s-vm
2022-01-19T15:40:30+08:00 INFO Waiting for automatic snapd restart...
microk8s (1.22/stable) v1.22.5 from Canonical✓ installed
microk8s-integrator-windows 0.1 from Canonical✓ installed
MicroK8s is up and running. See the available commands with `microk8s --help`.

会在 Hyper-V 管理员看到 microk8s-vm 的虚拟机
https://ithelp.ithome.com.tw/upload/images/20220119/20145879pGPAnurkpi.png

查看 Microk8s 状态

microk8s status
microk8s is running
high-availability: no
  datastore master nodes: 127.0.0.1:19001
  datastore standby nodes: none
addons:
  enabled:
    ha-cluster           # Configure high availability on the current node
  disabled:
    ambassador           # Ambassador API Gateway and Ingress
    cilium               # SDN, fast with full network policy
    dashboard            # The Kubernetes dashboard
    dns                  # CoreDNS
    fluentd              # Elasticsearch-Fluentd-Kibana logging and monitoring
    gpu                  # Automatic enablement of Nvidia CUDA
    helm                 # Helm 2 - the package manager for Kubernetes
    helm3                # Helm 3 - Kubernetes package manager
    host-access          # Allow Pods connecting to Host services smoothly
    ingress              # Ingress controller for external access
    istio                # Core Istio service mesh services
    jaeger               # Kubernetes Jaeger operator with its simple config
    kata                 # Kata Containers is a secure runtime with lightweight VMS
    keda                 # Kubernetes-based Event Driven Autoscaling
    knative              # The Knative framework on Kubernetes.
    kubeflow             # Kubeflow for easy ML deployments
    linkerd              # Linkerd is a service mesh for Kubernetes and other frameworks
    metallb              # Loadbalancer for your Kubernetes cluster
    metrics-server       # K8s Metrics Server for API access to service metrics
    multus               # Multus CNI enables attaching multiple network interfaces to pods
    openebs              # OpenEBS is the open-source storage solution for Kubernetes
    openfaas             # openfaas serverless framework
    portainer            # Portainer UI for your Kubernetes cluster
    prometheus           # Prometheus operator for monitoring and logging
    rbac                 # Role-Based Access Control for authorisation
    registry             # Private image registry exposed on localhost:32000
    storage              # Storage class; allocates storage from host directory
    traefik              # traefik Ingress controller for external access

虚拟交换器

建立虚拟交换器

https://ithelp.ithome.com.tw/upload/images/20220119/20145879q3vyCJ7FQP.png
https://ithelp.ithome.com.tw/upload/images/20220119/20145879DanAgI3yWs.png

填入名称 LANBridge
https://ithelp.ithome.com.tw/upload/images/20220119/20145879QgFHVtyHkR.png

设定交换器

https://ithelp.ithome.com.tw/upload/images/20220119/20145879wjUFET32xo.png
https://ithelp.ithome.com.tw/upload/images/20220119/20145879MiMelZL6r9.png
https://ithelp.ithome.com.tw/upload/images/20220119/20145879brwMUlJ8IZ.png

microk8s-vm 设定

停止 Microk8s

microk8s stop

设定 microk8s-vm 虚拟机网路介面卡
https://ithelp.ithome.com.tw/upload/images/20220119/201458794u8gPYliOQ.png
https://ithelp.ithome.com.tw/upload/images/20220119/20145879HK8uUMmYPA.png
https://ithelp.ithome.com.tw/upload/images/20220119/20145879tPr3XtAWUm.png

启动 Microk8s

microk8s start

查看 microk8s-vm 讯息

multipass info microk8s-vm
Name:           microk8s-vm
State:          Running
IPv4:           172.21.68.80
                10.1.254.64
Release:        Ubuntu 18.04.6 LTS
Image hash:     7bd21b98e344 (Ubuntu 18.04 LTS)
Load:           0.69 0.53 0.37
Disk usage:     2.3G out of 48.3G
Memory usage:   1.3G out of 3.9G
Mounts:         --

透过 multipass 编辑 microk8s-vm 网路设定

multipass exec microk8s-vm -- sudo vi /etc/netplan/50-cloud-init.yaml

Vi 文字编辑器

  • 按 A 或 I 键 可进入输入模式
  • 按 ESC 结束输入模式
  • 命令列模式输入 :wq 存档离开

原有设定

network:
    ethernets:
        eth0:
            dhcp4: true
            match:
                macaddress: 52:54:00:5e:ec:bd
            set-name: eth0
    version: 2

修改後设定

network:
    ethernets:
        eth0:
            dhcp4: true
            match:
                macaddress: 52:54:00:5e:ec:bd
            set-name: eth0
        eth1:
            addresses: [192.168.1.101/24]
    version: 2

配置网路设定

multipass exec microk8s-vm -- sudo netplan apply

查看 microk8s-vm 讯息

multipass info microk8s-vm
Name:           microk8s-vm
State:          Running
IPv4:           172.21.68.80
                192.168.1.101
                10.1.254.64
Release:        Ubuntu 18.04.6 LTS
Image hash:     7bd21b98e344 (Ubuntu 18.04 LTS)
Load:           0.78 0.59 0.41
Disk usage:     2.3G out of 48.3G
Memory usage:   1.3G out of 3.9G
Mounts:         --

确认可以连到该主机

ping 192.168.1.101
Pinging 192.168.1.101 with 32 bytes of data:
Reply from 192.168.1.101: bytes=32 time<1ms TTL=64
Reply from 192.168.1.101: bytes=32 time<1ms TTL=64
Reply from 192.168.1.101: bytes=32 time<1ms TTL=64
Reply from 192.168.1.101: bytes=32 time<1ms TTL=64

Ping statistics for 192.168.1.101:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

更改 microk8s 主机IP
https://ithelp.ithome.com.tw/upload/images/20220119/20145879Eev9G9Ng6a.png

修改为 192.168.1.101
https://ithelp.ithome.com.tw/upload/images/20220119/20145879ikOdwNDoGO.png

重新开机後输入指令,可以正常显示讯息就成功了

microk8s status

参考文章
使用 MicroK8s 架设 Kubernetes 丛集的完整过程解析


<<:  将资料表的资料转成Json抛出成API用

>>:  mail server的特殊功能

[Day11] VS Code + Git - 使用 Git Graph

Git Graph操作 点击 commit 名称就可以展开细节,再点一次收回 右边栏位绿字为新增的项...

如何修复“硬碟仅显示一半容量”

通过本文,您将能够解决Windows 10/8/7中的“硬碟仅显示少量容量”问题,并使用“Bitwa...

[15] 建立 class 创建实例的继承

考量到要建立各种 class 所以统一建立一个 class 给所有人继承 可以直接跳过,但注意之後有...

水深火热CSS Day 1

不难发现,问题在於该用什麽标准来做决定呢?梁晓声曾讲过,友谊,好比一瓶酒,封存的时间越长,价值则越高...

#22 No-code 之旅 — 静态网站可以部署到哪里?~

大家~ 今天文章会特别短Q 来讲一下静态网站可以部署到哪里呢?大家应该常听到 Github Page...