Kubernetes入门实战

更多请参考Kubernetes官方教程网站:

https://kubernetes.io/docs/tutorials/kubernetes-basics


一.创建集群

1 查看minikube 版本:

$ minikube version
minikube version: v0.25.0

2 启动 minikube:

$ minikube start
Starting local Kubernetes v1.9.0 cluster...
Starting VM...
Getting VM IP address...
Moving files into cluster...
Setting up certs...
Connecting to cluster...
Setting up kubeconfig...
Starting cluster components...
Kubectl is now configured to use the cluster.
Loading cached images from config file.

minikube 启动之后,会创建一个单节点 Kubernetes 集群。

3 查看集群版本

$ kubectl version
Client Version: version.Info{Major:"1", Minor:"9", GitVersion:"v1.9.0", GitCommit:"925c127ec6b946659ad0fd596fa959be43f0cc05", GitTreeState:"clean", BuildDate:"2017-12-15T21:07:38Z", GoVersion:"go1.9.2", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"", Minor:"", GitVersion:"v1.9.0", GitCommit:"925c127ec6b946659ad0fd596fa959be43f0cc05", GitTreeState:"clean", BuildDate:"2018-01-26T19:04:38Z", GoVersion:"go1.9.1", Compiler:"gc", Platform:"linux/amd64"}

这里有两个版本,client version 指的是 kubectl 命令行工具的版本,而 server version 才是 Kubernetes 的版本;

4 查看集群信息

$ kubectl cluster-info
Kubernetes master is running at https://172.17.0.77:8443

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

集群所在主机的 ip 为 172.17.0.77。

注意:这里的 master 指的是 Kubernetes 集群的 master 节点(在 Kubernetes 集群中,节点分为两类,一类是 master 节点,一类是 node 节点),那怎么看到 node 节点呢?

5 查看node节点信息

$ kubectl get node
NAME      STATUS    ROLES     AGE       VERSION
host01    Ready     <none>    20m       v1.9.0

host01 就是 node 节点,在当前环境中,实际上只有一台主机。这台主机既作为 master 节点,也作为 node 节点;

二.部署应用

下面以部署一个 nginx 为例来演示部署应用的过程:

$ kubectl run first-app --image=nginx --port=80
deployment "first-app" created

通过 run 命令创建一个名为 first-app 的 deployment,使用的是 docker hub 上最新的 nginx 镜像,并指定了应用端口为 80。deployment 是干嘛的呢?别急,往下看:

查看当前的 deployment:

$ kubectl get deployment
NAME        DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
first-app   1         1         1            1           1m

查看当前的 pod:

$ kubectl get pod
NAME                        READY     STATUS    RESTARTS   AGE
first-app-6db44b474-dbbtp   1/1       Running   0          4m

当前有一个名为 first-app-6db44b474-dbbtp 的 pod,仔细看看这个 pod 的名称 ,是不是就是刚刚创建的 deployment 的名称加了一串后缀?确实是这样。在 Kubernetes 中,用 deployment 来声明和管理 pod(其实中间还有一层,这里就不展开讲了),而每个 pod 里面包含至少一个 container。

查看更详细的 pod 内容:

$ kubectl describe pod first-app-6db44b474-dbbtp
Name:           first-app-6db44b474-dbbtp
Namespace:      default
Node:           host01/172.17.0.77
Start Time:     Fri, 02 Mar 2018 06:48:02 +0000
Labels:         pod-template-hash=286006030
                run=first-app
Annotations:    <none>
Status:         Running
IP:             172.18.0.4
Controlled By:  ReplicaSet/first-app-6db44b474
Containers:
  first-app:
    Container ID:   docker://54eacc7ff536d7181fa366883f7ed4cf632492ad6ed391207fea436d22d219a9
    Image:          nginx
    Image ID:       docker-pullable://nginx@sha256:4771d09578c7c6a65299e110b3ee1c0a2592f5ea2618d23e4ffe7a4cab1ce5de
    Port:           80/TCP
    State:          Running
      Started:      Fri, 02 Mar 2018 06:48:14 +0000
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-zkqw6 (ro)
Conditions:
  Type           Status
  Initialized    True
  Ready          True
  PodScheduled   True
Volumes:
  default-token-zkqw6:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-zkqw6
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     <none>
Events:
  Type    Reason                 Age   From               Message
  ----    ------                 ----  ----               -------
  Normal  Scheduled              7m    default-scheduler  Successfully assigned first-app-6db44b474-dbbtp to host01
  Normal  SuccessfulMountVolume  7m    kubelet, host01    MountVolume.SetUp succeeded for volume "default-token-zkqw6"
  Normal  Pulling                7m    kubelet, host01    pulling image "nginx"
  Normal  Pulled                 7m    kubelet, host01    Successfully pulled image "nginx"
  Normal  Created                7m    kubelet, host01    Created container
  Normal  Started                7m    kubelet, host01    Started container

三.发布服务

已经部署好了一个 nginx 应用,那么要怎么去访问呢?这时候就需要用到 service。
创建一个 service:

$ kubectl expose deployment/first-app --type="NodePort" --port=80
service "first-app" exposed

查看创建好的名为 first-app 的 service :

$ kubectl get svc first-app
NAME        TYPE       CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
first-app   NodePort   10.102.0.12   <none>        80:30491/TCP   1m

在 PORT(S) 一栏中,除了 80 端口,后面还有一个 30491 端口。这是使用了“NodePort”类型创建 service 分配的端口,通过主机 ip 和这个端口,就可以访问到这个 service 了。可以使用 curl 工具进行访问:

$ curl 172.17.0.77:30491
<!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>

四.扩缩应用

刚刚已经成功访问到 nginx 应用,但有些时候,可能需要多个 nginx 来横向扩展,那么在 Kubernetes 中怎么实现呢?

$ kubectl scale deployment/first-app --replicas=3
deployment "first-app" scaled

再查看当前的 pod,可以看到当前已经有了 3 个 first-app 了:

$ kubectl get pod
NAME                        READY     STATUS    RESTARTS   AGE
first-app-6db44b474-6vlrj   1/1       Running   0          39s
first-app-6db44b474-dbbtp   1/1       Running   0          19m
first-app-6db44b474-gjzgg   1/1       Running   0          39s

5 .更新应用

最常用的就是更新镜像。之前使用的是 docker hub 上最新的 nginx,现在用 1.10.3 这个比较老的版本来替代最新版本。先查看当前使用的 nginx 版本。这里有一个很简单的方法,访问一个不存在的页面,如下:

$ curl 172.17.0.77:30491/abc
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.13.9</center>
</body>
</html>

当前使用的 nginx 版本是 1.13.9,接下来进行更新操作:

$ kubectl set image deployment/first-app first-app=nginx:1.10
deployment "first-app" image updated

再查看当前 nginx 的版本:

 $ curl 172.17.0.77:30491/abc
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.10.3</center>
</body>
</html>

nginx 版本已经成功更新为 1.10.3。

6.删除应用

之前是通过 deployment 来创建应用,所以只需要删除 deployment 就可以删除对应的应用了;

$ kubectl delete deployment/first-app
deployment "first-app" deleted

再查看一下当前的 pod:

$ kubectl get pod
No resources found.

所有的 pod 都已经删除了!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值