Kubernetes系列(三)Deployment
本文主要介绍基于上篇构建的 docker 镜像,通过 Deployment 方式部署到 minikube 中。。
Kubernetes 的控制器类型
-
• 控制器又被称为工作负载,pod通过控制器实现应用的运维,比如伸缩、升级等
-
• Kubernetes中内建了很多controller(控制器),这些相当于一个状态机,用来控制Pod的具体状态和行为
-
1. deployment:适合无状态的服务部署
-
2. StatefullSet:适合有状态的服务部署
-
3. DaemonSet:一次部署,所有的node节点都会部署,例如一些典型的应用场景:
-
• 运行集群存储 daemon,例如在每个Node上运行 glusterd、ceph
-
• 在每个Node上运行日志收集 daemon,例如 fluentd、 logstash
-
• 在每个Node上运行监控 daemon,例如 Prometheus Node Exporter
-
4. Job:一次性的执行任务
-
5. Cronjob:周期性的执行任务
Deployment 控制器
Deployment实现了Pod 的“水平扩展 / 收缩”(horizontal scaling out/in),适合部署无状态的应用服务,用来管理pod和replicaset,具有上线部署、副本设定、滚动更新、回滚等功能。
Deployment 实例
- • 创建 deployment.yaml 内容如下
# 创建名为dev的命名空间
# 版本号
apiVersion: v1
# 表明创建的种类为命名空间
kind: Namespace
# 定义资源的元数据信息,比如资源的名称、namespace、标签等信息
metadata:
#定义命名空间的名称
name: dev
---
# 创建 service (一组pod 对外访问的包装)
# 版本号
apiVersion: v1
# 表明创建资源的种类为service
kind: Service
metadata:
# 定义service的名称,在同一namespace中必须是唯一的
name: nginx-svc
# 定义资源属于哪一个命名空间
namespace: dev
# 定义service所需要的参数属性
spec:
ports:
# 当type=Nodeport时,指定映射到物理机的端口
- nodePort: 32036
# 服务本身监听的端口
port: 80
protocol: TCP
# 需要转发到后端的端口,nginx默认端口为80
targetPort: 80
selector:
# 如何确定需要代理哪一个服务,通过标签,设置后会自动匹配同一命名空间下标签为nignx的pod代理
app: nginx
# service的类型,指定service的访问方式,默认ClusterIP
type: NodePort
#ClusterIP:虚拟的服务ip地址,用于k8s集群内部的pod访问,在Node上kube-porxy通过设置的iptables规则进行转发
#NodePort:使用宿主机端口,能够访问各Node的外部客户端通过Node的IP和端口就能访问服务器
#LoadBalancer:使用外部负载均衡器完成到服务器的负载分发,
---
# 版本号
apiVersion: apps/v1
# 类型
kind: Deployment
metadata:
# 名字
name: nginx
# 属于的命名空间
namespace: dev
labels:
# 给自己打个标签
app: nginx
spec:
# 副本数量,为2,稍后会创建两个pod
replicas: 2
selector:
matchLabels:
# 这里设置将要匹配的标签
app: nginx
template:
metadata:
labels:
# 设置创建pod后将要给它打上的标签
app: nginx
spec:
containers:
# 资源名,创建时会自动加后缀,防止名称重复
- name: nginx
# 创建资源的镜像,这里选择的是之前上传到harbor仓库的镜像
image: harbor.k8s.info.com/yueyong/centos-nginx:1.22.0
# 拉取策略,这里的意思是如果本地没有就从远程仓库拉取
imagePullPolicy: IfNotPresent
ports:
- containerPort: 80 #容器对外开放的端口
- • 执行命令,创建资源
PS D:\workspace\docker\demo2> kubectl apply -f .\deployment.yaml
namespace/dev unchanged
service/nginx-svc unchanged
deployment.apps/nginx created
PS D:\workspace\docker\demo2>
- • 查看相关资源信息
PS D:\workspace\docker\demo2> kubectl get -f .\deployment.yaml
NAME STATUS AGE
namespace/dev Active 2m50s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/nginx-svc NodePort 10.99.22.250 <none> 80:32036/TCP 2m6s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 0/2 2 0 64s
PS D:\workspace\docker\demo2>
若 deployment 一直未 READY,可以通过查看 pod 详情,排查问题:
PS D:\workspace\docker\demo2> kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
nginx-6ff8d55f64-cvr7p 0/1 ImagePullBackOff 0 85s
nginx-6ff8d55f64-spgxt 0/1 ImagePullBackOff 0 85s
PS D:\workspace\docker\demo2> kubectl describe pod nginx-6ff8d55f64-cvr7p -n dev
Name: nginx-6ff8d55f64-cvr7p
Namespace: dev
Priority: 0
Node: minikube/172.29.68.52
Start Time: Sun, 17 Jul 2022 21:59:40 +0800
Labels: app=nginx
pod-template-hash=6ff8d55f64
Annotations: <none>
Status: Pending
IP: 172.17.0.3
IPs:
IP: 172.17.0.3
Controlled By: ReplicaSet/nginx-6ff8d55f64
Containers:
nginx:
Container ID:
Image: harbor.k8s.brains-info.com/yueyong/centos-nginx:1.22.0
Image ID:
Port: 80/TCP
Host Port: 0/TCP
State: Waiting
Reason: ImagePullBackOff
Ready: False
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-6pv22 (ro)
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
Volumes:
kube-api-access-6pv22:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 104s default-scheduler Successfully assigned dev/nginx-6ff8d55f64-cvr7p to minikube
Warning Failed 65s (x3 over 101s) kubelet Failed to pull image "harbor.k8s.brains-info.com/yueyong/centos-nginx:1.22.0": rpc error: code = Unknown desc = Error response from daemon: Get "https://harbor.k8s.brains-info.com/v2/": dial tcp: lookup harbor.k8s.brains-info.com on 172.29.64.1:53: no such host
Normal BackOff 26s (x5 over 101s) kubelet Back-off pulling image "harbor.k8s.brains-info.com/yueyong/centos-nginx:1.22.0"
Warning Failed 26s (x5 over 101s) kubelet Error: ImagePullBackOff
Normal Pulling 13s (x4 over 103s) kubelet Pulling image "harbor.k8s.brains-info.com/yueyong/centos-nginx:1.22.0"
Warning Failed 13s (x4 over 101s) kubelet Error: ErrImagePull
Warning Failed 13s kubelet Failed to pull image "harbor.k8s.brains-info.com/yueyong/centos-nginx:1.22.0": rpc error: code = Unknown desc = Error response from daemon: Get "https://harbor.k8s.brains-info.com/v2/": x509: certificate signed by unknown authority
PS D:\workspace\docker\demo2>
可以看到报错 x509 ,说明 k8s 中的 docker 未能认证 harbor 仓库,通过下面设置可以忽略:
# 删除集群
PS D:\workspace\docker\demo2> minikube delete --all
# 启动集群时,增加 insecure-registry
PS D:\workspace\docker\demo2> minikube config set insecure-registry harbor.k8s.brains-info.com
# 重新执行 deployment.yaml
PS D:\workspace\docker\demo2> kubectl apply -f .\deployment.yaml
namespace/dev created
service/nginx-svc created
deployment.apps/nginx created
# 稍等一会查看创建的资源 都已 READY
PS D:\workspace\docker\demo2> kubectl get -f .\deployment.yaml
NAME STATUS AGE
namespace/dev Active 63s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/nginx-svc NodePort 10.107.143.96 <none> 80:32036/TCP 63s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/nginx 2/2 2 2 63s
- • 通过节点 ip + yaml 中 定义的 nodeport 类型的 svc 端口 32036 可以访问部署在 k8s 中的服务,或者通过如下命令获取:
# minikube service [svc name] -n [命名空间] -- url
PS D:\workspace\docker\demo2> minikube service nginx-svc -n dev --url
http://172.29.69.45:32036
PS D:\workspace\docker\demo2>
上一篇
欢迎关注我的公众号“云原生拓展”,原创技术文章第一时间推送。