前面的筆記大致上介紹如何建立與部署應用程式, 後面的筆記將會比較零散的補充一些主題, 本篇筆記將補充介紹 Kubernetes 的一個基本概念 - Namespaces
Namespaces 主要的功能為提供多個虛擬(邏輯上)的叢集, 主要的應用情境如下:
1. 企業中區分不同的組織權責劃分
2. 提供不同目的的環境做邏輯上的區隔, 例如開發環境, 測試環境與生產環境
3. 可以提供多租用戶(Multi-Tenant)的客戶服務區隔
4. 可以用以應用程式分類
以下的 yaml 部署檔案中, 定義不同功能的應用程式 namespaces, 在大型專案下, 經常會以這種方式建立 namespaces. 不過建議有兩個原則: (1) 避免過大的 namespace, (2) 避免為建立 namespeace 而建立
建立 namespace 的格式如下:
apiVersion: v1kind: Namespace
metadata:name: web
Sample yaml file: sample-infrastructure.yaml
apiVersion: v1
kind: Namespace
metadata:
name: web
---
apiVersion: v1
kind: Namespace
metadata:
name: auth
---
apiVersion: v1
kind: Namespace
metadata:
name: cart
---
apiVersion: v1
kind: Namespace
metadata:
name: social
---
apiVersion: v1
kind: Namespace
metadata:
name: catalog
---
apiVersion: v1
kind: Namespace
metadata:
name: quote
---
apiVersion: v1
kind: Namespace
metadata:
name: purchasing
---
apiVersion: v1
kind: Namespace
metadata:
name: infra
---
apiVersion: v1
kind: Pod
metadata:
name: homepage-dev
namespace: web
labels:
env: development
dev-lead: karthik
team: web
application_type: ui
release-version: "12.0"
spec:
containers:
- name: helloworld
image: karthequian/helloworld:latest
---
apiVersion: v1
kind: Pod
metadata:
name: homepage-staging
namespace: web
labels:
env: staging
team: web
dev-lead: karthik
application_type: ui
release-version: "12.0"
spec:
containers:
- name: helloworld
image: karthequian/helloworld:latest
---
apiVersion: v1
kind: Pod
metadata:
name: homepage-prod
namespace: web
labels:
env: production
team: web
dev-lead: karthik
application_type: ui
release-version: "12.0"
spec:
containers:
- name: helloworld
image: karthequian/helloworld:latest
---
apiVersion: v1
kind: Pod
metadata:
name: login-dev
namespace: auth
labels:
env: development
team: auth
dev-lead: jim
application_type: api
release-version: "1.0"
spec:
containers:
- name: login
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: login-staging
namespace: auth
labels:
env: staging
team: auth
dev-lead: jim
application_type: api
release-version: "1.0"
spec:
containers:
- name: login
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: login-prod
namespace: auth
labels:
env: production
team: auth
dev-lead: jim
application_type: api
release-version: "1.0"
spec:
containers:
- name: login
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: cart-dev
namespace: cart
labels:
env: development
team: ecommerce
dev-lead: carisa
application_type: api
release-version: "1.0"
spec:
containers:
- name: cart
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: cart-staging
namespace: cart
labels:
env: staging
team: ecommerce
dev-lead: carisa
application_type: api
release-version: "1.0"
spec:
containers:
- name: cart
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: cart-prod
namespace: cart
labels:
env: production
team: ecommerce
dev-lead: carisa
application_type: api
release-version: "1.0"
spec:
containers:
- name: cart
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: social-dev
namespace: social
labels:
env: development
team: marketing
dev-lead: carisa
application_type: api
release-version: "2.0"
spec:
containers:
- name: social
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: social-staging
namespace: social
labels:
env: staging
team: marketing
dev-lead: marketing
application_type: api
release-version: "1.0"
spec:
containers:
- name: social
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: social-prod
namespace: social
labels:
env: production
team: marketing
dev-lead: marketing
application_type: api
release-version: "1.0"
spec:
containers:
- name: social
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: catalog-dev
namespace: catalog
labels:
env: development
team: ecommerce
dev-lead: daniel
application_type: api
release-version: "4.0"
spec:
containers:
- name: catalog
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: catalog-staging
namespace: catalog
labels:
env: staging
team: ecommerce
dev-lead: daniel
application_type: api
release-version: "4.0"
spec:
containers:
- name: catalog
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: catalog-prod
namespace: catalog
labels:
env: production
team: ecommerce
dev-lead: daniel
application_type: api
release-version: "4.0"
spec:
containers:
- name: catalog
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: quote-dev
namespace: quote
labels:
env: development
team: ecommerce
dev-lead: amy
application_type: api
release-version: "2.0"
spec:
containers:
- name: quote
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: quote-staging
namespace: quote
labels:
env: staging
team: ecommerce
dev-lead: amy
application_type: api
release-version: "2.0"
spec:
containers:
- name: quote
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: quote-prod
namespace: quote
labels:
env: production
team: ecommerce
dev-lead: amy
application_type: api
release-version: "1.0"
spec:
containers:
- name: quote
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: ordering-dev
namespace: purchasing
labels:
env: development
team: purchasing
dev-lead: chen
application_type: backend
release-version: "2.0"
spec:
containers:
- name: ordering
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: ordering-staging
namespace: purchasing
labels:
env: staging
team: purchasing
dev-lead: chen
application_type: backend
release-version: "2.0"
spec:
containers:
- name: ordering
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: ordering-prod
namespace: purchasing
labels:
env: production
team: purchasing
dev-lead: chen
application_type: backend
release-version: "2.0"
spec:
containers:
- name: ordering
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: logger
namespace: infra
labels:
env: production
team: infrastructure
dev-lead: tracey
application_type: backend
release-version: "2.0"
spec:
containers:
- name: logger
image: karthequian/ruby:latest
---
apiVersion: v1
kind: Pod
metadata:
name: monitor
namespace: infra
labels:
env: production
team: infrastructure
dev-lead: tracey
application_type: backend
release-version: "2.0"
spec:
containers:
- name: monitor
image: karthequian/ruby:latest
---
PS C:\k8s\namespaces> kubectl apply -f .\sample-infrastructure.yaml
namespace "web" created
namespace "auth" created
namespace "cart" created
namespace "social" created
namespace "catalog" created
namespace "quote" created
namespace "purchasing" created
namespace "infra" created
<略…>
namespaces
可以列出目前的 namespacesPS C:\k8s\namespaces> kubectl get namespaces
NAME STATUS AGE
auth Active 3m
cart Active 3m
catalog Active 3m
default Active 14d
docker Active 14d
infra Active 3m
kube-public Active 14d
kube-system Active 14d
purchasing Active 3m
quote Active 3m
social Active 3m
web Active 3m
--all-namespaces
可以列出目前的所有 namespaces 下的 podsPS C:\k8s\namespaces> kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
auth login-dev 1/1 Running 0 4m
auth login-prod 1/1 Running 0 4m
auth login-staging 1/1 Running 0 4m
cart cart-dev 1/1 Running 0 4m
cart cart-prod 1/1 Running 0 4m
cart cart-staging 1/1 Running 0 4m
catalog catalog-dev 1/1 Running 0 4m
catalog catalog-prod 1/1 Running 0 4m
catalog catalog-staging 1/1 Running 0 4m
default aspnetapp-deployment-74c677458b-bwck7 1/1 Running 7 14d
default aspnetapp-deployment-74c677458b-sx7lk 1/1 Running 7 14d
default aspnetapp-interactive-delopyment-dbdd784f9-7nn6l 1/1 Running 7 14d
default aspnetapp-interactive-delopyment-dbdd784f9-rz585 1/1 Running 7 14d
default aspnetapp-interactive-delopyment-dbdd784f9-wgql6 1/1 Running 7 14d
default minecraft-deployment-6ccdd5bfd-8jpgz 1/1 Running 2 6h
default my-release-minecraft-64b8664b79-fk7hr 1/1 Running 6 7h
default torpid-cat-jenkins-6b8fb6d958-lphds 1/1 Running 1 6h
default wishlist-chart-69796586bb-2st6r 3/3 Running 3 9h
default wishlist-chart-69796586bb-6ntlf 3/3 Running 3 9h
default wishlist-chart-69796586bb-8jlzr 3/3 Running 3 9h
default wishlist-deployment-6cb7f6c4c9-24q2k 3/3 Running 3 21h
default wishlist-deployment-6cb7f6c4c9-qdhrs 3/3 Running 3 21h
default wishlist-deployment-6cb7f6c4c9-vl5hs 3/3 Running 3 21h
docker compose-7447646cf5-ntjnb 1/1 Running 7 14d
docker compose-api-6fbc44c575-m7sm7 1/1 Running 11 14d
infra logger 1/1 Running 0 4m
infra monitor 1/1 Running 0 4m
kube-system etcd-docker-for-desktop 1/1 Running 14 14d
kube-system kube-apiserver-docker-for-desktop 1/1 Running 9 14d
kube-system kube-controller-manager-docker-for-desktop 1/1 Running 12 14d
kube-system kube-dns-86f4d74b45-dkjjr 3/3 Running 25 14d
kube-system kube-proxy-ld8zj 1/1 Running 8 14d
kube-system kube-scheduler-docker-for-desktop 1/1 Running 11 14d
kube-system tiller-deploy-6fd8d857bc-c8zvj 1/1 Running 1 9h
purchasing ordering-dev 1/1 Running 0 4m
purchasing ordering-prod 1/1 Running 0 4m
purchasing ordering-staging 1/1 Running 0 4m
quote quote-dev 1/1 Running 0 4m
quote quote-prod 1/1 Running 0 4m
quote quote-staging 1/1 Running 0 4m
social social-dev 1/1 Running 0 4m
social social-prod 1/1 Running 0 4m
social social-staging 1/1 Running 0 4m
web homepage-dev 1/1 Running 0 4m
web homepage-prod 1/1 Running 0 4m
web homepage-staging 1/1 Running 0 4m
以上是簡單的介紹 namespace, 下一篇筆記, 將介紹 Label 與 selector 觀念.