在 MacOS 中使用 multipass 安装 microk8s 环境

Multipass & MicroK8s 介绍

What is Kubernetes?

Kubernetes clusters host containerised applications in a reliable and scalable way. Having DevOps in mind, Kubernetes makes maintenance tasks such as upgrades dead simple.

What is Multipass?

Multipass is a lightweight VM manager for Linux, Windows and macOS. It’s designed for developers who want a fresh Ubuntu environment with a single command. It uses KVM on Linux, Hyper-V on Windows and HyperKit on macOS to run the VM with minimal overhead. It can also use VirtualBox on Windows and macOS. Multipass will fetch images for you and keep them up to date.

What is MicroK8s?

MicroK8s is a CNCF certified upstream Kubernetes deployment that runs entirely on your workstation or edge device. Being a snap it runs all Kubernetes services natively (i.e. no virtual machines) while packing the entire set of libraries and binaries needed. Installation is limited by how fast you can download a couple of hundred megabytes and the removal of MicroK8s leaves nothing behind.

安装 multipass & microk8s

安装 multipass 服务

brew search multipass
brew cask info multipass
brew cask install multipass
multipass version

通过 multipass 安装和启动 microk8s 环境

multipass launch --name microk8s-vm --mem 4G --disk 40G
multipass list
multipass stop microk8s-vm
multipass delete microk8s-vm
multipass purge

在虚机中安装 microk8s 服务

multipass exec microk8s-vm -- sudo snap install microk8s --classic
multipass exec microk8s-vm -- sudo iptables -P FORWARD ACCEPT

查看 microk8s 的 snap 包信息,比如版本信息

 multipass exec microk8s-vm -- sudo snap info microk8s

增加账号访问权限,简化操作

# 默认 ubuntu 账号无权限操作集群,均需要 sudo
# 可将 ubuntu 账号加入 microk8s 用户组以便简化访问
multipass exec microk8s-vm -- sudo usermod -a -G microk8s ubuntu
multipass exec microk8s-vm -- sudo sudo chown -f -R ubuntu ~/.kube

增加访问公钥,简化操作

# 在 ~/.ssh/authorized_keys 增加自己的公钥,则可方便的进行SSH登录
multipass shell microk8s-vm
ssh ubuntu@192.168.64.2

查看磁盘空间

multipass exec microk8s-vm -- df -kh

查看 kubeconfig 配置

multipass exec microk8s-vm -- /snap/bin/microk8s.config

在 kubeconfig 中可以找到集群信息,可登录查看

server: https://192.168.64.2:16443
username: admin
password: xxx

增加 DNS 插件,必须安装,多处依赖使用

multipass exec microk8s-vm -- /snap/bin/microk8s.enable dns
multipass exec microk8s-vm -- /snap/bin/microk8s.enable dashboard

尝试访问 Grafana 地址

https://192.168.64.2:16443/api/v1/namespaces/kube-system/services/monitoring-grafana/proxy

安装 Dashboard UI

multipass exec microk8s-vm -- /snap/bin/microk8s.kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0/aio/deploy/recommended.yaml

使用 Bearer Token 进行鉴权访问

# 为安全考虑,Dashboard UI 需要使用 Bearer Token 进行鉴权访问,使用如下命令获取 Token
multipass exec microk8s-vm -- /snap/bin/microk8s.kubectl -n kube-system get secret | grep default-token | cut -d " " -f1
multipass exec microk8s-vm -- /snap/bin/microk8s.kubectl -n kube-system describe secret default-token-qqt75

访问 Dashboard UI

https://192.168.64.2:16443/api/v1/namespaces/kubernetes-dashboard/services/https:kubernetes-dashboard:/proxy/

查看集群组件状态

multipass exec microk8s-vm -- /snap/bin/microk8s.status

可通过指定配置文件进行访问

