ubuntu 22.04 安装 minikube 和 istio

ubuntu 22.04 安装 minikube 和 istio

1. 使用 vmware 安装 ubuntu22.04 服务器

​ 步骤简单,自己百度即可

2. 安装minikube

借鉴安装脚本: https://blog.csdn.net/LeoForBest/article/details/126524892

#!/usr/bin/bash
# ~~~~~~~~~
# Ubuntu 22.04 Minikube install
# Update Author: yuluo
# Usage: bash install-minikube.sh (不要 root, 使用普通用户)

echo "正在准备环境..."
sudo apt-get update -y
sudo apt-get install ca-certificates curl gnupg lsb-release apt-transport-https -y

function install_docker() {
    echo "正在卸载旧版本docker..."
    sudo apt-get remove docker docker-engine docker.io containerd runc -y
    echo "正在添加docker gpg..."
    sudo mkdir -p /etc/apt/keyrings
    if [ -f "/etc/apt/keyrings/docker.gpg" ]; then
        sudo rm /etc/apt/keyrings/docker.gpg
    fi

    sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
    sudo chmod a+r /etc/apt/keyrings/docker.gpg
    echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list >/dev/null
    echo "正在安装docker..."
    sudo apt-get update
    sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin -y
    echo "正在添加当前用户${USER}到docker组..."
    sudo usermod -aG docker "$USER"
    echo "正在设置docker registry国内镜像..."
    if [ -f "/etc/docker/daemon.json" ]; then
        sudo mv /etc/docker/daemon.json{,.bak}
    fi
    cat <<EOF | sudo tee /etc/docker/daemon.json >/dev/null
{
 "registry-mirrors": ["https://docker.mirrors.ustc.edu.cn", "https://registry.docker-cn.com"]
}
EOF
    # 将 docker.sock 文件所有者修改为当前用户,确保 minikube 启动成功
    sudo chown $USER /var/run/docker.sock

    sudo systemctl restart docker.service
    echo "Docker安装完成."
}

function install_kubectl() {
	
    echo "正在下载安装 kubectl"
    # 和 minukube 同理
    # sudo curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl 
    sudo chmod +x ./kubectl
    sudo mv ./kubectl /usr/local/bin/
    echo "kubectl 安装完成..."
}

install_kubectl

function install_minikube() {
    echo "正在下载安装minikube-linux-amd64..."
    # 提前下在 minikube 到当前路径下,因为网络原因下载太慢,因此注释此步骤
    # sudo curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
    sudo install minikube-linux-amd64 /usr/local/bin/minikube
    echo "正在启动minikube..."
    # minikube 清除了所有内容,谨慎使用
    # minikube delete
    # --kubernetes-version=v1.23.8 https://github.com/kubernetes/minikube/issues/14477
    
    minikube start

    minikube status
    
    echo "minikube 启动成功,安装minikube完毕..."
}

install_docker

# echo "正在安装virtualbox..."

# 这里在物理linux机器上运行时使用,如果已经在 vm 虚拟机上,裸机运行即可
# sudo apt install virtualbox virtualbox-ext-pack -y

install_minikube

echo -e "\n\n"

cat <<EOF
**************************************
            docker version
**************************************
EOF

sudo docker version

cat <<EOF
*******************************************
   设置 minikube kubectl 别名为 kubectl
*******************************************
EOF

# 可选
echo 'alias kubectl="minikube kubectl --"' >> ~/.profile
source ~/.profile

cat <<EOF
**************************************
       kubectl -- get po -A
**************************************
EOF

kubectl get pods -A

echo -e "\n 为 root 用户添加 kubectl 的执行权限,原因如下:因为 minikube 在普通用户下启动,root 用户下无 minikube 应用。所以使用时会 8080 refused"

sudo mkdir -p /root/.kube
sudo cp $HOME/.kube/config /root/.kube
sudo su

echo -e "\n 更多信息可参考: https://minikube.sigs.k8s.io/docs/start/"

安装最终效果如下:

