K8s 搭建與使用

本文內容

  1. 主要對K8s 的安裝做記錄
  2. 安裝前,請準備兩臺以上的linux服務器,且都已經安裝好docker了
  3. 安裝大致步驟
    1. 安裝 kubectl
    2. 安裝 minikube
    3. 啓動可視化界面 dashboard

安裝 kubectl

  1. 下載 kubectl

    1. 下載最新的穩定版本
    curl -LO https://storage.googleapis.com/kubernetes-release/release/`curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt`/bin/linux/amd64/kubectl
    
    1. 常用版本下載
    curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.18.0/bin/linux/amd64/kubectl
    
  2. 添加執行權限

    chmod +x ./kubectl
    
  3. 使其在全局都可執行

    sudo mv ./kubectl /usr/local/bin/kubectl
    

安裝 minikube

  1. 下載minikube
    curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 && chmod +x minikube
    
  2. 使其在全局都可執行
    mv minikube /usr/local/bin/
    

minikube 不可以使用 root 用戶執行

  1. 添加用戶
    $ useradd test
    $ echo 1234 | passwd test --stdin
    $ visudo  # 將用戶添加到 sudo 文件中,就可以使用 sudo 進行授權執行高權限命令了
    # 改成下面這樣
    root     ALL=(ALL)   ALL
    test     ALL=(ALL)   ALL
    
  2. 將用戶 添加到 root組
    $ usermod -aG root test
    
  3. 將用戶添加到 docker組
    $ sudo usermod -aG docker test  && newgrp docker
    

啓動 minikube

  1. 進入 test 用戶
    $ su test
    
  2. 使用國內的docker鏡像源啓動 minikube
    $ minikube start --image-mirror-country='cn' --registry-mirror=https://registry.docker-cn.com --driver=docker
    
  3. 查看是否安裝成功
    $ minikube status
    type: Control Pline
    host: Running
    kubelet: Running
    kubeconfig: Configured
    
  4. 查看k8s節點
    $ kubectl get nodes
    NAME      STATUS  ROLES   AGE   VERSION
    minikube  Ready   master  73m   v1.18.3
    
  5. 創建部署文件,deployment.yaml
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: hello-world
    spec:
      selector:
        matchLabels:
          run: load-balancer-example
      replicas: 2
      template:
        metadata:
          labels:
            run: load-balancer-example
        spec:
          containers:
            - name: hello-world
              image: registry.cn-hangzhou.aliyuncs.com/aliyun_google/google-sample-node-hello:1.0
              ports:
                - containerPort: 8080
                  protocol: TCP
    
  6. 在該文件的目錄下執行命令,部署hello-world
    $ kubectl apply -f deployment.yaml
    
  7. 暴露端口
    $ kubectl expose deployment hello-world --type=NodePort --name=node-service
    
  8. 查看訪問的地址
    $ minikube service list
    或者
    $ minikube service node-service --url
    
  9. 出現 Hello Kubernetes!

啓動 dashboard

  1. minikube dashboard

  2. 報錯如下

    Failed to open connection to "session" message bus: Unable to autolaunch a dbus-daemon without a $DISPLAY for X11
    Running without a11y support!
    Error: no DISPLAY environment variable specified
    
    • 因爲linux除ubuntu外沒有瀏覽器,所以需要加上 --url
    • minikube dashboard --url
  3. 默認地址只能是 本機地址,暴露地址 1.1.1.1 是服務器的地址 8888 是訪問端口

    nohup kubectl proxy  --port=8888 --address='1.1.1.1' --accept-hosts='^1.1.1.1$'  >/dev/null 2>&1& 
    
    • 參考https://www.jianshu.com/p/ef020fa8ca97
  4. 界面展示

在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章