kubectl version
kubectl get nodes
說明: 提供deployment名稱和app鏡像地址(docker鏡像地址)node
kubectl run kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1 --port=8080
再如:linux
run test --image=preparedman/mytomcat:tagname --port=8088
kubectl proxy
測試:curl http://localhost:8001/versiongit
{ "major": "1", "minor": "13", "gitVersion": "v1.13.3", "gitCommit": "721bfa751924da8d1680787490c54b9179b1fed0", "gitTreeState": "clean", "buildDate": "2019-02-01T20:00:57Z", "goVersion": "go1.11.5", "compiler": "gc", "platform": "linux/amd64" }
獲取pod
名字docker
export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
測試:echo Name of the Pod: $POD_NAMEtomcat
主要使用以下命令bash
kubectl get - list resources 列出資源 kubectl describe - show detailed information about a resource 顯示資源詳情 kubectl logs - print the logs from a container in a pod 打印`pod` 中container的日誌 kubectl exec - execute a command on a container in a pod 在`pod`中的container上執行命令
查看應用是否在運行app
kubectl get pods
查看pod
中有哪些container
負載均衡
kubectl describe pods
結果以下:curl
Name: kubernetes-bootcamp-6bf84cb898-jk4jc Namespace: default Priority: 0 PriorityClassName: <none> Node: minikube/172.17.0.72 Start Time: Wed, 24 Apr 2019 13:21:58 +0000 Labels: pod-template-hash=6bf84cb898 run=kubernetes-bootcamp Annotations: <none> Status: Running IP: 172.18.0.4 Controlled By: ReplicaSet/kubernetes-bootcamp-6bf84cb898 Containers: kubernetes-bootcamp: Container ID: docker://55491b363d26b62e432cd4841ed4f65cc5b98e645d172c6ed88feaebcb4ec06c Image: gcr.io/google-samples/kubernetes-bootcamp:v1 Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af Port: 8080/TCP Host Port: 0/TCP State: Running Started: Wed, 24 Apr 2019 13:22:00 +0000 Ready: True Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-l7v8b (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: default-token-l7v8b: Type: Secret (a volume populated by a Secret) SecretName: default-token-l7v8b Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled 3m6s default-scheduler Successfully assigned default/kubernetes-bootcamp-6bf84cb898-jk4jc to minikube Normal Pulled 3m4s kubelet, minikube Container image "gcr.io/google-samples/kubernetes-bootcamp:v1" already present on machine Normal Created 3m4s kubelet, minikube Created container Normal Started 3m4s kubelet, minikube Started container
container
日誌kubectl logs $POD_NAME
container
中直接執行命令好比:獲取pod
名字是kubernetes-bootcamp-6bf84cb898-jk4jc的日期,默認使用第一個container container by defaultide
kubectl exec kubernetes-bootcamp-6bf84cb898-jk4jc date
再入:進入container
的命令行環境
kubectl exec kubernetes-bootcamp-6bf84cb898-jk4jc bash
退出使用
exit
service
service
kubectl get services
service
並暴露給外部流量kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
service
詳情$ kubectl describe services/kubernetes-bootcamp
結果:
Name: kubernetes-bootcamp Namespace: default Labels: run=kubernetes-bootcamp Annotations: <none> Selector: run=kubernetes-bootcamp Type: NodePort IP: 10.105.231.53 Port: <unset> 8080/TCP TargetPort: 8080/TCP NodePort: <unset> 32400/TCP Endpoints: 172.18.0.4:8080 Session Affinity: None External Traffic Policy: Cluster Events: <none>
NODE_PORT
,它的值等於service
暴露的端口export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}') echo NODE_PORT=$NODE_PORT
測試:
curl $(minikube ip):$NODE_PORT
labels
label
kubectl describe deployment
你能看到這樣一行:
Labels: run=kubernetes-bootcamp
label
查詢podkubectl get pods -l run=kubernetes-bootcamp
label
查詢servicekubectl get services -l run=kubernetes-bootcamp
POD_NAME
中export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}') echo name of the pod: $POD_NAME
lebel
使用label命令
kubectl label pod $POD_NAME app=v1
查看:
kubectl describe pods $POD_NAME
service
label
刪除servicekubectl delete service -l run=kubernetes-bootcamp
確認刪除:
kubectl get services
確認沒有暴露給集羣外部:
curl $(minikube ip):$NODE_PORT
確認集羣內部還能夠訪問:
kubectl exec -ti $POD_NAME curl localhost:8080
deployments
的replica
數量爲4kubectl scale deployments/kubernetes-bootcamp --replicas=4
查看結果:
能夠看到修改replica
設置生效
NAME READY UP-TO-DATE AVAILABLE AGE kubernetes-bootcamp 4/4 4 4 3m17s
pod
數量已經改變,查看詳情
kubectl get pods -o wide
結果:
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES kubernetes-bootcamp-6bf84cb898-7tbrg 1/1 Running 0 2m50s 172.18.0.5 minikube <none> <none> kubernetes-bootcamp-6bf84cb898-fx68f 1/1 Running 0 3m52s 172.18.0.4 minikube <none> <none> kubernetes-bootcamp-6bf84cb898-prgsc 1/1 Running 0 2m50s 172.18.0.6 minikube <none> <none> kubernetes-bootcamp-6bf84cb898-qv4gc 1/1 Running 0 2m50s 172.18.0.7 minikube <none> <none>
查看4個pod
kubectl describe deployments/kubernetes-bootcamp
結果:
Replicas: 4 desired | 4 updated | 4 total | 4 available | 0 unavailable
service
是不是負載均衡的kubectl describe services/kubernetes-bootcamp
結果:
Endpoints: 172.18.0.2:8080,172.18.0.4:8080,172.18.0.6:8080 + 1 more...
NODE_PORT
\export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}') echo NODE_PORT=$NODE_PORT
能夠看到,每次請求的都是不一樣的pod
curl $(minikube ip):$NODE_PORT
結果:
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-2l975 | v=1 $ curl $(minikube ip):$NODE_PORT Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-zbmj4 | v=1 $ curl $(minikube ip):$NODE_PORT Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-qg5xh | v=1 $ curl $(minikube ip):$NODE_PORT Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-zbmj4 | v=1 $ curl $(minikube ip):$NODE_PORT Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-bn98t | v=1 $ curl $(minikube ip):$NODE_PORT Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-zbmj4 | v=1 $ curl $(minikube ip):$NODE_PORT Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-zbmj4 | v=1 $ curl $(minikube ip):$NODE_PORT Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-zbmj4 | v=1
kubectl scale deployments/kubernetes-bootcamp --replicas=2 ## 更新到版本2 kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
驗證更新
查看暴露出來的ip和端口
kubectl describe services/kubernetes-bootcamp
結果:
Name: kubernetes-bootcamp Namespace: default Labels: run=kubernetes-bootcamp Annotations: <none> Selector: run=kubernetes-bootcamp Type: NodePort IP: 10.98.28.235 Port: <unset> 8080/TCP TargetPort: 8080/TCP NodePort: <unset> 31419/TCP Endpoints: 172.18.0.10:8080,172.18.0.11:8080,172.18.0.8:8080 + 1 more... Session Affinity: None External Traffic Policy: Cluster Events: <none>
建立環境變量
export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}') echo NODE_PORT=$NODE_PORT
訪問:
curl $(minikube ip):$NODE_PORT
結果:
訪問版本2,且每次訪問不一樣的地址
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5bf4d5689b-tcxpf | v=2 $ curl $(minikube ip):$NODE_PORT Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5bf4d5689b-tcxpf | v=2 $ curl $(minikube ip):$NODE_PORT Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5bf4d5689b-86c8g | v=2 $ curl $(minikube ip):$NODE_PORT Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5bf4d5689b-fx9tf | v=2 $ curl $(minikube ip):$NODE_PORT Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-5bf4d5689b-tcxpf | v=2
kubectl rollout status deployments/kubernetes-bootcamp
更新到版本10
kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=gcr.io/google-samples/kubernetes-bootcamp:v10
查看發現報錯,由於版本庫中沒有版本10
kubectl get deployments kubectl get pods kubectl describe pods
報錯
Warning Failed 38s (x3 over 77s) kubelet, minikube Failed to pull image "gcr.io/google-samples/kubernetes-bootcamp:v10": rpc error: code = Unknown desc = Error response from daemon: manifest for gcr.io/google-samples/kubernetes-bootcamp:v10 not found
執行會滾
kubectl rollout undo deployments/kubernetes-bootcamp