部署第一个deployment

目标: 通过apply创建了 ipquery 对应的deployment,它具有4个副本。通过get pods 可以查询4个副本的状态。 并且通过curl访问ipquery对应的pod

(1) 创建deployment声明文件

ipquery-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: ipquery-v1
  labels:
    app: ipquery-v1
spec:
  replicas: 4
  selector:
    matchLabels:
      app: ipquery-v1
  template:
    metadata:
      labels:
        app: ipquery-v1
    spec:
      containers:
      - name: ipquery-k8s
        image: lorahz/ipquery:1.10

(2) 使用kubctl apply 或者 kubctl create创建deployment


//创建deployment
[root@t1 ipquery]# kubectl apply -f ipquery-deployment.yaml 
deployment.apps/ipquery-v1 created

//查询deployment对应的pods列表
[root@t1 ipquery]# kubectl get pods
NAME                                READY   STATUS    RESTARTS   AGE
ipquery-v1-867579b494-9xnkp         1/1     Running   0          2m11s
ipquery-v1-867579b494-c28s5         1/1     Running   0          2m11s
ipquery-v1-867579b494-cbtrs         1/1     Running   0          2m11s
ipquery-v1-867579b494-g7lgr         1/1     Running   0          2m11s
nginx-deployment-55fbd9fd6d-dtfbf   1/1     Running   0          19m

//查询deployment
[root@t1 ipquery]# kubectl get deployments
NAME               READY   UP-TO-DATE   AVAILABLE   AGE
ipquery-v1         4/4     4            4           3m18s
nginx-deployment   1/1     1            1           20m

(3) 如何访问Pod

查询Pod对应IP地址,通过curl命名访问

kubectl describe pod ipquery-v1-867579b494-dtpxq
[root@t1 ipquery]# kubectl describe pod ipquery-v1-867579b494-dtpxq
Name:         ipquery-v1-867579b494-dtpxq
Namespace:    production
Priority:     0
Node:         t2/10.105.90.27
Start Time:   Tue, 08 Oct 2019 22:21:33 +0800
Labels:       app=ipquery-v1
              pod-template-hash=867579b494
Annotations:  cni.projectcalico.org/podIP: 10.100.2.202/32
Status:       Running
IP:           10.100.2.202


[root@t1 ipquery]# curl 10.100.2.202:8888
{"result":"Welcome Michal.Pan's IPQuery Server!","status":200,"uuid":"2761c592-55c2-4fc7-afb3-00a3aaaa0be1"}

另外可以通过日志命令,查看访问的服务确实Curl访问的实例是ipquery-v1-867579b494-dtpxq

kubectl logs -f ipquery-v1-867579b494-dtpxq

[root@t1 ipquery]# kubectl logs -f ipquery-v1-867579b494-dtpxq
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.

[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
 - using env:   export GIN_MODE=release
 - using code:  gin.SetMode(gin.ReleaseMode)

[GIN-debug] GET    /metrics                  --> github.com/chenjiandongx/ginprom.PromHandler.func1 (4 handlers)
[GIN-debug] GET    /                         --> michaelapp.com/ipquery/query.(*QueryMgr).welcomehandler-fm (4 handlers)
[GIN-debug] GET    /query                    --> michaelapp.com/ipquery/query.(*QueryMgr).queryiphandler-fm (4 handlers)
[GIN-debug] Listening and serving HTTP on :8888
2019/10/08 14:28:16 welcomehandler start
[GIN] 2019/10/08 - 14:28:16 | 200 |     245.275µs |      10.100.1.0 | GET      /
2019/10/08 14:28:51 welcomehandler start
[GIN] 2019/10/08 - 14:28:51 | 200 |     135.818µs |      10.100.1.0 | GET      /
[GIN] 2019/10/08 - 14:29:58 | 200 |      95.319µs |      10.100.1.0 | GET      /
2019/10/08 14:29:58 welcomehandler start
[GIN] 2019/10/08 - 14:30:00 | 200 |      98.468µs |      10.100.1.0 | GET      /
2019/10/08 14:30:00 welcomehandler start
2019/10/08 14:30:01 welcomehandler start
[GIN] 2019/10/08 - 14:30:01 | 200 |     115.632µs |      10.100.1.0 | GET      /
[GIN] 2019/10/08 - 14:30:02 | 200 |     115.179µs |      10.100.1.0 | GET      /
2019/10/08 14:30:02 welcomehandler start

(4) 删除deployment

//删除deployment
[root@t1 ipquery]# kubectl delete -f ipquery-deployment.yaml 
deployment.apps "ipquery-v1" deleted

(5) 备注 节点和pod的关系

效果图