文章目錄
- 一、k8s是什麼?
- 二、Cgroup的作用?
-
- 1.swap分區的作用?
- 2.kubeadm,kubelet和kubectl
- 三.如何知道我們的docker軟體安裝了沒有?
- 四.k8s的容器編排功能,它的實作背後有哪些東西?
- 五.Deployment 控制器
- 六.k8s術語和指令
- 七.為什麼master上沒有啟動pod?
提示:以下是本篇文章正文内容,下面案例可供參考
一、k8s是什麼?
生産級别的容器編排系統,自動化的容器部署、擴充和管理
Kubernetes 是用于自動部署,擴充和管理容器化應用程式的開源系統。
雲原生: 與k8s相關的技術 ,prometheus 可以用來監控容器
Prometheus于 2016 年加入 雲原生計算基金會,作為繼Kubernetes之後的第二個托管項目。
Minikube 是一種輕量級的 Kubernetes 實作,可在本地計算機上建立 VM 并部署僅包含一個節點的簡單叢集。
k8s的安裝 --》
将Google内部的容器編排工具borg,使用go語言重寫,然後開源的
[[email protected] ~]# cat /etc/centos-release
CentOS Linux release 8.4.2105
[[email protected] ~]#
二、Cgroup的作用?
1.swap分區的作用?
s**wap分區**:交換分區,從磁盤裡劃分出一塊空間來充當記憶體使用,性能比真正的實體記憶體要差。
docker 容器在記憶體裡運作—》不允許容器到swap分區裡運作–》為了追求性能
[[email protected] ~]# cat /proc/sys/vm/swappiness
30
[[email protected] ~]# free -m
total used free shared buff/cache available
Mem: 3709 338 2828 8 542 3145
Swap: 4031 0 4031
[[email protected] ~]#
[[email protected] ~]# cat /proc/swaps
Filename Type Size Used Priority
[[email protected] ~]# swapon -a
[[email protected] ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/dm-1 partition 4128764 0 -2
[[email protected] ~]# swapoff -a
[[email protected] ~]# cat /proc/swaps
Filename Type Size Used Priority
[[email protected] ~]#
2.kubeadm,kubelet和kubectl
kubeadm --》k8s的管理程式–》在master上運作的–》建立整個k8s叢集
kubelet --》在node節點上用來管理容器的–》管理docker,告訴docker程式去啟動容器
master和node通信用的–》管理docker,告訴docker程式去啟動容器
一個在叢集中每個節點(node)上運作的代理。 它保證容器(containers)都 運作在 Pod 中。
kubectl --》在master上用來給node節點發号施令的程式,用來控制node節點的,告訴它們做什麼事情的,是指令行操作的工具
三.如何知道我們的docker軟體安裝了沒有?
1.啟動服務
2.檢視軟體清單
rpm -qa|grep docker
3.檢視程序
[[email protected] ~]# service docker restart
Redirecting to /bin/systemctl restart docker.service
[[email protected] ~]# rpm -qa|grep docker
docker-scan-plugin-0.8.0-3.el8.x86_64
docker-ce-rootless-extras-20.10.8-3.el8.x86_64
docker-ce-cli-20.10.8-3.el8.x86_64
docker-ce-20.10.8-3.el8.x86_64
[[email protected] ~]#
四.k8s的容器編排功能,它的實作背後有哪些東西?
docker ps
ps aux|grep kube
docker images
kube-apiserver 外交部長 :對外的一個接口服務
kube-scheduler 排程器 : 負責容器被配置設定到那個node節點上啟動
kube-proxy 負責暴露服務後的負載均衡 ,将流量導入到各個容器
kube-controller-manager 控制管理程式: 副本數量20個pod等
etcd 資料庫:存儲資料的地方
coredns 内部dns伺服器 ,内部域名查詢使用的
flannel 叢集裡的節點伺服器之間通信使用的
pause pod裡都會啟動一個pause容器,讓整個pod共享一個命名空間(網絡,mount,程序等),整個pod裡的容器可以互相通路
node節點上的kube相關的程序
kubelet :在node節點上幫着master管理容器的,是agent代理。
kube-proxy: 暴露服務(釋出服務–》dna
檢視程序
[[email protected] ~]# ps aux|grep kube
root 223212 3.7 4.1 1826444 156064 ? Ssl 04:22 12:48 /usr/bin/kubelet --bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf --config=/var/lib/kubelet/config.yaml --network-plugin=cni --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.5
root 346348 0.6 1.0 747708 40540 ? Ssl 10:02 0:00 /usr/local/bin/kube-proxy --config=/var/lib/kube-proxy/config.conf --hostname-override=node3
root 346643 0.7 1.0 1265668 38092 ? Ssl 10:02 0:00 /opt/bin/flanneld --ip-masq --kube-subnet-mgr
root 346845 0.0 0.0 12
五.Deployment 控制器
pod 是k8s裡容器排程的最小的單元,一個pod裡可以有多個容器,這些容器可以是一樣的鏡像,也是可以使用不一樣的鏡像,可以全部是nginx,也可以有nginx,MySQL,redis等容器組合成一個pod,pod裡的容器的數量,可以是1,也可以是多個。
pod裡的容器共享一個ip位址(網絡命名空間),共享mount空間等命名空間
[email protected] bin]# kubectl delete pod sc-nginx 删除pod sc-nginx
pod "sc-nginx" deleted
[[email protected] bin]#
[[email protected] bin]# kubectl get namespace 檢視有哪些命名空間
NAME STATUS AGE
default Active 23h
kube-node-lease Active 23h
kube-public Active 23h
kube-system Active 23h
[[email protected] bin]#
[[email protected] bin]# kubectl get pod -A 檢視所有命名空間裡的pod
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-7f6cbbb7b8-6fwrz 1/1 Running 10 (74m ago) 21h
kube-system coredns-7f6cbbb7b8-hmbtm 1/1 Running 10 (74m ago) 21h
kube-system etcd-master 1/1 Running 7 (87m ago) 23h
kube-system kube-apiserver-master 1/1 Running 7 (87m ago) 23h
kube-system kube-controller-manager-master 1/1 Running 10 (87m ago) 23h
kube-system kube-flannel-ds-55q8d 1/1 Running 2 (73m ago) 21h
kube-system kube-flannel-ds-nlj5v 1/1 Running 1 (74m ago) 22h
kube-system kube-flannel-ds-vs84g 1/1 Running 3 (87m ago) 22h
kube-system kube-flannel-ds-wtxc9 1/1 Running 1 (73m ago) 22h
kube-system kube-proxy-2jg9r 1/1 Running 2 (73m ago) 23h
kube-system kube-proxy-jtc74 1/1 Running 1 (74m ago) 23h
kube-system kube-proxy-s4gvn 1/1 Running 2 (73m ago) 21h
kube-system kube-proxy-v58gz 1/1 Running 8 (87m ago) 23h
kube-system kube-scheduler-master 1/1 Running 8 (87m ago) 23h
[[email protected] bin]#
[[email protected] bin]# kubectl --help
kubectl controls the Kubernetes cluster manager.
Find more information at: https://kubernetes.io/docs/reference/kubectl/overview/
六.k8s術語和指令
Basic Commands (Beginner):
create Create a resource from a file or from stdin
expose Take a replication controller, service, deployment or pod and expose it as a new Kubernetes service
run 在叢集中運作一個指定的鏡像
set 為 objects 設定一個指定的特征
Basic Commands (Intermediate):
explain Get documentation for a resource
get 顯示一個或更多 resources
edit 在伺服器上編輯一個資源
delete Delete resources by file names, stdin, resources and names, or by resources and label selector
Deploy Commands:
rollout Manage the rollout of a resource
scale Set a new size for a deployment, replica set, or replication controller
autoscale Auto-scale a deployment, replica set, stateful set, or replication controller
Cluster Management Commands:
certificate 修改 certificate 資源.
cluster-info Display cluster information
top Display resource (CPU/memory) usage
cordon 标記 node 為 unschedulable
uncordon 标記 node 為 schedulable
drain Drain node in preparation for maintenance
taint 更新一個或者多個 node 上的 taints
Troubleshooting and Debugging Commands:
describe 顯示一個指定 resource 或者 group 的 resources 詳情
logs 輸出容器在 pod 中的日志
attach Attach 到一個運作中的 container
exec 在一個 container 中執行一個指令
port-forward Forward one or more local ports to a pod
proxy 運作一個 proxy 到 Kubernetes API server
cp Copy files and directories to and from containers
auth Inspect authorization
debug Create debugging sessions for troubleshooting workloads and nodes
Advanced Commands:
diff Diff the live version against a would-be applied version
apply Apply a configuration to a resource by file name or stdin
patch Update fields of a resource
replace Replace a resource by file name or stdin
wait Experimental: Wait for a specific condition on one or many resources
kustomize Build a kustomization target from a directory or URL.
Settings Commands:
label 更新在這個資源上的 labels
annotate 更新一個資源的注解
completion Output shell completion code for the specified shell (bash or zsh)
Other Commands:
api-resources Print the supported API resources on the server
api-versions Print the supported API versions on the server, in the form of "group/version"
config 修改 kubeconfig 檔案
plugin Provides utilities for interacting with plugins
version 輸出 client 和 server 的版本資訊
Usage:
kubectl [flags] [options]
Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).
[[email protected] bin]#
啟動nginx的pod,裡面的副本數量5個
–replicas 5 副本
kubectl create deployment d-sc-nginx --image nginx:laste -r 5
[[email protected] bin]# kubectl create deployment d-sc-nginx --image nginx:latest
deployment.apps/d-sc-nginx created
[[email protected] bin]# kubectl get pod
NAME READY STATUS RESTARTS AGE
d-sc-nginx-676fb46b7-p4449 0/1 ImagePullBackOff 0 15s
[[email protected] bin]# kubectl log
logger login loginctl logname
[[email protected] bin]# kubectl log
logger login loginctl logname
[[email protected] bin]# kubectl logs
[[email protected] bin]# kubectl logs d-sc-nginx-676fb46b7-p4449 --》檢視pod日志,用來排錯
Error from server (BadRequest): container "nginx" in pod "d-sc-nginx-676fb46b7-p4449" is waiting to start: trying and failing to pull image
[[email protected] bin]#
[[email protected] bin]# kubectl get deployment 檢視deployment
NAME READY UP-TO-DATE AVAILABLE AGE
d-sc-nginx 0/1 1 0 3m44s
[[email protected] bin]# kubectl delete deployment d-sc-nginx 删除deployment
deployment.apps "d-sc-nginx" deleted
[[email protected] bin]#
[[email protected] bin]# kubectl create deployment d-sc-nginx --image nginx:latest
deployment.apps/d-sc-nginx created
[[email protected] bin]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
d-sc-nginx 0/1 1 0 3s
[[email protected] bin]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
d-sc-nginx 1/1 1 1 9s
[[email protected] bin]#
[[email protected] bin]# kubectl get pod
NAME READY STATUS RESTARTS AGE
d-sc-nginx-587c6c899-6rmwr 1/1 Running 0 38s
[[email protected] bin]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
d-sc-nginx-587c6c899-6rmwr 1/1 Running 0 82s 10.244.3.7 node2 <none> <none>
[[email protected] bin]#
[[email protected] bin]# kubectl scale --replicas 5 deployment d-sc-nginx
deployment.apps/d-sc-nginx scaled
[[email protected] bin]#
[[email protected] bin]# kubectl get rs
NAME DESIRED CURRENT READY AGE
d-sc-nginx-587c6c899 5 5 5 8m26s
[[email protected] bin]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
d-sc-nginx 5/5 5 5 8m45s
[[email protected] bin]# kubectl get pod
NAME READY STATUS RESTARTS AGE
d-sc-nginx-587c6c899-6j7vs 1/1 Running 0 3m31s
d-sc-nginx-587c6c899-6rmwr 1/1 Running 0 11m
d-sc-nginx-587c6c899-7bkhh 1/1 Running 0 3m31s
d-sc-nginx-587c6c899-7fllb 1/1 Running 0 3m31s
d-sc-nginx-587c6c899-fvdrr 1/1 Running 0 3m31s
[[email protected] bin]#
[[email protected] bin]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
d-sc-nginx-587c6c899-6j7vs 1/1 Running 0 4m56s 10.244.2.6 node3 <none> <none>
d-sc-nginx-587c6c899-6rmwr 1/1 Running 0 12m 10.244.3.7 node2 <none> <none>
d-sc-nginx-587c6c899-7bkhh 1/1 Running 0 4m56s 10.244.3.8 node2 <none> <none>
d-sc-nginx-587c6c899-7fllb 1/1 Running 0 4m56s 10.244.2.7 node3 <none> <none>
d-sc-nginx-587c6c899-fvdrr 1/1 Running 0 4m56s 10.244.3.9 node2 <none> <none>
[[email protected] bin]#
七.為什麼master上沒有啟動pod?
答案: 這是因為scheduler 排程器會根據排程政策,避免了在master上建立pod
污點: taint
根據pod排程政策和方法:
1.deployment: 全自動排程
2.node selector:定向排程
3.nodeaffinity --》盡量把不同的pod放到一台node上
4.podaffinity --》盡量把相同的pod放到一起
5.taints和tolerations 污點和容忍