Kubernetes Clusters
一群電腦或vm組成的叢集,包括兩種
Control Plane用來控制所有其它節點
Node是實體機或VM,用來真正執行工作
Deployment是一個執行工作的基本單位,由yaml
檔組成,啟動pod就是啟動一個容器,就是執行一個deployment。
什麼是pods
當你建立一個deployment時,k8s就會建立一個pod來承載你的應用程式實例。pod就是k8s中承載一個或多個應用程式容器(如docker)的抽象單位。並且會建立這些容器之間的共享資源包括
一個pod建立一個以應用程式程式指定的邏輯主機,並且可以容納不同的應用程式容器,這些容器是相對緊密耦合在一起的。舉例來說,一個pod可以包括了node的應用程式和一個提供資料的容器。這些容器共享一個ip。
pod是k8s最小的執行單位,在k8s建立deployment時,就會建立pod並且讓容器執行在裏面。這和docker直接建立容器是完全不同的。每個pod都會綁定在一個node上,視其排程器的安排,並且一直存在於node中,直到手動或設定檔中要求他被移除。當這個node當機時,排程器會在其它可用的node上再度啟動這個pod
什麼是node
node就是一台獨立的主機,可以是實體機或vm,vm可以在本機或公有雲上。node由control plane來管理。一台node可以執行多個pod。k8s會自動控制每一台node上的pod數量,視你pod的執行策略來決定。一個node會執行下面幾個元件
service
pods的生命是有限的,其有一個生命週期。node當機時,上面的pod就都會不見。我們會設定ReplicaSet來規定一個cluster上pod的數量,讓這pods在其它node上重新建立以保證應用程式不會當機。
舉例來說,如果有一個網頁是由三個replicas,前端程式不會管後端有幾個replicas,如果pod死了再被建立了,也沒有人知道。在k8s中所謂的service一個抽象層,用來定義pods和存取其規則。service讓pods鬆散的結合在一起。
service也是用yaml
來定義。雖然每個pod都有獨一的ip,但我們的service會有一個共用ip來代表這些pods組成的service的服務。
kubectl
kubectl
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
kind
Kind 是 Kubernetes In Docker 的縮寫,就是使用 Docker 容器作為 Node 並將 Kubernetes 佈署至其中的一个工具。官方文件中也把 Kind 作为一種本機叢集搭建的工具推薦使用。
安裝kind
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.11.1/kind-linux-amd64
chmod +x ./kind
sudo mv ./kind /usr/bin
建立一個k8s的cluster
kind create cluster
觀察這個新建的cluster
(immust3)joshhu:~/ $ kind get clusters [21:32:03]
kind
(immust3)joshhu:~/ $
觀察主機上執行的容器
(immust3)joshhu:~/ $ docker ps [21:32:13]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
0e4ada413e60 kindest/node:v1.21.1 "/usr/local/bin/entr…" 10 minutes ago Up 10 minutes 127.0.0.1:38999->6443/tcp kind-control-plane
(immust3)joshhu:~/ $
觀察這個master執行了哪些元件
(immust3)joshhu:~/ $ docker exec -it kind-control-plane crictl ps [21:33:56]
CONTAINER IMAGE CREATED STATE NAME ATTEMPT POD ID
34376338903e3 296a6d5035e2d 11 minutes ago Running coredns 0 689666ced3c49
e7582ecfb1036 e422121c9c5f9 11 minutes ago Running local-path-provisioner 0 bc249b1a7b70c
262f980bbca58 296a6d5035e2d 11 minutes ago Running coredns 0 84b5cd1b9a809
c2a502cc2e7e8 6de166512aa22 11 minutes ago Running kindnet-cni 0 d065fb17413e3
2a388d60f1efa 0e124fb3c695b 11 minutes ago Running kube-proxy 0 668f230cc3329
34781a331c6d8 0369cf4303ffd 11 minutes ago Running etcd 0 136f6db45a627
d05b2f0e4c6d8 94ffe308aeff9 11 minutes ago Running kube-apiserver 0 e9240175b4dc0
4c753b3c38df6 96a295389d472 11 minutes ago Running kube-controller-manager 0 c99e5069272f3
3956f1516c5c5 1248d2d503d37 11 minutes ago Running kube-scheduler 0 6939e4acae6ea
(immust3)joshhu:~/ $
yaml
設定檔來建立k8s的cluster就像docker-compose
使用設定檔一樣,可以用yaml
設定檔來建立kind的cluster
kind create cluster --config kind-example-config.yaml
設定檔
# three node (two workers) cluster config
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: worker
- role: worker
建立叢集
(immust3)joshhu:~/ $ kind create cluster --config multi.yaml --name multi [21:40:22]
Creating cluster "multi" ...
✓ Ensuring node image (kindest/node:v1.21.1)
✓ Preparing nodes
✓ Writing configuration
✓ Starting control-plane ️
✓ Installing CNI
✓ Installing StorageClass
✓ Joining worker nodes
Set kubectl context to "kind-multi"
You can now use your cluster with:
kubectl cluster-info --context kind-multi
Not sure what to do next? Check out https://kind.sigs.k8s.io/docs/user/quick-start/
查看叢集
(immust3)joshhu:~/ $ [21:41:42]
(immust3)joshhu:~/ $ kind get clusters [21:41:55]
kind
multi
(immust3)joshhu:~/ $
查看kind建立的docker容器
(immust3)joshhu:~/ $ docker ps [21:42:33]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
73b04b0fc7c3 kindest/node:v1.21.1 "/usr/local/bin/entr…" About a minute ago Up About a minute multi-worker2
a3157c461c2e kindest/node:v1.21.1 "/usr/local/bin/entr…" About a minute ago Up About a minute 127.0.0.1:34137->6443/tcp multi-control-plane
c0a20e60c44f kindest/node:v1.21.1 "/usr/local/bin/entr…" About a minute ago Up About a minute multi-worker
0e4ada413e60 kindest/node:v1.21.1 "/usr/local/bin/entr…" 20 minutes ago Up 20 minutes 127.0.0.1:38999->6443/tcp kind-control-plane
(immust3)joshhu:~/ $
查看worker執行的元件
(immust3)joshhu:~/ $ docker exec -it 73 crictl ps [21:42:53]
CONTAINER IMAGE CREATED STATE NAME ATTEMPT POD ID
24839fbfd54ee 6de166512aa22 2 minutes ago Running kindnet-cni 0 b6097e581f591
51fb769ac8e03 0e124fb3c695b 2 minutes ago Running kube-proxy 0 4456c031eeab3
(immust3)joshhu:~/ $
設定檔ha.yaml
# a cluster with 3 control-plane nodes and 3 workers
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
nodes:
- role: control-plane
- role: control-plane
- role: control-plane
- role: worker
- role: worker
- role: worker
建立叢集
(immust3)joshhu:~/ $ kind create cluster --config ha.yaml --name ha [21:47:51]
Creating cluster "ha" ...
✓ Ensuring node image (kindest/node:v1.21.1)
✓ Preparing nodes
✓ Configuring the external load balancer ⚖️
✓ Writing configuration
✓ Starting control-plane ️
✓ Installing CNI
✓ Installing StorageClass
✓ Joining more control-plane nodes
✓ Joining worker nodes
Set kubectl context to "kind-ha"
You can now use your cluster with:
kubectl cluster-info --context kind-ha
Thanks for using kind!
(immust3)joshhu:~/ $
觀察建立的nodes
(immust3)joshhu:~/ $ docker ps [21:53:24]
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
f9da7122fac6 kindest/node:v1.21.1 "/usr/local/bin/entr…" 7 minutes ago Up 7 minutes ha-worker2
0f095ebb7fc3 kindest/node:v1.21.1 "/usr/local/bin/entr…" 7 minutes ago Up 7 minutes 127.0.0.1:38717->6443/tcp ha-control-plane
9a7064838f74 kindest/node:v1.21.1 "/usr/local/bin/entr…" 7 minutes ago Up 7 minutes ha-worker3
4fac6b7372b6 kindest/node:v1.21.1 "/usr/local/bin/entr…" 7 minutes ago Up 7 minutes 127.0.0.1:35005->6443/tcp ha-control-plane3
10c7d23d8f51 kindest/haproxy:v20200708-548e36db "/docker-entrypoint.…" 7 minutes ago Up 7 minutes 127.0.0.1:39013->6443/tcp ha-external-load-balancer
f747b5e6c2f7 kindest/node:v1.21.1 "/usr/local/bin/entr…" 7 minutes ago Up 7 minutes ha-worker
35a99822bc65 kindest/node:v1.21.1 "/usr/local/bin/entr…" 7 minutes ago Up 7 minutes 127.0.0.1:43573->6443/tcp ha-control-plane2
(immust3)joshhu:~/ $
kubectl
指令kubectl get
- list resourceskubectl describe
- show detailed information about a resourcekubectl logs
- print the logs from a container in a podkubectl exec
- execute a command on a container in a podminikube是一個在單機的虛擬中執行k8s的模擬環境,只建立一台node,這個node的角色既是master又是worker,只用來模擬,無法用在生產環境。
https://minikube.sigs.k8s.io/docs/start/
Linux的安裝,可在虛擬機中,2cpu,2g的ram
(joshhuAI)joshhu:~/ $ curl -LO https://storage.googleapis.com/minikube/releases/latest/minikube-linux-amd64 [1:26:41]
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 66.3M 100 66.3M 0 0 10.4M 0 0:00:06 0:00:06 --:--:-- 10.8M
(joshhuAI)joshhu:~/ $ sudo install minikube-linux-amd64 /usr/local/bin/minikube [1:26:49]
(joshhuAI)joshhu:~/ $ minikube start [1:26:55]
* minikube v1.24.0 on Ubuntu 16.04
* Automatically selected the docker driver. Other choices: kvm2, virtualbox, none, ssh
* Starting control plane node minikube in cluster minikube
* Pulling base image ...
* Downloading Kubernetes v1.22.3 preload ...
> preloaded-images-k8s-v13-v1...: 501.73 MiB / 501.73 MiB 100.00% 6.49 MiB
> gcr.io/k8s-minikube/kicbase: 355.77 MiB / 355.78 MiB 100.00% 4.60 MiB p/
* Creating docker container (CPUs=2, Memory=16000MB) ...
* Preparing Kubernetes v1.22.3 on Docker 20.10.8 ...
- Generating certificates and keys ...
- Booting up control plane ...
- Configuring RBAC rules ...
* Verifying Kubernetes components...
- Using image gcr.io/k8s-minikube/storage-provisioner:v5
* Enabled addons: storage-provisioner, default-storageclass
* kubectl not found. If you need it, try: 'minikube kubectl -- get pods -A'
* Done! kubectl is now configured to use "minikube" cluster and "default" namespace by default
(joshhuAI)joshhu:~/ $ minikube kubectl -- get po -A [1:29:55]
> kubectl.sha256: 64 B / 64 B [--------------------------] 100.00% ? p/s 0s
> kubectl: 44.73 MiB / 44.73 MiB [-------------] 100.00% 11.63 MiB p/s 4.0s
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-78fcd69978-79kh4 1/1 Running 0 84s
kube-system etcd-minikube 1/1 Running 0 96s
kube-system kube-apiserver-minikube 1/1 Running 0 96s
kube-system kube-controller-manager-minikube 1/1 Running 0 96s
kube-system kube-proxy-jtz9k 1/1 Running 0 84s
kube-system kube-scheduler-minikube 1/1 Running 0 96s
kube-system storage-provisioner 1/1 Running 1 (53s ago) 95s
(joshhuAI)joshhu:~/ $
alias kubectl="minikube kubectl --"
(joshhuAI)joshhu:~/ $ kubectl create deployment hello-minikube --image=k8s.gcr.io/echoserver:1.4 [1:46:09]
deployment.apps/hello-minikube created
(joshhuAI)joshhu:~/ $ kubectl expose deployment hello-minikube --type=NodePort --port=8080 [1:46:19]
service/hello-minikube exposed
(joshhuAI)joshhu:~/ $
(joshhuAI)joshhu:~/ $ kubectl get services hello-minikube [1:46:39]
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
hello-minikube NodePort 10.104.176.60 <none> 8080:31799/TCP 23s
(joshhuAI)joshhu:~/ $
(joshhuAI)joshhu:~/ $ minikube service hello-minikube [1:47:04]
|-----------|----------------|-------------|---------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|----------------|-------------|---------------------------|
| default | hello-minikube | 8080 | http://192.168.49.2:31799 |
|-----------|----------------|-------------|---------------------------|
* Opening service default/hello-minikube in default browser...
/usr/bin/xdg-open: 778: /usr/bin/xdg-open: www-browser: not found
/usr/bin/xdg-open: 778: /usr/bin/xdg-open: links2: not found
/usr/bin/xdg-open: 778: /usr/bin/xdg-open: elinks: not found
/usr/bin/xdg-open: 778: /usr/bin/xdg-open: links: not found
/usr/bin/xdg-open: 778: /usr/bin/xdg-open: lynx: not found
/usr/bin/xdg-open: 778: /usr/bin/xdg-open: w3m: not found
xdg-open: no method available for opening 'http://192.168.49.2:31799'
X Exiting due to HOST_BROWSER: exit status 3
*
╭─────────────────────────────────────────────────────────────────────────────────────────────╮
│ │
│ * If the above advice does not help, please let us know: │
│ https://github.com/kubernetes/minikube/issues/new/choose │
│ │
│ * Please run `minikube logs --file=logs.txt` and attach logs.txt to the GitHub issue. │
│ * Please also attach the following file to the GitHub issue: │
│ * - /tmp/minikube_service_e74d0a9fabf6c91e3100b1bcc7d95ef8b3cff051_0.log │
│ │
╰─────────────────────────────────────────────────────────────────────────────────────────────╯
(joshhuAI)joshhu:~/ $ kubectl port-forward service/hello-minikube 7080:8080 [1:47:30]
Forwarding from 127.0.0.1:7080 -> 8080
Forwarding from [::1]:7080 -> 8080
建立服務
(joshhuAI)joshhu:~/ $ kubectl create deployment balanced --image=k8s.gcr.io/echoserver:1.4 [1:52:16]
deployment.apps/balanced created
(joshhuAI)joshhu:~/ $ kubectl expose deployment balanced --type=LoadBalancer --port=8080 [1:52:21]
service/balanced exposed
(joshhuAI)joshhu:~/ $
建立tunnel
(joshhuAI)joshhu:~/ $ minikube tunnel [1:52:26]
Status:
machine: minikube
pid: 70653
route: 10.96.0.0/12 -> 192.168.49.2
minikube: Running
services: [balanced]
errors:
minikube: no errors
router: no errors
loadbalancer emulator: no errors
找到外部ip
(joshhuAI)joshhu:~/ $ kubectl get services balanced [1:54:05]
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
balanced LoadBalancer 10.111.95.98 10.111.95.98 8080:30203/TCP 105s
(joshhuAI)joshhu:~/ $
網頁結果
minikube pause
Unpause a paused instance:
minikube unpause
Halt the cluster:
minikube stop
Increase the default memory limit (requires a restart):
minikube config set memory 16384
Browse the catalog of easily installed Kubernetes services:
minikube addons list
Create a second cluster running an older Kubernetes release:
minikube start -p aged --kubernetes-version=v1.16.1
Delete all of the minikube clusters:
minikube delete --all
指令參考
https://minikube.sigs.k8s.io/docs/commands/
minikube
https://kubernetes.io/docs/tutorials/kubernetes-basics/create-cluster/cluster-interactive/
查看機器os
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="18.04.5 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.5 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic
查看記憶體
$ free -h
total used free shared buff/cache available
Mem: 2.4G 644M 531M 1.6M 1.3G 1.7G
Swap:
查看cpu
htop
查看硬碟使用
$ df -H
Filesystem Size Used Avail Use% Mounted on
udev 1.3G 0 1.3G 0% /dev
tmpfs 259M 840k 258M 1% /run
/dev/mapper/host01--vg-root 205G 13G 181G 7% /
tmpfs 1.3G 0 1.3G 0% /dev/shm
tmpfs 5.3M 0 5.3M 0% /run/lock
tmpfs 1.3G 0 1.3G 0% /sys/fs/cgroup
tmpfs 259M 0 259M 0% /run/user/0
$
查看資訊
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.4", GitCommit:"e87da0bd6e03ec3fea7933c4b5263d151aafd07c", GitTreeState:"clean", BuildDate:"2021-02-18T16:12:00Z", GoVersion:"go1.15.8", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"20", GitVersion:"v1.20.2", GitCommit:"faecb196815e248d3ecfb03c680a4507229c2a56", GitTreeState:"clean", BuildDate:"2021-01-13T13:20:00Z", GoVersion:"go1.15.5", Compiler:"gc", Platform:"linux/amd64"}
$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
minikube Ready control-plane,master 7m14s v1.20.2
$
建立app
kubectl create deployment kubernetes-bootcamp --image=gcr.io/google-samples/kubernetes-bootcamp:v1
查看app
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1/1 1 1 41s
$
在第二個terminal輸入指令
Extra Interactive Bash Terminal
$
$ echo -e "\n\n\n\e[92mStarting Proxy. After starting it will not output e first Terminal Tab\n"; the
Starting Proxy. After starting it will not output a response. Please click the first Terminal Tab
$ kubectl proxy
Starting to serve on 127.0.0.1:8001
回到第一個視窗
$ curl http://localhost:8001/version
{
"major": "1",
"minor": "20",
"gitVersion": "v1.20.2",
"gitCommit": "faecb196815e248d3ecfb03c680a4507229c2a56",
"gitTreeState": "clean",
"buildDate": "2021-01-13T13:20:00Z",
"goVersion": "go1.15.5",
"compiler": "gc",
"platform": "linux/amd64"
}$
取得pod
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-fb5c67579-hw27x 0/1 ContainerCreating 0 5s
取得pod資訊
$ kubectl describe pods
Name: kubernetes-bootcamp-fb5c67579-hw27x
Namespace: default
Priority: 0
Node: minikube/172.17.0.54
Start Time: Thu, 18 Nov 2021 05:58:01 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=fb5c67579
Annotations: <none>
Status: Running
IP: 172.18.0.6
IPs:
IP: 172.18.0.6
Controlled By: ReplicaSet/kubernetes-bootcamp-fb5c67579
Containers:
kubernetes-bootcamp:
Container ID: docker://80b559ed4500eb4f09ced11a10e1ddc18969c080f72551c9345f1624c7fd1638
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Status: Running
IP: 172.18.0.6
IPs:
IP: 172.18.0.6
Controlled By: ReplicaSet/kubernetes-bootcamp-fb5c67579
Containers:
kubernetes-bootcamp:
Container ID: docker://80b559ed4500eb4f09ced11a10e1ddc18969c080f72551c9345f1624c7fd1638
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Thu, 18 Nov 2021 05:58:07 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-krflw (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-krflw:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-krflw
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 35s default-scheduler Successfully assigned default/kubernetes-bootcamp-fb5c67579-hw27x to minikube
Normal Pulled 29s kubelet Container image "gcr.io/google-samples/kubernetes-bootcamp:v1" already present on machine
Normal Created 29s kubelet Created container kubernetes-bootcamp
Normal Started 28s kubelet Started container kubernetes-bootcamp
執行app
新開一個視窗並執行下面程式
Extra Interactive Bash Terminal
$
$ echo -e "\n\n\n\e[92mStarting Proxy. After starting it will not outpe first Terminal Tab\n"; kubectl proxy
Starting Proxy. After starting it will not output a response. Please click the first Terminal Tab
Starting to serve on 127.0.0.1:8001
建立新的服務
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-fb5c67579-k2kth 1/1 Running 0 27m
$
查看服務
$ kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 27m
$
開放通訊埠
$ kubectl expose deployment/kubernetes-bootcamp --type="NodePort" --port 8080
service/kubernetes-bootcamp exposed
$ kubectl get servicesNAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 49s
kubernetes-bootcamp NodePort 10.103.195.109 <none> 8080:32288/TCP 6s
$
查看服務
$ kubectl describe services/kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
Labels: app=kubernetes-bootcamp
Annotations: <none>
Selector: app=kubernetes-bootcamp
Type: NodePort
IP Families: <none>
IP: 10.103.195.109
IPs: 10.103.195.109
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 32288/TCP
Endpoints: 172.18.0.3:8080
Session Affinity: None
External Traffic Policy: Cluster
Events:
取得變數
$ export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
$ echo NODE_PORT=$NODE_PORT
NODE_PORT=32288
存取該服務
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-fb5c67579-x6vlw | v=1
$
使用labels
$ kubectl describe deployment
Name: kubernetes-bootcamp
Namespace: default
CreationTimestamp: Thu, 18 Nov 2021 07:17:47 +0000
Labels: app=kubernetes-bootcamp
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=kubernetes-bootcamp
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=kubernetes-bootcamp
Containers:
kubernetes-bootcamp:
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Port: 8080/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: kubernetes-bootcamp-fb5c67579 (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 5m37s deployment-controller Scaled up replica set kubernetes-bootcamp-fb5c67579 to 1
查看pods
$ kubectl get pods -l app=kubernetes-bootcamp
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-fb5c67579-x6vlw 1/1 Running 0 6m21s
$
查看service
$ kubectl get services -l app=kubernetes-bootcamp
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes-bootcamp NodePort 10.103.195.109 <none> 8080:32288/TCP 6m29s
$
取的pod的名稱
$ export POD_NAME=$(kubectl get pods -o go-template --template '{{range .items}}{{.metadata.name}}{{"\n"}}{{end}}')
$ echo Name of the Pod: $POD_NAME
Name of the Pod: kubernetes-bootcamp-fb5c67579-x6vlw
$
將此pod用label命名
$ kubectl label pods $POD_NAME version=v1
pod/kubernetes-bootcamp-fb5c67579-x6vlw labeled
$
取得pod資訊
$ kubectl describe pods $POD_NAME
Name: kubernetes-bootcamp-fb5c67579-x6vlw
Namespace: default
Priority: 0
Node: minikube/172.17.0.118
Start Time: Thu, 18 Nov 2021 07:18:01 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=fb5c67579
version=v1
Annotations: <none>
Status: Running
IP: 172.18.0.3
......(略)
使用label存取
$ kubectl get pods -l version=v1
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-fb5c67579-x6vlw 1/1 Running 0 9m9s
$
說明
k8s最棒的地方就是可以讓同一個應用程式建立多個pod做為負載平衡或熱備份之用,實作起來非常簡單,只要在deployment中的replicas中設定pod的個數即可。
單個pod
多個pod
查看depolyment
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1/1 1 1 111s
查看replicaset
$ kubectl get rs
NAME DESIRED CURRENT READY AGE
kubernetes-bootcamp-fb5c67579 1 1 1 2m49s
$
將複製數設定成4
$ kubectl scale deployments/kubernetes-bootcamp --replicas=4
deployment.apps/kubernetes-bootcamp scaled
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 1/4 4 1 4m8s
查看是不是pods變成4了
$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kubernetes-bootcamp-fb5c67579-555qc 1/1 Running 0 4m36s 172.18.0.2 minikube <none> <none>
kubernetes-bootcamp-fb5c67579-mxsz4 1/1 Running 0 45s 172.18.0.7 minikube <none> <none>
kubernetes-bootcamp-fb5c67579-swmx5 1/1 Running 0 45s 172.18.0.9 minikube <none> <none>
kubernetes-bootcamp-fb5c67579-zxr5x 1/1 Running 0 45s 172.18.0.8 minikube <none> <none>
$
$ kubectl describe deployments/kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
CreationTimestamp: Thu, 18 Nov 2021 07:35:20 +0000
Labels: app=kubernetes-bootcamp
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=kubernetes-bootcamp
Replicas: 4 desired | 4 updated | 4 total | 4 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=kubernetes-bootcamp
Containers:
kubernetes-bootcamp:
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Port: 8080/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Progressing True NewReplicaSetAvailable
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: kubernetes-bootcamp-fb5c67579 (4/4 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 5m13s deployment-controller Scaled up replica set kubernetes-bootcamp-fb5c67579 to 1
Normal ScalingReplicaSet 82s deployment-controller Scaled up replica set kubernetes-bootcamp-fb5c67579 to 4
查看是不是有load balancing
$ kubectl describe services/kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
Labels: app=kubernetes-bootcamp
Annotations: <none>
Selector: app=kubernetes-bootcamp
Type: NodePort
IP Families: <none>
IP: 10.106.251.229
IPs: 10.106.251.229
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 32307/TCP
Endpoints: 172.18.0.2:8080,172.18.0.7:8080,172.18.0.8:8080 + 1 more...
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
$
輸出nodeport
$ echo NODE_PORT=$NODE_PORT
NODE_PORT=32307
$
存取服務
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-fb5c67579-mxsz4 | v=1
$
把複製數降為2
$ kubectl scale deployments/kubernetes-bootcamp --replicas=2
deployment.apps/kubernetes-bootcamp scaled
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 2/2 2 2 9m54s
$ kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kubernetes-bootcamp-fb5c67579-555qc 1/1 Running 0 10m 172.18.0.2 minikube <none> <none>
kubernetes-bootcamp-fb5c67579-zxr5x 1/1 Running 0 6m21s 172.18.0.8 minikube <none> <none>
取得deployment
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 4/4 4 4 3m48s
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-fb5c67579-b6mj7 1/1 Running 0 3m42s
kubernetes-bootcamp-fb5c67579-hkqrb 1/1 Running 0 3m42s
kubernetes-bootcamp-fb5c67579-n2wxj 1/1 Running 0 3m42s
kubernetes-bootcamp-fb5c67579-pkk78 1/1 Running 0 3m42s
$
查看pods
$ kubectl describe pods
Name: kubernetes-bootcamp-fb5c67579-b6mj7
Namespace: default
Priority: 0
Node: minikube/172.17.0.37
Start Time: Thu, 18 Nov 2021 07:47:28 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=fb5c67579
Annotations: <none>
Status: Running
IP: 172.18.0.5
IPs:
IP: 172.18.0.5
Controlled By: ReplicaSet/kubernetes-bootcamp-fb5c67579
Containers:
kubernetes-bootcamp:
Container ID: docker://5f978454433092a210061234f565fad7d1bdc46f1b80a6b6ea41f429b5d0113b
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Thu, 18 Nov 2021 07:47:34 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-xhlrp (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-xhlrp:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-xhlrp
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 4m5s default-scheduler Successfully assigned default/kubernetes-bootcamp-fb5c67579-b6mj7 to minikube
Normal Pulled 4m kubelet Container image "gcr.io/google-samples/kubernetes-bootcamp:v1" already present on machine
Normal Created 4m kubelet Created container kubernetes-bootcamp
Normal Started 3m59s kubelet Started container kubernetes-bootcamp
Name: kubernetes-bootcamp-fb5c67579-hkqrb
Namespace: default
Priority: 0
Node: minikube/172.17.0.37
Start Time: Thu, 18 Nov 2021 07:47:28 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=fb5c67579
Annotations: <none>
Status: Running
IP: 172.18.0.3
IPs:
IP: 172.18.0.3
Controlled By: ReplicaSet/kubernetes-bootcamp-fb5c67579
Containers:
kubernetes-bootcamp:
Container ID: docker://ecd4e3700c9bb065337c5a9f9cdcd75204048fd2c7d5600664ea93d0b8602840
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Thu, 18 Nov 2021 07:47:33 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-xhlrp (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-xhlrp:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-xhlrp
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 4m5s default-scheduler Successfully assigned default/kubernetes-bootcamp-fb5c67579-hkqrb to minikube
Normal Pulled 4m2s kubelet Container image "gcr.io/google-samples/kubernetes-bootcamp:v1" already present on machine
Normal Created 4m1s kubelet Created container kubernetes-bootcamp
Normal Started 4m kubelet Started container kubernetes-bootcamp
Name: kubernetes-bootcamp-fb5c67579-n2wxj
Namespace: default
Priority: 0
Node: minikube/172.17.0.37
Start Time: Thu, 18 Nov 2021 07:47:28 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=fb5c67579
Annotations: <none>
Status: Running
IP: 172.18.0.4
IPs:
IP: 172.18.0.4
Controlled By: ReplicaSet/kubernetes-bootcamp-fb5c67579
Containers:
kubernetes-bootcamp:
Container ID: docker://fe417b5ad79c1646af10a8c6f99ce1452b7173e84f4e9277683bd4089e425bda
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Thu, 18 Nov 2021 07:47:35 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-xhlrp (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-xhlrp:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-xhlrp
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 4m5s default-scheduler Successfully assigned default/kubernetes-bootcamp-fb5c67579-n2wxj to minikube
Normal Pulled 4m kubelet Container image "gcr.io/google-samples/kubernetes-bootcamp:v1" already present on machine
Normal Created 3m59s kubelet Created container kubernetes-bootcamp
Normal Started 3m58s kubelet Started container kubernetes-bootcamp
Name: kubernetes-bootcamp-fb5c67579-pkk78
Namespace: default
Priority: 0
Node: minikube/172.17.0.37
Start Time: Thu, 18 Nov 2021 07:47:28 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=fb5c67579
Annotations: <none>
Status: Running
IP: 172.18.0.6
IPs:
IP: 172.18.0.6
Controlled By: ReplicaSet/kubernetes-bootcamp-fb5c67579
Containers:
kubernetes-bootcamp:
Container ID: docker://63ae903f268e8e8ef7eddcbad74b069f444f9d245a64a7032acf0cc7721ecb0f
Image: gcr.io/google-samples/kubernetes-bootcamp:v1
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:0d6b8ee63bb57c5f5b6156f446b3bc3b3c143d233037f3a2f00e279c8fcc64af
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Thu, 18 Nov 2021 07:47:35 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-xhlrp (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-xhlrp:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-xhlrp
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 4m5s default-scheduler Successfully assigned default/kubernetes-bootcamp-fb5c67579-pkk78 to minikube
Normal Pulled 3m59s kubelet Container image "gcr.io/google-samples/kubernetes-bootcamp:v1" already present on machine
Normal Created 3m59s kubelet Created container kubernetes-bootcamp
Normal Started 3m58s kubelet Started container kubernetes-bootcamp
更新應用程式的映像檔
$ kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=jocatalin/kubernetes-bootcamp:v2
deployment.apps/kubernetes-bootcamp image updated
$
查看更新狀態
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-7d44784b7c-78xjv 1/1 Running 0 31s
kubernetes-bootcamp-7d44784b7c-7fkms 1/1 Running 0 35s
kubernetes-bootcamp-7d44784b7c-88gcp 1/1 Running 0 32s
kubernetes-bootcamp-7d44784b7c-pmnlt 1/1 Running 0 35s
kubernetes-bootcamp-fb5c67579-b6mj7 0/1 Terminating 0 5m43s
kubernetes-bootcamp-fb5c67579-hkqrb 1/1 Terminating 0 5m43s
kubernetes-bootcamp-fb5c67579-n2wxj 0/1 Terminating 0 5m43s
kubernetes-bootcamp-fb5c67579-pkk78 0/1 Terminating 0 5m43s
$
確定應用程式更新了
$ kubectl describe services/kubernetes-bootcamp
Name: kubernetes-bootcamp
Namespace: default
Labels: app=kubernetes-bootcamp
Annotations: <none>
Selector: app=kubernetes-bootcamp
Type: NodePort
IP Families: <none>
IP: 10.100.28.65
IPs: 10.100.28.65
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 30841/TCP
Endpoints: 172.18.0.10:8080,172.18.0.11:8080,172.18.0.12:8080 + 1 more...
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
取得nodeport
$ export NODE_PORT=$(kubectl get services/kubernetes-bootcamp -o go-template='{{(index .spec.ports 0).nodePort}}')
$ echo NODE_PORT=$NODE_PORT
NODE_PORT=30841
存取這個應用程式
每次你使用curl
時都會去不同的pod,而且發現應用程式已經更新成v2了。
$ curl $(minikube ip):$NODE_PORT
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-7d44784b7c-78xjv | v=2
查看是否更新
$ kubectl rollout status deployments/kubernetes-bootcamp
deployment "kubernetes-bootcamp" successfully rolled out
查看新的pods
$ kubectl describe pods
Name: kubernetes-bootcamp-7d44784b7c-78xjv
Namespace: default
Priority: 0
Node: minikube/172.17.0.37
Start Time: Thu, 18 Nov 2021 07:52:40 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=7d44784b7c
Annotations: <none>
Status: Running
IP: 172.18.0.13
IPs:
IP: 172.18.0.13
Controlled By: ReplicaSet/kubernetes-bootcamp-7d44784b7c
Containers:
kubernetes-bootcamp:
Container ID: docker://e403cfa735b862aaad95ef9fcc3c4f5a9db0380283444f79f59d7b7c9b7bb804
Image: jocatalin/kubernetes-bootcamp:v2
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Thu, 18 Nov 2021 07:52:42 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-xhlrp (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-xhlrp:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-xhlrp
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 3m26s default-scheduler Successfully assigned default/kubernetes-bootcamp-7d44784b7c-78xjv to minikube
Normal Pulled 3m24s kubelet Container image "jocatalin/kubernetes-bootcamp:v2" already present on machine
Normal Created 3m24s kubelet Created container kubernetes-bootcamp
Normal Started 3m24s kubelet Started container kubernetes-bootcamp
Name: kubernetes-bootcamp-7d44784b7c-7fkms
Namespace: default
Priority: 0
Node: minikube/172.17.0.37
Start Time: Thu, 18 Nov 2021 07:52:37 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=7d44784b7c
Annotations: <none>
Status: Running
IP: 172.18.0.11
IPs:
IP: 172.18.0.11
Controlled By: ReplicaSet/kubernetes-bootcamp-7d44784b7c
Containers:
kubernetes-bootcamp:
Container ID: docker://f3ab7b28a3dfad5161a491a081828f8b59b1083ab11a039e0377726db2b99799
Image: jocatalin/kubernetes-bootcamp:v2
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Thu, 18 Nov 2021 07:52:39 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-xhlrp (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-xhlrp:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-xhlrp
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 3m29s default-scheduler Successfully assigned default/kubernetes-bootcamp-7d44784b7c-7fkms to minikube
Normal Pulled 3m28s kubelet Container image "jocatalin/kubernetes-bootcamp:v2" already present on machine
Normal Created 3m27s kubelet Created container kubernetes-bootcamp
Normal Started 3m27s kubelet Started container kubernetes-bootcamp
Name: kubernetes-bootcamp-7d44784b7c-88gcp
Namespace: default
Priority: 0
Node: minikube/172.17.0.37
Start Time: Thu, 18 Nov 2021 07:52:39 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=7d44784b7c
Annotations: <none>
Status: Running
IP: 172.18.0.12
IPs:
IP: 172.18.0.12
Controlled By: ReplicaSet/kubernetes-bootcamp-7d44784b7c
Containers:
kubernetes-bootcamp:
Container ID: docker://494a323701ca7f3a872d56e7824cb5d41c861dc1a6ab73d49115c9d4cf4c9e08
Image: jocatalin/kubernetes-bootcamp:v2
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Thu, 18 Nov 2021 07:52:42 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-xhlrp (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-xhlrp:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-xhlrp
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 3m27s default-scheduler Successfully assigned default/kubernetes-bootcamp-7d44784b7c-88gcp to minikube
Normal Pulled 3m25s kubelet Container image "jocatalin/kubernetes-bootcamp:v2" already present on machine
Normal Created 3m24s kubelet Created container kubernetes-bootcamp
Normal Started 3m24s kubelet Started container kubernetes-bootcamp
Name: kubernetes-bootcamp-7d44784b7c-pmnlt
Namespace: default
Priority: 0
Node: minikube/172.17.0.37
Start Time: Thu, 18 Nov 2021 07:52:36 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=7d44784b7c
Annotations: <none>
Status: Running
IP: 172.18.0.10
IPs:
IP: 172.18.0.10
Controlled By: ReplicaSet/kubernetes-bootcamp-7d44784b7c
Containers:
kubernetes-bootcamp:
Container ID: docker://bf9c990603cb40552938caeb7c9b6120d6549316f5eb9ac84a1b8d2f5e55b3ee
Image: jocatalin/kubernetes-bootcamp:v2
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Thu, 18 Nov 2021 07:52:39 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-xhlrp (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-xhlrp:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-xhlrp
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 3m30s default-scheduler Successfully assigned default/kubernetes-bootcamp-7d44784b7c-pmnlt to minikube
Normal Pulled 3m28s kubelet Container image "jocatalin/kubernetes-bootcamp:v2" already present on machine
Normal Created 3m28s kubelet Created container kubernetes-bootcamp
Normal Started 3m27s kubelet Started container kubernetes-bootcamp
繼續更新程式
$ kubectl set image deployments/kubernetes-bootcamp kubernetes-bootcamp=gcr.io/google-samples/kubernetes-bootcamp:v10
deployment.apps/kubernetes-bootcamp image updated
$ kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
kubernetes-bootcamp 3/4 2 3 10m
$
列出pods
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-59b7598c77-j8mpm 0/1 ImagePullBackOff 0 32s
kubernetes-bootcamp-59b7598c77-r2rxf 0/1 ErrImagePull 0 32s
kubernetes-bootcamp-7d44784b7c-78xjv 0/1 Terminating 0 5m11s
kubernetes-bootcamp-7d44784b7c-7fkms 1/1 Running 0 5m15s
kubernetes-bootcamp-7d44784b7c-88gcp 1/1 Running 0 5m12s
kubernetes-bootcamp-7d44784b7c-pmnlt 1/1 Running 0 5m15s
$
列出pods詳細資訊
$ kubectl describe pods
Name: kubernetes-bootcamp-59b7598c77-j8mpm
Namespace: default
Priority: 0
Node: minikube/172.17.0.37
Start Time: Thu, 18 Nov 2021 07:57:19 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=59b7598c77
Annotations: <none>
Status: Pending
IP: 172.18.0.4
IPs:
IP: 172.18.0.4
Controlled By: ReplicaSet/kubernetes-bootcamp-59b7598c77
Containers:
kubernetes-bootcamp:
Container ID:
Image: gcr.io/google-samples/kubernetes-bootcamp:v10
Image ID:
Port: 8080/TCP
Host Port: 0/TCP
State: Waiting
Reason: ImagePullBackOff
Ready: False
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-xhlrp (ro)
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
Volumes:
default-token-xhlrp:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-xhlrp
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 74s default-scheduler Successfully assigned default/kubernetes-bootcamp-59b7598c77-j8mpm to minikube
Normal Pulling 28s (x3 over 72s) kubelet Pulling image "gcr.io/google-samples/kubernetes-bootcamp:v10"
Warning Failed 28s (x3 over 72s) kubelet Failed to pull image "gcr.io/google-samples/kubernetes-bootcamp:v10": rpc error: code = Unknown desc = Error response from daemon: manifest for gcr.io/google-samples/kubernetes-bootcamp:v10 not found: manifest unknown: Failed to fetch "v10" from request "/v2/google-samples/kubernetes-bootcamp/manifests/v10".
Warning Failed 28s (x3 over 72s) kubelet Error: ErrImagePull
Normal BackOff 2s (x4 over 71s) kubelet Back-off pulling image "gcr.io/google-samples/kubernetes-bootcamp:v10"
Warning Failed 2s (x4 over 71s) kubelet Error: ImagePullBackOff
Name: kubernetes-bootcamp-59b7598c77-r2rxf
Namespace: default
Priority: 0
Node: minikube/172.17.0.37
Start Time: Thu, 18 Nov 2021 07:57:19 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=59b7598c77
Annotations: <none>
Status: Pending
IP: 172.18.0.3
IPs:
IP: 172.18.0.3
Controlled By: ReplicaSet/kubernetes-bootcamp-59b7598c77
Containers:
kubernetes-bootcamp:
Container ID:
Image: gcr.io/google-samples/kubernetes-bootcamp:v10
Image ID:
Port: 8080/TCP
Host Port: 0/TCP
State: Waiting
Reason: ImagePullBackOff
Ready: False
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-xhlrp (ro)
Conditions:
Type Status
Initialized True
Ready False
ContainersReady False
PodScheduled True
Volumes:
default-token-xhlrp:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-xhlrp
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 74s default-scheduler Successfully assigned default/kubernetes-bootcamp-59b7598c77-r2rxf to minikube
Normal Pulling 30s (x3 over 73s) kubelet Pulling image "gcr.io/google-samples/kubernetes-bootcamp:v10"
Warning Failed 29s (x3 over 72s) kubelet Failed to pull image "gcr.io/google-samples/kubernetes-bootcamp:v10": rpc error: code = Unknown desc = Error response from daemon: manifest for gcr.io/google-samples/kubernetes-bootcamp:v10 not found: manifest unknown: Failed to fetch "v10" from request "/v2/google-samples/kubernetes-bootcamp/manifests/v10".
Warning Failed 29s (x3 over 72s) kubelet Error: ErrImagePull
Normal BackOff 2s (x5 over 72s) kubelet Back-off pulling image "gcr.io/google-samples/kubernetes-bootcamp:v10"
Warning Failed 2s (x5 over 72s) kubelet Error: ImagePullBackOff
Name: kubernetes-bootcamp-7d44784b7c-7fkms
Namespace: default
Priority: 0
Node: minikube/172.17.0.37
Start Time: Thu, 18 Nov 2021 07:52:37 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=7d44784b7c
Annotations: <none>
Status: Running
IP: 172.18.0.11
IPs:
IP: 172.18.0.11
Controlled By: ReplicaSet/kubernetes-bootcamp-7d44784b7c
Containers:
kubernetes-bootcamp:
Container ID: docker://f3ab7b28a3dfad5161a491a081828f8b59b1083ab11a039e0377726db2b99799
Image: jocatalin/kubernetes-bootcamp:v2
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Thu, 18 Nov 2021 07:52:39 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-xhlrp (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-xhlrp:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-xhlrp
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 5m56s default-scheduler Successfully assigned default/kubernetes-bootcamp-7d44784b7c-7fkms to minikube
Normal Pulled 5m55s kubelet Container image "jocatalin/kubernetes-bootcamp:v2" already present on machine
Normal Created 5m54s kubelet Created container kubernetes-bootcamp
Normal Started 5m54s kubelet Started container kubernetes-bootcamp
Name: kubernetes-bootcamp-7d44784b7c-88gcp
Namespace: default
Priority: 0
Node: minikube/172.17.0.37
Start Time: Thu, 18 Nov 2021 07:52:39 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=7d44784b7c
Annotations: <none>
Status: Running
IP: 172.18.0.12
IPs:
IP: 172.18.0.12
Controlled By: ReplicaSet/kubernetes-bootcamp-7d44784b7c
Containers:
kubernetes-bootcamp:
Container ID: docker://494a323701ca7f3a872d56e7824cb5d41c861dc1a6ab73d49115c9d4cf4c9e08
Image: jocatalin/kubernetes-bootcamp:v2
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Thu, 18 Nov 2021 07:52:42 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-xhlrp (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-xhlrp:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-xhlrp
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 5m54s default-scheduler Successfully assigned default/kubernetes-bootcamp-7d44784b7c-88gcp to minikube
Normal Pulled 5m52s kubelet Container image "jocatalin/kubernetes-bootcamp:v2" already present on machine
Normal Created 5m51s kubelet Created container kubernetes-bootcamp
Normal Started 5m51s kubelet Started container kubernetes-bootcamp
Name: kubernetes-bootcamp-7d44784b7c-pmnlt
Namespace: default
Priority: 0
Node: minikube/172.17.0.37
Start Time: Thu, 18 Nov 2021 07:52:36 +0000
Labels: app=kubernetes-bootcamp
pod-template-hash=7d44784b7c
Annotations: <none>
Status: Running
IP: 172.18.0.10
IPs:
IP: 172.18.0.10
Controlled By: ReplicaSet/kubernetes-bootcamp-7d44784b7c
Containers:
kubernetes-bootcamp:
Container ID: docker://bf9c990603cb40552938caeb7c9b6120d6549316f5eb9ac84a1b8d2f5e55b3ee
Image: jocatalin/kubernetes-bootcamp:v2
Image ID: docker-pullable://jocatalin/kubernetes-bootcamp@sha256:fb1a3ced00cecfc1f83f18ab5cd14199e30adc1b49aa4244f5d65ad3f5feb2a5
Port: 8080/TCP
Host Port: 0/TCP
State: Running
Started: Thu, 18 Nov 2021 07:52:39 +0000
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from default-token-xhlrp (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
default-token-xhlrp:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-xhlrp
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 5m57s default-scheduler Successfully assigned default/kubernetes-bootcamp-7d44784b7c-pmnlt to minikube
Normal Pulled 5m55s kubelet Container image "jocatalin/kubernetes-bootcamp:v2" already present on machine
Normal Created 5m55s kubelet Created container kubernetes-bootcamp
Normal Started 5m54s kubelet Started container kubernetes-bootcamp
回復到先前版本
$ kubectl rollout undo deployments/kubernetes-bootcamp
deployment.apps/kubernetes-bootcamp rolled back
查看pods
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-59b7598c77-j8mpm 0/1 Terminating 0 2m53s
kubernetes-bootcamp-59b7598c77-r2rxf 0/1 Terminating 0 2m53s
kubernetes-bootcamp-7d44784b7c-6rhst 1/1 Running 0 32s
kubernetes-bootcamp-7d44784b7c-7fkms 1/1 Running 0 7m36s
kubernetes-bootcamp-7d44784b7c-88gcp 1/1 Running 0 7m33s
kubernetes-bootcamp-7d44784b7c-pmnlt 1/1 Running 0 7m36s
可以使用kubectl describe pods
再次查看。