kubernetes—五个概念入门(一):https://developer.aliyun.com/article/1417667
Deployment
介绍
在kubernetes中,Pod是最小的控制单元,但是kubernetes很少直接控制Pod,一般都是通过Pod控制器来完成的。Pod控制器用于pod的管理,确保pod资源符合预期的状态,当pod的资源出现故障时,会尝试进行重启或重建pod。
在kubernetes中Pod控制器的种类有很多,本章节只介绍一种:Deployment,别的Pod控制器预计在下下篇博文进行详细介绍。Deployment和pod关系的建立是通过标签label进行建立的,如下图:
操作
命令操作
# 创建deployment # 命令格式: kubectl create deployment 名称 [参数] # --image 指定pod的镜像 # --port 指定端口 # --replicas 指定创建pod数量 # --namespace 指定namespace [root@master k8sYamlForCSDN]# kubectl create deployment deploy-nginx --image=nginx --port=80 --replicas=3 --namespace=dev deployment.apps/deploy-nginx created # 查看创建的Pod ```bash [root@master k8sYamlForCSDN]# kubectl get pods -n dev NAME READY STATUS RESTARTS AGE deploy-nginx-fb669cc4f-5xd5j 1/1 Running 0 3m32s deploy-nginx-fb669cc4f-69pln 1/1 Running 0 3m32s deploy-nginx-fb669cc4f-tpfhp 1/1 Running 0 3m32s # 查看deployment的信息 [root@master k8sYamlForCSDN]# kubectl get deployment -n dev NAME READY UP-TO-DATE AVAILABLE AGE deploy-nginx 3/3 3 3 4m21s # UP-TO-DATE:成功升级的副本数量 # AVAILABLE:可用副本的数量 # 查看deployment和pod的详细信息 [root@master k8sYamlForCSDN]# kubectl get deployment,pods -n dev --show-labels NAME READY UP-TO-DATE AVAILABLE AGE LABELS deployment.apps/deploy-nginx 3/3 3 3 5m42s app=deploy-nginx NAME READY STATUS RESTARTS AGE LABELS pod/deploy-nginx-fb669cc4f-5xd5j 1/1 Running 0 5m42s app=deploy-nginx,pod-template-hash=fb669cc4f pod/deploy-nginx-fb669cc4f-69pln 1/1 Running 0 5m42s app=deploy-nginx,pod-template-hash=fb669cc4f pod/deploy-nginx-fb669cc4f-tpfhp 1/1 Running 0 5m42s app=deploy-nginx,pod-template-hash=fb669cc4f [root@master k8sYamlForCSDN]# kubectl get deployment -n dev -o wide NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR deploy-nginx 3/3 3 3 5m58s nginx nginx app=deploy-nginx
# 查看deployment的详细信息 [root@master k8sYamlForCSDN]# kubectl describe deployment -n dev Name: deploy-nginx Namespace: dev CreationTimestamp: Mon, 28 Feb 2022 21:10:32 +0800 Labels: app=deploy-nginx Annotations: deployment.kubernetes.io/revision: 1 Selector: app=deploy-nginx Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable StrategyType: RollingUpdate MinReadySeconds: 0 RollingUpdateStrategy: 25% max unavailable, 25% max surge Pod Template: Labels: app=deploy-nginx Containers: nginx: Image: nginx Port: 80/TCP Host Port: 0/TCP Environment: <none> Mounts: <none> Volumes: <none> Conditions: Type Status Reason ---- ------ ------ Available True MinimumReplicasAvailable Progressing True NewReplicaSetAvailable OldReplicaSets: <none> NewReplicaSet: deploy-nginx-fb669cc4f (3/3 replicas created) Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal ScalingReplicaSet 6m29s deployment-controller Scaled up replica set deploy-nginx-fb669cc4f to 3
# # 删除deployment Pod控制器,删除了pod对应的deployment,则pod也会被删除 [root@master k8sYamlForCSDN]# kubectl delete deployments.apps deploy-nginx -n dev deployment.apps "deploy-nginx" deleted [root@master k8sYamlForCSDN]# kubectl get deployment,pods -n dev No resources found in dev namespace.
配置操作
# 创建一个deploy-nginx.yaml vi deploy-nginx.yaml
apiVersion: apps/v1 kind: Deployment metadata: name: deploy-nginx namespace: dev spec: replicas: 3 selector: matchLabels: run: "nginx" template: metadata: labels: run: "nginx" spec: containers: - name: pod image: nginx ports: - containerPort: 80 protocol: TCP
# 创建 [root@master k8sYamlForCSDN]# kubectl apply -f deploy-nginx.yaml deployment.apps/deploy-nginx created # 查看 [root@master k8sYamlForCSDN]# kubectl get deployments.apps -n dev NAME READY UP-TO-DATE AVAILABLE AGE deploy-nginx 0/3 3 0 11s [root@master k8sYamlForCSDN]# kubectl get deployments.apps,pods -n dev NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/deploy-nginx 0/3 3 0 15s NAME READY STATUS RESTARTS AGE pod/deploy-nginx-579f68df74-pn2c8 0/1 ContainerCreating 0 15s pod/deploy-nginx-579f68df74-sv9nz 0/1 ContainerCreating 0 15s pod/deploy-nginx-579f68df74-wpndc 0/1 ContainerCreating 0 15s # 删除 [root@master k8sYamlForCSDN]# kubectl delete -f deploy-nginx.yaml deployment.apps "deploy-nginx" deleted
Service
介绍
目前已经能够利用Deployment来创建一组Pod来提供具有高可用性的服务。
虽然每个Pod都会分配一个单独的Pod IP,然而却存在如下两问题:
- Pod IP 会随着Pod的重建产生变化
- Pod IP 仅仅是集群内可见的虚拟IP,外部无法访问
这样对于访问这个服务带来了难度。因此,kubernetes设计了Service来解决这个问题。
Service可以看作是一组同类Pod对外的访问接口。借助Service,应用可以方便地实现服务发现和负载均衡。
操作
操作一:创建集群内部可访问的Service
这里先简单介绍一下–type=ClusterIP,意思是只有集群内部ip可以访问这个service
# 暴露Service [root@master k8sYamlForCSDN]# kubectl expose deployment deploy-nginx -n dev \ --name=svc-deploy-nginx --type=ClusterIP --port=80 --target-port=80 service/svc-deploy-nginx exposed # 查看service [root@master k8sYamlForCSDN]# kubectl get service svc-deploy-nginx -n dev -o wide NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR svc-deploy-nginx ClusterIP 10.99.101.216 <none> 80/TCP 47s run=nginx # 这里产生了一个CLUSTER-IP,这就是service的IP,在Service的生命周期中,这个地址是不会变动的 # 可以通过这个IP访问当前service对应的POD [root@master k8sYamlForCSDN]# curl 10.99.101.216:80 <!DOCTYPE html> ... ... </html>
操作二:创建集群外部也可访问的Service
# 上面创建的Service的type类型为ClusterIP,这个ip地址只用集群内部可访问 # 如果需要创建外部也可以访问的Service,需要修改type为NodePort [root@master k8sYamlForCSDN]# kubectl expose deployment deploy-nginx -n dev \ > --name=svc-deploy-nginx --type=NodePort --port=80 --target-port=80 service/svc-deploy-nginx exposed # # 此时查看,会发现出现了NodePort类型的Service,而且有一对Port(80:31928/TC) [root@master k8sYamlForCSDN]# kubectl get service svc-deploy-nginx -n dev -o wide NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR svc-deploy-nginx NodePort 10.99.149.210 <none> 80:30945/TCP 10s run=nginx # 接下来就可以通过集群外的主机访问 节点IP:30945访问服务了 # 例如在的电脑主机上通过浏览器访问下面的地址 http://192.168.109.100:30945/
删除Service
[root@master k8sYamlForCSDN]# kubectl delete svc svc-deploy-nginx -n dev service "svc-deploy-nginx" deleted
配置方式
不论是ClusterIP还是NodePort,yaml文件中的clusterIP: 10.109.68.72可以不配,不配置就是自动分配
# 创建一个svc-nginx.yaml vi svc-deploy-nginx.yaml
apiVersion: v1 kind: Service metadata: name: svc-deploy-nginx namespace: dev spec: type: ClusterIP clusterIP: 10.109.68.72 selector: run: "nginx" ports: - port: 80 targetPort: 80 protocol: TCP
[root@master k8sYamlForCSDN]# kubectl apply -f svc-deploy-nginx.yaml service/svc-deploy-nginx created [root@master k8sYamlForCSDN]# kubectl get service svc-deploy-nginx -n dev -o wide NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR svc-deploy-nginx ClusterIP 10.109.68.72 <none> 80/TCP 10s run=nginx [root@master k8sYamlForCSDN]# kubectl delete -f svc-deploy-nginx.yaml service "svc-deploy-nginx" deleted
apiVersion: v1 kind: Service metadata: name: svc-deploy-nginx namespace: dev spec: type: NodePort clusterIP: 10.109.68.72 selector: run: "nginx" ports: - port: 6872 targetPort: 80 protocol: TCP
apiVersion: v1 kind: Service metadata: name: svc-deploy-nginx namespace: dev spec: type: NodePort clusterIP: 10.109.68.72 selector: run: "nginx" ports: - port: 6872 targetPort: 80 protocol: TCP
[root@master k8sYamlForCSDN]# kubectl apply -f svc-deploy-nginx.yaml service/svc-deploy-nginx created [root@master k8sYamlForCSDN]# kubectl get service svc-deploy-nginx -n dev -o wide NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR svc-deploy-nginx NodePort 10.105.26.121 <none> 6872:31758/TCP 4s run=nginx [root@master k8sYamlForCSDN]# kubectl delete -f svc-deploy-nginx.yaml service "svc-deploy-nginx" deleted