1. k8s集群的安装


Kubernetes借鉴了Borg的设计理念,比如Pod、Service、Labels和单Pod单IP等。Kubernetes的整体架构跟Borg非常像,如下图所示:

1、架构:

k8s第二回之k8s集群的安装-LMLPHP

除了核心组件,还有一些推荐的Add-ons:

2、环境准备

3台主机,IP地址、主机名的host解析分别如下(最好再做一下SSH免秘钥的登录,实验环境方便操作!)

10.0.0.11  k8s-master
10.0.0.12  k8s-node-1
10.0.0.13  k8s-node-2

3、master节点安装etcd

1. 安装 etcd
yum install etcd -y

2. 编辑配置文件
6行:ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"
21行:ETCD_ADVERTISE_CLIENT_URLS="http://10.0.0.11:2379"

3. 启动并自启服务
systemctl start etcd.service
systemctl enable etcd.service

----------------------------------------------------------
etcdctl set testdir/testkey0 0
etcdctl get testdir/testkey0

etcdctl -C http://10.0.0.11:2379 cluster-health

注: etcd原生支持做集群,etcd服务可以安装在其他服务器做成etcd集群,通过kubernets连接

4、 master节点安装kubernetes

1. 安装kubernetes
yum install kubernetes-master.x86_64 -y

2. 编辑配置
vim /etc/kubernetes/apiserver
8行:  KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0"
11行:KUBE_API_PORT="--port=8080"
14行: KUBELET_PORT="--kubelet-port=10250"
17行:KUBE_ETCD_SERVERS="--etcd-servers=http://10.0.0.11:2379"
23行:KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"

vim /etc/kubernetes/config
22行:KUBE_MASTER="--master=http://10.0.0.11:8080"

3. 重启并自启k8s
systemctl enable kube-apiserver.service
systemctl restart kube-apiserver.service
systemctl enable kube-controller-manager.service
systemctl restart kube-controller-manager.service
systemctl enable kube-scheduler.service
systemctl restart kube-scheduler.service

检查服务是否安装正常

[root@k8s-master ~]# kubectl get componentstatus
NAME                 STATUS    MESSAGE             ERROR
scheduler            Healthy   ok
controller-manager   Healthy   ok
etcd-0               Healthy   {"health":"true"}

k8s第二回之k8s集群的安装-LMLPHP

5、node节点安装kubernetes

#1. 安装kubernetes-node
yum install kubernetes-node.x86_64 -y

#2. 编辑配置
vim /etc/kubernetes/config
22行:KUBE_MASTER="--master=http://10.0.0.11:8080"

vim /etc/kubernetes/kubelet
5行:KUBELET_ADDRESS="--address=0.0.0.0"
8行:KUBELET_PORT="--port=10250"
11行:KUBELET_HOSTNAME="--hostname-override=10.0.0.12"
14行:KUBELET_API_SERVER="--api-servers=http://10.0.0.11:8080"

#3. 启动并自启服务
systemctl enable kubelet.service
systemctl restart kubelet.service
systemctl enable kube-proxy.service
systemctl restart kube-proxy.service
systemctl enable docker

在master节点检查验证:

[root@k8s-master  /etc/kubernetes ]#: kubectl get nodes
NAME        STATUS    AGE
10.0.0.12   Ready     2m
10.0.0.13   Ready     5s

6、所有节点配置flannel网络

flannel和overlay网络类似需要数据库,flannel需要etcd数据库

yum install flannel -y
sed -i 's#http://127.0.0.1:2379#http://10.0.0.11:2379#g' /etc/sysconfig/flanneld

##master节点:
etcdctl mk /atomic.io/network/config   '{ "Network": "172.18.0.0/16","Backend": {"Type": "vxlan"} }'

#所有节点执行
systemctl enable flanneld.service
systemctl restart flanneld.service

##node节点:
systemctl enable flanneld.service
systemctl restart flanneld.service
systemctl  restart  docker
systemctl restart kubelet.service
systemctl restart kube-proxy.service

vim /usr/lib/systemd/system/docker.service
#在[Service]区域下增加一行
ExecStartPost=/usr/sbin/iptables -P FORWARD ACCEPT
systemctl daemon-reload
systemctl restart docker
systemd中有这样功能的配置:
1) ExexStart
2) ExexStartPost

k8s第二回之k8s集群的安装-LMLPHP

最后在master节点验证结果如下:

k8s第二回之k8s集群的安装-LMLPHP

7、配置master为镜像仓库

#所有节点执行
##1. 配置镜像加速
vi /etc/docker/daemon.json
{
"registry-mirrors": ["https://registry.docker-cn.com"],
"insecure-registries": ["10.0.0.11:5000"]
}

##2. 重载docker
systemctl restart docker

#master节点
yum install docker -y
systemctl enable docker
systemctl start docker
docker run -d -p 5000:5000 --restart=always --name registry -v /opt/myregistry:/var/lib/registry  registry
12-20 19:42