linux操作文件-k8s基本命令

從我的全世界路過發表於2020-10-17

linux操作文件-k8s基本命令

一、建立資源

建立Pod控制器,deployment。可以用來控制多個容器。

建立Service,可以為Pod提供一個統一的訪問介面,保證內外網能夠訪問。預設的type為ClusterIP,叢集內任意節點都可訪問;type改為NodePort則外網可訪問。

1.用命令列的方式建立

1.建立Deployment

建立deployment,名稱為web,映象為nginx,容器數量5

master ~]# kubectl run web --image=nginx --replicas=5
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/web created

檢視控制器情況

master ~]# kubectl get deployments.
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
web    5/5     5            5           2m9s

檢視資源詳細資訊

master ~]# kubectl get deployments. -o wide
NAME   READY   UP-TO-DATE   AVAILABLE   AGE     CONTAINERS   IMAGES   SELECTOR
web    5/5     5            5           3m35s   web          nginx    run=web

PS:加上-n選項可以檢視指定的控制器情況

2.建立Service

如果想要外網能夠訪問服務,可以暴露deployment資源,得到service
資源,但svc資源的型別必須為NodePort(大小寫必須嚴格按照要求)

master ~]# kubectl expose deployment web --name=web-svc --port=80 --type=NodePort
service/web-svc exposed

檢視service情況

master ~]# kubectl get svc
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        20d
web-svc      NodePort    10.111.135.250   <none>        80:32282/TCP   3m59s

3.服務的擴容與縮容

master ~]# kubectl scale deployment web --replicas=7
deployment.extensions/web scaled
master ~]# kubectl get deployments.
NAME   READY   UP-TO-DATE   AVAILABLE   AGE
web    7/7     7            7           13m
master ~]# kubectl edit deployments. web 
...
spec:
  progressDeadlineSeconds: 600
  replicas: 7           //數量
  revisionHistoryLimit: 10
  selector:
    matchLabels:
...

4.常用命令集合

命令作用
kubectl run建立一個deployment管理建立的容器
kubectl get顯示一個或多個資源,可以使用標籤過濾,預設檢視當前名稱空間的資源
kubectl expose將一個資源暴露為一個新的kubernetes的service資源(連線外網)
kubectl describe顯示特定資源或資源組的詳細資訊
kubectl scale可以對Deployment, ReplicaSet, Replication,Controller, 或者StatefulSet設定新的值,可以指定一個或多個先決條件
kubectl set更改現有的應用程式資源
kubectl roollout資源回滾管理

2.使用配置清單建立

一級欄位的名稱及作用

名稱作用
apiVersionapi版本資訊
kind資源物件的類別
metadata後設資料 名稱欄位必寫
spec使用者期望的狀態
status資源現在處於什麼樣的狀態(非必須)

1.建立Deployment

可以使用kubectl explain 命令檢視我們要寫的資源物件的yaml檔案怎麼
寫。比如檢視deployment資源的話就可以寫成:

master ~]# kubectl explain deploy
KIND:     Deployment
VERSION:  extensions/v1beta1      //版本資訊

DESCRIPTION:
     DEPRECATED - This group version of Deployment is deprecated by
     apps/v1beta2/Deployment. See the release notes for more information.
     Deployment enables declarative updates for Pods and ReplicaSets.

FIELDS:
   apiVersion	<string>
     APIVersion defines the versioned schema of this representation of an
     object. Servers should convert recognized schemas to the latest internal
     value, and may reject unrecognized values. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#resources

   kind	<string>
     Kind is a string value representing the REST resource this object
     represents. Servers may infer this from the endpoint the client submits
     requests to. Cannot be updated. In CamelCase. More info:
     https://git.k8s.io/community/contributors/devel/api-conventions.md#types-kinds

   metadata	<Object>
     Standard object metadata.

   spec	<Object>
     Specification of the desired behavior of the Deployment.

   status	<Object>
     Most recently observed status of the Deployment.

使用yaml配置檔案建立deployment

master ~]# mkdir yaml
master ~]# cd yaml/
master yaml]# vim web2.yaml
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
  name: web2
spec:
  replicas: 4
  template:
    metadata:
      labels:
        app: web2
    spec:
      containers:
      - name: web2
        image: nginx
master yaml]# kubectl apply -f web2.yaml    //執行命令
deployment.extensions/web2 created

檢視執行結果

master yaml]# kubectl get deployments. -o wide
NAME   READY   UP-TO-DATE   AVAILABLE   AGE    CONTAINERS   IMAGES   SELECTOR
web    7/7     7            7           45m    web          nginx    run=web
web2   4/4     4            4           5m1s   web2         nginx    app=web2

2.建立Service(外網可訪問)

master yaml]# vim web2-svc.yaml
kind: Service
apiVersion: v1
metadata:
  name: web-svc
spec:
  type: NodePort      // 指定型別,讓外網可訪問
  selector:
    app: web2       //使用相同的標籤和標籤選擇器內容,使兩個資源物件相互關聯
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
    nodePort: 30033      //指定叢集對映埠,範圍是30000-32767
master yaml]# kubectl apply -f web2-svc.yaml    //執行命令
service/web-svc created

檢視執行結果

master yaml]# kubectl get svc
NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1       <none>        443/TCP        20d
web-svc      NodePort    10.104.94.122   <none>        80:30033/TCP   4s

STER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443/TCP 20d
web-svc NodePort 10.104.94.122 80:30033/TCP 4s




相關文章