https://medium.com/containermind/a-beginners-guide-to-kubernetes-7e8ca56420b6
https://www.freecodecamp.org/news/learn-kubernetes-in-under-3-hours-a-detailed-guide-to-orchestrating-containers-114ff420e882/
https://devopscube.com/kubernetes-deployment-tutorial/
一. 基本概念
Pod 共享网络、存储和计算资源的容器化服务的集合
Deployment & ReplicaSet
Deployment 的作用是管理和控制 Pod 和 ReplicaSet,管控它们运行在用户期望的状态
ReplicaSet 的作用就是管理和控制 Pod
Service & Ingress
Service 和 Ingress 负责管控 Pod 网络服务
Service 主要负责 K8S 集群内部的网络拓扑
Ingress 是整个 K8S 集群的接入层,复杂集群内外通讯
Namespace
K8S 集群内创建 namespace 来分隔资源和对象
二. deployment yaml文件
YAML由 apiVersion、Kind、metadata、spec 4个部分组成
apiVersion
# kubectl api-versions
admissionregistration.k8s.io/v1
apiextensions.k8s.io/v1
apiregistration.k8s.io/v1
apps/v1
authentication.k8s.io/v1
authorization.k8s.io/v1
autoscaling/v1
autoscaling/v2beta1
autoscaling/v2beta2
batch/v1
batch/v1beta1
certificates.k8s.io/v1
coordination.k8s.io/v1
crd.projectcalico.org/v1
discovery.k8s.io/v1
discovery.k8s.io/v1beta1
events.k8s.io/v1
events.k8s.io/v1beta1
flowcontrol.apiserver.k8s.io/v1beta1
networking.k8s.io/v1
node.k8s.io/v1
node.k8s.io/v1beta1
policy/v1
policy/v1beta1
rbac.authorization.k8s.io/v1
scheduling.k8s.io/v1
storage.k8s.io/v1
storage.k8s.io/v1beta1
v1
# kubectl api-resources
kind
componentstatuses
configmaps
daemonsets
deployments
events
endpoints
horizontalpodautoscalers
ingress
jobs
limitranges
namespaces
nodes
pods
persistentvolumes
persistentvolumeclaims
resourcequotas
replicasets
replicationcontrollers
serviceaccounts
services
Metadata
labels 、name 、 namespace 、annotations
metadata:
name: resource-name # deployment name
namespace: deployment-demo
labels:
app: web
platform: java
release: 18.0
annotations:
monitoring: true
prod: true
Spec
# Replicas 确保任一时间运行pod数量
spec:
replicas: 3
# Selector 匹配标签的pod将被管理
selector:
matchLabels:
app: nginx
# Template 包含镜像、端口、环境变量、命令参数等信息,用于复用
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
name: nginx
三. 部署示例
1. 建立namespace
cat > /tmp/namespace.yaml << EOF
apiVersion: v1
kind: Namespace
metadata:
name: kube-demo
labels:
apps: web-based
annotations:
type: demo
EOF
# kubectl apply -f /tmp/namespace.yaml
namespace/kube-demo created
# kubectl get namespaces -o wide
NAME STATUS AGE
default Active 3h50m
ingress-nginx Active 3h6m
kube-demo Active 23s
kube-node-lease Active 3h50m
kube-public Active 3h50m
kube-system Active 3h50m
metallb-system Active 3h13m
# kubectl describe namespaces kube-demo
Name: kube-demo
Labels: apps=web-based
kubernetes.io/metadata.name=kube-demo
Annotations: type: demo
Status: Active
No resource quota.
No LimitRange resource.
2. namespace指派资源
cat > /tmp/resourceQuota.yaml << EOF
apiVersion: v1
kind: ResourceQuota
metadata:
name: mem-cpu-quota
namespace: kube-demo
spec:
hard:
requests.cpu: "4"
requests.memory: 8Gi
limits.cpu: "8"
limits.memory: 16Gi
EOF
# kubectl apply -f /tmp/resourceQuota.yaml
resourcequota/mem-cpu-quota created
# kubectl describe namespaces kube-demo
Name: kube-demo
Labels: apps=web-based
kubernetes.io/metadata.name=kube-demo
Annotations: type: demo
Status: Active
Resource Quotas
Name: mem-cpu-quota
Resource Used Hard
-------- --- ---
limits.cpu 0 8
limits.memory 0 16Gi
requests.cpu 0 4
requests.memory 0 8Gi
No LimitRange resource.
3. 建立deployment
cat > /tmp/deployment.yaml << EOF
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
app: nginx
namespace: kube-demo
annotations:
monitoring: "true"
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: 192.168.100.198:5000/nginx:1.21
name: nginx
tty: true
stdin: true
ports:
- containerPort: 80
resources:
limits:
memory: "2Gi"
cpu: "1000m"
requests:
memory: "1Gi"
cpu: "500m"
EOF
# kubectl apply -f /tmp/deployment.yaml
deployment.apps/nginx created
# kubectl describe namespaces kube-demo
Name: kube-demo
Labels: apps=web-based
kubernetes.io/metadata.name=kube-demo
Annotations: type: demo
Status: Active
Resource Quotas
Name: mem-cpu-quota
Resource Used Hard
-------- --- ---
limits.cpu 3 8
limits.memory 6Gi 16Gi
requests.cpu 1500m 4
requests.memory 3Gi 8Gi
No LimitRange resource.
# kubectl get deployments -n kube-demo -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
nginx 3/3 3 3 34s nginx 192.168.100.198:5000/nginx:1.21 app=nginx
# kubectl get pods -n kube-demo -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-796bbc89ff-8jvhw 1/1 Running 0 51s 10.240.2.144 vm-197 <none> <none>
nginx-796bbc89ff-cftfh 1/1 Running 0 51s 10.240.2.143 vm-197 <none> <none>
nginx-796bbc89ff-qk59q 1/1 Running 0 51s 10.240.36.12 vm-207 <none> <none>
# kubectl get deployments -n kube-demo -o yaml
https://kubernetes.io/docs/concepts/services-networking/connect-applications-service
4. 使用service提供服务
cat > /tmp/service.yaml << EOF
apiVersion: v1
kind: Service
metadata:
labels:
app: nginx
name: nginx
namespace: kube-demo
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: nginx
type: ClusterIP
EOF
# kubectl apply -f /tmp/service.yaml
service/nginx created
# kubectl get services -o wide -n kube-demo
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
nginx ClusterIP 10.97.122.87 <none> 80/TCP 9s app=nginx
# kubectl get services -n kube-demo -o yaml
type: NodePort
# kubectl apply -f /tmp/service.yaml
service/nginx configured
# kubectl get services -o wide -n kube-demo
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
nginx NodePort 10.97.122.87 <none> 80:31853/TCP 3h8m app=nginx
type: LoadBalancer
# kubectl apply -f /tmp/service.yaml
service/nginx configured
# kubectl get services -o wide -n kube-demo
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
nginx LoadBalancer 10.97.122.87 <pending> 80:31853/TCP 3h8m app=nginx
5. 测试
curl -l http://10.97.122.87