Pod和Controller关系:
Deployment应用场景:
kubectl create deployment web --image=nginx --dry-run -o yaml > web.yaml
W0323 16:06:22.869058 58281 helpers.go:598] --dry-run is deprecated and can be replaced with --dry-run=client.
cat web.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: web
name: web
spec:
replicas: 1
selector:
matchLabels:
app: web
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: web
spec:
containers:
- image: nginx
name: nginx
resources: {}
status: {}
kubectl apply -f web.yaml
kubectl scale deployment web --replicas=3
也可以在创建deployment时指明副本数量
kubectl create deployment web --image=nginx --replicas=3 --dry-run -o yaml > web.yaml
W0323 17:24:22.044115 103293 helpers.go:598] --dry-run is deprecated and can be replaced with --dry-run=client.
查看创建的deployment的yaml
cat web.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: web
name: web
spec:
replicas: 3
selector:
matchLabels:
app: web
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: web
spec:
containers:
- image: nginx
name: nginx
resources: {}
status: {}
kubectl expose deployment web --port=80 --target-port=80 --type=NodePort --dry-run -o yaml > service.yaml
cat service.yaml
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
labels:
app: web
name: web
spec:
ports:
- port: 80
protocol: TCP
targetPort: 80
selector:
app: web
type: NodePort
status:
loadBalancer: {}
kubectl apply -f service.yaml
查看pod、svc
kubectl get pods,svc
NAME READY STATUS RESTARTS AGE
pod/nginx-85b98978db-2fmpx 1/1 Running 0 5h18m
pod/web-76b56fd968-c7nnr 1/1 Running 0 53m
pod/web-76b56fd968-r8gdd 1/1 Running 0 53m
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 6h54m
service/nginx NodePort 10.99.10.254 <none> 80:30407/TCP 5h35m
service/web NodePort 10.103.247.181 <none> 80:31870/TCP 9s
kubectl set image deployment web nginx=nginx:1.15
kubectl rollout undo deployment web
kubectl rollout status deployment web
kubectl rollout history deployment web
kubectl rollout undo deployment web --to-revision=2
kubectl scale deployment web --replicas=3