[純紀錄]Kubernetes基本操作

學習 Kubernetes 與基本操作紀錄

Kubernetes 文章

kubectl的簡單查看,新增,重開

kubectl的簡單查看,新增,重開

  • kubectl get pod -A

kubectl create 建立 後面可以直接加參數或是用yaml來建立

  • 簡單建立deployment
    啟動 minikube 之後,我們可以透過 kubectl run 在 minikube 上運行一個 Google 提供的 hello-minikube docker image,

    1
    2
    3
    4
    5
    $ kubectl create deployment first-deployment --image=katacoda/docker-http-server
    deployment.apps/first-deployment created
    $ kubectl get pods
    NAME READY STATUS RESTARTS AGE
    first-deployment-8cbf74484-xgc4l 1/1 Running 0 25s
  • 建立namespaces

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    >kubectl get namespaces
    NAME STATUS AGE
    default Active 7m52s
    kube-node-lease Active 7m53s
    kube-public Active 7m53s
    kube-system Active 7m53s

    //Create a new Namespaces
    D:\k8syaml>kubectl create namespace my
    namespace/my created
  • 建立deployment
    https://kubernetes.io/zh/docs/concepts/workloads/controllers/deployment/
    D:\k8syaml>kubectl create -f my-first-pod.yaml 編輯要部屬的程式
    deployment.apps/my-pod created

  • 查看所有pod
    D:\k8syaml>kubectl get pod -A

  • 看LOG
    kubectl -n NsName logs my-pod-5b788d95bc-kx76k

  • 刪除
    D:\k8syaml>kubectl delete deployment my-pod -n NsName 先刪除
    deployment.apps “my-pod” deleted

  • 重開
    D:\k8syaml>kubectl -n NsName rollout restart deployment nginx179 執行重開指令
    deployment.apps/nginx179 restarted

  • 編輯一樣可以達到重開效果
    kubectl edit deployment -n NsName nginx179
    kubectl get pod -A
    NAMESPACE NAME READY STATUS RESTARTS AGE
    NsName nginx179-5ddfbfccb8-76qz8 1/1 Running 0 84s//啟動成功

minikube

Kubernetes —學習好幫手minikube — 01
將K8s所需的Master/Worker node封裝在一個虛擬機器中
https://medium.com/@sniperbean/kubernetes-%E5%AD%B8%E7%BF%92%E5%A5%BD%E5%B9%AB%E6%89%8Bminikube-01-aedfaf8b00fe
1.install kubuctl
你可以使用 Kubectl 命令行工具管理 Kubernetes 集群。 kubectl 在 $HOME/.kube 目录中查找一个名为 config 的配置文件。

2.Minikube

Minikube 是由 Google 發布的一個輕量級工具。讓開發者可以在本機上輕易架設一個 Kubernetes Cluster,快速上手 Kubernetes 的指令與環境。Minikube 會在本機上跑起一個 virtual machine,並且在這 VM 裡建立一個 signle-node Kubernetes Cluster,本身並不支援 HA (High availability),也不推薦在實際應用上運行。

安裝:https://minikube.sigs.k8s.io/docs/start/

*開啟docker的k8s設定
docker desktop ->settings->

1
2
3
4
5
6
7
8
9
10
11
12
13
>kubectl get nodes
No resources found

- 啟動minikube start
>minikube start
* minikube v1.16.0 on Microsoft Windows 10 Enterprise 10.0.17763 Build 17763
* Using the docker driver based on existing profile
* Starting control plane node minikube in cluster minikube
* Restarting existing docker container for "minikube" ...
* Preparing Kubernetes v1.20.0 on Docker 20.10.0 ...
* Verifying Kubernetes components...
* Enabled addons: storage-provisioner, default-storageclass
* Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
  • 確認kubectl version

    1
    2
    3
    >kubectl version
    Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.3", GitCommit:"1e11e4a2108024935ecfcb2912226cedeafd99df", GitTreeState:"clean", BuildDate:"2020-10-14T12:50:19Z", GoVersion:"go1.15.2", Compiler:"gc", Platform:"windows/amd64"}
    Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.0", GitCommit:"af46c47ce925f4c4ad5cc8d1fca46c7b77d13b38", GitTreeState:"clean", BuildDate:"2020-12-08T17:51:19Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"}

    server version需要minikube有安裝才會出現

    1
    2
    3
    4
    5
    6
    7
    >kubectl get nodes
    NAME STATUS ROLES AGE VERSION
    minikube Ready control-plane,master 3d v1.20.0


    PS C:\> minikube dashboard
    Opening kubernetes dashboard in default browser...
  • 啟動 minikube 之後,我們可以透過 kubectl run 在 minikube 上運行一個 Google 提供的 hello-minikube docker image,

1
2
3
4
5
$ kubectl create deployment first-deployment --image=katacoda/docker-http-server
deployment.apps/first-deployment created
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
first-deployment-8cbf74484-xgc4l 1/1 Running 0 25s
  • 在 minikube 上透過 kubectl get

來自 https://ithelp.ithome.com.tw/articles/10197186

