概念:Pod 类似于共享名字空间和文件系统卷的一组 Docker 容器。
简单部署
文件nginx-pod.yaml
,
1 2 3 4 5 6 7 8 9 10 11 12
| apiVersion: v1 kind: Pod metadata: name: nginx labels: app: nginx spec: containers: - name: nginx image: nginx:alpine ports: - containerPort: 80
|
基本操作:
1 2 3 4
| kubectl apply -f nginx-pod.yaml kubectl get po kubectl describe pods nginx kubectl delete -f nginx-pod.xml
|
Pod 建议不要直接创建,而是使用工作负载资源创建的。原因:Kubernetes Pods 有确定的生命周期。在pod所在的节点重启之后,pod需要重新创建。
见k8s volume挂载卷
概念:将运行在一组 Pods 上的应用程序公开为网络服务的抽象方法。
简单部署
文件nginx-service.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13
| apiVersion: v1 kind: Service metadata: name: nginx-service spec: type: NodePort selector: app: nginx ports: - protocol: TCP port: 9376 targetPort: 80 nodePort: 30000
|
基本操作
1 2 3 4
| kubectl apply -f nginx-service.yaml kubectl get service kubectl describe service nginx-service kubectl delete -f nginx-service.yaml
|
概念:一个 Deployment 为 Pod 和 ReplicaSet 提供声明式的更新能力。
简单部署
文件nginx-Deployment.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment labels: app: nginx spec: replicas: 2 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: volumes: - name: nginx-data persistentVolumeClaim: claimName: nginx-pvc containers: - name: nginx image: nginx:alpine ports: - containerPort: 80 volumeMounts: - mountPath: "/usr/share/nginx/html" name: nginx-data
|
基本操作
1 2 3 4 5 6 7 8 9 10 11 12 13
| kubectl apply -f nginx-deployment.yaml kubectl get deploy kubectl describe deploy nginx-deployment kubectl delete -f nginx-deployment.yaml kubectl edit deploy/nginx-deployment kubectl rollout status deployment/nginx-deployment kubectl rollout restart deploy/nginx-deployment kubectl get rs kubectl rollout history deployment/nginx-deployment kubectl rollout history deployment/nginx-deployment --revision=2 kubectl rollout undo deployment/nginx-deployment kubectl rollout undo deployment/nginx-deployment --to-revision=2 kubectl scale deployment/nginx-deployment --replicas=10
|
概念:定制资源(Custom Resource) 是对 Kubernetes API 的扩展,存取结构化的数据。
Operator