k8s学习笔记-01

1 minute read

继上上一篇文章部署了microk8s之后, 本文来介绍如何用k8s部署应用.

第一步: 创建一个集群

创建集群的步骤请参考安装microk8s, 按照流程部署完以后你就得到了一个只有一个节点(node)的集群

联系使用一下kubectl命令

~$ microk8s.kubectl version
Client Version: version.Info{Major:"1", Minor:"12", GitVersion:"v1.12.2", GitCommit:"17c77c7898218073f14c8d573582e8d2313dc740", GitTreeState:"clean", BuildDate:"2018-10-24T06:54:59Z", GoVersion:"go1.10.4", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"12", GitVersion:"v1.12.2", GitCommit:"17c77c7898218073f14c8d573582e8d2313dc740", GitTreeState:"clean", BuildDate:"2018-10-24T06:43:59Z", GoVersion:"go1.10.4", Compiler:"gc", Platform:"linux/amd64"}

~$ microk8s.kubectl get nodes
NAME             STATUS   ROLES    AGE   VERSION
vegetta-laptop   Ready    <none>   21h   v1.12.2

~$ microk8s.kubectl cluster-info
Kubernetes master is running at http://127.0.0.1:8080

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.

第二部: 部署应用

  1. pull实验用的docker镜像,
      microk8s.docker pull jocatalin/kubernetes-bootcamp:v1  # 这个镜像google镜像服务器里的标准镜像是一模一样的, 为了避开gfw的问题, 我们采用民间缓存版本.
    
  2. 部署 ``` ~$ microk8s.kubectl run bootcamp –image=jocatalin/kubernetes-bootcamp:v1 –port=8080 # 创建deployment kubectl run –generator=deployment/apps.v1beta1 is DEPRECATED and will be removed in a future version. Use kubectl create instead. deployment.apps/bootcamp created

~$ microk8s.kubectl get pods # 查看信息 NAME READY STATUS RESTARTS AGE bootcamp-7b7d9d7648-hzf9c 1/1 Running 0 13s

~$ microk8s.kubectl get all # 也可以这样查看 NAME READY STATUS RESTARTS AGE pod/bootcamp-7b7d9d7648-hzf9c 1/1 Running 0 3m33s

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/kubernetes ClusterIP 10.152.183.1 443/TCP 10m

NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE deployment.apps/bootcamp 1 1 1 1 3m33s

NAME DESIRED CURRENT READY AGE replicaset.apps/bootcamp-7b7d9d7648 1 1 1 3m33s

~$ microk8s.kubectl describe pod bootcamp-7b7d9d7648-hzf9c #查看某个资源的具体信息


3. 访问app

部署完了以后,我们的app还处在k8s集群的内网中, 不能从外部访问, 这一步就是配置访问.我们新开一个终端

~$ microk8s.kubectl proxy # 按ctrl+c退出 Starting to serve on 127.0.0.1:8001


上面这个命令的作用是给k8s集群见了一个本地代理, 通过<code>127.0.0.1:8001</code>可以访问到k8s集群的所有服务

我们尝试```curl http://localhost:8001/version```

{ “major”: “1”, “minor”: “12”, “gitVersion”: “v1.12.2”, “gitCommit”: “17c77c7898218073f14c8d573582e8d2313dc740”, “gitTreeState”: “clean”, “buildDate”: “2018-10-24T06:43:59Z”, “goVersion”: “go1.10.4”, “compiler”: “gc”, “platform”: “linux/amd64” }


运行命令

curl http://localhost:8001/api/v1/namespaces/default/pods/$POD_NAME/proxy/ # 把$POD_NAME换成kubectl get pods里面的pod名, 在我的电脑上是bootcamp-7b7d9d7648-hzf9c ```

输出: Hello Kubernetes bootcamp! | Running on: bootcamp-7b7d9d7648-hzf9c | v=1