1. Create a custom namespace dev:
kubectl create ns dev
2. Deploy a deployment through the yaml file in the dev namespace. The deployment content is as follows:
a. Use nginx mirror;
b. The number of copies is 3;
c. The upgrade strategy is rolling upgrade;
d. Configure readiness probe and survival probe;
e. Set the data persistence method to HostPath;
nginxpod.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: pc-deployment
namespace: dev
labels:
app: nginx
spec:
replicas: 3
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.19.1
imagePullPolicy: IfNotPresent
ports:
- name: http
protocol: TCP
containerPort: 80
livenessProbe:
tcpSocket:
port: 80
initialDelaySeconds: 45
periodSeconds: 15
readinessProbe:
failureThreshold: 3
tcpSocket:
port: 80
initialDelaySeconds: 20
periodSeconds: 3
successThreshold: 1
timeoutSeconds: 2
resources:
limits:
cpu: "1.0"
memory: 512Mi
requests:
cpu: "0.5"
memory: 128Mi
volumeMounts:
- mountPath: /usr/share/nginx/html
name: html
volumes:
- name: html
hostPath:
path: /html
type: DirectoryOrCreate
kubectl create -f nginxpod.yaml
kubectl get pod -A
f. Modify nginx home page;
3. Access the k8s homepage through external access;
kubectl expose deploy pc-deployment --name=svc-nginx --type=NodePort --port=80 --target-port=80 -n dev
kubectl get svc svc-nginx -n dev -o wide
4. Expand and shrink the created deployment, restart it, change the image version, and roll back the version;
Expansion:
kubectl scale deploy pc-deployment --replicas=5 -n dev
Shrink:
kubectl edit deploy pc-deployment -n dev
kubectl get deploy pc-deployment -n dev
Restart:
kubectl rollout restart deployment pc-deployment -n dev
Change image version:
kubectl set image deployment pc-deployment nginx=nginx:1.17.2 -n dev
Version rollback:
kubectl rollout undo deployment pc-deployment --to-revision=1 -n dev