Talk About Kubernetes [1] - Run an Application

This article is to lead you to run an application with kubernetes and experience kubernetes .


Talk about kubernetes - run an application
kubernetes study notes, share them, hope to help some people in need, communicate with each other if you have any questions.
This article is to lead you to run an application with kubernetes and experience kubernetes .
1. Use the official website interactive environment to run an application
to the official website and enter the kubernetes interactive environment. https://kubernetes.io/docs/tutorials/kubernetes-basics/create-cluster/cluster-interactive/
Execute the following commands in sequence
# start kubernetes
$ minikube start
# View node information
$ kubectl get nodes
# View cluster information
$ kubectl cluster - info
# run an application
$ kubectl run kubernetes -bootcamp --image =docker.io/ jocatalin /kubernetes- bootcamp:v 1 --port =8080

The final output will be: deployment.apps " kubernetes -bootcamp" created, indicating that our application is running.
View Pods:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp- 56 cdd766d-zjx5j 1 / 1 Running 0 3 m
kubernetes-bootcamp-56cdd766d-zjx5 is the Pod where the application is located
By default, Pods can only be accessed inside the cluster, and external access to the application needs to be accessed through the port exposed by the service.
Expose the application as a service, making it accessible to the outside world:
$ kubectl expose deployment/ kubernetes -bootcamp -- type = " NodePort " --port 8080
Output: service " kubernetes -bootcamp" exposed
See the following services:
$ kubectl get services
Take a look at the results:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443 / TCP 12m
kubernetes -bootcamp NodePort 10.103.30.96 8080 : 30623 / TCP 40s
kubernetes is the default service, let's leave it alone, kubernetes -bootcamp is the service of our application. Port 30623 after 8080:30623 is randomly assigned.
2. Scale application
By default, there is only one copy of the application. Use the kubectl get deployments command to view the status of the copy:
$ kubectl get deployments
result:
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
kubernetes -bootcamp 1 1 1 1 11 m
We saw 1 copy.
Increase the copy with the following command:
$ kubectl scale deployments/ kubernetes -bootcamp --replicas =3
Output: deployment.extensions " kubernetes -bootcamp" scaled
Then, check out the current copy:
$ kubectl get deployments
result:
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
kubernetes -bootcamp 3
The final output will be: deployment.apps " kubernetes -bootcamp" created, indicating that our application is running.
View Pods:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp- 56 cdd766d-zjx5j 1 / 1 Running 0 3 m
kubernetes-bootcamp-56cdd766d-zjx5 is the Pod where the application is located
By default, Pods can only be accessed inside the cluster, and external access to the application needs to be accessed through the port exposed by the service.
Expose the application as a service, making it accessible to the outside world:
$ kubectl expose deployment/ kubernetes -bootcamp -- type = " NodePort " --port 8080
Output: service " kubernetes -bootcamp" exposed
See the following services:
$ kubectl get services
Take a look at the results:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 443 / TCP 12m
kubernetes -bootcamp NodePort 10.103.30.96 8080 : 30623 / TCP 40s
kubernetes is the default service, let's leave it alone, kubernetes -bootcamp is the service of our application. Port 30623 after 8080:30623 is randomly assigned.
2. Scale application
By default, there is only one copy of the application. Use the kubectl get deployments command to view the status of the copy:
$ kubectl get deployments
result:
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
kubernetes -bootcamp 1 1 1 1 11 m
We saw 1 copy.
Increase the copy with the following command:
$ kubectl scale deployments/ kubernetes -bootcamp --replicas =3
Output: deployment.extensions " kubernetes -bootcamp" scaled
Then, check out the current copy:
$ kubectl get deployments
result:
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
kubernetes -bootcamp 3 3 3 3 15 m
I saw 3 copies, and the expansion speed is very fast.
kubectl get pods command to view our pods
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-56cdd766d-f74cg 1/1 Running 0 1m
kubernetes-bootcamp-56cdd766d-zjx5j 1/1 Running 0 11m
kubernetes-bootcamp-56cdd766d-8sdjc 1/1 Running 0
1m
通过curl nodeName:port来访问应用:
$ curl minikube:30623
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-56cdd766d-zjx5j |v=1
$ curl minikube:30623
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-56cdd766d-f74cg |v=1
$ curl minikube:30623
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp-56cdd766d-zjx5j |v=1
$ curl minikube:30623
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp -56 cdd766d-f74cg |v= 1
$ curl minikube: 30623
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp -56 cdd766d -8 sdjc |v= 1
$
Looking at the above results, it is found that the application is automatically load balanced.
The minikube in minikube:30623 is our node name, obtained through kubectl get nodes .
Let's try reducing copies:
$ kubectl scale deployments/ kubernetes -bootcamp --replicas =2
Output: deployment.extensions " kubernetes -bootcamp" scaled
The same command, just changed the parameters of
--replicas , let's take a look at the current pods:
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp- 56 cdd766d-f74cg 1 / 1 Running 0 7 m
kubernetes-bootcamp- 56 cdd766d-zjx5j 1 / 1 Running 0 22 m
Found one missing application (pods) kubernetes-bootcamp-56cdd766d-8sdjc
3. App update
Application upgrade
In order to demonstrate the application update, we set the replica to 5 to see the application update process
$ kubectl scale deployments/kubernetes-bootcamp --replicas=5
deployment.extensions "kubernetes-bootcamp" scaled
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-56cdd766d-722v7 1/1 Running 0 12s
kubernetes-bootcamp-56cdd766d-f74cg 1/1 Running 0 12m
kubernetes-bootcamp-56cdd766d-vft4v 1/1 Running 0 12s
kubernetes-bootcamp-56cdd766d-z525n 1/1 Running 0 12s
kubernetes-bootcamp- 56 cdd766d-zjx5j 1 / 1 Running 0 26 m
At this point, 5 copies have been run, and the next step is to update the application. We used the mirror v2 version:
$ kubectl set image deployments/ kubernetes -bootcamp kubernetes -bootcamp=docker.io/ jocatalin /kubernetes- bootcamp:v 2
deployment.apps " kubernetes- bootcamp" image updated
$ kubectl get podsNAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-56cdd766d-722v7 1/1 Terminating 0 2m
kubernetes-bootcamp-56cdd766d-f74cg 1/1 Terminating 0 14m
kubernetes-bootcamp-56cdd766d-vft4v 1/1 Terminating 0 2m
kubernetes-bootcamp-56cdd766d-z525n 1/1 Terminating 0 2m
kubernetes-bootcamp-56cdd766d-zjx5j 1/1 Running 0 29m
kubernetes-bootcamp-5bddb78d8f-7b2jr 0/1 Pending 0 3s
kubernetes-bootcamp-5bddb78d8f-c75zp 1/1 Running 0 5s
kubernetes-bootcamp-5bddb78d8f-g5mc7 0/1 ContainerCreating 0 3s
kubernetes-bootcamp-5bddb78d8f-hlndn 1/1 Running 0 4s
kubernetes-bootcamp-5bddb78d8f-w6s4s 1/1 Running 0 5s
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-56cdd766d-722v7 1/1 Terminating 0 2m
kubernetes-bootcamp-56cdd766d-f74cg 1/1 Terminating 0 14m
kubernetes-bootcamp-56cdd766d-vft4v 1/1 Terminating 0 2m
kubernetes-bootcamp-56cdd766d-z525n 1/1 Terminating 0 2m
kubernetes-bootcamp-56cdd766d-zjx5j 1/1 Terminating 0 29m
kubernetes-bootcamp-5bddb78d8f-7b2jr 1/1 Running 0 12s
kubernetes-bootcamp-5bddb78d8f-c75zp 1/1 Running 0 14s
kubernetes-bootcamp-5bddb78d8f-g5mc7 1/1 Running 0 12s
kubernetes-bootcamp-5bddb78d8f-hlndn 1/1 Running 0 13s
kubernetes-bootcamp-5bddb78d8f-w6s4s 1/1 Running 0 14s
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-56cdd766d-722v7 1/1 Terminating 0 2m
kubernetes-bootcamp-56cdd766d-f74cg 1/1 Terminating 0 14m
kubernetes-bootcamp-56cdd766d-vft4v 1/1 Terminating 0 2m
kubernetes-bootcamp-56cdd766d-z525n 1/1 Terminating 0 2m
kubernetes-bootcamp-56cdd766d-zjx5j 1/1 Terminating 0 29m
kubernetes-bootcamp-5bddb78d8f-7b2jr 1/1 Running 0 19s
kubernetes-bootcamp-5bddb78d8f-c75zp 1/1 Running 0 21s
kubernetes-bootcamp-5bddb78d8f-g5mc7 1/1 Running 0 19s
kubernetes-bootcamp-5bddb78d8f-hlndn 1/1 Running 0 20s
kubernetes-bootcamp-5bddb78d8f-w6s4s 1/1 Running 0 21s
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-56cdd766d-zjx5j 1/1 Terminating 0 29m
kubernetes-bootcamp-5bddb78d8f-7b2jr 1/1 Running 0 35s
kubernetes-bootcamp-5bddb78d8f-c75zp 1/1 Running 0 37s
kubernetes-bootcamp-5bddb78d8f-g5mc7 1/1 Running 0 35s
kubernetes-bootcamp-5bddb78d8f-hlndn 1/1 Running 0 36s
kubernetes-bootcamp -5 bddb78d8f-w6s4s 1 / 1 Running 0 37s _
$
we quickly use kubectl get pods looks at the pods and finds that it first creates 5 new pods and then gradually deletes the old pods
app downgrade
Downgrading an application is as simple as using the undo command:
$ kubectl rollout undo deployments/ kubernetes -bootcamp
deployment.apps " kubernetes -bootcamp"
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-56cdd766d-2j2tp 1/1 Running 0 4s
kubernetes-bootcamp-56cdd766d-gt45v 0/1 Pending 0 2s
kubernetes-bootcamp-56cdd766d-x6wxb 1/1 Running 0 3s
kubernetes-bootcamp-56cdd766d-xwbc6 0/1 ContainerCreating 0 2s
kubernetes-bootcamp-5bddb78d8f-7b2jr 1/1 Terminating 0 4m
kubernetes-bootcamp-5bddb78d8f-c75zp 1/1 Running 0 4m
kubernetes-bootcamp-5bddb78d8f-g5mc7 1/1 Terminating 0 4m
kubernetes-bootcamp-5bddb78d8f-hlndn 1/1 Terminating 0 4m
kubernetes-bootcamp-5bddb78d8f-w6s4s 1/1 Running 0 4m
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
kubernetes-bootcamp-56cdd766d-2j2tp 1/1 Running 0 13s
kubernetes-bootcamp-56cdd766d-gt45v 1/1 Running 0 11s
kubernetes-bootcamp-56cdd766d-kfxcj 1/1 Running 0 8s
kubernetes-bootcamp-56cdd766d-x6wxb 1/1 Running 0 12s
kubernetes-bootcamp-56cdd766d-xwbc6 1/1 Running 0 11s
kubernetes-bootcamp-5bddb78d8f-7b2jr 1/1 Terminating 0 4m
kubernetes-bootcamp-5bddb78d8f-c75zp 1/1 Terminating 0 4m
kubernetes-bootcamp-5bddb78d8f-g5mc7 1/1 Terminating 0 4m
kubernetes-bootcamp-5bddb78d8f-hlndn 1/1 Terminating 0 4m
kubernetes-bootcamp-5bddb78d8f-w6s4s 1/1 Terminating 0 4m
$
Looking at the output, the same process as the application upgrade, first create 5 pods, and then gradually delete the old pods.
At this time, you can use curl to verify the version
$ curl minikube: 30623
Hello Kubernetes bootcamp! | Running on: kubernetes-bootcamp -56 cdd766d-xwbc6 |v= 1
When you see the last v=1, the version has been rolled back.
4. Summary
Kubernetes looks a bit complicated, but think about what she does with simple commands, such as: application deployment, application update, do you still think she is complicated ? I just feel: strong!