k8s基本概念

kubernetes架构
Master 节点

Master 是 Kubernetes Cluster 的大脑,运行着如下 Daemon 服务:kube-apiserver、kube-scheduler、kubecontroller-manager、etcd 和 Pod 网络

API Server(kube-apiserver)

API Server 提供 HTTP/HTTPS RESTful API,即 Kubernetes API。API Server 是 Kubernetes Cluster 的前端接口,各种客户端工具(CLI 或 UI)以及 Kubernetes 其他组件可以通过它管理 Cluster 的各种资源。

Scheduler(kube-scheduler)

Scheduler 负责决定将 Pod 放在哪个 Node 上运行。Scheduler 在调度时会充分考虑 Cluster 的拓扑结构,当前各个节点的负载,以及应用对高可用、性能、数据亲和性的需求。

Controller Manager(kube-controller-manager)

Controller Manager 负责管理 Cluster 各种资源,保证资源处于预期的状态。Controller Manager 由多种controller 组成,包括 replication controller、endpoints controller、namespace controller、serviceaccountscontroller 等。 不同的 controller 管理不同的资源。例如 replication controller 管理 Deployment、StatefulSet、DaemonSet 的生命周期,namespace controller 管理 Namespace 资源。

etcd

etcd 负责保存 Kubernetes Cluster 的配置信息和各种资源的状态信息。当数据发生变化时,etcd 会快速地通知Kubernetes 相关组件。

Node 节点

Node 是 Pod 运行的地方,Kubernetes 支持 Docker、rkt 等容器 Runtime。 Node上运行的 Kubernetes 组件有kubelet、kube-proxy 和 Pod 网络。

kubelet

kubelet 是 Node 的 agent,当 Scheduler 确定在某个 Node 上运行 Pod 后,会将 Pod 的具体配置信息(image、volume 等)发送给该节点的 kubelet,kubelet 根据这些信息创建和运行容器,并向 Master 报告运
行状态。

kube-proxy

service 在逻辑上代表了后端的多个 Pod,外界通过 service 访问 Pod。 每个 Node 都会运行 kube-proxy 服务,它负责将访问 service 的
TCP/UPD 数据流转发到后端的容器。如果有多个副本,kube-proxy 会实现负载均衡。

Pod 网络

Pod 要能够相互通信,Kubernetes Cluster 必须部署 Pod 网络,flannel 是其中一个可选方案

[root@master ~]# kubectl get pod --all-namespaces -o wide
NAMESPACE     NAME                             READY   STATUS    RESTARTS   AGE     IP               NODE     NOMINATED NODE   READINESS GATES
kube-system   coredns-6955765f44-g6r8s         1/1     Running   1          2d20h   10.244.0.5       master   <none>           <none>
kube-system   coredns-6955765f44-rfs4k         1/1     Running   1          2d20h   10.244.0.4       master   <none>           <none>
kube-system   etcd-master                      1/1     Running   1          2d20h   192.168.19.160   master   <none>           <none>
kube-system   kube-apiserver-master            1/1     Running   1          2d20h   192.168.19.160   master   <none>           <none>
kube-system   kube-controller-manager-master   1/1     Running   1          2d20h   192.168.19.160   master   <none>           <none>
kube-system   kube-flannel-ds-amd64-fh8xp      1/1     Running   2          2d20h   192.168.19.160   master   <none>           <none>
kube-system   kube-flannel-ds-amd64-nrth4      1/1     Running   1          2d20h   192.168.19.162   node2    <none>           <none>
kube-system   kube-flannel-ds-amd64-pdnd2      1/1     Running   2          2d20h   192.168.19.161   node1    <none>           <none>
kube-system   kube-proxy-8fpsm                 1/1     Running   1          2d20h   192.168.19.161   node1    <none>           <none>
kube-system   kube-proxy-bk6z5                 1/1     Running   1          2d20h   192.168.19.160   master   <none>           <none>
kube-system   kube-proxy-t4bqz                 1/1     Running   1          2d20h   192.168.19.162   node2    <none>           <none>
kube-system   kube-scheduler-master            1/1     Running   1          2d20h   192.168.19.160   master   <none>           <none>

