在对K8S进行学习以前,需要准备好一些基本条件,首先docker的安装就不多说了
实验准备了三个节点,一个master,两个node节点,并配置好主机名解析
1、etcd的安装
etcd的功能及作用参见K8s官网:https://etcd.io/,
etcd is a strongly consistent, distributed key-value store that provides a reliable way to store data that needs to be accessed by a distributed system or cluster of machines. It gracefully handles leader elections during network partitions and can tolerate machine failure, even in the leader node.
Applications of any complexity, from a simple web app to Kubernetes, can read data from and write data into etcd.
Your applications can read from and write data into etcd. A simple use case is storing database connection details or feature flags in etcd as key-value pairs. These values can be watched, allowing your app to reconfigure itself when they change. Advanced uses take advantage of etcd’s consistency guarantees to implement database leader elections or perform distributed locking across a cluster of workers.
etcd的安装仅在master节点安装,并修改配置文件
6 ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379" #监听地址
21 ETCD_ADVERTISE_CLIENT_URLS="http://10.0.0.29:2379" #申明客户端地址
修改以后可以对etcd进行测试:
[root@k8s_master ~]# etcdctl set testdir/testkey0 123 #将testdir/testkey0存为123
123
[root@k8s_master ~]# etcdctl get testdir/testkey0
123
etcdctl --endpoint=http://10.0.0.29:2379 cluster-health #若是集群安装,检查集群状态
etcdctl --endpoint=http://127.0.0.1:2379 cluster-health
etcdctl -C http://127.0.0.1:2379 cluster-health
2、在master节点安装K8s服务
这里采用最简单的yum安装,这种方法最容易成功,GitHub上有人写了借助ansible二进制安装的办法,可以去查看一下。
[root@k8s_master ~]# yum install -y kubernetes-master.x86_64
Installed:
kubernetes-master.x86_64 0:1.5.2-0.7.git269f928.el7
Dependency Installed:
kubernetes-client.x86_64 0:1.5.2-0.7.git269f928.el7
Complete!
修改配置文件:[
root@k8s_master ~]# vim /etc/kubernetes/apiserver #这个类似于openstack中的keystone角色
[root@k8s_master kubernetes]# diff apiserver apiserver.bak
8c8
< KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0" #监听地址
---
> KUBE_API_ADDRESS="--insecure-bind-address=127.0.0.1"
11c11
< KUBE_API_PORT="--port=8080" #api端口
---
> # KUBE_API_PORT="--port=8080"
14c14
< KUBELET_PORT="--kubelet-port=10250" #K8S_node端口
---
> # KUBELET_PORT="--kubelet-port=10250"
17c17
< KUBE_ETCD_SERVERS="--etcd-servers=http://10.0.0.29:2379" #etcd服务端口
---
> KUBE_ETCD_SERVERS="--etcd-servers=http://127.0.0.1:2379"
[root@k8s_master kubernetes]#
[root@k8s_master kubernetes]# vi config
[root@k8s_master kubernetes]# diff config config.bak
22c22
< KUBE_MASTER="--master=http://10.0.0.29:8080" #K8s_master地址
---
> KUBE_MASTER="--master=http://127.0.0.1:8080"
systemctl start kube-apiserver
systemctl start kube-controller-manager
systemctl start kube-scheduler
systemctl enable kube-apiserver
systemctl enable kube-controller-manager
systemctl enable kube-scheduler
[root@k8s_master kubernetes]# systemctl | grep kube
kube-apiserver.service loaded active running Kubernetes API Server #接受并相应用户的请求
kube-controller-manager.service loaded active running Kubernetes Controller Manager #控制器的概念,保障容器的存活
kube-scheduler.service loaded active running Kubernetes Scheduler Plugin #调度器,选择容器启动的node节点
3、在node节点安装K8s服务:yum install -y kubernetes-node.x86_64,修改配置文件
[root@k8s_node_2 kubernetes]# diff kubelet kubelet.bak
5c5
< KUBELET_ADDRESS="--address=0.0.0.0" #开放地址
---
> KUBELET_ADDRESS="--address=127.0.0.1"
8c8
< KUBELET_PORT="--port=10250" #node端口
---
> # KUBELET_PORT="--port=10250"
11c11
< KUBELET_HOSTNAME="--hostname-override=k8s_node_2" #local-hostname
---
> KUBELET_HOSTNAME="--hostname-override=127.0.0.1"
14c14
< KUBELET_API_SERVER="--api-servers=http://10.0.0.29:8080" #k8s api端口
---
> KUBELET_API_SERVER="--api-servers=http://127.0.0.1:8080"
[root@k8s_node_2 kubernetes]# diff config config.bak #修改config文件
22c22
< KUBE_MASTER="--master=http://10.0.0.29:8080" #master地址
---
> KUBE_MASTER="--master=http://127.0.0.1:8080"
[root@k8s_node_1 kubernetes]# systemctl | grep kube
kube-proxy.service loaded active running Kubernetes Kube-Proxy Server #提供容器网络访问
kubelet.service loaded active running Kubernetes Kubelet Server #调用docker,来管理容器的生命周期
检查状态,在master节点查看节点状态
[root@k8s_master kubernetes]# kubectl get nodes
NAME STATUS AGE
10.0.0.30 Ready 36s
10.0.0.31 Ready 45s
K8S核心组件:
etcd:保存整个集群的状态
apiserver:提供了资源操作的统一入口,并提供认证、授权、访问控制、API注册和发现等机制;
controller manager:负责维护集群的状态,比如故障检查、自动扩展、滚动更新。故障检查是指发现某个容器异常时,会自动尝试拉起,如果出现宕机,会自动重建一个新的容器,从而保证业务正常可用
scheduler:负责资源的调度,按照预定的调度策略将pod调度到相应的节点上。
kubelet:负责维护容器的生命周期,会直接调用docker进行生命周期管理,同时也负责volume(CVI),同时也负责网络(vni)的管理
container runtime:负责镜像管理以及pod和容器的真正运行(CRI);
kube-proxy:负责为service提供cluster内部的服务发现和负载均衡;换言之,保证外部能访问容器服务;