前面的系列文章已介紹kubernetes架構(gòu),安裝,升級和快速入門,讀者通過文章的實(shí)操已對kubernetes已有初步的認(rèn)識和理解,從本章開始逐步介紹kubernetes中的基礎(chǔ)概念概念和核心概念,基礎(chǔ)概念包括:namespace,labels,annotations,pods,volumes等;核心概念包含kubernetes中各種controller,包含以下幾種:
創(chuàng)新互聯(lián)公司基于成都重慶香港及美國等地區(qū)分布式IDC機(jī)房數(shù)據(jù)中心構(gòu)建的電信大帶寬,聯(lián)通大帶寬,移動(dòng)大帶寬,多線BGP大帶寬租用,是為眾多客戶提供專業(yè)服務(wù)器托管報(bào)價(jià),主機(jī)托管價(jià)格性價(jià)比高,為金融證券行業(yè)服務(wù)器托管德陽,ai人工智能服務(wù)器托管提供bgp線路100M獨(dú)享,G口帶寬及機(jī)柜租用的專業(yè)成都idc公司。
配置和密鑰ConfigMaps和Secrets
本文從最基礎(chǔ)的概念pod開始講解,后續(xù)逐步介紹應(yīng)用部署,存儲(chǔ),負(fù)載均衡等相關(guān)的控制器,kubernetes內(nèi)部由多個(gè)不同的控制器組成,每個(gè)控制器完成不同的功能。
容器是一種便攜式,輕量級別的容器虛擬化技術(shù),使用linux cggroup技術(shù)實(shí)現(xiàn)各種資源的隔離,如cpu,memory,pid,mount,IPC等,相比于虛擬化技術(shù)如KVM,容器技術(shù)更加輕量級,它的產(chǎn)生主要解決環(huán)境的環(huán)境發(fā)布的問題,目前主流的容器技術(shù)是docker,說到容器,一般都等同于docker。
要運(yùn)行容器首先需要有鏡像,應(yīng)用和應(yīng)用依賴的環(huán)境運(yùn)行在容器中,在kubernetes中不會(huì)直接運(yùn)行container,而是運(yùn)行pod,一個(gè)pod里面包含多個(gè)container,container之間共享相同的namespace,network,storage等。鏡像存儲(chǔ)在私有鏡像或者公有鏡像中,運(yùn)行時(shí)通過docker image pull的方式拉取到本地運(yùn)行,images的拉取策略包含有兩種:
Pods是kubernetes中最小的調(diào)度單位,Pods內(nèi)運(yùn)行一個(gè)或者多個(gè)container,container之間共享pod的網(wǎng)絡(luò)ip資源,存儲(chǔ)volume資源,計(jì)算等資源,方便pod內(nèi)部的container之間能夠?qū)崿F(xiàn)快速的訪問和交互。
如上圖所示,Pod的使用方式通常包含兩種:
kubernetes中通過定義生申明式的方式定義資源,即通過在yaml文件中定義所需的資源,kubernetes通過controller-manager按照yaml文件中定義的資源去生成所需的資源(match the current state to desired state)。通常在kubernetes中通過yaml文件的方式定義資源,然后通過kubectl create -f 文件.yaml的方式應(yīng)用配置,如下演示創(chuàng)建一個(gè)nginx應(yīng)用的操作。
1、編寫yaml文件,定義一個(gè)pod資源
[root@node-1 demo]# cat nginx.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx-demo
labels:
name: nginx-demo
spec:
containers:
- name: nginx-demo
image: nginx:1.7.9
imagePullPolicy: IfNotPresent
ports:
- name: nginx-port-80
protocol: TCP
containerPort: 80
關(guān)于配置文件,說明如下:
2、創(chuàng)建pod應(yīng)用
[root@node-1 demo]# kubectl apply -f nginx.yaml
pod/nginx-demo created
3、訪問應(yīng)用
獲取容器的IP地址
[root@node-1 demo]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
demo-7b86696648-8bq7h 1/1 Running 0 8h 10.244.1.11 node-2
demo-7b86696648-8qp46 1/1 Running 0 8h 10.244.1.10 node-2
demo-7b86696648-d6hfw 1/1 Running 0 8h 10.244.1.12 node-2
nginx-demo 1/1 Running 0 50s 10.244.2.11 node-3
訪問站點(diǎn)內(nèi)容:
[root@node-1 demo]# curl http://10.244.2.11
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
前面我們我們學(xué)習(xí)過kubernetes支持滾動(dòng)升級RollingUpdate,彈性擴(kuò)容replicas等特性,如何給Pod做滾動(dòng)升級保障業(yè)務(wù)不中斷,如何提高Pod的副本個(gè)數(shù)保障高可用呢?答案是:不支持。Pod是單個(gè),無法支持一些高級特性,高級特性需要通過高級的副本控制器如ReplicaSets,Deployments,StatefulSets,DaemonSets等才能支持。Pod在實(shí)際應(yīng)用中很少用,除了測試和運(yùn)行一些簡單的功能外,實(shí)際使用建議使用Deployments代替,Pod的定義以Template的方式嵌入在副本控制器中。
前面我們提到過kubernetse是申明式的方式部署應(yīng)用,應(yīng)用的部署都定義在yaml文件中來實(shí)現(xiàn),如何來編寫應(yīng)用的yaml文件呢,下面我來分享兩個(gè)世紀(jì)使用的技巧:
1、通過定義模版快速生成,kubectl create apps -o yaml --dry-run的方式生成,--dry-run僅僅是試運(yùn)行,并不實(shí)際在k8s集群中運(yùn)行,通過指定-o yaml輸出yaml格式文件,生成后給基于模版修改即可,如下:
[root@node-1 demo]# kubectl create deployment demo --image=nginx:latest --dry-run -o yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: demo
name: demo
spec:
replicas: 1
selector:
matchLabels:
app: demo
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: demo
spec:
containers:
- image: nginx:latest
name: nginx
resources: {}
status: {}
2、explain命令,explain命令堪稱是語法查詢器,可以查到每個(gè)字段的含義,使用說明和使用方式,如想要查看Pod的spec中containers其他支持的字段,可以通過kubectl explain Pod.spec.containers的方式查詢,如下:
[root@node-1 demo]# kubectl explain Pods.spec.containers
KIND: Pod
VERSION: v1
RESOURCE: containers <[]Object>
DESCRIPTION:
List of containers belonging to the pod. Containers cannot currently be
added or removed. There must be at least one container in a Pod. Cannot be
updated.
A single application container that you want to run within a pod.
FIELDS:
args <[]string> #命令參數(shù)
Arguments to the entrypoint. The docker image's CMD is used if this is not
provided. Variable references $(VAR_NAME) are expanded using the
container's environment. If a variable cannot be resolved, the reference in
the input string will be unchanged. The $(VAR_NAME) syntax can be escaped
with a double $$, ie: $$(VAR_NAME). Escaped references will never be
expanded, regardless of whether the variable exists or not. Cannot be
updated. More info:
https://kubernetes.io/docs/tasks/inject-data-application/define-command-argument-container/#running-a-command-in-a-shell
image #鏡像定義
Docker image name. More info:
https://kubernetes.io/docs/concepts/containers/images This field is
optional to allow higher level config management to default or override
container images in workload controllers like Deployments and StatefulSets.
ports <[]Object> #端口定義
List of ports to expose from the container. Exposing a port here gives the
system additional information about the network connections a container
uses, but is primarily informational. Not specifying a port here DOES NOT
prevent that port from being exposed. Any port which is listening on the
default "0.0.0.0" address inside a container will be accessible from the
network. Cannot be updated.
readinessProbe
關(guān)于explain內(nèi)容解釋說明
如繼續(xù)上面的內(nèi)容,如果需要查看resource資源定義,可以通過explain pods.spec.containers.resource來查看具體的使用方法。
通過上面兩個(gè)工具的介紹,平時(shí)在日常工作中找到編寫yaml文件部署應(yīng)用的地圖,建議手工多寫幾次,注意語法鎖進(jìn),多寫幾次就熟悉了。Pod中設(shè)計(jì)到有很多的特性,如資源分配,健康檢查,存儲(chǔ)掛載等(參考附錄文章),后續(xù)我們做詳細(xì)介紹,Pod將以Template的方式嵌入到副本控制器如Deployments中。
容器鏡像介紹:https://kubernetes.io/docs/concepts/containers/images/
Pod介紹:https://kubernetes.io/docs/concepts/workloads/pods/pod-overview/
Resource限定內(nèi)存資源:https://kubernetes.io/docs/tasks/configure-pod-container/assign-memory-resource/
Resource限定CPU資源:https://kubernetes.io/docs/tasks/configure-pod-container/assign-cpu-resource/
Pod掛載存儲(chǔ):https://kubernetes.io/docs/tasks/configure-pod-container/configure-volume-storage/
Pod配置健康檢查:https://kubernetes.io/docs/tasks/configure-pod-container/configure-liveness-readiness-probes/
返回kubernetes系列教程目錄