What is Kubernetes?
Kubernetes這個單詞來自于希臘語,含義是 舵手 或 領航員
簡介說明
Production-Grade Container Orchestration Automated container deployment, scaling, and management
生産級的容器編排 自動化的容器部署、擴充和管理
Kubernetes,也稱為K8S,其中8是代表中間“ubernete”的8個字元,是Google在2014年開源的一個容器編排引擎,用于自動化容器化應用程式的部署、規劃、擴充和管理,它将組成應用程式的容器分組為邏輯單元,以便于管理和發現,用于管理雲平台中多個主機上的容器化的應用,Kubernetes 的目标是讓部署容器化的應用簡單并且高效,很多細節都不需要運維人員去進行複雜的手工配置和處理;
Kubernetes擁有Google在生産環境上15年運作的經驗,并結合了社群中最佳實踐;
K8S是
CNCF畢業的項目,本來Kubernetes是Google的内部項目,後來開源出來,又後來為了其茁壯成長,捐給了CNCF;
CNCF全稱Cloud Native Computing Foundation(雲原生計算基金會)
官網:
https://kubernetes.io/代碼:
https://github.com/kubernetes/kubernetesKubernetes是采用Go語言開發的,Go語言是谷歌2009釋出的一款開源程式設計語言;
編排是什麼意思?
- 按照一定的目的依次排列;
- 調配、安排;
整體架構
Master
k8s叢集控制節點,對叢集進行排程管理,接受叢集外使用者去叢集操作請求;
Master Node 由 API Server、Scheduler、ClusterState Store(ETCD 資料庫)和 Controller MangerServer 所組成;
Nodes
叢集工作節點,運作使用者業務應用容器;
Nodes節點也叫Worker Node,包含kubelet、kube proxy 和 Pod(Container Runtime);
搭建方式
部署 Kubernetes 環境(叢集)主要有多種方式:
- minikube
minikube可以在本地運作Kubernetes的工具,minikube可以在個人計算機(包括Windows,macOS和Linux PC)上運作一個單節點Kubernetes叢集,以便您可以試用Kubernetes或進行日常開發工作;
https://kubernetes.io/docs/tutorials/hello-minikube/- kind
Kind和minikube類似的工具,讓你在本地計算機上運作Kubernetes,此工具需要安裝并配置Docker;
https://kind.sigs.k8s.io/- kubeadm
Kubeadm是一個K8s部署工具,提供kubeadm init 和 kubeadm join兩個操作指令,可以快速部署一個Kubernetes叢集;
官方位址:
https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm/ https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/- 二進制包
從Github下載下傳發行版的二進制包,手動部署安裝每個元件,組成Kubernetes叢集,步驟比較繁瑣,但是能讓你對各個元件有更清晰的認識;
- yum安裝
通過yum安裝Kubernetes的每個元件,組成Kubernetes叢集,不過yum源裡面的k8s版本已經比較老了,是以這種方式用得也比較少了;
- 第三方工具
有一些大神封裝了一些工具,利用這些工具進行k8s環境的安裝;
- 花錢購買
直接購買類似阿裡雲這樣的公有雲平台k8s,一鍵搞定;
Kubeadm部署
kubeadm是官方社群推出的一個用于快速部署 kubernetes 叢集的工具,這個工具能通過兩條指令完成一個kubernetes叢集的部署;
- 建立一個Master節點:
kubeadm init
- 将Node節點加入到Master叢集中:
$ kubeadm join <Master節點的IP和端口>
環境要求
- 一台或多台機器,作業系統CentOS 7.x-86_x64
- 硬體配置:記憶體2GB或2G+,CPU 2核或CPU 2核+;
- 叢集内各個機器之間能互相通信;
- 叢集内各個機器可以通路外網,需要拉取鏡像;(非必須)
- 禁止swap分區;
如果環境不滿足要求,會報錯,比如:
環境準備
- 關閉防火牆
systemctl stop firewalld systemctl disable firewalld
- 關閉selinux
sed -i 's/enforcing/disabled/' /etc/selinux/config #永久 setenforce 0 #臨時
- 關閉swap(k8s禁止虛拟記憶體以提高性能)
sed -ri 's/.*swap.*/#&/' /etc/fstab #永久 swapoff -a #臨時
- 在master添加hosts
cat >> /etc/hosts << EOF 172.16.45.131 k8smaster 172.16.45.132 k8snode1 172.16.45.133 k8snode2 EOF
- 設定網橋參數
systemctl stop firewalld systemctl disable firewalld
- 時間同步
systemctl stop firewalld systemctl disable firewalld
安裝步驟
所有伺服器節點安裝 Docker/kubeadm/kubelet/kubectl
Kubernetes 預設容器運作環境是Docker,是以首先需要安裝Docker;
安裝 Docker
#更新docker的yum源
yum install wget -y
wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
#安裝指定版本的docker:
yum install docker-ce-19.03.13 -y
#yum install docker -y (這個安裝的Docker版本偏舊) 1.13.x
#配置加速器加速下載下傳 登入該網址擷取加速位址(https://cr.console.aliyun.com/)
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://bz93554o.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
#然後執行以下指令不然會提示警告;
systemctl enable docker.service
#那麼接下來需要搭建:kubeadm、kubelet、kubectl
配置k8s的阿裡雲YUM源
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
到時候下載下傳k8s的相關元件才能找到下載下傳源;
安裝 kubeadm,kubelet 和 kubectl
yum install kubelet-1.19.4 kubeadm-1.19.4 kubectl-1.19.4 -y
#然後執行以下指令不然會提示警告;
systemctl enable kubelet.service
#檢視有沒有安裝:
yum list installed | grep kubelet
yum list installed | grep kubeadm
yum list installed | grep kubectl
#檢視安裝的版本:
kubelet --version
Kubelet:運作在cluster所有節點上,負責啟動POD和容器;
Kubeadm:用于初始化cluster的一個工具;
Kubectl:kubectl是kubenetes指令行工具,通過kubectl可以部署和管理應用,檢視各種資源,建立,删除和更新元件;
切記:此時應該重新開機一下centos;
重要的事情說三遍!!!
部署Master主節點
在master機器上執行以下指令;
kubeadm init --apiserver-advertise-address=172.16.45.131 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.19.4 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16
說明:
service-cidr 的選取不能和PodCIDR及本機網絡有重疊或者沖突,一般可以選擇一個本機網絡和PodCIDR都沒有用到的私網位址段,比如PODCIDR使用10.244.0.0/16, 那麼service cidr可以選擇10.96.0.0/12,網絡無重疊沖突即可;
執行成功後會顯示以下資訊
[root@k8smaster ~]# kubeadm init --apiserver-advertise-address=172.16.45.131 --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.19.4 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16
W0602 21:10:27.153412 8853 configset.go:348] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io]
[init] Using Kubernetes version: v1.19.4
[preflight] Running pre-flight checks
[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
[preflight] Pulling images required for setting up a Kubernetes cluster
[preflight] This might take a minute or two, depending on the speed of your internet connection
[preflight] You can also perform this action in beforehand using 'kubeadm config images pull'
[certs] Using certificateDir folder "/etc/kubernetes/pki"
[certs] Generating "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [k8smaster kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 172.16.45.131]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [k8smaster localhost] and IPs [172.16.45.131 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [k8smaster localhost] and IPs [172.16.45.131 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "sa" key and public key
[kubeconfig] Using kubeconfig folder "/etc/kubernetes"
[kubeconfig] Writing "admin.conf" kubeconfig file
[kubeconfig] Writing "kubelet.conf" kubeconfig file
[kubeconfig] Writing "controller-manager.conf" kubeconfig file
[kubeconfig] Writing "scheduler.conf" kubeconfig file
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Starting the kubelet
[control-plane] Using manifest folder "/etc/kubernetes/manifests"
[control-plane] Creating static Pod manifest for "kube-apiserver"
[control-plane] Creating static Pod manifest for "kube-controller-manager"
[control-plane] Creating static Pod manifest for "kube-scheduler"
[etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests"
[wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s
[apiclient] All control plane components are healthy after 14.502742 seconds
[upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
[kubelet] Creating a ConfigMap "kubelet-config-1.19" in namespace kube-system with the configuration for the kubelets in the cluster
[upload-certs] Skipping phase. Please see --upload-certs
[mark-control-plane] Marking the node k8smaster as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node k8smaster as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: uk5qox.2z2gpcq1qtjl7hlr
[bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes
[bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace
[kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key
[addons] Applied essential addon: CoreDNS
[addons] Applied essential addon: kube-proxy
Your Kubernetes control-plane has initialized successfully!
To start using your cluster, you need to run the following as a regular user:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/
Then you can join any number of worker nodes by running the following on each as root:
kubeadm join 172.16.45.131:6443 --token uk5qox.2z2gpcq1qtjl7hlr \
--discovery-token-ca-cert-hash sha256:ce6240b7d71a93309c46e99d450028d2d36fcb508960c557e6ce56bfaf0b1c58
接下來在master機器上繼續執行:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
檢視節點
kubectl get nodes
部署Node節點
kubeadm join 172.16.45.131:6443 --token uk5qox.2z2gpcq1qtjl7hlr \
--discovery-token-ca-cert-hash sha256:ce6240b7d71a93309c46e99d450028d2d36fcb508960c557e6ce56bfaf0b1c58
成功後會顯示以下資訊
[root@k8snode1 ~]# kubeadm join 172.16.45.131:6443 --token uk5qox.2z2gpcq1qtjl7hlr \
> --discovery-token-ca-cert-hash sha256:ce6240b7d71a93309c46e99d450028d2d36fcb508960c557e6ce56bfaf0b1c58
[preflight] Running pre-flight checks
[WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/
[preflight] Reading configuration from the cluster...
[preflight] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml'
[kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
[kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
[kubelet-start] Starting the kubelet
[kubelet-start] Waiting for the kubelet to perform the TLS Bootstrap...
This node has joined the cluster:
* Certificate signing request was sent to apiserver and a response was received.
* The Kubelet was informed of the new secure connection details.
Run 'kubectl get nodes' on the control-plane to see this node join the cluster.
此時在master上檢視節點顯示以下資訊說明加入成功
[root@k8smaster ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8smaster NotReady master 172m v1.19.4
k8snode1 NotReady <none> 120m v1.19.4
k8snode2 NotReady <none> 120m v1.19.4
部署網絡插件
下載下傳kube-flannel.yml檔案
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
應用kube-flannel.yml檔案得到運作時容器
#在master機器上執行
kubectl apply -f kube-flannel.yml
[root@k8smaster ~]# kubectl apply -f kube-flannel.yml
podsecuritypolicy.policy/psp.flannel.unprivileged created
clusterrole.rbac.authorization.k8s.io/flannel created
clusterrolebinding.rbac.authorization.k8s.io/flannel created
serviceaccount/flannel created
configmap/kube-flannel-cfg created
daemonset.apps/kube-flannel-ds created
#(稍等幾分鐘後) 檢視節點資訊 會看見狀态發生變化
[root@k8smaster ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
k8smaster Ready master 172m v1.19.4
k8snode1 Ready <none> 120m v1.19.4
k8snode2 Ready <none> 120m v1.19.4
至此我們的k8s環境就搭建好了;
檢視運作時容器pod (一個pod裡面運作了多個docker容器)
kubectl get pods -n kube-system
部署容器化應用
#部署nginx
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --type=NodePort
kubectl get pod,svc
#通路位址:http://NodeIP:Port
#部署Tomcat:
kubectl create deployment tomcat --image=tomcat
kubectl expose deployment tomcat --port=8080 --type=NodePort
#通路位址:http://NodeIP:Port
K8s部署微服務
1、項目打包(jar、war)-->可以采用一些工具git、maven、jenkins
2、制作Dockerfile檔案,生成鏡像;
3、kubectl create deployment nginx --image= 你的鏡像
4、你的springboot就部署好了,是以docker容器的方式運作在pod裡面的;