1
2
3
4
5
6
7
8
9
10
D:\k8syaml>kubectl get namespaces
NAME STATUS AGE
default Active 7m52s
kube-node-lease Active 7m53s
kube-public Active 7m53s
kube-system Active 7m53s

Create a new Namespaces
D:\k8syaml>kubectl create namespace NsName
namespace/NsName created
  • 建立deployment

    1
    2
    3
    4
    5
    6
    https://kubernetes.io/zh/docs/concepts/workloads/controllers/deployment/
    D:\k8syaml>kubectl create -f my-first-pod.yaml 編輯要部屬的程式
    deployment.apps/my-pod created

    D:\k8syaml>kubectl get pods
    No resources found in default namespace.
  • D:\k8syaml>kubectl get pod -A

    1
    2
    3
    4
    5
    6
    7
    8
    9
    NAMESPACE     NAME                               READY   STATUS    RESTARTS   AGE
    NsName my-pod-5b788d95bc-kx76k 0/1 Error 0 46s 建立不成功因為有相依
    kube-system coredns-74ff55c5b-wf484 1/1 Running 0 10m
    kube-system etcd-minikube 1/1 Running 0 10m
    kube-system kube-apiserver-minikube 1/1 Running 0 10m
    kube-system kube-controller-manager-minikube 1/1 Running 0 10m
    kube-system kube-proxy-skl6r 1/1 Running 0 10m
    kube-system kube-scheduler-minikube 1/1 Running 0 10m
    kube-system storage-provisioner 1/1 Running 0 10m
  • 看LOG
    kubectl -n NsName logs my-pod-5b788d95bc-kx76k

  • D:\k8syaml>kubectl delete deployment my-pod -n NS名稱 先刪除
    deployment.apps “my-pod” deleted
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
D:\k8syaml>kubectl get pod -A
NAMESPACE NAME READY STATUS RESTARTS AGE
xx my-pod-5b788d95bc-kx76k 0/1 Terminating 5 6m20s
kube-system coredns-74ff55c5b-wf484 1/1 Running 0 16m
kube-system etcd-minikube 1/1 Running 0 16m
kube-system kube-apiserver-minikube 1/1 Running 0 16m
kube-system kube-controller-manager-minikube 1/1 Running 0 16m
kube-system kube-proxy-skl6r 1/1 Running 0 16m
kube-system kube-scheduler-minikube 1/1 Running 0 16m
kube-system storage-provisioner 1/1 Running 0 16m

D:\k8syaml>kubectl get pod -A
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-74ff55c5b-wf484 1/1 Running 0 16m
kube-system etcd-minikube 1/1 Running 0 16m
kube-system kube-apiserver-minikube 1/1 Running 0 16m
kube-system kube-controller-manager-minikube 1/1 Running 0 16m
kube-system kube-proxy-skl6r 1/1 Running 0 16m
kube-system kube-scheduler-minikube 1/1 Running 0 16m
kube-system storage-provisioner 1/1 Running 0 16m

D:\k8syaml>kubectl create -f my-first-nginx179.yaml //重新建立一個
deployment.apps/nginx179 created

D:\k8syaml>kubectl get pod -A
NAMESPACE NAME READY STATUS RESTARTS AGE
NsName nginx179-75c6f6f5b8-bwzgm 1/1 Running 0 98s //啟動成功
kube-system coredns-74ff55c5b-wf484 1/1 Running 0 20m
kube-system etcd-minikube 1/1 Running 0 20m
kube-system kube-apiserver-minikube 1/1 Running 0 20m
kube-system kube-controller-manager-minikube 1/1 Running 0 20m
kube-system kube-proxy-skl6r 1/1 Running 0 20m
kube-system kube-scheduler-minikube 1/1 Running 0 20m
kube-system storage-provisioner 1/1 Running 0 20m
1
2
3
4
5
6
7
8
D:\k8syaml>kubectl -n NsName rollout restart deployment  nginx179   執行重開指令
deployment.apps/nginx179 restarted

編輯一樣可以達到重開效果
kubectl edit deployment -n NsName nginx179
kubectl get pod -A
NAMESPACE NAME READY STATUS RESTARTS AGE
NsName nginx179-5ddfbfccb8-76qz8 1/1 Running 0 84s//啟動成功
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
D:\k8syaml>kubectl apply -f my-web.yaml
service/nginx created
statefulset.apps/web created
D:\k8syaml>kubectl get pods -A
NAMESPACE NAME READY STATUS RESTARTS AGE
default web-0 1/1 Running 0 39s
default web-1 1/1 Running 0 27s

D:\k8syaml>kubectl delete pod -l app=nginx
pod "web-0" deleted
pod "web-1" deleted

D:\k8syaml>kubectl apply -f my-web.yaml
service/nginx unchanged
statefulset.apps/web created

D:\k8syaml>kubectl edit statefulset -n NsName my-web
Edit cancelled, no changes made.


作者

Mini Lab Memo

發表於

2020-12-31

更新於

2023-04-26

許可協議

評論