A journey of a thousand miles begins with a single step

When we learn a new programming language, we always start with Hello world.

When we learn how to deploy an application on K8S, we deploy a simple Nginx with access to its configuration page. Because it has all five organs, simple function, stateless, can be used as k8S deployment application Hello, world.

This article will learn how to start deploying your first application using Pod, Deployment, and Service

  • Deploy your first application using K8S
  • Series of articles: Personal server operation and maintenance guide

If it is helpful to you, please help me click a star on shfshanyue/op-note.

Pod

A pod is the smallest choreography unit in Kubernetes and usually consists of a single container (sometimes multiple containers).

Here is a minimal example of a POD resource profile. For details, see Kubernetes V1.16Pod

We deployed a Pod using Nginx: Alpine as an image and exposed port 80

apiVersion: v1
kind: Pod
metadata:
  name: nginx
  # specify label for easy retrieval
  labels:
    app: nginx
spec:
  containers:
  - name: nginx
    # select mirror
    image: nginx:alpine
    # specify the exposed port
    ports:
    - containerPort: 80
Copy the code

Using Kubectly Apply, deploy the Pod

$ kubectl apply -f nginx.yaml
pod/nginx created
Copy the code

Verify the deployment STATUS. If the STATUS is Running, the deployment is successful

Get Pod deployment status, especially IP
# -o wide Lists IP/Node information
$ kubectl get pods nginx -o wide
NAME    READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES
nginx   1/1     Running   0          14m   10.244.1.9   shuifeng   <none>           <none>
Copy the code

Use -o wide to get the POD IP address and access the IP address to see if you can access the classic nginx configuration page

Get more detailed information
$ kubectl describe pod nginx

Each POD has an IP address. Access the IP address directly to get content$curl 10.244.1.9 <! DOCTYPE html> <html> <head> <title>Welcome to nginx! </title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx! </h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
Copy the code

At this point we can use Kubectl exec to access the Pod’s internal container. If there are multiple containers in Pod, use kubectl exec -c to specify the containers

$ kubectl exec -it nginx sh
Copy the code

Execute commands in the Pod container to verify socket status and nginx service

Execute the command in POD

# You can see port 80 from Nginx
$ netstat -tan
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN

# Access nginx and return the content of the configuration page correctly
# -q: does not output wget information
# -o -: redirect to standard output$ wget -q -O - localhost <! DOCTYPE html> <html> <head> <title>Welcome to nginx! </title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx! </h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
Copy the code

Deployment

Orchestration application in K8S can do elastic expansion and load balancing better. Since balancing is not possible with one Pod, it is natural to deploy multiple pods

Docker-compose can be easily augmented by docker-compose scale, not to mention K8S.

Managing pods in K8S is called a Controller, and we can use Deployment Controller to scale up pods and of course it can roll upgrades, rollbacks, canaries, etc

Let’s write a Deployment resource configuration file

  • spec.template: Specifies the Pod to deploy
  • spec.replicas: Specifies the number of deployments
  • spec.selector: Locate the Pod to be managed
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        ports:
        - containerPort: 80
Copy the code

We used Kubectl Apply to check the Pod and Deployment status after the Deployment took effect

$ kubectl apply -f nginx.yaml

# nginx-deployment All three pods deployed successfully
$ kubectl get pods -o wide -l 'app=nginx'NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES Nginx 1/1 Running 1 4H29m 10.244.1.9 shuifeng <none> <none> nginx-deployment-54f57cf6bf-57g8l 1/1 Running 0 23m 10.244.1.10 shuifeng <none> <none> Nginx-deployment-54f57cf6bf-ltdf7 1/1 Running 0 23m 10.244.1.11 shuifeng < None > <none> nginx-deployment-54f57cf6bf-n8ppt 1/1 Running 0 23m 10.244.1.12 shuifeng <none> <none># READY 3/3 Indicates that all deployment is successful
$ kubectl get deploy nginx-deployment
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
nginx-deployment   3/3     3            3           23m
Copy the code

Service

Now that we have deployed a Deployment with three PODS and three IP addresses, how can we request service from these three PODS? Besides, new Pod IP will be generated every time we deploy online. How do we do service discovery

We can solve this problem with services by doing a network layer abstraction that specifies Deployment or a Pod for a particular collection

The configuration file is as follows

  1. spec.selector: Specify how to selectPod
  2. spec.ports: Specify how to expose ports
apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
Copy the code

We used Kubectl Apply to check the Service status after the deployment took effect

$ kubectl get svc nginx-service -o wide NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR nginx-service ClusterIP 10.108.9.49 < None > 80/TCP 11m app=nginxCopy the code

ClusterIP means that the service can only be accessed within the cluster. At this point, access 10.108.9.49 Access the service

$curl 10.108.9.49 <! DOCTYPE html> <html> <head> <title>Welcome to nginx! </title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx! </h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
Copy the code

Service discovery, we only need to know the name of the service to access the service, only IP access is too low. Service is certainly not so low

In k8s, all of the Service can be through my – SVC. My – namespace. SVC. Cluster. The local Service discovery, for a deployed Service is nginx – Service. The default. SVC. Cluster. The local

Access the service through the domain name in any Pod in the cluster. The access succeeds

$ curl nginx-service.default.svc.cluster.local <! DOCTYPE html> <html> <head> <title>Welcome to nginx! </title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx! </h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>
Copy the code

summary

By configuring Deployment and Service, we can now discover access domain names through services in the cluster. The complete configuration file is as follows

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:alpine
        ports:
        - containerPort: 80

---

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  selector:
    app: nginx
  ports:
  - protocol: TCP
    port: 80
    targetPort: 80
Copy the code

How do we access domain names outside the cluster when we still need to expose services to the Internet?

Follow public account

Welcome to follow the public account Shanyuexing. I will regularly share some articles on the front and back end and operation and maintenance, and there will be a daily review and summary of technology and life. Welcome to follow and exchange