# 把kubeconfig保存至本地 /Users/xxx/.kube/microk8s-vm.yml,则可通过指定配置文件进行访问
kubectl --insecure-skip-tls-verify --kubeconfig="/Users/xxx/.kube/microk8s-vm.yml" get pods --all-namespaces # 把kubeconfig保存至本地 ~/.kube/config,则可通过指定配置文件进行访问
kubectl --insecure-skip-tls-verify get pods --all-namespaces

安装 registry 组件


# The MicroK8s registry will not be enabled by default, so needs run the following to enable it.
multipass exec microk8s-vm -- /snap/bin/microk8s.enable registry

查看集群内组件状态

multipass exec microk8s-vm -- /snap/bin/microk8s.status | grep enabled

部署业务应用

业务应用 Demo 代码

urban-iptable-management # 简单的IP地址查询服务,服务自治,无外部依赖
urban-district-management # 简单的省市区查询服务,服务自治,无外部依赖
urban-traffic-management # 简单的模拟服务间调用,依赖 district 服务查询城市信息
urban-gateway-management # 模拟API网关,将访问转发至其他服务

本地 docker image 构建

cd urbanboot

docker build -t urban-iptable-management-app:latest --file ./urban-iptable-management/docker/Dockerfile .
docker build -t urban-district-management-app:latest --file ./urban-district-management/docker/Dockerfile .
docker build -t urban-traffic-management-app:latest --file ./urban-traffic-management/docker/Dockerfile .
docker build -t urban-gateway-management-app:latest --file ./urban-gateway-management/docker/Dockerfile .

删除无用镜像

docker images
docker rmi --force $(docker images | grep "^<none>" | awk '{print $3}')
docker images

保存本地镜像至文件

# Save one or more images to a tar archive
docker save -o urban-iptable-management-app.tar urban-iptable-management-app:latest
docker save -o urban-district-management-app.tar urban-district-management-app:latest
docker save -o urban-traffic-management-app.tar urban-traffic-management-app:latest
docker save -o urban-gateway-management-app.tar urban-gateway-management-app:latest

拷贝镜像文件至 microk8s 机器

scp ./urban-iptable-management-app.tar ubuntu@192.168.64.2:/tmp
scp ./urban-district-management-app.tar ubuntu@192.168.64.2:/tmp
scp ./urban-traffic-management-app.tar ubuntu@192.168.64.2:/tmp
scp ./urban-gateway-management-app.tar ubuntu@192.168.64.2:/tmp

安装镜像至 registry

multipass exec microk8s-vm -- /snap/bin/microk8s.ctr namespaces list
multipass exec microk8s-vm -- /snap/bin/microk8s.ctr images list -q multipass exec microk8s-vm -- /snap/bin/microk8s.ctr images import /tmp/urban-iptable-management-app.tar
multipass exec microk8s-vm -- /snap/bin/microk8s.ctr images import /tmp/urban-district-management-app.tar
multipass exec microk8s-vm -- /snap/bin/microk8s.ctr images import /tmp/urban-traffic-management-app.tar
multipass exec microk8s-vm -- /snap/bin/microk8s.ctr images import /tmp/urban-gateway-management-app.tar multipass exec microk8s-vm -- /snap/bin/microk8s.ctr images list -q | grep urban

删除镜像


multipass exec microk8s-vm -- /snap/bin/microk8s.ctr images remove docker.io/library/urban-iptable-management-app:latest
multipass exec microk8s-vm -- /snap/bin/microk8s.ctr images remove docker.io/library/urban-district-management-app:latest
multipass exec microk8s-vm -- /snap/bin/microk8s.ctr images remove docker.io/library/urban-traffic-management-app:latest
multipass exec microk8s-vm -- /snap/bin/microk8s.ctr images remove docker.io/library/urban-gateway-management-app:latest

在部署文件中配置镜像位置


# 替换deployment.yaml文件中的image路径
/Users/xxx/g/github/urbanboot/urban-district-management/kubernetes/deployment.yaml

