k8s实战
NameSpace
In Kubernetes, namespaces provides a mechanism for isolating groups of resources within a single cluster. Names of resources need to be unique within a namespace, but not across namespaces. Namespace-based scoping is applicable only for namespaced objects (e.g. Deployments, Services, etc) and not for cluster-wide objects (e.g. StorageClass, Nodes, PersistentVolumes, etc) .
namespace提供了一种在集群中隔离资源组的机制,每一个资源都有所属的namespace。可以使用kubectl get ns查询namespace的种类。也可以使用"-n"参数查询指定namespace的资源。
可以使用kubectl create/delete ns name对namespace进行创建/删除,注意删除时会将namespace中的资源共同删除。
也可以使用yaml配置文件创建namespace,同理,删除.yaml文件(kubectl delete -f .yaml)就对namespace的删除。
apiVersion: v1 # 版本号
kind: Namespace # 资源类型
metadata:
name: hello # namespace的名称
# 使用kubectl apply -f对该配置文件进行应用,就成功创建了名称为hello的namespace
Pod
Pods are the smallest deployable units of computing that you can create and manage in Kubernetes. A Pod (as in a pod of whales or pea pod) is a group of one or more containers, with shared storage and network resources, and a specification for how to run the containers. A Pod’s contents are always co-located and co-scheduled, and run in a shared context. A Pod models an application-specific “logical host”: it contains one or more application containers which are relatively tightly coupled. In non-cloud contexts, applications executed on the same physical or virtual machine are analogous to cloud applications executed on the same logical host.
Pod是运行的一组容器,k8s创建和管理的最小的可部署计算单元。
可以使用kubectl run mynginx --image=nginx创建pod,此时创建的pod在default的namesapce下。也可以使用.yaml配置文件来创建。注意:因为端口号冲突,一个pod中不能启动两个相同的镜像。
apiVersion: v1
kind: Pod
metadata:
labels:
run: mynginx
name: mynginx # pod名称
namespace: default
spec:
containers: # 容器
- image: nginx # 镜像名称
name: mynginx
- image: tomcat
name: mytomcat
kubectl describe pod xx 查看pod的状况。
kubectl delete pod -n namespace xx 删除pod。
kubectl logs xx 查看pod日志,加入-f可以追踪pod运行日志。
kubectl get pod -owide 查看pod详细信息。k8s会为每一个pod分配ip,使用pod ip+容器port就可以访问对应的应用。集群中任一机器及任意应用都可以访问该应用,但是不能在集群外部访问。同理,-w可以追踪pod日志。
kubectl exec -it mynginx -- /bin/bash 进入k8s中pod节点的某个容器的控制台。
Deployment
A Deployment provides declarative updates for Pods and ReplicaSets. You describe a desired state in a Deployment, and the Deployment Controller changes the actual state to the desired state at a controlled rate. You can define Deployments to create new ReplicaSets, or to remove existing Deployments and adopt all their resources with new Deployments.
Deployment用来控制Pod,使Pod拥有多副本、自愈、扩缩容等能力。
kubectl create deployment [deploymentName] nginx --image=nginx --replicas=3 创建由deployment控制的、名称为mytomcat的Pod。也可以使用.yaml配置文件创建。
apiVersion: apps/v1
kind: Deployment
metadata:
labesl:
app: my-dep
name: my-dep # deployment名称
spec:
replicas: 3 # 副本数量
selector:
matchLabels:
app: my-dep
template:
metadata:
labels:
app: my-dep
spec:
containers:
- image: nginx # 镜像
name: nginx # 镜像名称
即使该Pod宕机被删除,deployment会在其他机器重新下载该Pod。只有kubectl delete deplyment ,才会真正删除该deployment控制的pod。
kubectl get deploy 查看所有的deployment状态。
- 扩缩容。deployment会根据当前网络和流量情况对pod进行动态扩缩容,也可以命令进行扩缩容。
kubectl scale deploy/my-dep --replicas=5。也可以通过kubectl edit deploy/my-dep修改.yaml配置文件的方式进行扩缩容。 - 自愈和故障转移。当pod出现问题,status不再是running时,deployment会对故障pod进行重启操作。当pod所处机器宕机,deployment在一个时间阈值内感知不到该节点,就会在其他机器上对该节点的pod进行创建。
- 滚动更新,即不停机更新。
kubectl set image deploy/my-dep nginx=nginx:1.16.1 --record此时会新启动一个pod,当状态为running时,会将旧镜像的pod状态设置为Terminating。 - 版本回退。
kubectl rollout history deploy/my-dep可以查询my-dep下所有pod的版本回退记录。kubectl rollout undo deploy/my-dep --to-revision=1可以将my-dep下的pod回退到第一次的历史版本。如果不携带–to-revison参数,默认回退到上一个版本。
其他工作负载
- deployment:应用是无状态的,适合微服务等。
- StatefulSet:需要数据的保留,如使用redis,此时提供稳定的存储和数据。
- DaemonSet:守护型部署应用,如每个pod都需要日志等应用组件。
- Job/Cronjob:定时任务部署,如垃圾清理组件。
开启掘金成长之旅!这是我参与「掘金日新计划 · 2 月更文挑战」的第 24 天,点击查看活动详情