k8s相關工具介紹:
Kubeadm Kubeadm解決了處理TLS加密配置、部署核心Kubernetes組件和確保其他節點可以輕松地加入集群的問題。生成的集群通過RBAC等機制得到保護。 有關Kubeadm的更多詳情,請參閱https://github.com/kubernetes/kubeadm Minikube
一個快速搭建單節點Kubenetes集群的工具,供希望嘗試Kubernetes或與其一起開發的用戶使用。
有關Minikube的更多詳情,請參閱:https://github.com/AliyunContainerService/minikube
minikube version #檢查是否已正確安裝 minikube start #啟動集群:
我英語不行,得多學。您可以略過。
Great! You now have a running Kubernetes cluster in your online terminal. Minikube started a virtual machine for you, and a Kubernetes cluster is now running in that VM. 太棒了!現在,您的在線終端中有一個正在運行的Kubernetes集群。Minikube為您啟動了一個虛擬機,一個Kubernetes集群現在正在該VM中運行。
step2: 集群信息:
如果節點標記為NotReady,則它仍在啟動組件。
$ kubectl cluster-info #集群及其健康狀態的詳細信息 Kubernetes master is running at https://172.17.0.20:8443 KubeDNS is running at https://172.17.0.20:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'. $ kubectl get nodes #查看集群中的節點 NAME STATUS ROLES AGE VERSION minikube Ready master 19m v1.13.3
step3: 部署容器
kubectl run first-deployment --image=katacoda/docker-http-server --port=80 #部署容器 kubectl get pods #查看部署狀態 kubectl expose deployment first-deployment --port=80 --type=NodePort #容器可以通過不同的網絡選項公開。最常用的是NodePort,它為容器提供動態端口。
#查找分配的端口並執行HTTP請求。
export PORT=$(kubectl get svc first-deployment -o go-template='{{range.spec.ports}}{{if .nodePort}}{{.nodePort}}{{"\n"}}{{end}}{{end}}') echo "Accessing host01:$PORT" curl host01:$PORT
Kubernetes Pods
When you created a Deployment in Module 2, Kubernetes created a Pod to host your application instance. A Pod is a Kubernetes abstraction that represents a group of one or more application containers (such as Docker or rkt), and some shared resources for those containers. Those resources include:
- Shared storage, as Volumes
- Networking, as a unique cluster IP address
- Information about how to run each container, such as the container image version or specific ports to use
A Pod models an application-specific "logical host" and can contain different application containers which are relatively tightly coupled. For example, a Pod might include both the container with your Node.js app as well as a different container that feeds the data to be published by the Node.js webserver. The containers in a Pod share an IP Address and port space, are always co-located and co-scheduled, and run in a shared context on the same Node.
Pods are the atomic unit on the Kubernetes platform. When we create a Deployment on Kubernetes, that Deployment creates Pods with containers inside them (as opposed to creating containers directly). Each Pod is tied to the Node where it is scheduled, and remains there until termination (according to restart policy) or deletion. In case of a Node failure, identical Pods are scheduled on other available Nodes in the cluster.
PODS概述:
一個pod總是運行在一個節點。Node是Kubernetes中的工作機器,可以是虛擬機器,也可以是物理機器。每個節點由master管理。節點可以有多個pods,Kubernetes主節點會自動處理跨集群中節點的調度。master的自動調度考慮到每個節點上的可用資源。
每個Kubernetes Node至少運行:
- Kubelet,一個負責Kubernetes Master和Node之間通信的過程;它管理在機器上運行的Pods和容器。
- 容器運行時(如Docker,Rkt)負責從注冊表中提取容器映像、解壓縮容器並運行應用程序。
只有當容器緊密耦合並且需要共享資源(如磁盤)時,才應該將它們排在同一個Pod中。
節點概述
在命令之后使用-幫助獲取有關可能的參數的其他信息。
kubectl get nodes --help
通過運行kubectl Version命令,檢查kubectl是否配置為與集群對話
這里我們看到可用的節點。Kubernetes將根據Node可用資源選擇在哪里部署我們的應用程序
- kubectl get - list resources 列出資源
- kubectl describe - show detailed information about a resource 顯示有關資源的詳細信息
- kubectl logs - print the logs from a container in a pod 對吊艙中的容器執行命令
- kubectl exec 對吊艙中的容器執行命令
部署應用程序
這將對指定的資源(如節點、容器)執行指定的操作(如創建、描述)。您可以在命令之后使用--help獲取有關可能的參數的其他信息(kubectl get node --help)。通過運行kubectl Version命令,檢查kubectl是否配置為與集群對話:
$ kubectl version Client Version: version.Info{Major:"1", Minor:"13", GitVersion:"v1.13.3", GitCommit:"721bfa751924da8d1680787490c54b9179b1fed0", GitTreeState:"clean", BuildDate:"2019-02-01T20:08:12Z", GoVersion:"go1.11.5", Compiler:"gc", Platform:"linux/amd64"} Server Version: version.Info{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"} $ kubectl get nodes NAME STATUS ROLES AGE VERSION minikube Ready master 2m13s v1.13.3
$ kubectl get nodes #要查看集群中的節點 NAME STATUS ROLES AGE VERSION minikube Ready master 12m v1.13.3
我們需要提供deployments名稱和應用程序鏡像位置(包括DockerHub外部托管的image的完整存儲庫url)。port: 在特定端口上運行應用程序,:
$ kubectl run kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1 --port=8080 #通過run創建一個deployments來部署應用程序 kubectl run --generator=deployment/apps.v1 is DEPRECATED and will be removed in a future version. Use kubectl run --generator=run-pod/v1 or kubectl create instead. deployment.apps/kubernetes-bootcamp created $ kubectl get deployments #列出部署 NAME READY UP-TO-DATE AVAILABLE AGE kubernetes-bootcamp 1/1 1 1 4s
剛剛。這為您執行了幾項任務:
1、搜索一個可以運行應用程序實例的合適節點(我們只有一個可用節點) 2、將應用程序安排在該節點上運行。 3、配置群集,以便在需要時重新安排新節點上的實例。
查看我們運行在Kubernetes內部的應用程序
Pods是在一個私有的、孤立的網絡上運行的。默認情況下,它們可以從同一Kubernetes集群中的其他豆莢和服務中看到,但不在該網絡之外。當我們使用kubectl時,我們通過API端點與應用程序進行交互。。以后再討論如何公開應用程序的其他選項。kubectl命令可以創建一個代理,將通信轉發到集群范圍內的專用網絡中。代理可以通過按Control-C來終止,並且在運行時不會顯示任何輸出。我們將打開第二個終端窗口來運行代理。
kubectl proxy
我們現在有了主機(在線終端)和Kubernetes集群之間的連接。代理允許從這些終端直接訪問API。您可以看到通過代理端點承載的所有api,現在可以通過http://localhost:8001.獲得這些api。例如,我們可以使用curl命令直接通過API查詢版本:
$ curl http://localhost:8001/version { "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" }
API服務器將根據POD名稱自動為每個POD創建一個端點,這個端點也可以通過代理訪問。首先,我們需要獲得POD名稱,然后將變量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 Name of the Pod: kubernetes-bootcamp-6bf84cb898-9nxtv
現在,我們可以向運行在該pod中的應用程序發出HTTP請求:
$ curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/proxy/ Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-2k8q5 | v=1
URL是到pod的api的路由。
步驟1檢查應用程序配置
kubectl get pods
$ kubectl get pods NAME READY STATUS RESTARTS AGE kubernetes-bootcamp-6bf84cb898-4bw7p 1/1 Running 0 3m27s
您可以使用這些命令查看應用程序何時部署、它們當前的狀態是什么、它們正在運行的位置以及它們的配置。現在我們已經了解了更多關於集群組件和命令行的信息,讓我們來探索一下我們的應用程序。
kubectl describe pods
我們在這里看到關於POD容器的詳細信息:IP地址、所使用的端口以及與POD生命周期相關的事件列表。Description命令的輸出非常廣泛,涵蓋了一些我們還沒有解釋過的概念,但是不要擔心,在這個入門營結束時,它們將變得熟悉起來。
$ kubectl describe pods Name: kubernetes-bootcamp-6bf84cb898-4bw7p Namespace: default Priority: 0 PriorityClassName: <none> Node: minikube/172.17.0.55 Start Time: Sat, 06 Apr 2019 13:02:52 +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://14c67655ffbc5375f478ac54c1089b581306a3a680623464fd675545a8f58d44 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: Sat, 06 Apr 2019 13:02:54 +0000 Ready: True Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-stx7p (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: default-token-stx7p: Type: Secret (a volume populated by a Secret) SecretName: default-token-stx7p 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 3m14s default-scheduler Successfully assigned default/kubernetes-bootcamp-6bf84cb898-4bw7p to minikube Normal Pulled 3m13s kubelet, minikube Container image "gcr.io/google-samples/kubernetes-bootcamp:v1" already present on machine Normal Created 3m13s kubelet, minikube Created container Normal Started 3m12s kubelet, minikube Started container
步驟2在終端中顯示應用程序
回想一下,pods是在一個獨立的私有網絡中運行的,所以我們需要代理訪問它們,以便調試和與它們交互。為此,我們將使用kubectl proxy命令在第二個終端窗口中運行代理。單擊下面的命令自動打開新終端並運行代理:
$ kubectl proxy Starting to serve on 127.0.0.1:8001
現在,我們再次獲得pod名稱,並直接通過代理查詢該pod。要獲取pod名稱並將其存儲在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
Name of the Pod: kubernetes-bootcamp-6bf84cb898-4bw7p
要查看應用程序的輸出,請運行cURL請求
$ curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/proxy/ Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-6bf84cb898-2k8q5 | v=1
url是到Pod的API的路徑。
步驟3查看容器日志:
應用程序通常發送給STDOUT的任何內容都會成為Pod中容器的日志。我們可以使用kubectl log命令檢索這些日志:
$ kubectl logs $POD_NAME
Note: We don’t need to specify the container name, because we only have one container inside the pod.
步驟4在容器上執行命令
一旦Pod啟動並運行,我們就可以直接在容器上執行命令。為此,我們使用exec命令並使用Pod的名稱作為參數。讓我們列出環境變量:
下面的命令必須開啟代理后,在新終端運行才可以。
$ kubectl exec $POD_NAME env PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin HOSTNAME=kubernetes-bootcamp-6bf84cb898-4bw7p KUBERNETES_PORT_443_TCP=tcp://10.96.0.1:443 KUBERNETES_PORT_443_TCP_PROTO=tcp KUBERNETES_PORT_443_TCP_PORT=443 KUBERNETES_PORT_443_TCP_ADDR=10.96.0.1 KUBERNETES_SERVICE_HOST=10.96.0.1 KUBERNETES_SERVICE_PORT=443 KUBERNETES_SERVICE_PORT_HTTPS=443 KUBERNETES_PORT=tcp://10.96.0.1:443 NPM_CONFIG_LOGLEVEL=info NODE_VERSION=6.3.1 HOME=/root
接下來,讓我們在Pod的容器中啟動bash會話:
$ kubectl exec -ti $POD_NAME bash root@kubernetes-bootcamp-6bf84cb898-4bw7p:/#
我們現在容器上有一個打開的控制台來運行NodeJS應用程序。應用程序的源代碼位於server.js文件中:
root@kubernetes-bootcamp-6bf84cb898-4bw7p:/# cat server.js var http = require('http'); var requests=0; var podname= process.env.HOSTNAME; var startTime; var host; var handleRequest = function(request, response) { response.setHeader('Content-Type', 'text/plain'); response.writeHead(200); response.write("Hello Kubernetes bootcamp! | Running on: "); response.write(host); response.end(" | v=1\n"); console.log("Running On:" ,host, "| Total Requests:", ++requests,"| App Uptime:", (new Date() - startTime)/1000 , "seconds", "| Log Time:",new Date()); } var www = http.createServer(handleRequest); www.listen(8080,function () { startTime = new Date();; host = process.env.HOSTNAME; console.log ("Kubernetes Bootcamp App Started At:",startTime, "| Running On: " ,host, "\n" ); });
您可以通過運行curl命令來檢查應用程序是否已啟動:
curl localhost:8080
注意:這里我們使用localhost,因為我們在NodeJS容器中執行了命令
若要關閉容器連接,請鍵入“exit”