创建 Namespace

kubectl --insecure-skip-tls-verify create namespace urbanboot

部署应用

kubectl --insecure-skip-tls-verify apply -f /Users/xxx/g/github/urbanboot/urban-iptable-management/kubernetes/deployment.yaml -n urbanboot
kubectl --insecure-skip-tls-verify apply -f /Users/xxx/g/github/urbanboot/urban-district-management/kubernetes/deployment.yaml -n urbanboot
kubectl --insecure-skip-tls-verify apply -f /Users/xxx/g/github/urbanboot/urban-traffic-management/kubernetes/deployment.yaml -n urbanboot
kubectl --insecure-skip-tls-verify apply -f /Users/xxx/g/github/urbanboot/urban-gateway-management/kubernetes/deployment.yaml -n urbanboot

查看部署

kubectl --insecure-skip-tls-verify get deployments -n urbanboot
kubectl --insecure-skip-tls-verify get pods -n urbanboot

删除部署,会自动删除 Pods

kubectl --insecure-skip-tls-verify delete deployment urban-iptable-management-app -n urbanboot
kubectl --insecure-skip-tls-verify delete deployment urban-district-management-app -n urbanboot
kubectl --insecure-skip-tls-verify delete deployment urban-traffic-management-app -n urbanboot
kubectl --insecure-skip-tls-verify delete deployment urban-gateway-management-app -n urbanboot
kubectl --insecure-skip-tls-verify get services -n urbanboot
multipass exec microk8s-vm -- /snap/bin/microk8s.kubectl expose -h multipass exec microk8s-vm -- /snap/bin/microk8s.kubectl expose deployment urban-iptable-management-app --type=ClusterIP --port=7200 --name=urban-iptable-management-app -n urbanboot
multipass exec microk8s-vm -- /snap/bin/microk8s.kubectl expose deployment urban-iptable-management-app --type=NodePort --port=7200 --name=urban-iptable-management-nodeport -n urbanboot multipass exec microk8s-vm -- /snap/bin/microk8s.kubectl expose deployment urban-district-management-app --type=ClusterIP --port=7200 --name=urban-district-management-app -n urbanboot
multipass exec microk8s-vm -- /snap/bin/microk8s.kubectl expose deployment urban-district-management-app --type=NodePort --port=7200 --name=urban-district-management-nodeport -n urbanboot multipass exec microk8s-vm -- /snap/bin/microk8s.kubectl expose deployment urban-traffic-management-app --type=ClusterIP --port=7200 --name=urban-traffic-management-app -n urbanboot
multipass exec microk8s-vm -- /snap/bin/microk8s.kubectl expose deployment urban-traffic-management-app --type=NodePort --port=7200 --name=urban-traffic-management-nodeport -n urbanboot multipass exec microk8s-vm -- /snap/bin/microk8s.kubectl expose deployment urban-gateway-management-app --type=ClusterIP --port=7200 --name=urban-gateway-management-app -n urbanboot
multipass exec microk8s-vm -- /snap/bin/microk8s.kubectl expose deployment urban-gateway-management-app --type=NodePort --port=7200 --name=urban-gateway-management-nodeport -n urbanboot multipass exec microk8s-vm -- /snap/bin/microk8s.kubectl expose deployment urban-traffic-management-app --type=LoadBalancer --port=7200 --name=urban-traffic-management-loadbalancer -n urbanboot

使用配置文件创建 Service


kubectl --insecure-skip-tls-verify expose -f /Users/xxx/g/github/urbanboot/urban-traffic-management/kubernetes/service.yaml -n urbanboot
kubectl --insecure-skip-tls-verify expose -f /Users/xxx/g/github/urbanboot/urban-traffic-management/kubernetes/nodeport.yaml -n urbanboot

删除 Service

