Kubernetes (b) the application deployment

Application deployment Kubernetes tutorial

Article Address: blog.piaoruiqing.com/blog/2019/0...

Foreword

This article and readers together, learning how to deploy applications on Kubernetes include: how to deploy the application, issued a document to explain to the local project publishing package.

Read this article you can harvest to:

  • Learn how to deploy applications in k8s
  • How to package Docker image, upload it to a private library

You need to read this:

Understand the description file

First, we have to understand the description file through the deployment of Nginx in Kubernetes.

In general, Kubernetes use yaml (or json) to describe the release configuration The following is a brief description of the file: nginx-pod.yaml

apiVersion: v1      # 描述文件所遵循KubernetesAPI的版本
kind: Pod           # 描述的类型是pod
metadata:
  name: nginx-pod   # pod的名称
  labels:           # 标签
    app: nginx-pod
    env: test
spec:
  containers:
    - name: nginx-pod     # 容器名
      image: nginx:1.15   # 镜像名称及版本
      imagePullPolicy: IfNotPresent   # 如果本地不存在就去远程仓库拉取
      ports:
        - containerPort: 80   # pod对外端口
  restartPolicy: Always
复制代码

We log on Master node, use the kubectlcommand to deploy the application described in this document. (Of course, using a dashboard release may be)

kubectl get podsCommand is used to view a list of the pod, use grepscreened nginx related pod. Nginx has been released at this time is finished. We can visualize from the dashboard to the application state.

Note: Delete pod can use the kubectl delete -f nginx-pod.yamlcommands can also be operated directly on the dashboard.

[Copyright]
This article published in Pu Ruiqing's blog , allows non-commercial use reproduced, reprinted but must retain the original author Pu Ruiqing and links: blog.piaoruiqing.com . If the authorization aspects of consultation or cooperation, please contact E-mail: piaoruiqing @ Gmail. COM .

How to Access Services

The previous section we deployed a Nginx pod, but we can not access to the Nginx.

Want access to the pod in service, the easiest way is through port forwarding, run the following command will host the 9999port with nginx-pod of 80port binding:

[root@nas-centos1 ~]$ kubectl port-forward --address 0.0.0.0 nginx-pod 9999:80
Forwarding from 0.0.0.0:9999 -> 80
Handling connection for 9999
复制代码

At this point, we Nginx can be accessed by visiting the host port 9999.

Deploying Local Project

The local development of the project to publish Kubernetes, you need to package the project into Docker mirror, the mirror will then be pushed to the warehouse (public / private warehouse available).

First of all, we need a local project can run, I use spring-boot build a simple web project:

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

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

    public static void main(String[] args) {
        SpringApplication.run(K8sTestApplication.class, args);
    }
}
复制代码

Docker mirror package

参考: Dockerfile reference

With the project, we need to package it into a Docker mirror, Dockerfilereads as follows:

FROM java:8-alpine
COPY ./k8s-test-0.0.1-SNAPSHOT.jar /usr/app/
WORKDIR /usr/app
ENTRYPOINT ["java", "-jar", "k8s-test-0.0.1-SNAPSHOT.jar"]
复制代码
  • FROM java:8-alpine: The Mirroring java-8-alpineMirroring.
  • COPY ./target/k8s-test-0.0.1-SNAPSHOT.jar /usr/app/: Compiled packaged jar copied to mirror /usr/appdirectory.
  • WORKDIR /usr/app: Working directory is specified /usr/app.
  • ENTRYPOINT ["java", "-jar", "k8s-test-0.0.1-SNAPSHOT.jar"]: When you start docker execution java -jar k8s-test-0.0.1-SNAPSHOT.jarcommand

Dockerfile into the directory where the execution docker build -t piaoruiqing/k8s-test .packaged. Be careful not to miss the last plane out of order ., it means the current directory.

[root@nas-centos1 k8s-test]$ docker build -t piaoruiqing/k8s-test .
复制代码

By docker imagescan view the local mirror list command:

[root@nas-centos1 k8s-test]$ docker images | grep k8s
piaoruiqing/k8s-test     latest         efe9e9625376        4 minutes ago       174MB
复制代码

Pushed to the remote repository

After complete package docker mirror, the mirror needs to be pushed to the warehouse for Kubernetes respective nodes. aliyun the paper container mirror service as an example.

First, log on to the Ali Cloud Console -> container mirror service , create a namespace:

# 登录, 用于登录的用户名为阿里云账号全名, 密码为开通服务时设置的密码.
[root@nas-centos1 k8s-test]$ docker login --username=[用户名] registry.cn-hangzhou.aliyuncs.com
# 打标签, 镜像ID可以通过 docker images 命令查看
[root@nas-centos1 k8s-test]$ docker tag efe9e9625376 registry.cn-hangzhou.aliyuncs.com/piaoruiqing/k8s-test:0.0.1
# 推送到阿里云镜像仓库
[root@nas-centos1 k8s-test]$ docker push registry.cn-hangzhou.aliyuncs.com/piaoruiqing/k8s-test:0.0.1
复制代码

deploy

Private library support

Since I use a private library, Kubernetes can not directly access, you need to create a secret in order 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=[用户名] --docker-password=[密码] --docker-email=[邮箱]
复制代码
  • docker-registry-secret: Key name specified key, definable.
  • docker-server: Docker warehouse designated address.
  • docker-username: Specifies the user name Docker warehouse.
  • docker-password: Specifies the login password Docker warehouse.
  • docker-email: Specifies the e-mail address (optional).

You can kubectl get secretsview all secret

[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
复制代码

Begin deployment

When accessing private library description file you need to specify the secret, 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    # 这里就是前文中创建的secret, 用来访问私库
复制代码

Run kubectl apply -f k8s-test-pod.yamlfor deployment, after which we can see in the dashboard.

Use kubectl port-forward --address 0.0.0.0 k8s-test-pod 80:8080can bind host (master) 8080 port and 80-port pod of. So we can access http://10.33.30.95/k8s-test/timestamp up .

[root@nas-centos2 ~]$ curl 10.33.30.95/k8s-test/timestamp
1569512136028
复制代码

Cluster deployment

Now, we have successfully deployed an application on Kubernetes example, under a production environment most cases we need a cluster of application deployment, this time, we can use to describe Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: k8s-test
  labels:
    app: k8s-test
spec:
  replicas: 3		# 副本数量
  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
复制代码

Similarly, using the kubectl apply -fcommand to publish the configuration.

[root@nas-centos1 k8s-test]$ kubectl apply -f k8s-test.yaml 
deployment.apps/k8s-test created
复制代码

If this article helpful, please point a praise it (¯ ▽ ¯) "for you

references

Recommended reading:

Welcome to public concern number (Code poetic):

[Copyright]
This article published in Pu Ruiqing's blog , allows non-commercial use reproduced, reprinted but must retain the original author Pu Ruiqing and links: blog.piaoruiqing.com . If the authorization aspects of consultation or cooperation, please contact E-mail: piaoruiqing @ Gmail. COM .

Guess you like

Origin juejin.im/post/5d8f74725188253e3c392b35