k8s實戰(zhàn)示例1:創(chuàng)建命名空間和簡單pod

本示例的文件可以從 https://github.com/hyperjiang/k8s-lessons/tree/master/demo1
獲取序调。

創(chuàng)建命名空間

我們創(chuàng)建命名空間的namespace.yaml如下:

apiVersion: v1
kind: Namespace
metadata:
  name: demo

然后運行 kubectl apply -f namespace.yaml 就創(chuàng)建了一個名字叫demo的命名空間拴清。
運行kubectl get ns可以查看namespace是否創(chuàng)建成功纲爸。

創(chuàng)建一個pod

我們創(chuàng)建pod的pod.yaml如下:

apiVersion: apps/v1
kind: Deployment
metadata:
  namespace: demo # An empty namespace is equivalent to the "default" namespace.
  name: alpine # Name must be unique within a namespace.
  labels: # Map of string keys and values that can be used to organize and categorize (scope and select) objects.
    project: alpine
    app: alpine
    namespace: demo
spec: # Specification of the desired behavior of the Deployment.
  selector: # Label selector for pods. It must match the pod template's labels.
    matchLabels: # The requirements are ANDed.
      project: alpine
      app: alpine
      namespace: demo
  replicas: 1 # Number of desired pods, defaults to 1.
  strategy: # The deployment strategy to use to replace existing pods with new ones.
    type: RollingUpdate # Can be "Recreate" or "RollingUpdate". Default is RollingUpdate.
  template: # Template describes the pods that will be created.
    metadata: # Standard object's metadata.
      labels:
        project: alpine
        app: alpine
        namespace: demo
    spec: # Specification of the desired behavior of the pod.
      restartPolicy: Always # Restart policy for all containers within the pod. One of Always, OnFailure, Never. Default to Always.
      containers: # List of containers belonging to the pod. Containers cannot be added or removed or updated. There must be at least one container in a Pod.
        - name: alpine # Name of the container specified as a DNS_LABEL. Each container in a pod must have a unique name (DNS_LABEL). Cannot be updated.
          image: alpine:3.11 # Docker image name.
          resources: # Compute Resources required by this container. Cannot be updated.
            limits: # Limits describes the maximum amount of compute resources allowed.
              cpu: 200m # 100m means one hundred millicpu = 0.1 cpu
              memory: 256Mi # or 256M
            requests: # Requests describes the minimum amount of compute resources required. If Requests is omitted for a container, it defaults to Limits if that is explicitly specified.
              cpu: 100m
              memory: 128Mi
          imagePullPolicy: IfNotPresent # One of Always, Never, IfNotPresent. Defaults to Always if :latest tag is specified, or IfNotPresent otherwise. Cannot be updated.
          command: ["/bin/sh"]
          args: ["-c", "while true; do echo hello; sleep 10; done"] # In order to keep the pod live

然后運行 kubectl apply -f pod.yaml 就創(chuàng)建了一個名字叫alpine的pod踩验。

查看和訪問pod

運行kubectl get po -n demo可以查看命名空間demo下面的pod的狀態(tài),會看到類似下面的輸出:

NAME                      READY   STATUS    RESTARTS   AGE
alpine-5b4987ffb9-9n6j6   1/1     Running   0          10s

然后我們可以運行kubectl exec -n demo -ti alpine-5b4987ffb9-9n6j6 /bin/sh連上這個pod的shell(注意中間的pod名字每次創(chuàng)建都是不同的)

刪除pod和namespace

實驗完了可以把剛才創(chuàng)建的pod和namespace清理掉:

kubectl delete -f pod.yaml
kubectl delete -f namespace.yaml
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末锌俱,一起剝皮案震驚了整個濱河市疹娶,隨后出現(xiàn)的幾起案子擂煞,更是在濱河造成了極大的恐慌,老刑警劉巖酵使,帶你破解...
    沈念sama閱讀 218,755評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件荐吉,死亡現(xiàn)場離奇詭異,居然都是意外死亡口渔,警方通過查閱死者的電腦和手機样屠,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,305評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來缺脉,“玉大人痪欲,你說我怎么就攤上這事」ダ瘢” “怎么了业踢?”我有些...
    開封第一講書人閱讀 165,138評論 0 355
  • 文/不壞的土叔 我叫張陵,是天一觀的道長秘蛔。 經(jīng)常有香客問我陨亡,道長傍衡,這世上最難降的妖魔是什么深员? 我笑而不...
    開封第一講書人閱讀 58,791評論 1 295
  • 正文 為了忘掉前任负蠕,我火速辦了婚禮,結(jié)果婚禮上倦畅,老公的妹妹穿的比我還像新娘遮糖。我一直安慰自己,他們只是感情好叠赐,可當我...
    茶點故事閱讀 67,794評論 6 392
  • 文/花漫 我一把揭開白布欲账。 她就那樣靜靜地躺著,像睡著了一般芭概。 火紅的嫁衣襯著肌膚如雪赛不。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,631評論 1 305
  • 那天罢洲,我揣著相機與錄音踢故,去河邊找鬼。 笑死惹苗,一個胖子當著我的面吹牛殿较,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播桩蓉,決...
    沈念sama閱讀 40,362評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼淋纲,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了院究?” 一聲冷哼從身側(cè)響起洽瞬,我...
    開封第一講書人閱讀 39,264評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎业汰,沒想到半個月后片任,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,724評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡蔬胯,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,900評論 3 336
  • 正文 我和宋清朗相戀三年对供,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片氛濒。...
    茶點故事閱讀 40,040評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡产场,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出舞竿,到底是詐尸還是另有隱情京景,我是刑警寧澤,帶...
    沈念sama閱讀 35,742評論 5 346
  • 正文 年R本政府宣布骗奖,位于F島的核電站确徙,受9級特大地震影響醒串,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜鄙皇,卻給世界環(huán)境...
    茶點故事閱讀 41,364評論 3 330
  • 文/蒙蒙 一芜赌、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧伴逸,春花似錦缠沈、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,944評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至顷锰,卻和暖如春柬赐,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背官紫。 一陣腳步聲響...
    開封第一講書人閱讀 33,060評論 1 270
  • 我被黑心中介騙來泰國打工肛宋, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人万矾。 一個月前我還...
    沈念sama閱讀 48,247評論 3 371
  • 正文 我出身青樓悼吱,卻偏偏與公主長得像,于是被迫代替她去往敵國和親良狈。 傳聞我的和親對象是個殘疾皇子后添,可洞房花燭夜當晚...
    茶點故事閱讀 44,979評論 2 355

推薦閱讀更多精彩內(nèi)容