kubectl --insecure-skip-tls-verify delete service urban-iptable-management-app -n urbanboot
kubectl --insecure-skip-tls-verify delete service urban-iptable-management-nodeport -n urbanboot
kubectl --insecure-skip-tls-verify delete service urban-district-management-app -n urbanboot
kubectl --insecure-skip-tls-verify delete service urban-district-management-nodeport -n urbanboot
kubectl --insecure-skip-tls-verify delete service urban-traffic-management-app -n urbanboot
kubectl --insecure-skip-tls-verify delete service urban-traffic-management-nodeport -n urbanboot
kubectl --insecure-skip-tls-verify delete service urban-gateway-management-app -n urbanboot
kubectl --insecure-skip-tls-verify delete service urban-gateway-management-nodeport -n urbanboot

查一下 TCP 端口监听

multipass exec microk8s-vm -- netstat -nl -t

查看部署事件,按照时间排序

kubectl --insecure-skip-tls-verify get events -n urbanboot --sort-by=.metadata.creationTimestamp

查看 Pod 日志

kubectl --insecure-skip-tls-verify get pods -n urbanboot
kubectl --insecure-skip-tls-verify describe pod urban-traffic-management-app-58d7578547-p277h -n urbanboot kubectl --insecure-skip-tls-verify logs urban-traffic-management-app-58d7578547-p277h -n urbanboot
kubectl --insecure-skip-tls-verify logs urban-traffic-management-app-58d7578547-p277h -n urbanboot --tail=20

查看 Endpoint 信息

# Spring Cloud Kubernetes 会通过 API 查询 Endpoints
kubectl --insecure-skip-tls-verify get services -n urbanboot
kubectl --insecure-skip-tls-verify get endpoints -n urbanboot
kubectl --insecure-skip-tls-verify get all --all-namespaces
kubectl --insecure-skip-tls-verify get all -n urbanboot
kubectl --insecure-skip-tls-verify describe services urban-traffic-management-nodeport -n urbanboot
kubectl --insecure-skip-tls-verify describe services urban-traffic-management-app -n urbanboot

访问 NodePort 端口

curl -s http://192.168.64.2:30211
curl -s http://192.168.64.2:30211 -i
curl -s http://192.168.64.2:30211 -v

登录 Pod 环境

kubectl --insecure-skip-tls-verify exec -it urban-traffic-management-app-58d7578547-p277h -n urbanboot -- /bin/bash

查看 Java 进程

java -version
env | grep JAVA
ps -ef|grep java

版权声明:本篇文章《在 MacOS 中使用 multipass 安装 microk8s 环境》由作者 Dennis Gao 发表自博客园个人技术博客,未经作者本人同意禁止以任何的形式转载,任何自动的或人为的爬虫转载行为均为耍流氓。

在 MacOS 中使用 multipass 安装 microk8s 环境的更多相关文章

  1. 02.01Linux中软件的安装、环境搭建

    图1 图2 图3 redis安装 图4 =====================linux下的软件的安装====================安装方式:Yum/rpm/源码安装yum:通过分析rp ...

  2. Linux中jdk的安装和环境变量的配置

    我安装的linux系统版本是RedHat4 64位,jdk版本为jdk-6u10-linux-i586.bin 1.首先找到安装包的位置->进入此文件夹,此bin格式的文件可用./命令直接进行安 ...

  3. SeetaFace教程(一) 在 VS 中的编译安装和环境配置

    SeetaFace开源库由FaceDetection.FaceAlignment.FaceIdentification三部分组成.FaceDetection是在一副图片中检测出人脸区域,以一个方形区域 ...

  4. macOS 中Boost的安装和使用

    1 安装Boost 1.1 使用源码安装 下载Boost源码 解压放在任意目录,例如/home/wang/ ./bootstrap.sh,默认的位置是在usr/local下面;可以通过--prefix ...

  5. macOS 下 PHPStorm + Xdebug 调试 Docker 环境中的代码

    0x00 描述 宿主机是 mac mini,构建的项目在 docker 中,所以需要在 PHPStorm 上配置 Xdebug 进行远程代码调试. 0x01 环境 宿主机:macOS High Sie ...

  6. macOS Sierra上面的php开发环境安装

    本文参考资料: 启动apache时,解决  How to Fix AH00558 and AH00557 httpd apr_sockaddr_info_get() Error Message     ...

  7. CentOS使用yum源中自带的rpm包安装LAMP环境

    CentOS使用yum源中自带的rpm包安装LAMP环境.这是Linux下安装LAMP的环境一种最基本最简便的方式.新手可以从容安装使用. 1. 安装基础包(可选安装)yum install -y w ...

  8. MAC机中安装RUBY环境

    在安装CocoaPods之前要先配置好RUBY环境,本文就怎么安装RUBY的环境进行一总结.安装Ruby环境首先需要安装Xcode然后需要安装Homebrew,接下来需要安装RVM最后安装Ruby环境 ...

  9. macOS中安装docker

    如官方文档中所说: 1.点击进入boot2docker/osx-installer release页面. 2.在下载页面中点击 Boot2Docker-x.x.x.pkg 来下载 Boot2Docke ...

