【利用K8S技術棧打造個人私有云系列文章目錄】
- 利用K8S技術棧打造個人私有云(連載之:初章)
- 利用K8S技術棧打造個人私有云(連載之:K8S集羣搭建)
- 利用K8S技術棧打造個人私有云(連載之:K8S環境理解和練手)
- 利用K8S技術棧打造個人私有云(連載之:基礎鏡像製作與實驗)
- 利用K8S技術棧打造個人私有云(連載之:資源控制研究)
- 利用K8S技術棧打造個人私有云(連載之:私有云客戶端打造)
- 利用K8S技術棧打造個人私有云(連載之:總結)(Coming Soon...)
在前文中我們已經搭建好了K8S集羣,接下來就來講述一下K8S的一些重要的概念和知識,並搞兩個例子在集羣中來實際練手感受一把!
K8S環境理解
kubernetes的概念非常多,組件也不少,本文主要闡述一下幾個最主要、平時用得最頻繁的概念,最詳細的學習文檔是官網教程!
K8S設計架構
該圖描述的設計架構是一個典型的M-S架構,一主兩從,這和我搭建的集羣基本是一致的,主節點和從節點上運行哪些kube組件,其實在我搭建集羣那篇文章也已經講過了,下面來闡述一下kubernetes集羣裏的重要概念和操作命令。
Pod
Pod是Kubernetes中最小的管理元素,Pod是一組容器的集合,且一個Pod只能運行在一個Node上,Pod是kubernetes調度、部署、擴展的基本單位
- kubectl run命令創建一個pod:
kubectl run pod-example --image=nginx
- kubectl create命令創建pod:
kubectl create -f create_pod.yaml
- 刪除pod:
kubectl delete po pod名
- 查看pod啓動情況:
kubectl get pods
,kubectl get pods -o wide
- 查看pod的詳細信息:
kubectl discrible po pod名
Namespace
Namespace是對一組資源和對象的抽象集合,比如可以用來將系統內部的對象劃分爲不同的項目組或用戶組。常見的pods, services, replication controllers和deployments等都是屬於某一個namespace的(默認是default),而node, persistentVolumes等則不屬於任何namespace。
- 創建
kubectl create namespace namespace名
- 刪除
kubectl delete namespaces namespace名
- 查詢
kubectl get namespaces
Replication Controller(RC)
Replication Controller 保證了在所有時間內,都有特定數量的Pod副本正在運行,如果太多了,Replication Controller就殺死幾個,如果太少了,Replication Controller會新建幾個,和直接創建的pod不同的是,Replication Controller會替換掉那些刪除的或者被終止的pod,而不管刪除的原因是什麼。
其實其CURD的命令和上面都差不多,這裏不一一列出了
- 創建:
kubectl create -f create_rc.json
- 查看rc啓動情況:
kubectl get rc
Service
Service 是一個定義了一組Pod的策略的抽象,可以理解爲抽象到用戶層的一個宏觀服務。其實這個概念在Swarm集羣裏也有,可以參照理解。
Service的這樣一層抽象最起碼可以抵禦兩個方面的問題:
- Pod重啓後IP地址的變化(保持用我們系統的客戶其IP不變)
- 提供負載均衡能力
其CURD的命令和上面都差不多,這裏依然不一一列出
- 創建service:
kubectr create -f redis-master-service.json
- 查看service的啓動情況:
kubectl get svc
這裏給一個Service的示例:
apiVersion: v1
kind: Service
metadata:
name: my-nginx-svc
labels:
app: nginx
spec:
type: LoadBalancer
ports:
- port: 80
selector:
app: nginx
好了,理論的東西看多了可能有點蒙,只能多找點不同的材料閱讀並理解了。下面開始做幾個練手示例,這幾個例子都源自於K8S官網,對於理解理論概念非常有好處,下面開始動手吧!
關於Pod的實例練手
本例子創建一個包含兩個容器的POD,並在容器之間完成文件交換
-
yaml配置文件如下:
apiVersion: v1 kind: Pod metadata: name: two-containers spec: restartPolicy: Never volumes: - name: shared-data emptyDir: {} containers: - name: nginx-container image: nginx volumeMounts: - name: shared-data mountPath: /usr/share/nginx/html - name: debian-container image: debian volumeMounts: - name: shared-data mountPath: /pod-data command: ["/bin/sh"] args: ["-c", "echo Hello from the debian container > /pod-data/index.html"]
- 創建pod
kubectl create -f two-container-pod.yaml
- 查看pod和容器的信息
kubectl get pod two-containers --output=yaml
執行完成輸出內容大致如下(經過縮減):
apiVersion: v1
kind: Pod
metadata:
...
name: two-containers
namespace: default
...
spec:
...
containerStatuses:
- containerID: docker://c1d8abd1 ...
image: debian
...
lastState:
terminated: // debian容器已停止
...
name: debian-container
...
- containerID: docker://96c1ff2c5bb ...
image: nginx
...
name: nginx-container
...
state:
running: // nginx容器已運行
...
可以看到debian容器已經停止了,nginx容器依然運行
- 我們進入nginx容器的/bin/bash並驗證nginx服務器
kubectl exec -it two-containers -c nginx-container -- /bin/bash
執行如下命令來安裝curl:
root@two-containers:/# apt-get update
root@two-containers:/# apt-get install curl procps
root@two-containers:/# ps aux
然後執行curl localhost
,可以獲得輸出:
Hello from the debian container
可見debian容器中寫入的東西在nginx容器中獲得了,因此文件交換也完成了
用Pod來做一件事很簡單吧!
關於RC和Service的實例練手
本示例演示的是:部署一個包含Redis集羣、基於PHP的留言板系統
- 創建redis-master的RC
redis-master-controller.yaml內容:
apiVersion: v1
kind: ReplicationController
metadata:
name: redis-master
spec:
replicas: 1
selector:
name: redis-master
template:
metadata:
name: redis-master
labels:
name: redis-master
spec:
containers:
- name: redis-master
image: kubeguide/redis-master
ports:
- containerPort: 6379
創建該RC並查看創建結果:
kubectl create -f redis-master-controller.yaml
kubectl get rc
kubectl get pods
- 創建redis-master的service
redis-master-service.yaml內容:
apiVersion: v1
kind: Service
metadata:
name: redis-master
labels:
name: redis-master
spec:
ports:
- port: 6379
targetPort: 6379
selector:
name: redis-master
創建該Service並查看:
kubectl create -f redis-master-service.yaml
kubectl get services
- 創建redis-slave的RC
redis-slave-controller.yaml文件內容:
apiVersion: v1
kind: ReplicationController
metadata:
name: redis-slave
spec:
replicas: 2
selector: # RC通過spec.selector來篩選要控制的Pod
name: redis-slave
template:
metadata:
name: redis-slave
labels: # Pod的label,可以看到這個label與spec.selector相同
name: redis-slave
spec:
containers:
- name: redis-slave
image: kubeguide/guestbook-redis-slave
env:
- name: GET_HOSTS_FROM
value: env
ports:
- containerPort: 6379
創建該RC並查看:
kubectl create -f redis-slave-controller.yaml
kubectl get rc
kubectl get pods
- 創建redis-slave的service
redis-slave-service.yaml文件內容:
apiVersion: v1
kind: Service
metadata:
name: redis-slave
labels:
name: redis-slave
spec:
ports:
- port: 6379
selector:
name: redis-slave
創建該Service並查看:
kubectl create -f redis-slave-service.yaml
kubectl get services
- 創建frontend的RC
frontend-controller.yaml內容如下:
apiVersion: v1
kind: ReplicationController
metadata:
name: frontend
labels:
name: frontend
spec:
replicas: 3
selector:
name: frontend
template:
metadata:
labels:
name: frontend
spec:
containers:
- name: frontend
image: kubeguide/guestbook-php-frontend
env:
- name: GET_HOSTS_FROM
value: env
ports:
- containerPort: 80
創建該RC並查看
kubectl create -f frontend-controller.yaml
kubectl get rc
kubectl get pods
- 創建frontend的Service
frontend-service.yaml文件內容如下:
apiVersion: v1
kind: Service
metadata:
name: frontend
labels:
name: frontend
spec:
type: NodePort
ports:
- port: 80
nodePort: 30001
selector:
name: frontend
創建該Service並查看
kubectl create -f frontend-service.yaml
kubectl get services
- 訪問
http://your-host:30001/
簡單的留言板系統就生成了!