yuluo@yuluo-ubuntu:~/minikube$ kubectl get pod -A
NAMESPACE     NAME                               READY   STATUS    RESTARTS        AGE
kube-system   coredns-5d78c9869d-s4hrm           1/1     Running   0               2m57s
kube-system   etcd-minikube                      1/1     Running   0               3m10s
kube-system   kube-apiserver-minikube            1/1     Running   0               3m10s
kube-system   kube-controller-manager-minikube   1/1     Running   0               3m10s
kube-system   kube-proxy-sbpzx                   1/1     Running   0               2m57s
kube-system   kube-scheduler-minikube            1/1     Running   0               3m10s
kube-system   storage-provisioner                1/1     Running   1 (2m36s ago)   3m9s
yuluo@yuluo-ubuntu:~/minikube$ 

部署 minikube dashboard

minikube dashboard

yuluo@yuluo-ubuntu:~$ kubectl get pods -A | grep dashboard
kubernetes-dashboard   dashboard-metrics-scraper-5dd9cbfd69-mzxzp   1/1     Running     0                104s
kubernetes-dashboard   kubernetes-dashboard-5c5cfc8747-np7qt        1/1     Running     0                104s

# 配置 minikube 远程访问
yuluo@yuluo-ubuntu:~$ kubectl proxy --address='0.0.0.0' --disable-filter=true
W1022 09:09:49.061124  193925 proxy.go:175] Request filter disabled, your proxy is vulnerable to XSRF attacks, please be cautious
Starting to serve on [::]:8001

浏览器访问:
http://ip:8001/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/

3. 测试部署应用

1. 编写 Go Application

package main

import (
	"fmt"
	"log"
	"net/http"
)

func main() {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintln(w, "Hello World!")
	})

	log.Fatalln(http.ListenAndServe(":80", nil))
}


2. 编译

  • go mod init

  • go mod tidy

  • GOOS=linux GOARCH=386 go build -ldflags '-s -w' -o webserver

3. 打包 docker 镜像

# docker build -t leo/webserver .
# 为了减小体积,使用scratch,实际使用golang官方镜像
FROM scratch

COPY ./webserver /webserver

CMD ["/webserver"]

4. 构建 Docker 镜像

# 1.本机制作go镜像
docker build -t yuluo/webserver .    (名称必须是 Dockerfile)
docker image save yuluo/webserver > webserver.tar
# 2.上传到minikube虚拟机中docker镜像库
minikube image load webserver.tar

5. 部署

1. 部署 Pod
  1. 编写 yaml

    apiVersion: v1
    kind: Pod
    metadata:
      name: webserver
      labels:
        name: webserver
    spec:
      containers:
      - name: webserver
        image: yuluo/webserver
        imagePullPolicy: Never
        resources:
          limits:
            memory: "128Mi"
            cpu: "500m"
        ports:
          - containerPort: 80
            hostPort: 8080
    

    该字段设置imagePullPolicy: Never使用本地的镜像,否则会从镜像仓库拉取最新导致失败Error: ErrImagePull

    同时 因为设置 hostPort,可以在 minikube node 上访问 minikubeIp:8080

  2. 部署到 minikube

    kubectl apply -f webserver-pod.yaml
    
    # 出现如下表明部署成功
    root@yuluo-ubuntu:/home/yuluo/app/test-deploy-app# kubectl get pods -A
    NAMESPACE     NAME                               READY   STATUS    RESTARTS       AGE
    default       webserver                          1/1     Running   0              7s
    kube-system   coredns-5d78c9869d-s4hrm           1/1     Running   6 (24m ago)    27h
    
  3. 查看 Pod 状态

    kubectl get pods webserver
    kubectl describe pods webserver
    
    root@yuluo-ubuntu:/home/yuluo/app/test-deploy-app# kubectl describe pod webserver
    Name:             webserver
    Namespace:        default
    Priority:         0
    Service Account:  default
    Node:             minikube/192.168.49.2						# 节点 ip
    Start Time:       Sat, 21 Oct 2023 04:22:54 +0000
    Labels:           name=webserver
    Annotations:      <none>
    Status:           Running
    IP:               10.244.0.10								# pod ip
    
  4. 访问测试

    # 使用 minikube ssh 到此 节点 上访问 pod 验证
    minikube ssh --node minikube
    
    curl podIp
    
    # 最终结果如下
    docker@minikube:~$ curl 10.244.0.10
    Hello World!
    
