K8S 基本概念和术语
Posted 未末0902
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了K8S 基本概念和术语相关的知识,希望对你有一定的参考价值。
文章目录
提示:以下是本篇文章正文内容,下面案例可供参考
一、k8s是什么?
生产级别的容器编排系统,自动化的容器部署、扩展和管理
Kubernetes 是用于自动部署,扩展和管理容器化应用程序的开源系统。
云原生: 与k8s相关的技术 ,prometheus 可以用来监控容器
Prometheus于 2016 年加入 云原生计算基金会,作为继Kubernetes之后的第二个托管项目。
Minikube 是一种轻量级的 Kubernetes 实现,可在本地计算机上创建 VM 并部署仅包含一个节点的简单集群。
k8s的安装 --》
将Google内部的容器编排工具borg,使用go语言重写,然后开源的
[root@manager17 ~]# cat /etc/centos-release
CentOS Linux release 8.4.2105
[root@manager17 ~]#
二、Cgroup的作用?
1.swap分区的作用?
s**wap分区**:交换分区,从磁盘里划分出一块空间来充当内存使用,性能比真正的物理内存要差。
docker 容器在内存里运行—》不允许容器到swap分区里运行–》为了追求性能
[root@k8s-master ~]# cat /proc/sys/vm/swappiness
30
[root@k8s-master ~]# free -m
total used free shared buff/cache available
Mem: 3709 338 2828 8 542 3145
Swap: 4031 0 4031
[root@k8s-master ~]#
[root@worker-2 ~]# cat /proc/swaps
Filename Type Size Used Priority
[root@worker-2 ~]# swapon -a
[root@worker-2 ~]# cat /proc/swaps
Filename Type Size Used Priority
/dev/dm-1 partition 4128764 0 -2
[root@worker-2 ~]# swapoff -a
[root@worker-2 ~]# cat /proc/swaps
Filename Type Size Used Priority
[root@worker-2 ~]#
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.查看进程
[root@k8s-master ~]# service docker restart
Redirecting to /bin/systemctl restart docker.service
[root@k8s-master ~]# 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
[root@k8s-master ~]#
四.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
查看进程
[root@node3 ~]# 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空间等命名空间
root@master bin]# kubectl delete pod sc-nginx 删除pod sc-nginx
pod "sc-nginx" deleted
[root@master bin]#
[root@master bin]# kubectl get namespace 查看有哪些命名空间
NAME STATUS AGE
default Active 23h
kube-node-lease Active 23h
kube-public Active 23h
kube-system Active 23h
[root@master bin]#
[root@master 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
[root@master bin]#
[root@master 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).
[root@master bin]#
启动nginx的pod,里面的副本数量5个
–replicas 5 副本
kubectl create deployment d-sc-nginx --image nginx:laste -r 5
[root@master bin]# kubectl create deployment d-sc-nginx --image nginx:latest
deployment.apps/d-sc-nginx created
[root@master bin]# kubectl get pod
NAME READY STATUS RESTARTS AGE
d-sc-nginx-676fb46b7-p4449 0/1 ImagePullBackOff 0 15s
[root@master bin]# kubectl log
logger login loginctl logname
[root@master bin]# kubectl log
logger login loginctl logname
[root@master bin]# kubectl logs
[root@master 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
[root@master bin]#
[root@master bin]# kubectl get deployment 查看deployment
NAME READY UP-TO-DATE AVAILABLE AGE
d-sc-nginx 0/1 1 0 3m44s
[root@master bin]# kubectl delete deployment d-sc-nginx 删除deployment
deployment.apps "d-sc-nginx" deleted
[root@master bin]#
[root@master bin]# kubectl create deployment d-sc-nginx --image nginx:latest
deployment.apps/d-sc-nginx created
[root@master bin]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
d-sc-nginx 0/1 1 0 3s
[root@master bin]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
d-sc-nginx 1/1 1 1 9s
[root@master bin]#
[root@master bin]# kubectl get pod
NAME READY STATUS RESTARTS AGE
d-sc-nginx-587c6c899-6rmwr 1/1 Running 0 38s
[root@master 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>
[root@master bin]#
[root@master bin]# kubectl scale --replicas 5 deployment d-sc-nginx
deployment.apps/d-sc-nginx scaled
[root@master bin]#
[root@master bin]# kubectl get rs
NAME DESIRED CURRENT READY AGE
d-sc-nginx-587c6c899 5 5 5 8m26s
[root@master bin]# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
d-sc-nginx 5/5 5 5 8m45s
[root@master 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
[root@master bin]#
[root@master 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>
[root@master bin]#
七.为什么master上没有启动pod?
答案: 这是因为scheduler 调度器会根据调度策略,避免了在master上建立pod
污点: taint
根据pod调度策略和方法:
1.deployment: 全自动调度
2.node selector:定向调度
3.nodeaffinity --》尽量把不同的pod放到一台node上
4.podaffinity --》尽量把相同的pod放到一起
5.taints和tolerations 污点和容忍
以上是关于K8S 基本概念和术语的主要内容,如果未能解决你的问题,请参考以下文章