本文對KuberNetes 中的概念做簡單介紹。同時記錄了一次KuberNetes和KuberSphere的部署經歷。
初識Kubernetes
生產級別的容器編排系統
自動化的容器部署、擴展和管理
簡單的說Kubernetes就是用來管理Docker的。它解決了Docker之上,應用之下,這個層面上許多復雜的問題。如自動部署,限制資源,自動重啟、遷移故障,簡化應用的訪問等。
准備知識
master
管理節點,與node
工作節點相對。node干活master管理。
其中master包含組件:
api server
操作入口。Kubernetes的所有操作指令由對此下達。
etcd
集群內部用鍵值數據庫。
scheduler
調度器。調度工作的。
controller
控制器。發布實際的指令。
node 中包含組件:
pod
幾個相關容器(docker),它們被統一管理。Kubernetes的基本管理單位。
kubelet
代理。master節點命令的實際執行者。
kube-proxy
網絡代理,網絡入口和出口。
kubectl
命令行管理工具。
volume
數據卷,保存數據。
概念:
deployment
部署。指在節點部署的pod。
service
服務,組合多個部署的pod,提供對外訪問。
label
標簽,selector
選擇器,如給節點打上某標簽,可以在部署應用時,只部署在包含標簽的節點上。
namespace
命名空間,做邏輯隔離。
kubeadm
集群部署工具。
Ingress
應用網關,統一出口、負載均衡。
架構圖
KubeSphere
面向雲原生應用的容器混合雲
Kubernetes的圖形化的、優秀的管理界面,代替命令行的操作方式。
可以管理多雲上的集群。將是日常工作中打交道最多的工具。
Kubernetes集群搭建
程序版本:
-
kubernetes v1.17.3
-
Docker version 18.03.1-ce, build 9ee9f40
-
KubeSphere 3.0.0
配置文件:
安裝過程中用到的配置文件。
https://gitee.com/blue1018/blue/tree/master/kubernetes_start/k8s
安裝前置條件
-
一台或多台機器,操作系統Centos7。
-
硬件配置:3072MB或更多RAM,2個CPU或更多CPU,硬盤30GB或更多。
-
各節點網絡通暢。
所有節點下的操作
操作系統設置
#修改時區
timedatectl set-timezone Asia/Shanghai
#關閉防火牆
systemctl stop firewalld
systemctl disable firewalld
#關閉Linux
sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0
#關閉swap
swapoff -a #臨時關閉
sed -ri 's/.*swap.*/#&/' /etc/fstab #永久關閉
free -g #驗證,swap必須為0
#將橋接的IPV4流量傳遞到iptables的鏈:
cat > /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system
設置阿里雲Centos鏡像源
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache
yum update -y
安裝Docker
#依賴
yum install -y yum-utils device-mapper-persistent-data lvm2
#源
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
#版本
yum list docker-ce --showduplicates | sort -r
#安裝
yum install docker-ce-18.03.1.ce-1.el7.centos -y
#啟動
systemctl start docker
systemctl enable docker
#設置阿里雲docker鏡像源
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://mt1tth70.mirror.aliyuncs.com"]
}
EOF
systemctl daemon-reload
systemctl restart docker
安裝 kubectl、kubeadm、kubelet
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum install -y kubelet-1.17.3 kubeadm-1.17.3 kubectl-1.17.3
systemctl enable kubelet && systemctl start kubelet
主節點下的操作
鏡像拉取
sh master_images.sh
初始化主節點
其中apiserver-advertise-address 要換成自己的IP。
kubeadm init \
--apiserver-advertise-address=192.168.31.201 \
--image-repository registry.cn-hangzhou.aliyuncs.com/google_containers \
--kubernetes-version v1.17.3 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=10.244.0.0/16
安裝正常時的返回結果:
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 192.168.31.201:6443 --token 09543n.9a2q9btquudvw8gb \
--discovery-token-ca-cert-hash sha256:d3b644e51a52fb80fb79037dfaf7e8c6af8435ea5f1a2567699b46cd02a800ec
執行返回結果中的命令
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
pod進度監控
新開個ssh窗口監視pod安裝進度
watch kubectl get all --all-namespaces -o wide
安裝網絡插件
kubectl apply -f kube-flannel.yml
子節點加入集群
主節點中建立TOKEN
kubeadm token create --print-join-command
子節點執行,執行返回結果中的命令,加入集群
kubeadm join 192.168.31.201:6443 --token sg47f3.4asffoi6ijb8ljhq \
--discovery-token-ca-cert-hash sha256:81fccdd29970cbc1b7dc7f171ac0234d53825bdf9b05428fc9e6767436991bfb
[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/
[WARNING SystemVerification]: this Docker version is not on the list of validated versions: 18.03.1-ce. Latest validated version: 19.03
[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] Downloading configuration for the kubelet from the "kubelet-config-1.17" ConfigMap in the kube-system namespace
[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...
在主節點查看集群的node
kubectl get nodes
NAME STATUS ROLES AGE VERSION
node201 Ready master 3m3s v1.17.3
node202 Ready <none> 111s v1.17.3
node203 Ready <none> 83s v1.17.3
部署Ingess
kubectl apply -f ingress-controller.yaml
部署一個nginx
以nginx為例測試Ingress
kubectl create deployment nginx --image=nginx
kubectl expose deployment nginx --port=80 --target-port=80 --type=NodePort
用Ingress暴露一個服務
kubectl apply -f nginx-ingress.yaml
通過Ingress訪問 Nginx服務
瀏覽器里通過 nginx.dev.com
訪問。
這里Ingress 用域名 nginx.dev.com
暴露的Nginx服務。需要修改本地host文件,把域名映射到部署了ingress的節點的可訪問IP。
KuberSphere 安裝
在主節點最小化安裝。
安裝openebs
Kubernetes的存儲方案
刪除污點,node201
換成你自己的master hostname。
kubectl taint nodes node201 node-role.kubernetes.io/master:NoSchedule-
建立命名空間、安裝
kubectl create ns openebs
kubectl apply -f openebs-operator.yaml
等待安裝結束
kubectl get sc
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
openebs-device openebs.io/local Delete WaitForFirstConsumer false 16m
openebs-hostpath openebs.io/local Delete WaitForFirstConsumer false 16m
openebs-jiva-default openebs.io/provisioner-iscsi Delete Immediate false 16m
openebs-snapshot-promoter volumesnapshot.external-storage.k8s.io/snapshot-promoter Delete Immediate false 16m
設置默認storageclass
kubectl patch storageclass openebs-hostpath -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}'
storageclass.storage.k8s.io/openebs-hostpath patched
安裝完成后還原污點
kubectl taint nodes node201 node-role.kubernetes.io/master=:NoSchedule
安裝kubesphere
kubectl apply -f kubesphere-installer.yaml
kubectl apply -f cluster-configuration.yaml
查看安裝日志
kubectl logs -n kubesphere-system $(kubectl get pod -n kubesphere-system -l app=ks-install -o jsonpath='{.items[0].metadata.name}') -f
等待幾分鍾的時間、日志出現下面內容就可以訪問了。
#####################################################
### Welcome to KubeSphere! ###
#####################################################
Console: http://10.0.2.12:30880
Account: admin
Password: P@88w0rd
NOTES:
1. After logging into the console, please check the
monitoring status of service components in
the "Cluster Management". If any service is not
ready, please wait patiently until all components
are ready.
2. Please modify the default password after login.
#####################################################
https://kubesphere.io 2021-11-15 16:21:06
#####################################################
結語
完成集群搭建后,可閱讀 Kubernetes、KuberSphere 官方文檔。做進一步探索,如高可用的Kubernetes集群、KuberSphere完整的DEVOPS流。
附錄
常用命令例子
kubectl get all --all-namespaces -o wide #查看集群所有資源
kubectl get pods --all-namespaces #查看所有空間下的pod
kubectl create deployment nginx --image=nginx #以nginx鏡像建立一個名為nginx的部署。
kubectl get deployment #查看部署
kubectl scale --replicas=4 deployment nginx #修改名為nginx的部署,副本數量為4
kubectl get pod nginx -o yaml #輸出名為nginx的pod的yaml配置信息。
kubectl create deployment nginx --image=nginx --dry-run -o yaml #dry-run不真正運行,只為輸出配置信息。
kubectl api-versions #查看api版本
kubectl expose deployment nginx --port=80 --target-port=80 --type=NodePort #暴露nginx服務,訪問端口80.
kubectl get ingress --all-namespaces #查看網關的路由。
kubectl describe pod redis-6fd6c6d6f9-zwvmd -n kubesphere-system #查看指定空間、指定名稱的pod的詳細信息。
kubectl describe ingress test #查看名為test的路由的詳細信息
kubectl delete ingress web #刪除名為web的ingress路由
kubectl edit ingress nginx-web#編輯名為nginx-web的路由
kubectl get nodes --show-labels #查看節點的標簽
kubectl apply -f nginx.yaml #根據配置文件進行操作
kubectl delete deployment.apps/nginx #刪除nginx部署
kubectl delete service/nginx #刪除nginx服務