Kubernetes tutorial application deployment

The article addresses: blog.piaoruiqing.com/2019/09/28/…

preface

This article will work with readers to learn how to deploy applications on Kubernetes. This includes: how to deploy the application, how to distribute the file, how to package the local project, and so on.

Read this article and you will learn:

  • Learn how to deploy applications in K8S
  • How to package Docker images and upload them to a private library

To read this article you need:

  • Familiar with Linux commands.
  • Have a Kubernetes test environment (see “Build K8S from Scratch” to deploy a Kubernetes test environment locally)

Understanding description files

First, we understand the description file by deploying Nginx in Kubernetes.

In general, Kubernetes uses YAML (or JSON) to describe the publishing configuration. Here is a simple description file: nginx-pod.yaml

apiVersion: v1      Describe the version of the KubernetesAPI that the file follows
kind: Pod           The type described is POD
metadata:
  name: nginx-pod   # the name of pod
  labels:           # label
    app: nginx-pod
    env: test
spec:
  containers:
    - name: nginx-pod     # container name
      image: Nginx: 1.15   Image name and version
      imagePullPolicy: IfNotPresent   # if local does not exist, go to remote repository
      ports:
        - containerPort: 80   # POD external port
  restartPolicy: Always
Copy the code

We log in to the Master node and use the kubectl command to deploy the application described in this file.

Kubectl get Pods command kubectl get Pods command kubectl get Pods command kubectl get Pods command At this point nginx has been released. We can see the application status visually from dashboard.

Note: You can run the kubectl delete -f nginx-pod.yaml command to delete pods, or directly operate in dashboard.

[Copyright Notice]


This article was published on
Park Seo-kyung’s blog, allow non-commercial reprint, but reprint must retain the original author
PiaoRuiQingAnd links:
blog.piaoruiqing.comFor negotiation or cooperation on authorization, please contact:
[email protected].

How to access the service

In the previous section we deployed an Nginx pod, but we could not access the Nginx.

The easiest way to access services in pod is through port forwarding. Bind host port 9999 to nginx-pod port 80 by executing the following command:

[root@nas-centos1 ~]$kubectl port-forward --address 0.0.0.0 nginx-pod 9999:80 Forwarding from 0.0.0.0:9999 -> 80 Handling connectionfor 9999
Copy the code

At this point, we can access Nginx by accessing port 9999 of the host machine.

Deploying local projects

Publishing a locally developed project to Kubernetes requires packaging the project as a Docker image and pushing the image to the repository (public or private).

First, we need a local project that can run. I built a simple Web project using Spring-Boot:

@RestController
@RequestMapping(value = "/k8s-test")
@SpringBootApplication
public class K8sTestApplication {

    @GetMapping(value = "/timestamp")
    publicResponseEntity<? > getTimestamp() {return ResponseEntity.ok(System.currentTimeMillis() + "\n");
    }

    public static void main(String[] args) { SpringApplication.run(K8sTestApplication.class, args); }}Copy the code

Package the Docker image

Reference: Dockerfile Reference

With the project, we need to package it into a Docker image, the Dockerfile content is as follows:

FROM java:8-alpine
COPY. / k8s - test - 0.0.1 - the SNAPSHOT. Jar/usr/app /WORKDIR /usr/app
ENTRYPOINT ["java"."-jar"."K8s - test - 0.0.1 - the SNAPSHOT. Jar"]
Copy the code
  • FROM java:8-alpine: The mirror is based onjava-8-alpineThe mirror.
  • COPY. / target/k8s - test - 0.0.1 - the SNAPSHOT. Jar/usr/app /: copies the compiled packaged JAR to the mirrored/usr/appDirectory.
  • WORKDIR /usr/app: Working directory is specified as/usr/app.
  • ENTRYPOINT [" Java ", "- the jar", "k8s - test - 0.0.1 - the SNAPSHOT. Jar"]: executed when docker is startedJava jar k8s - test - 0.0.1 - the SNAPSHOT. The jarThe command

Go to the Dockerfile directory and run the docker build -t piaoruiqing/k8s-test command. Be careful not to omit the. At the end of the command, which represents the current directory.

[root@nas-centos1 k8s-test]$ docker build -t piaoruiqing/k8s-test .
Copy the code

A list of local images can be viewed using the docker images command:

