Kubernetes管理的基本教程

這篇文章主要講解了“Kubernetes管理的基本教程”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Kubernetes管理的基本教程”吧!

創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:成都網(wǎng)站設(shè)計、成都做網(wǎng)站、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的鎮(zhèn)巴網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

1. 創(chuàng)建Container / Pod##

Kubernetes的編排基本單元是Pod,故而哪怕你只有一個Container,也要創(chuàng)建一個Pod來容納這個Container.

apiVersion: v1
kind: Pod
metadata:
  name: hello-world # pod資源名稱,集群unique的
spec:  # specification of the pod’s contents
  restartPolicy: Never # 運行這個容器一次,然后就結(jié)束這個pod
  containers:
  - name: hello # 只是這個container的nickname
    image: "ubuntu:14.04" # image 名, 默認(rèn)使用Docker Hub
    command: ["/bin/echo","hello”,”world"]

部分解釋見上,其中command覆蓋了Docker容器的Entrypoint, Command參數(shù)(對應(yīng)于Docker的Cmd)可以使用args來聲明:

command: ["/bin/echo"]
    args: ["hello","world"]

創(chuàng)建pod則為:

$ kubectl create -f ./hello-world.yaml --validate # 將會進(jìn)行驗證并且給出WARN,但是出問題的話,依然會創(chuàng)建Pod,會輸出WARN信息
pods/hello-world
Container環(huán)境變量和變量展開####
apiVersion: v1
kind: Pod
metadata:
  name: hello-world
spec:  # specification of the pod’s contents
  restartPolicy: Never
  containers:
  - name: hello
    image: "ubuntu:14.04"
    env: # 這是一個環(huán)境變量
    - name: MESSAGE
      value: "hello world"
    command: ["/bin/sh","-c"] # Kubernetes并不會自動運行shell,需要手動指定
    args: ["/bin/echo \"${MESSAGE}\""] # 這是用到的環(huán)境變量展開

如果沒有shell我們的環(huán)境變量依然可以用$(ENVVAR)來展開。例如:

command: ["/bin/echo"]
    args: ["$(MESSAGE)"]

##2. 查看Pod的狀態(tài)##

$ kubectl get pods
NAME          READY     STATUS    RESTARTS   AGE
hello-world   0/1       Pending   0          0s

狀態(tài)如下:

unscheduled - 一開始創(chuàng)建POD,還沒選擇到節(jié)點來運行Pod,然后立即schedule
scheduled - Pod已經(jīng)被Scheduled,準(zhǔn)備在目標(biāo)節(jié)點pull鏡像
running - 從鏡像啟動容器成功, READY列表示多少個容器正常
ExitCode:0 - 正常退出,容器不再運行

##3. 刪除Pod##

$ kubectl delete pod hello-world
pods/hello-world

或者 resource/name 格式來刪除

$ kubectl delete pods/hello-world
pods/hello-world

會把容器和其輸出的日志都刪除。

##4. 創(chuàng)建Replication Controller## Replication Controller可以保證『副本數(shù)量正確』的Pod在運行,下面簡稱RC/rc。下面是2副本的Nginx:

apiVersion: v1
kind: ReplicationController # 不再是Pod
metadata:
  name: my-nginx
spec:
  replicas: 2
  template: # 這是一個PodTemplateSpec,下面聲明了Pod的規(guī)范
    metadata: # 無需聲明Pod名稱,因為他們由RC自動生成
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80 # 對外開放的端口

##5. 刪除Replication Controller##

$ kubectl delete rc my-nginx
replicationcontrollers/my-nginx

Label

Kubernetes使用Lable來分類、識別不同的資源集合,例如Pods和Replication Controllers。 我們可以用app這個key和nginx這個value來讀取pods和rc:

$ kubectl get pods -L app
NAME             READY     STATUS    RESTARTS   AGE       APP
my-nginx-afv12   0/1       Running   0          3s        nginx
my-nginx-lg99z   0/1       Running   0          3s        nginx
$ kubectl get rc my-nginx -L app
CONTROLLER   CONTAINER(S)   IMAGE(S)   SELECTOR    REPLICAS   APP
my-nginx     nginx          nginx      app=nginx   2          nginx

也可以使用golang的模板來獲取信息

$ kubectl get rc my-nginx -o template --template="{{.spec.selector}}"
map[app:nginx]

6. 創(chuàng)建Service##

下面是一個服務(wù)的yaml文件。服務(wù)是與Pod松耦合的??梢宰杂山M合。一旦創(chuàng)建完畢就分配一個clusterIP,而對這個clusterIP會有個簡單的load balancer.

apiVersion: v1
kind: Service
metadata:
  name: nginxsvc
  labels:
    app: nginx
spec: 
  ports:
  - port: 80 # 訪問的地址為 http://clusterIP:80/
    protocol: TCP
  selector: # 意思是由app=nginx的pod來處理
    app: nginx

一個Service背后可以有多個endpoints,來進(jìn)行處理。

$ kubectl describe svc nginxsvc
Name:          nginxsvc
Namespace:     default
Labels:            app=nginx
Selector:      app=nginx
Type:          ClusterIP
IP:            10.0.116.146
Port:          <unnamed> 80/TCP
Endpoints:     10.245.0.14:80,10.245.0.15:80
Session Affinity:  None
No events.

$ kubectl get ep
NAME         ENDPOINTS
nginxsvc     10.245.0.14:80,10.245.0.15:80

感謝各位的閱讀,以上就是“Kubernetes管理的基本教程”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Kubernetes管理的基本教程這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識點的文章,歡迎關(guān)注!

當(dāng)前名稱:Kubernetes管理的基本教程
文章URL:http://muchs.cn/article8/jchcop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供、企業(yè)網(wǎng)站制作網(wǎng)站制作、網(wǎng)站設(shè)計公司小程序開發(fā)、全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)