一、pod相关:
yaml语言
pod的yaml文件详解
pod的yaml文件实例
重启pod的方法
二、deployment相关:
# 导出yaml文件
[root@k8s-master paas]# kubectl create deployment nginx --image=nginx:1.14 --dry-run=client -oyaml > nginx-deployment.yaml
# 修改后的yaml为:
[root@k8s-master paas]# cat nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
namespace: kube-system
name: nginx
spec:
replicas: 3
selector:
matchLabels:
app: web
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: web
spec:
nodeSelector:
nodeName: node
containers:
- image: nginx:1.14
name: nginx
ports:
- name: http
containerPort: 80
protocol: TCP
resources: {}
status: {}
# 生成pod
[root@k8s-master paas]# kubectl apply -f nginx-deployment.yaml
[root@k8s-master paas]# kubectl get pod -nkube-system |grep nginx
nginx-6bb96fb8c5-cnw9n 1/1 Running 0 3m34s
nginx-6bb96fb8c5-zcxt7 1/1 Running 0 3m36s
nginx-6bb96fb8c5-zqvlr 1/1 Running 0 3m32s
# 手动扩(缩)容
[root@k8s-master paas]# kubectl scale deployment nginx -nkube-system --replicas=1
# describe deployment中的events能看到扩缩容历史
[root@k8s-master paas]# kubectl describe deployment nginx -nkube-system |grep -A 100 Events
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 48m deployment-controller Scaled up replica set nginx-689f89d84 to 3
Normal ScalingReplicaSet 37m deployment-controller Scaled up replica set nginx-689f89d84 to 4
Normal ScalingReplicaSet 25m deployment-controller Scaled up replica set nginx-689f89d84 to 11
Normal ScalingReplicaSet 25m (x2 over 45m) deployment-controller Scaled down replica set nginx-689f89d84 to 1
Normal ScalingReplicaSet 20m deployment-controller Scaled up replica set nginx-9b586479c to 1
Normal ScalingReplicaSet 20m deployment-controller Scaled down replica set nginx-689f89d84 to 0
Normal ScalingReplicaSet 16m deployment-controller Scaled down replica set nginx-9b586479c to 2
Normal ScalingReplicaSet 16m deployment-controller Scaled up replica set nginx-6bb96fb8c5 to 2
Normal ScalingReplicaSet 7m44s (x2 over 18m) deployment-controller Scaled up replica set nginx-9b586479c to 3
Normal ScalingReplicaSet 6m53s (x2 over 17m) deployment-controller Scaled up replica set nginx-6bb96fb8c5 to 1
Normal ScalingReplicaSet 6m49s (x2 over 16m) deployment-controller Scaled down replica set nginx-9b586479c to 1
Normal ScalingReplicaSet 6m49s deployment-controller Scaled up replica set nginx-6bb96fb8c5 to 3
Normal ScalingReplicaSet 6m47s (x10 over 16m) deployment-controller (combined from similar events): Scaled down replica set nginx-9b586479c to 0
升级与回滚:
# 升级镜像(--record 记录命令信息方便后期查看)
[root@k8s-master paas]# kubectl set image deployment/nginx nginx=nginx:1.7.9 -nkube-system --record
# 查看升级历史
[root@k8s-master paas]# kubectl rollout history deployment nginx -nkube-system
deployment.apps/nginx
REVISION CHANGE-CAUSE
1 <none> # 第一次升级没有加--record,所以看不到命令信息
4 kubectl set image deployment/nginx nginx=nginx:1.9.1 --namespace=kube-system --record=true
5 kubectl set image deployment/nginx nginx=nginx:1.7.9 --namespace=kube-system --record=true
# 回滚到上一个版本
[root@k8s-master paas]# kubectl rollout undo deployment nginx -nkube-system
deployment.apps/nginx rolled back
#回退到指定版本
[root@k8s-master paas]# kubectl rollout undo deployment nginx -nkube-system --to-revision=1
deployment.apps/nginx rolled back
# 查看升级与回滚状态
[root@k8s-master paas]# kubectl rollout status deployment nginx -nkube-system
deployment "nginx" successfully rolled out
三、service
#1)ClusterIP 类型
# 生成pod
[root@k8s-master paas]# kubectl apply -f nginx-deployment.yaml
[root@k8s-master paas]# kubectl get pod -nkube-system | grep nginx
nginx-689f89d84-9x9vl 1/1 Running 0 39m
nginx-689f89d84-l8zm9 1/1 Running 0 39m
nginx-689f89d84-mpb8t 1/1 Running 0 39m
nginx-689f89d84-slvqv 1/1 Running 0 39m
# 生成service的yaml文件,默认type为ClusterIP
[root@k8s-master paas]# kubectl expose deployment nginx --port=80 --target-port=80 --dry-run=client -oyaml -nkube-system> nginx-service.yaml
[root@k8s-master paas]# cat nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
name: nginx
namespace: kube-system
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: web
status:
loadBalancer: {}
# 生成svc
[root@k8s-master paas]# kubectl apply -f nginx-service.yaml
[root@k8s-master paas]# kubectl get svc -nkube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 3d6h
nginx ClusterIP 10.98.9.96 <none> 80/TCP 48s
# 集群任一节点访问nginx
[root@k8s-node1 ~]# curl 10.98.9.96:80
<!DOCTYPE html>
……
<title>Welcome to nginx!</title>
……
</html>
2)NodePort类型
#
# 创建新的service.yaml,其中type: NodePort
[root@k8s-master paas]# cat nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
name: nginx-nodeport-type
namespace: kube-system
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: web
type: NodePort
status:
loadBalancer: {}
[root@k8s-master paas]# kubectl apply -f nginx-service.yaml
[root@k8s-master paas]# kubectl get svc -nkube-system | grep NodePort
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-nodeport-type NodePort 10.96.110.166 <none> 80:32529/TCP 9m41s
可以用集群任一节点ip:32529访问nginx,也可以在集群内用CLUSTER-IP访问,
下面是用node节点访问:
下面是在集群内部用Cluster-IP访问
[root@k8s-node2 ~]# curl 10.96.110.166
<!DOCTYPE html>
……
<h1>Welcome to nginx!</h1>
……
</html>
3)LoadBalancer类型,可使用公有云负载均衡器