2. 创建 Service 关联 Pod
  1. 编写 yaml 资源文件

    # service
    apiVersion: v1
    kind: Service
    metadata:
      name: webserver-svc
    spec:
      selector:
        name: webserver
      ports:
        - port: 80
          targetPort: 80
          protocol: TCP
    

    上面的示例定义了一个ClusterIP Service。到 ClusterIP 上端口 80 的流量将转发到你的Pod 上的端口 8080 (targetPort配置项),携带 name: webserver 标签的 Pod 将被添加到 Service中作为作为服务的可用端点

  2. 部署 svc

    kubectl apply -f webserver-pod.yaml
    
  3. 查看 SVC 状态

    kubectl get svc
    NAME            TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
    kubernetes      ClusterIP   10.96.0.1       <none>        443/TCP   27h
    webserver-svc   ClusterIP   10.103.70.226   <none>        80/TCP    76s
    
    # kubectl describe service  webserver-svc 通过此命令查看 service 和 pod 的关系 
    root@yuluo-ubuntu:/home/yuluo/app/test-deploy-app# kubectl describe service  webserver-svc
    Name:              webserver-svc
    Namespace:         default
    Labels:            <none>
    Annotations:       <none>
    Selector:          name=webserver
    Type:              ClusterIP
    IP Family Policy:  SingleStack
    IP Families:       IPv4
    IP:                10.103.70.226
    IPs:               10.103.70.226
    Port:              <unset>  80/TCP
    TargetPort:        80/TCP
    Endpoints:         10.244.0.10:80
    Session Affinity:  None
    Events:            <none>
    
  4. 测试访问

    # service 测试访问
    minikube ssh --node minikube
    
    # 显示如下
    docker@minikube:~$ curl 10.244.0.10
    Hello World!
    
3. 创建 Ingress 暴露服务

