kubectl get pods -n kube-system kubectl describe pods -n kube-system
# Flannel 网络插件 https://github.com/flannel-io/flannel/tree/master # curl https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml --output kube-flannel.yml # net-conf.json: | # { # "Network": "10.10.0.0/16", # "Backend": { # "Type": "vxlan" # } # } kubectl apply -f https://github.com/flannel-io/flannel/releases/latest/download/kube-flannel.yml kubectl get node # NAME STATUS ROLES AGE VERSION # master Ready control-plane,master 14h v1.23.16
# show join command in control-plane kubeadm token create --print-join-command # work join; 云服务记得开放入站端口 telnet 172.21.0.5 6443 systemctl enable kubelet.service kubeadm join 172.21.0.5:6443 --token xxx --discovery-token-ca-cert-hash sha256:xxx --v=9 # check in control-plane kubectl get nodes # NAME STATUS ROLES AGE VERSION # master Ready control-plane,master 14h v1.23.16 # vm-0-9-centos Ready <none> 3m27s v1.23.16
# run nginx kubectl run ngx --image=nginx:alpine kubectl get pod -o wide # NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES # ngx 1/1 Running 0 52m 10.10.1.2 woker01 <none> <none>
18 Deployment 部署应用
“单一职责”和“对象组合”。既然 Pod 管理不了自己,那么我们就再创建一个新的对象,由它来管理 Pod,采用和 Job/CronJob 一样的形式——“对象套对象”。
Deployment 实际上并不“持有”Pod 对象,它只是帮助 Pod 对象能够有足够的副本数量运行。
通过标签这种设计,Kubernetes 就解除了 Deployment 和模板里 Pod 的强绑定,把组合关系变成了“弱引用”。
# replicas: 2 kubectl apply -f ngx-dep.yml
kubectl get deploy # NAME READY UP-TO-DATE AVAILABLE AGE # ngx-dep 2/2 2 2 57s kubectl get pod # NAME READY STATUS RESTARTS AGE # ngx-dep-bfbb5f64b-96scb 1/1 Running 0 3m20s # ngx-dep-bfbb5f64b-qnzbh 1/1 Running 0 3m20s
# 测试自启恢复 kubectl delete pod ngx-dep-bfbb5f64b-qnzbh kubectl get pod # NAME READY STATUS RESTARTS AGE # ngx-dep-bfbb5f64b-7n724 1/1 Running 0 33s # ngx-dep-bfbb5f64b-96scb 1/1 Running 0 4m52s
# 测试伸缩 kubectl scale --replicas=5 deploy ngx-dep kubectl get pod # NAME READY STATUS RESTARTS AGE # ngx-dep-bfbb5f64b-7n724 1/1 Running 0 77s # ngx-dep-bfbb5f64b-7xhbs 1/1 Running 0 7s # ngx-dep-bfbb5f64b-96scb 1/1 Running 0 5m36s # ngx-dep-bfbb5f64b-97qp5 1/1 Running 0 7s # ngx-dep-bfbb5f64b-vjn4q 1/1 Running 0 7s
# 筛选标签 ==、!=、in、notin kubectl get pod -l app=nginx kubectl get pod -l 'app in (ngx, nginx, ngx-dep)'
19 DaemonSet 看门狗
在 Deployment 看来,Pod 的运行环境与功能是无关的,只要 Pod 的数量足够,应用程序应该会正常工作。
有些场景下,要在集群里的每个节点上都运行 Pod,也就是说 Pod 的数量与节点数量保持同步。防止在集群里漂移。
# Run the Ingress Controller kubectl apply -f deployment/nginx-ingress.yaml
# check kubectl get pods --namespace=nginx-ingress # NAME READY STATUS RESTARTS AGE # nginx-ingress-5f98f8f5f9-nnkv7 1/1 Running 0 3m14s
# Get Access to the Ingress Controller kubectl create -f service/nodeport.yaml kubectl get service -n nginx-ingress # NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE # nginx-ingress NodePort 10.111.210.52 <none> 80:31754/TCP,443:30188/TCP 5s
# debug kubectl get IngressClass kubectl get ing -n nginx-ingress kubectl get deploy -n nginx-ingress kubectl get pod -n nginx-ingress -o wide
kubectl describe service -n nginx-ingress -o wide kubectl describe pod -n nginx-ingress
kubectl get ing kubectl get ingressclass kubectl get pod -n=nginx-ingress kubectl describe pod -n=nginx-ingress kubectl get deploy -n=nginx-ingress kubectl get svc -n=nginx-ingress
# 在服务器上 kubectl get pod -n=nginx-ingress -o=wide # NAME READY STATUS RESTARTS AGE IP NODE # wp-kic-dep-68579bc688-d64zs 1/1 Running 0 10m 172.21.0.9 woker01 curl 172.21.0.9 -H "HOST: wp.test"