K8s和istio服务网格初体验

Posted by zengchengjie on Thursday, February 10, 2022

本文安装参考文章链接

使用Minikube 安装k8s集群

  • 下载 Minikube 的二进制文件,并添加执行权限

    $ curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube
    
    
  • 将 Minikube 可执行文件添加至 $PATH

    $ sudo mkdir -p /usr/local/bin/
    $ sudo install minikube /usr/local/bin/
    
  • 启动 Minikube 并创建一个集群。

    $ minikube start
    
  • 问题1:此处会报错如下

    $ minikube start
    😄  Centos 7.9.2009 上的 minikube v1.25.1
    ✨  自动选择 docker 驱动。其他选项:ssh, none
    🛑  The "docker" driver should not be used with root privileges.
    💡  If you are running minikube within a VM, consider using --driver=none:
    📘    https://minikube.sigs.k8s.io/docs/reference/drivers/none/
    
    ❌  Exiting due to DRV_AS_ROOT: The "docker" driver should not be used with root privileges.
    

    解决方案:使用非root用户登录进行操作

    若无非root用户,则新建一个:

    adduser otherUser
    passwd otherUser
    

    切换:

    su otherUser
    
  • 问题2:再次启动后报错如下

    $ minikube start
    😄  Centos 7.9.2009 上的 minikube v1.25.1
    👎  Unable to pick a default driver. Here is what was considered, in preference order:
        ▪ docker: Not healthy: "docker version --format {{.Server.Os}}-{{.Server.Version}}" exit status 1: Got permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock: Get "http://%2Fvar%2Frun%2Fdocker.sock/v1.24/version": dial unix /var/run/docker.sock: connect: permission denied
        ▪ docker: Suggestion: Add your user to the 'docker' group: 'sudo usermod -aG docker $USER && newgrp docker' <https://docs.docker.com/engine/install/linux-postinstall/>
    💡  Alternatively you could install one of these drivers:
        ▪ kvm2: Not installed: exec: "virsh": executable file not found in $PATH
        ▪ vmware: Not installed: exec: "docker-machine-driver-vmware": executable file not found in $PATH
        ▪ podman: Not installed: exec: "podman": executable file not found in $PATH
        ▪ virtualbox: Not installed: unable to find VBoxManage in $PATH
    
    ❌  Exiting due to DRV_NOT_HEALTHY: Found driver(s) but none were healthy. See above for suggestions how to fix installed drivers.
    

    原因:minikube需要docker组启动

    解决方案:

    #创建docker组
    sudo groupadd docker
    #将您的用户添加到该docker组
    sudo usermod -aG docker $USER
    #在Linux上,运行以下命令来激活对组的更改
    newgrp docker
    

    重新(在非root账号下)执行 minikube start 命令即可 官方文档链接

  • 安装kubectl

    #下载
    curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
    
    #安装
    sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
    
  • 配置环境变量

    export no_proxy=$no_proxy,$(minikube ip)
    export NO_PROXY=$NO_PROXY,$(minikube ip)
    
  • 查看k8s集群各个组件状态

    minikube status
    
  • 使用kubectl查看minikube创建的虚拟机节点

    kubectl get node -o wide
    
  • 可登录到该节点上查看,密码为root

    ssh root@$(minikube ip)
    

    到这里,已经完成了k8s的安装。

    需要特别说明的是,minikube创建的k8s环境使用的docker-daemon与宿主机上的docker-daemon不同,所以你会发现在宿主机上执行docker ps看不到k8s集群中的容器实例。

    要想在宿主机上查看k8s集群中的容器实例,可在宿主机上执行eval $(minikube -p minikube docker-env)将docker-daemon切换到minikube创建的docker-daemon。

安装istio

Istio v1.5 版本开始,使用 helm 安装的方式已经废弃,需改用 istioctl 安装

  • Istio release 页面下载与操作系统匹配的安装包,这里以 Istio 1.5 版本为例:

    $ curl -L https://istio.io/downloadIstio | sh -
    
  • istioctl客户端路径加入 $PATH 中:

    export PATH=$PATH:$(pwd)/istio-1.5.1/bin
    
  • 安装配置文件

    1.8以下版本执行

    istioctl manifest apply
    

    1.8版本

    istioctl install
    

    1.8以上版本(本文版本1.9.9)

    istioctl install --set profile=demo -y
    

    遇到问题:

    Error: Get "http://localhost:8080/api?timeout=32s": dial tcp [::1]:8080: connect: connection refused
    

    解决方法(原因是在安装k8s的时候用户不同,存在普通用户和root用户的区别):