spec
section describes the disired state, along with some basic information, such as
the object's name.apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15.11
ports:
- containerPort: 80
apiVersion
field:
kind
:
metadata
:
spec
(the first level, not the spec.template.spec)
spec.template
.metadata
and spec
and loses the apiVersion
and kind
- both being
replaced by tempalte
.spec.template.spec
we define the desired state of the Pod.status
field to
the object.apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.15.11
ports:
- containerPort: 80
apiVersion
field must specify v1 for the Pod
object definition.apiVersion: v1
kind: ReplicationController
metadata:
name: nginx
spec:
replicas: 3
selector:
app: nginx
template:
metadata:
name: nginx
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: frontend
labels:
app: guestbook
tier: frontend
spec:
# modify replicas according to your case
replicas: 3
selector:
matchLabels:
tier: frontend
template:
metadata:
labels:
tier: frontend
spec:
containers:
- name: php-redis
image: gcr.io/google_samples/gb-frontend:v3
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
$ kubectl get namespace
NAME STATUS AGE
default Active 2d
kube-node-lease Active 2d
kube-public Active 2d
kube-system Active 2d
create deployment.
$ kubectl create deployment mynginx --image=nginx:1.15-alpine
error: no matches for kind "Deployment" in version "extensions/v1beta1"
if we see that, chances are mismatch version as it says about version.
Cehck kubectl version.
$ kubectl version
Client Version: version.Info{Major:"1", Minor:"10", GitVersion:"v1.10.1", GitCommit:"d4ab47518836c750f9949b9e0d387f20fb92260b", GitTreeState:"clean", BuildDate:"2018-04-12T14:26:04Z", GoVersion:"go1.9.3", Compiler:"gc", Platform:"windows/amd64"}
Server Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.2", GitCommit:"59603c6e503c87169aea6106f57b9f242f64df89", GitTreeState:"clean", BuildDate:"2020-01-18T23:22:30Z", GoVersion:"go1.13.5", Compiler:"gc", Platform:"linux/amd64"}
Tha it is, Client is versioned 1.10.1, while server is 1.17.2
Download kubectl
$ curl -LO https://storage.googleapis.com/kubernetes-release/release/v1.17.0/bin/windows/amd64/kubectl.exe
$ which kubectl
/cygdrive/c/ProgramData/chocolatey/bin/kubectl
$ mv kubectl.exe "c:\ProgramData\chocolatey\bin"
$ kubectl version --client
Client Version: version.Info{Major:"1", Minor:"17", GitVersion:"v1.17.0", GitCommit:"70132b0f130acc0bed193d9ba59dd186f0e634cf", GitTreeState:"clean", BuildDate:"2019-12-07T21:20:10Z", GoVersion:"go1.13.4", Compiler:"gc", Platform:"windows/amd64"}
Create deployment one more time.
$ kubectl create deployment mynginx --image=nginx:1.15-alpine
deployment.apps/mynginx created
To get deployment, replicaSet, Pod we can use
$ kubectl get deploy,rs,po
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/mynginx 1/1 1 1 5m58s
NAME DESIRED CURRENT READY AGE
replicaset.apps/mynginx-7d5785cdbd 1 1 1 5m58s
NAME READY STATUS RESTARTS AGE
pod/mynginx-7d5785cdbd-bg8nm 1/1 Running 0 5m58s
to filter it with label
$ kubectl get deploy,rs,po -l app=mynginx
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/mynginx 1/1 1 1 6m11s
NAME DESIRED CURRENT READY AGE
replicaset.apps/mynginx-7d5785cdbd 1 1 1 6m11s
NAME READY STATUS RESTARTS AGE
pod/mynginx-7d5785cdbd-bg8nm 1/1 Running 0 6m11s
Now let's scale the previous deployment's replica to 3
$ kubectl scale --replicas=3 deploy/mynginx
deployment.apps/mynginx scaled
Verified the result
$ kubectl get deploy,rs,po -l app=mynginx
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/mynginx 3/3 3 3 11m
NAME DESIRED CURRENT READY AGE
replicaset.apps/mynginx-7d5785cdbd 3 3 3 11m
NAME READY STATUS RESTARTS AGE
pod/mynginx-7d5785cdbd-9tsrv 1/1 Running 0 47s
pod/mynginx-7d5785cdbd-bg8nm 1/1 Running 0 11m
pod/mynginx-7d5785cdbd-ms2xx 1/1 Running 0 47s
To describe deployment.
$ kubectl describe deploy mynginx
Name: mynginx
Namespace: default
CreationTimestamp: Mon, 24 Feb 2020 11:43:00 +0700
Labels: app=mynginx
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=mynginx
Replicas: 3 desired | 3 updated | 3 total | 3 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=mynginx
Containers:
nginx:
Image: nginx:1.15-alpine
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Progressing True NewReplicaSetAvailable
Available True MinimumReplicasAvailable
OldReplicaSets: <none>
NewReplicaSet: mynginx-7d5785cdbd (3/3 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 14m deployment-controller Scaled up replica set mynginx-7d5785cdbd to 1
Normal ScalingReplicaSet 3m34s deployment-controller Scaled up replica set mynginx-7d5785cdbd to 3
Check rollout history
$ kubectl rollout history deploy mynginx
deployment.apps/mynginx
REVISION CHANGE-CAUSE
1 <none>
Revision 1 is associated to nginx:1.15-alpine and replicaSet of
replicaset.apps/mynginx-7d5785cdbd
To get more details on revision 1
$ kubectl rollout history deploy mynginx --revision=1
deployment.apps/mynginx with revision #1
Pod Template:
Labels: app=mynginx
pod-template-hash=7d5785cdbd
Containers:
nginx:
Image: nginx:1.15-alpine
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
but no revision 2
$ kubectl rollout history deploy mynginx --revision=2
error: unable to find the specified revision
Now let's update nginx image version. (Update can be downgrade or upgrade)
$ kubectl set image deployment mynginx nginx=nginx:1.16-alpine
deployment.apps/mynginx image updated
Check history
$ kubectl rollout history deployment mynginx
deployment.apps/mynginx
REVISION CHANGE-CAUSE
1 <none>
2 <none>
Check revision 1 first
$ kubectl rollout history deployment mynginx --revision=1
deployment.apps/mynginx with revision #1
Pod Template:
Labels: app=mynginx
pod-template-hash=7d5785cdbd
Containers:
nginx:
Image: nginx:1.15-alpine
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Check revision 2
$ kubectl rollout history deployment mynginx --revision=2
deployment.apps/mynginx with revision #2
Pod Template:
Labels: app=mynginx
pod-template-hash=5dfd47c855
Containers:
nginx:
Image: nginx:1.16-alpine
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Check k8s objects of our app
$ kubectl get deployment,rs,pod -l app=mynginx
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/mynginx 3/3 3 3 29m
NAME DESIRED CURRENT READY AGE
replicaset.apps/mynginx-5dfd47c855 3 3 3 4m47s
replicaset.apps/mynginx-7d5785cdbd 0 0 0 29m
NAME READY STATUS RESTARTS AGE
pod/mynginx-5dfd47c855-n5ckf 1/1 Running 0 4m36s
pod/mynginx-5dfd47c855-nqz6r 1/1 Running 0 4m47s
pod/mynginx-5dfd47c855-vx9kl 1/1 Running 0 4m37s
Let's rollback to revision 1
$ kubectl rollout undo deployment mynginx --to-revision=1
deployment.apps/mynginx rolled back
Let's check revision history
$ kubectl rollout history deployment mynginx
deployment.apps/mynginx
REVISION CHANGE-CAUSE
2 <none>
3 <none>
$ kubectl rollout history deployment mynginx --revision=3
deployment.apps/mynginx with revision #3
Pod Template:
Labels: app=mynginx
pod-template-hash=7d5785cdbd
Containers:
nginx:
Image: nginx:1.15-alpine
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
$ kubectl rollout history deployment mynginx --revision=1
error: unable to find the specified revision
$ kubectl rollout history deployment mynginx --revision=2
deployment.apps/mynginx with revision #2
Pod Template:
Labels: app=mynginx
pod-template-hash=5dfd47c855
Containers:
nginx:
Image: nginx:1.16-alpine
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Check deployment object.
$ kubectl get deployment,rs,pod -l app=mynginx
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/mynginx 3/3 3 3 42m
NAME DESIRED CURRENT READY AGE
replicaset.apps/mynginx-5dfd47c855 0 0 0 18m
replicaset.apps/mynginx-7d5785cdbd 3 3 3 42m
NAME READY STATUS RESTARTS AGE
pod/mynginx-7d5785cdbd-8625s 1/1 Running 0 6m58s
pod/mynginx-7d5785cdbd-fc69n 1/1 Running 0 7m
pod/mynginx-7d5785cdbd-fcb55 1/1 Running 0 6m55s
$ kubectl rollout --help
Manage the rollout of a resource.
Valid resource types include:
* deployments
* daemonsets
* statefulsets
Examples:
# Rollback to the previous deployment
kubectl rollout undo deployment/abc
# Check the rollout status of a daemonset
kubectl rollout status daemonset/foo
Available Commands:
history View rollout history
pause Mark the provided resource as paused
restart Restart a resource
resume Resume a paused resource
status Show the status of the rollout
undo Undo a previous rollout
Usage:
kubectl rollout SUBCOMMAND [options]
Use "kubectl <command> --help" for more information about a given command.
Use "kubectl options" for a list of global command-line options (applies to all commands).