[root@nas-centos1 k8s-test]$ docker images | grep k8s
piaoruiqing/k8s-test     latest         efe9e9625376        4 minutes ago       174MB
Copy the code

Push to remote repository

After the docker image is packaged, it needs to be pushed to the image warehouse for use by each Kubernetes node. This paper takes Ali Cloud’s container mirroring service as an example.

First, log in to ali Cloud Console -> Container Image service and create a namespace:

# Login: The user name used for login is the full name of Ali Cloud account, and the password is set when the service is opened.[root@nas-centos1 k8s-test]$docker login --username=[username] registry.cn-hangzhou.aliyuncs.comThe image ID can be viewed using the Docker images command[root @ nas - centos1 k8s - test] $docker tag efe9e9625376 registry.cn-hangzhou.aliyuncs.com/piaoruiqing/k8s-test:0.0.1# Push to Ali Cloud mirror warehouse[root @ nas - centos1 k8s - test] $docker push registry.cn-hangzhou.aliyuncs.com/piaoruiqing/k8s-test:0.0.1Copy the code

The deployment of

The private library support

Because the author uses a private library, Kubernetes cannot access it directly, so we need to create Secret to support access to the private library before publishing.

kubectl create secret docker-registry docker-registry-secret --docker-server=registry.cn-hangzhou.aliyuncs.com --docker-username=[username] --docker-password=[password] --docker-email=[email]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 user name.
  • docker-password: Specifies the Docker warehouse login password.
  • docker-email: Specify an email address (optional).

Kubectl get secrets

[root@nas-centos1 k8s-test]$ kubectl get secrets
NAME                     TYPE                                  DATA   AGE
default-token-s7bps      kubernetes.io/service-account-token   3      13d
docker-registry-secret   kubernetes.io/dockerconfigjson        1      2m50s
Copy the code

Start the deployment

To access a private library, specify secret in the description file as follows:

apiVersion: v1
kind: Pod
metadata:
  name: k8s-test-pod
  labels:
    app: k8s-test-pod
spec:
  containers:
    - name: k8s-test-pod
      image: registry.cn-hangzhou.aliyuncs.com/piaoruiqing/k8s-test:0.0.1
      imagePullPolicy: IfNotPresent
  restartPolicy: Always
  imagePullSecrets:
    - name: docker-registry-secret    # here is the secret created above to access the private library
Copy the code

Run the kubectl apply-f k8s-test-pod.yaml command to deploy it. After the deployment is complete, you can view it on the dashboard.

Use kubectl port-forward –address 0.0.0.0 k8S-test-pod 80:8080 to bind the master port 80 to the POD port 8080. So we can go to http://10.33.30.95/k8s-test/timestamp.

[root@nas-centos2 ~]$curl 10.33.30.95/k8s-test/timestamp 1569512136028Copy the code

The deployment of the cluster

Now that we have successfully deployed an application instance on Kubernetes, we can use Deployment to describe our application in most cases in a production environment where cluster Deployment is required:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: k8s-test
  labels:
    app: k8s-test
spec:
  replicas: 3		# number of copies
  template:
    metadata:
      name: k8s-test
      labels:
        app: k8s-test
        env: test
    spec:
      containers:
        - name: k8s-test
          image: registry.cn-hangzhou.aliyuncs.com/piaoruiqing/k8s-test:0.0.1
          imagePullPolicy: IfNotPresent
          ports:
            - name: http-port
              containerPort: 8080
      imagePullSecrets:
        - name: docker-registry-secret
      restartPolicy: Always
  selector:
    matchLabels:
      app: k8s-test
Copy the code

Similarly, use kubectl apply -f to publish the configuration.

[root@nas-centos1 k8s-test]$ kubectl apply -f k8s-test.yaml 
deployment.apps/k8s-test created
Copy the code

If this article is helpful to you, please give a thumbs up (~ ▽ ~)”

reference

  • kubernetes.io

series

  • Build K8S from scratch with official documentation
  • Kubernetes(2) Application deployment
  • How do I access the service from outside

Welcome to pay attention to the public account (code such as poetry):

[Copyright Notice]


This article was published on
Park Seo-kyung’s blog, allow non-commercial reprint, but reprint must retain the original author
PiaoRuiQingAnd links:
blog.piaoruiqing.comFor negotiation or cooperation on authorization, please contact:
[email protected].