1. 程式人生 > 程式設計 >K8S學習指南(一)---安裝單機版K8S

K8S學習指南(一)---安裝單機版K8S

K8S學習指南(一)---安裝單機版K8S

Kubernetes 概述

Kubernetes(以下簡稱K8S) 是一個完備的分散式系統支撐平臺。包含安全防護,負載均衡,多粒度的資源配額管理能力. 以下介紹K8S組成部分

Service

  • Service是分散式叢集架構的核心,一個Service物件擁有如下關鍵特徵.
    • 擁有一個唯一指定的名字(比如redis-server).
    • 擁有一個虛擬IP(Cluster Ip或VIP) 和埠號.
    • 能夠提供某種遠端服務能力.
    • 能被對映到提供這種服務能力的一組容器應用上.

Pod

  • Pod是K8S最小的管理元素.
    • 它是一個或多個容器的組合。這些容器共享儲存、網路和名稱空間,以及如何執行的規範
    • 一個Pod的共享上下文是Linux名稱空間、cgroups和其它潛在隔離內容的集合
    • 在不同Pod中的容器,擁有不同的IP地址,因此不能夠直接在程式間進行通訊。容器間通常使用Pod IP地址進行通訊.
    • Pod執行在節點Node的環境上.
    • 每個Pod執行一個特殊容器Pause.其他則為業務容器.
    • 這些業務容器共享Pause容器的網路棧和Volume掛載卷.

Node

  • Node是Pod真正執行的主機,可以物理機,也可以是虛擬機器器.
    • 為了管理Pod,每個Node節點上至少要執行container runtime(比如docker或者rkt)、kubelet和kube-proxy服務.
    • 這些程式負責Pod
      的建立,啟動,監控,重啟,銷燬,以及實現軟體模式的負載均衡器.
    • 節點的狀態資訊包含
      • Addresses. 描述網路地址
      • Condition. 描述所有Running節點的狀態
      • Capacity. 描述節點上可用硬體資源:CPU,RAM,DISK,最大Pod數等.
      • Info. 描述節點基礎資訊.如核心版本,OS名稱等.

搭建Kubernetes

系統準備

* OS: Ubuntu 18.04.1 LTS X86_64
* USER: root
* CPU: 2+   (一定要 2croe +,否則建立minikube 建立的時候會報錯)
* RAM: 2Gib+
複製程式碼

機器建議使用能連線上谷歌的,因為K8S很多包和源都在海外.

搭建步驟

  1. 關閉swap
sudo swapoff -a
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
複製程式碼
  1. 重新整理apt list和更新元件
apt-get update -y
apt-get upgrade -y
apt-get install -y curl wget apt-transport-https -y
複製程式碼
  1. 安裝docker
## install docker
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt update && sudo apt install -y docker-ce

## 設定開機自啟動
sudo systemctl enable docker
systemctl start docker
複製程式碼
  1. 安裝minikube(請確保能連線上Google)
wget https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64
chmod +x ./minikube-linux-amd64
mkdir -p /usr/local/bin/
cp minikube-linux-amd64 /usr/local/bin/minikube

## 檢視版本
sudo minikube version

minikube version: v1.5.2
commit: 792dbf92a1de583fcee76f8791cff12e0c9440ad-dirty
複製程式碼
  1. 安裝Kubectl kubeadm kubernetes-cni
# 先新增金鑰
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" | tee /etc/apt/sources.list.d/kubernetes.list

apt-get update
apt-get install kubectl -y
sudo apt install -y kubeadm  kubernetes-cni

## 檢視版本號
kubectl version -o json

{
  "clientVersion": {
    "major": "1","minor": "16","gitVersion": "v1.16.3","gitCommit": "b3cbbae08ec52a7fc73d334838e18d17e8512749","gitTreeState": "clean","buildDate": "2019-11-13T11:23:11Z","goVersion": "go1.12.12","compiler": "gc","platform": "linux/amd64"
  }
}
複製程式碼
  1. 使用minikube安裝單機版K8S
## minikube cleanup
minikube delete
# debug

## --vm-driver=none  不使用虛擬機器器而是用本機,會降低安全性
export CHANGE_MINIKUBE_NONE_USER=true
minikube start --vm-driver=none
# minikube start --alsologtostderr -v=8    ## debug

* minikube v1.5.2 on Ubuntu 18.04
* Running on localhost (CPUs=2,Memory=3784MB,Disk=50331MB) ...
* OS release is Ubuntu 18.04.1 LTS
* Preparing Kubernetes v1.16.2 on Docker '19.03.5' ...
  - kubelet.resolv-conf=/run/systemd/resolve/resolv.conf
* Downloading kubeadm v1.16.2
* Downloading kubelet v1.16.2
* Pulling images ...
* Launching Kubernetes ... 
* Configuring local host environment ...
* 
! The 'none' driver provides limited isolation and may reduce system security and reliability.
! For more information,see:
  - https://minikube.sigs.k8s.io/docs/reference/drivers/none/
* 
! kubectl and minikube configuration will be stored in /root
! To use kubectl or minikube commands as your own user,you may need to relocate them. For example,to overwrite your own settings,run:
* 
  - sudo mv /root/.kube /root/.minikube $HOME
  - sudo chown -R $USER $HOME/.kube $HOME/.minikube
* 
* This can also be done automatically by setting the env var CHANGE_MINIKUBE_NONE_USER=true
* Waiting for: apiserver
* Done! kubectl is now configured to use "minikube"
複製程式碼

檢查安裝元件的版本

檢查叢集狀態
#kubectl cluster-info

Kubernetes master is running at https://172.19.0.15:8443
KubeDNS is running at https://172.19.0.15:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems,use 'kubectl cluster-info dump'.
複製程式碼
檢查Kubectl的預設配置
# kubectl config view

apiVersion: v1
clusters:
- cluster:
    certificate-authority: /root/.minikube/ca.crt
    server: https://172.19.0.15:8443
  name: minikube
contexts:
- context:
    cluster: minikube
    user: minikube
  name: minikube
current-context: minikube
kind: Config
preferences: {}
users:
- name: minikube
  user:
    client-certificate: /root/.minikube/client.crt
    client-key: /root/.minikube/client.key
複製程式碼
檢查正在執行的節點
# kubectl get nodes

NAME       STATUS   ROLES    AGE   VERSION
minikube   Ready    master   11m   v1.16.2
複製程式碼
檢查Minikube的狀態
# minikube status
host: Running
kubelet: Running
apiserver: Running
kubeconfig: Configured
複製程式碼

列出所有minikube外掛

# minikube addons list

- addon-manager: enabled
- dashboard: enabled
- default-storageclass: enabled
- efk: disabled
- freshpod: disabled
- gvisor: disabled
- heapster: disabled
- helm-tiller: disabled
- ingress: disabled
- ingress-dns: disabled
- logviewer: disabled
- metrics-server: disabled
- nvidia-driver-installer: disabled
- nvidia-gpu-device-plugin: disabled
- registry: disabled
- registry-creds: disabled
- storage-provisioner: enabled
- storage-provisioner-gluster: disabled
複製程式碼

快速安裝指令碼

curl -sL https://raw.githubusercontent.com/charSLee013/Kubernetes-learn/master/chapter01/kubernetes-install-ubuntu.sh | bash
複製程式碼

到這裡,一個單機版的Kubernetes叢集環境已經安裝啟動完成了


更多學習文章在點選訪問