k8s基本功能

部署应用:
[root@master ~]# kubectl run myweb --image=nginx  --port=80

通过kubectl run 部署了一个应用,命名为 myweb
Docker 镜像通过 --image 指定。 --port 设置应用对外服务的端口

查看当前的 pod

[root@master ~]# kubectl  get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE    IP           NODE    NOMINATED NODE   READINESS GATES
myweb-7bfcd86fd6-6qhvq   1/1     Running   0          2m1s   10.244.2.2   node2   <none>           <none>

myweb-7bfcd86fd6-6qhvq 就是应用的 Pod。

访问应用:

默认情况下,所有 Pod 只能在集群内部访问。对于上面这个例子,要访问应用只能直接访问容器的 80 端口。为了能够从外部访问应用,我们需要将容器的 80端口映射到节点的端口。

[root@master ~]# kubectl expose deployment  myweb  --type=NodePort    --port=80
service/myweb exposed

查看应用:

[root@master ~]# kubectl get service
NAME         TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1      <none>        443/TCP        2d20h
myweb        NodePort    10.96.42.130   <none>        80:30920/TCP   13s

访问对应的 IP 端口

在这里插入图片描述

Scale 伸缩

查看副本数:

[root@master ~]# kubectl  get deployments.apps 
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
myweb   1/1     1            1           6m49s

将副本数增加到三个:

[root@master ~]# kubectl  scale  deployment  myweb --replicas=3
deployment.apps/myweb scaled

其他节点若没有相应的镜像,会自动下载。

查看 pod:

[root@master ~]# kubectl  get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE     IP           NODE    NOMINATED NODE   READINESS GATES
myweb-7bfcd86fd6-6qhvq   1/1     Running   0          8m43s   10.244.2.2   node2   <none>           <none>
myweb-7bfcd86fd6-8zkxt   1/1     Running   0          84s     10.244.2.3   node2   <none>           <none>
myweb-7bfcd86fd6-xx2jv   1/1     Running   0          84s     10.244.1.2   node1   <none>           <none>

将副本数减少到两个:

[root@master ~]# kubectl  scale  deployment  myweb --replicas=2
deployment.apps/myweb scaled

查看副本:

root@master ~]# kubectl  get pod 
NAME                     READY   STATUS    RESTARTS   AGE
myweb-7bfcd86fd6-6qhvq   1/1     Running   0          9m30s
myweb-7bfcd86fd6-xx2jv   1/1     Running   0          2m11s
滚动更新

当前使用 nginx 镜像,将其改为 httpd:

更新
[root@master ~]# kubectl set image deployments/myweb  myweb=httpd
deployment.apps/myweb image updated

查看 pod:

[root@master ~]# kubectl  get pod -o wide
NAME                    READY   STATUS    RESTARTS   AGE   IP           NODE    NOMINATED NODE   READINESS GATES
myweb-f867f887d-26t2f   1/1     Running   0          91s   10.244.1.3   node1   <none>           <none>
myweb-f867f887d-mqd2n   1/1     Running   0          50s   10.244.2.4   node2   <none> 

在更新的过程中,会依次停掉服务,再重启部署,所以 IP 会有变化

访问页面:

[root@master ~]# curl  10.244.1.3
<html><body><h1>It works!</h1></body></html>
[root@master ~]# curl  10.244.2.4
<html><body><h1>It works!</h1></body></html>

可以看到,当前为 httpd 页面

回滚:
[root@master ~]# kubectl  rollout  undo deployment  myweb
deployment.apps/myweb rolled back

查看 pod:

[root@master ~]# kubectl  get pod -o wide
NAME                     READY   STATUS    RESTARTS   AGE   IP           NODE    NOMINATED NODE   READINESS GATES
myweb-7bfcd86fd6-jp2lz   1/1     Running   0          53s   10.244.1.4   node1   <none>           <none>
myweb-7bfcd86fd6-t4sgl   1/1     Running   0          35s   10.244.2.5   node2   <none>           <none>

访问页面:

[root@master ~]# curl 10.244.1.4
<!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>
[root@master ~]# curl 10.244.2.5
<!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。

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