随机推荐

  1. CC2530ADC转换

    一.ADC简介 ADC支持 14 位的模拟数字转换,具有多达12 位的 ENOB(有效数字位).它包括一个模拟多路转换器,具有多达8 个各自可配置的通道,以及一个参考电压发生器.转换结果通过DMA写入 ...

  2. Spring Boot学习 之 Spring Boot Actuator(一)

    Spring Boot版本:2.1.4.RELEASE 启用: spring-boot-actuator模块提供了一系列的用于监控的端点.最简单的开启这个功能的方法就是,在pom文件中添加如下的依赖. ...

  3. 【Hadoop离线基础总结】工作流调度器azkaban

    目录 Azkaban概述 工作流调度系统的作用 工作流调度系统的实现 常见工作流调度工具对比 Azkaban简单介绍 安装部署 Azkaban的编译 azkaban单服务模式安装与使用 azkaban ...

  4. 更加灵活的参数校验,Spring-boot自定义参数校验注解

    上文我们讨论了如何使用@Min.@Max等注解进行参数校验,主要是针对基本数据类型和级联对象进行参数校验的演示,但是在实际中我们往往需要更为复杂的校验规则,比如注册用户的密码和确认密码进行校验,这个时 ...

  5. Mysql 常用函数(23)- sign 函数

    Mysql常用函数的汇总,可看下面系列文章 https://www.cnblogs.com/poloyy/category/1765164.html sign 的作用 返回参数的符号 sign 的语法 ...

  6. Zookeeper入门一篇就够了

    谈点分布式 什么是分布式呢? 起初,我们的应用流量比较小,所有东西全部部署在一个服务器,比如全部丢给一个tomcat来处理,顶多做一个tomcat的多节点部署多分,再挂一台Nginx做一下负载均衡就O ...

  7. like模糊查询是否走索引

    1.模糊查询 后通配 走索引 前通配 走全表 2.where条件用in或or 不会走索引索引的本质是平衡b+数,是为了方便查询的平衡多路查找树 B-Tree相比,B+Tree有以下不同点: 每个节点的 ...

  8. git 常用 指令累积

    1.查询指定文件的修改所有修改日志git log --pretty=oneline 文件名 1. git log filename 可以看到fileName相关的commit记录2. git log ...

  9. vue v-for 渲染input 输入有问题 解决方案

    v-for循环input标签的时候输入信息两个输入框一同显示输入信息 解决方案: <input :placeholder="items.title" v-model = &q ...

  10. Nginx服务器的安装和卸载

    Nginx的安装 安装Nginx之前,需要先获取Nginx的安装文件.我们可以在http://nginx.org/en/download.html获取各个版本的Nginx安装文件.大家可以按照自己的需 ...