Ingress 实际上是与Service完全不同的资源,算是Service上面的一层代理,通常在 Service前使用Ingress来提供HTTP路由配置。它让我们可以设置外部 URL、基于域名的虚拟主机、SSL 和负载均衡。此处使用nginx-ingress作为控制器,它使用NGINX服务器作为反向代理来把流量路由给后面的Service。

  1. 设置代理(处理 ingress-nginx image 可能 pull 失败的情况,需要重启 minikube

    1. sudo vim /etc/profile.d/proxy.sh

    2. 添加以下内容到文件中

      export http_proxy="http://10.10.1.10:8080/"
      export https_proxy="http://10.10.1.10:8080/"
      
    3. sudo chmod +x /etc/profile.d/proxy.sh

    4. source /etc/profile.d/proxy.sh
      #查看环境变量进行确认是否生效
      env | grep -i proxy
      
    5. 取消代理

      unset http_proxy
      unset https_proxy
      
    6. 重启 minikube

      yuluo@yuluo-ubuntu:~$ minikube start
      * minikube v1.31.2 on Ubuntu 22.04
      * Using the docker driver based on existing profile
      * Starting control plane node minikube in cluster minikube
      * Pulling base image ...
      * Restarting existing docker container for "minikube" ...
      * Found network options:
        - http_proxy=http://192.168.2.9:7890/
      ! You appear to be using a proxy, but your NO_PROXY environment does not include the minikube IP (192.168.49.2).
      * Please see https://minikube.sigs.k8s.io/docs/handbook/vpn_and_proxy/ for more details
        - https_proxy=http://192.168.2.9:7890/
      * Preparing Kubernetes v1.27.4 on Docker 24.0.4 ...
        - env HTTP_PROXY=http://192.168.2.9:7890/
        - env HTTPS_PROXY=http://192.168.2.9:7890/
      * Configuring bridge CNI (Container Networking Interface) ...
      * Verifying Kubernetes components...
        - Using image gcr.io/k8s-minikube/storage-provisioner:v5
      * Enabled addons: default-storageclass, storage-provisioner
      * Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
      
  2. 环境配置

    # 为了在 minikube 中使用 nginx-ingress ,必须执行以下命令启用
    minikube addons enable ingress
    
    kubectl get pods -A  # 查看 ingress-nginx 是否启动成功,如没有 使用以下命令重试
    kubectl get pod podName -n nameSpace -o yaml | kubectl replace --force -f -
    
    # 如下所示即为成功状态:
    root@yuluo-ubuntu:/home/yuluo/app/test-deploy-app# kubectl get pods -n ingress-nginx | grep ingress-nginx-controller
    ingress-nginx-controller-7799c6795f-29dnh   1/1     Running     0          21h
    
  3. 编写 yaml 资源配置文件

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
      name: webserver-ingress
    spec:
      ingressClassName: nginx-ingress
      rules:
        - host: "webserver.com"
          http:
            paths:
              - path: "/"
                pathType: Prefix
                backend:
                  service:
                    name: webserver-svc
                    port:
                      number: 80
    
  4. 部署 Ingress

    kubectl apply -f webserver-ingress.yaml
    
  5. 查看状态

    # 通过 kubectl get ingress 查看已经创建的 ingress 资源
    
    # 通过 kubectl describe ingress webserver-ingress 查看 service 和 ingress 的关系
    root@yuluo-ubuntu:/home/yuluo/app/test-deploy-app# kubectl describe ingress webserver-ingress
    Name:             webserver-ingress
    Labels:           <none>
    Namespace:        default
    Address:          
    Ingress Class:    nginx-ingress
    Default backend:  <default>
    Rules:
      Host           Path  Backends
      ----           ----  --------
      webserver.com  
                     /   webserver-svc:80 (10.244.0.10:80)
    Annotations:     <none>
    Events:          <none>
    
  6. 测试访问

    # 设置 hosts 文件创建映射关系
    vim /etc/hosts
    <minikube ip> webserver.com
    
    # 测试
    curl webserver.com:8080
    
    # 结果如下:
    root@yuluo-ubuntu:/home/yuluo/app/test-deploy-app# curl webserver.com:8080
    Hello World!
    

4. 安装 Istio

1. 下载 istio 上传到服务器

Istio 安装包地址:https://github.com/istio/istio/releases

2. 安装

# 解压缩
tar -zxvf istio-1.19.3

# 添加 bin 目录到系统 path
export PATH=$HOME/istio/istio-1.19.3/bin:$PATH

# 检查
istioctl version

# 检查是否可以安装 istio
root@yuluo-ubuntu:/home/yuluo# istioctl x precheck
✔ No issues found when checking the cluster. Istio is safe to install or upgrade!
  To get started, check out https://istio.io/latest/docs/setup/getting-started/
  
# 安装 Istio
istioctl install  输入 y
出现如下:安装成功
root@yuluo-ubuntu:/home/yuluo# istioctl install
This will install the Istio 1.19.3 "default" profile (with components: Istio core, Istiod, and Ingress gateways) into the cluster. Proceed? (y/N) y
✔ Istio core installed                              
✔ Istiod installed                               
✔ Ingress gateways installed                                     
✔ Installation complete
Made this installation the default for injection and validation.

# kubectl get pods -A | grep istio-system
yuluo@yuluo-ubuntu:~$ kubectl get pods -A | grep istio-system
istio-system    istio-ingressgateway-cf99dfc5c-f5bnw        1/1     Running     0                11m
istio-system    istiod-78c4f7f756-lnd7g                     1/1     Running     0                11m

3. 安装 Istio dashboard

  1. 导入 grafana ,参考: https://istio.io/latest/zh/docs/tasks/observability/metrics/using-istio-dashboard/

    kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.19/samples/addons/grafana.yaml
    
    # 通过以下命令启动
    istioctl dashboard grafana
    
    # 映射本地访问 (这里 不指定时,只能使用 127.0.0.1 访问,使用 ipv4 地址访问需要指明)
    kubectl port-forward grafana-5f9b8c6c5d-jnd6n -n istio-system --address 192.168.2.13 3000:3000
    
    # 访问如下地址
    http://192.168.2.13:3000/d/G8wLrJIZk/istio-mesh-dashboard
    
  2. 导入 promethems

    kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.19/samples/addons/prometheus.yaml
    
    istioctl dashboard prometheus
    
    kubectl port-forward prometheus-5d5d6d6fc-w7rk4 -n istio-system --address 192.168.2.13 9090:9090
    
  3. 安装 kiali

    kubectl apply -f https://raw.githubusercontent.com/istio/istio/release-1.19/samples/addons/kiali.yaml
    
    kubectl port-forward kiali-7c9d5f9f96-b8bpj -n istio-system --address 172.23.235.246 20001:20001
    
    http://172.23.235.246:20001/
    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值