preface

Here are some things you can learn from reading this article:

  • How do I deploy native applications to Kubernetes

preparation

To read this article, the following conditions must be met

  • Docker Registry setup
  • How to build K8S from scratch
  • The application is packaged as a mirror

Application deployment

The application is packaged as a mirror

Can you learn how to package an application as a mirror

Push to a private repository

Once the image is successfully packaged, we need to push the image to the private repository

Log in to the private repository
[root@k8s-master k8s]$docker login 10.8.135.104:5000
View the list of local mirrors
[root@k8s-master k8s]$ docker images
REPOSITORY TAG IMAGE ID CREATED SIZE K8s-owater/Kubedocker 0.0.1-SNAPSHOT 5920a6f4995d About an hour ago 663MB # tagging [root@k8s-master k8s]$Docker tag 5920a6f4995d 10.8.135.104:5000/ k8S-owater /kubedocker:0.0.1-SNAPSHOT # push to private repository [root@k8s-master k8S]$Docker Push 10.8.135.104:5000/ k8S-owater/Kubedocker :0.0.1-SNAPSHOTCopy the code
  • Docker tag [IMAGE ID] [private hub domain]/[project name]/[IMAGE name]:[IMAGE version]
  • Docker Push [private hub domain]/[project name]/[image name]:[image version]
View the private mirror repository

Because the blogger’s warehouse has authentication, so you need a browser login to view

View the private mirror repository

Kubernetes connects to a private repository

Create key
[root@k8s-master ~]$ kubectl create secret docker-registry docker-registry-secret --docker-server=IP:5000 --docker-username=[username] --docker-password=[password]Copy the code
  • Docker-registry -secret: specifies the key name of the key
  • Docker-server: specifies the docker repository address
  • Docker-username: specifies the docker repository username
  • Docker-password: specifies the docker warehouse login password

Kubectl get secrets to see the keys

Look at the key

Start the deployment

Create k8S-owater-pod. yaml as follows

apiVersion: v1
kind: Pod
metadata:
  name: k8s-test-pod
  labels:
 app: k8s-test-pod spec:  containers:  - name: k8s-test-pod Image: 10.8.135.104:5000 / k8s - owater/kubedocker: 0.0.1 - the SNAPSHOT imagePullPolicy: Always  restartPolicy: Always  imagePullSecrets:  - name: docker-registry-secret # here is the secret created above to access the private library Copy the code

Run the command to deploy the vm

[root@k8s-master ~]$ kubectl apply -f k8s-owater-pod.yaml
Copy the code

Logging in to the Dashboard

Logging in to the Dashboard

To access the application

To this, we have successfully to deploy applications to the Kubernetes, at this time we went to visit the curl http://10.8.135.104:8080/api/user

To bind port 8080 on the host to port 8080 on the POD, run the following command

# Bind port
[root@k8s-master ~]$kubectl port-forward --address 0.0.0.0 k8s-owater 8080:8080Copy the code

At this point, you may find that when you exit, you will not be able to access the application. Yes, this method is not suitable for production environment, only suitable for development and debugging. In the next chapter, I will explain how to expose interface access

Cluster deployment mode

We have successfully deployed single-machine mode, but in the production environment, we basically need to do cluster mode. In this case, we need to deploy in Deployment mode. The configuration file is as follows

apiVersion: apps/v1
kind: Deployment
metadata:
  name: k8s-owater
  labels:
 app: k8s-owater spec:  replicas: 3 # number of copies  template:  metadata:  name: k8s-owater  labels:  app: k8s-owater  env: test  spec:  containers:  - name: k8s-owater Image: 10.8.135.104:5000 / k8s - owater/kubedocker: 0.0.1 - the SNAPSHOT imagePullPolicy: Always  ports:  - name: http-port  containerPort: 8080  imagePullSecrets:  - name: docker-registry-secret  restartPolicy: Always  selector:  matchLabels:  app: k8s-owater Copy the code

Run the following command to deploy

[root@k8s-master ~]$ kubectl apply -f k8s-owater-deployment.yaml
Copy the code
Cluster deployment mode

K8s-owater = k8S-Owater = k8S-Owater = K8S-Owater = K8S-Owater = K8S-Owater = K8S-Owater

View the current pod
[root@k8s-master ~]$ kubectl get pod
NAME                          READY   STATUS    RESTARTS   AGE
k8s-owater-6b689cc98c-gvxgx   1/1     Running   0          13m
k8s-owater-6b689cc98c-wbpv5   1/1     Running   0          11m
k8s-owater-6b689cc98c-x8n5v 1/1 Running 0 13m Copy the code

Dynamic sizing

Kubernetes is very powerful in clustering, we can dynamically adjust the cluster size, you just need to adjust, it will automatically create new pods for you. And when a pod dies, it will automatically create a new pod for you, you can delete the pod experience

Dynamic sizing
Dynamic sizing