The original article

Deployed nginx Deployment

If you have finished building Kubernetes, I will join me in deploying the first application. Install Kubernetes 1.21 using kubeadm if Kubernetes 1.21 is not installed

Create a YAML file

Create file nginx-deploy.yaml with the following contents:

apiVersion: apps/v1	Kubectl API -versions can be used to view the versions supported by the current cluster
kind: Deployment	For this configuration type, we are using Deployment
metadata:	        Metadata is some basic attributes and information about Deployment
  name: nginx-deployment	# the name of the Deployment
  labels:	    # tag, which can be flexibly located one or more resources, including key and value can be customized, can define multiple groups, do not need to understand
    app: nginx	# set the key to app and value to nginx tag for this Deployment
spec:	        How do you expect this Deployment to be used in K8S
  replicas: 1	Create an application instance using this Deployment
  selector:	    # tag selector, which works with the tags above, is not currently understood
    matchLabels: # Select the resource that contains the tag app:nginx
      app: nginx
  template:	    This is the template for the Pod selected or created
    metadata:	#Pod metadata
      labels:	#Pod tag, the selector above selects the Pod containing the tag app:nginx
        app: nginx
    spec:	    # Expected Pod functionality (that is, deployed in Pod)
      containers:	Create a container, the same type of container as docker
      - name: nginx	# the name of the container
        image: Nginx: 1.7.9	Nginx :1.7.9 Creating a Container, which is accessible on port 80 by default
Copy the code

Apply YAML files

kubectl apply -f nginx-deploy.yaml
Copy the code

Viewing deployment Results

#Check the Deployment
kubectl get deployments

#Check the Pod
kubectl get pods
Copy the code

As shown above, you can see a Deployment named nginx-Deployment and a Pod named nginx-deployment-XXXXxxx respectively

Kubectl common command

kubectl get

Display resource list

#Kubectl get Resource type

#Gets the list of resources of type Deployment
kubectl get deployments

#Gets a list of resources of type Pod
kubectl get pods

#Gets a list of resources of type Node
kubectl get nodes
Copy the code

The namespace

Add -a or –all-namespaces to the command to view objects in all namespaces. Run -n to view objects in the specified namespace

#Look at Deployment for all namespaces
kubectl get deployments -A
kubectl get deployments --all-namespaces
#Look at Deployment for the kube-system namespace
kubectl get deployments -n kube-system
Copy the code

kubectl describe

Displays detailed information about the resource

#Kubectl describe Resource type Resource name

#View information about the Pod named nginx-xxxxxx
kubectl describe pod nginx-XXXXXX	

#View the information for the Deployment named nginx
kubectl describe deployment nginx	
Copy the code

kubectl logs

View the print logs for containers in the POD

#Kubectl logs Pod name

#View the logs printed by the container inside the pod named nginx-pod-XXXXxxx
#In this case, nginx-Pod does not output logs, so you see empty results
kubectl logs -f nginx-pod-XXXXXXX
Copy the code

kubectl exec

Execute commands within the container environment in pod

# kubectl execPod Name Operation command

#Run bash in pod named nginx-pod-xxxxxx
kubectl exec -it nginx-pod-xxxxxx /bin/bash
Copy the code

Create the Service for Nginx Deployment

Create file nginx-service.yaml

apiVersion: v1
kind: Service
metadata:	Metadata is some basic attributes and information about Deployment
  name: nginx-service	# the name of the Service
  labels:	# tag, can flexibly locate one or more resources, including key and value can be customized, can define multiple groups
    app: nginx	# set the key to app and value to nginx tag for this Deployment
spec:	    This is the definition of the Service. It describes how the Service selects a Pod and how it is accessed
  selector:	    # tag selector
    app: nginx	# Select Pod containing the tag app:nginx
  ports:
  - name: nginx-port	The name of the port
    protocol: TCP	    # Protocol type TCP/UDP
    port: 80	        Other container groups in the cluster can access the Service through port 80
    nodePort: 30080   Access Service through port 30080 on any node
    targetPort: 80	Forward the request to port 80 that matches the Pod
  type: NodePort	# Serive type, ClusterIP/NodePort/LoaderBalancer
Copy the code

Execute the command

kubectl apply -f nginx-service.yaml
Copy the code

Checking the Execution Result

kubectl get services -o wide
Copy the code

As shown in the preceding figure, you can view the nginx-service service.

Access the service

Curl < IP address of any node >:30080Copy the code

Scaling application

Scaling can be done by changing replicas (the number of copies) deployed in the nginx-deployment.yaml file

spec:
  replicas: 2    Create two application instances using this Deployment
Copy the code

Modify the nginx-deploy.yaml file

apiVersion: apps/v1     Kubectl API -versions can be used to view the versions supported by the current cluster
kind: Deployment        For this configuration type, we are using Deployment
metadata:               Metadata is some basic attributes and information about Deployment
  name: nginx-deployment        # the name of the Deployment
  labels:           # tag, which can be flexibly located one or more resources, including key and value can be customized, can define multiple groups, do not need to understand
    app: nginx  # set the key to app and value to nginx tag for this Deployment
spec:           How do you expect this Deployment to be used in K8S
  replicas: 2   Create an application instance using this Deployment
  selector:         # tag selector, which works with the tags above, is not currently understood
    matchLabels: # Select the resource that contains the tag app:nginx
      app: nginx
  template:         This is the template for the Pod selected or created
    metadata:   #Pod metadata
      labels:   #Pod tag, the selector above selects the Pod containing the tag app:nginx
        app: nginx
    spec:           # Expected Pod functionality (that is, deployed in Pod)
      containers:       Create a container, the same type of container as docker
      - name: nginx     # the name of the container
        image: Nginx: 1.7.9      Nginx :1.7.9 Creating a Container, which is accessible on port 80 by default
Copy the code

Execute the command

kubectl apply -f nginx-deployment.yaml
Copy the code

View the results

watch kubectl get pods -o wide
Copy the code

As shown in the figure above, you will see that there are two applications running, running multiple application instances that can perform rolling updates without downtime.

Scroll to update

Rolling updates allow the following operations:

  • Upgrade the application from the ready to go live environment to production (by updating the container image)
  • Roll back to a previous version
  • Continuous integration and continuous delivery of applications without downtime

To scroll the update, run the following command

kubectl apply -f nginx-deployment.yaml
Copy the code

Viewing the process and result Run the command to view the pod replacement process one by one.

watch kubectl get pods -l app=nginx
Copy the code

The original article