一.环境准备
三台vm虚拟机(centos7)
192.168.125.164 node1
192.168.125.165 node2
192.168.125.166 node3
1.各节点修改各自的主机名(hostnamectl set-hostname node1),并将上面的内容添加到/etc/hosts中。
2.各节点安装kubernetes #yum -y install kubernetes ,目前是kubernetes.x86_64 0:1.5.2-0.5.gita552679.el7版本
3.在master节点上安装etcd #yum -y install etcd ,目前是3.1.3-1.el7版本
4.将各节点的防火墙关闭
#systemctl disable firewalld
#systemctl stop firewalld
二.配置
1.每个节点修改/etc/kubernetes/config
KUBE_MASTER="--master=http://192.168.125.164:8080"
KUBE_ETCD_SERVERS="--etcd_servers=http://192.168.125.164:4001"
2.在master节点上vi /etc/kubernetes/apiserver
KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0"
KUBE_API_PORT="--port=8080"
3.在master启动服务
for SERVICES in etcd kube-apiserver kube-controller-manager kube-scheduler;do
systemctl restart $SERVICES
systemctl enable $SERVICES
systemctl status $SERVICES
done
将上面的保存成sh执行
#ps -ef|grep kube //从进程中查看前面的参数配置是否生效
4.在node节点修改vi /etc/kubernetes/kubelet
KUBELET_ADDRESS="--address=0.0.0.0"
KUBELET_PORT="--port=10250" (可不用解开注释,默认即可)
KUBELET_HOSTNAME="--hostname-override=node2"
KUBELET_API_SERVER="--api-servers=http://192.168.125.164:8080"
5.node节点机启动kube-proxy和kubelet,docker服务即可。
启动中可通过#tail -f /var/log/messages |grep kube来排查问题
三. 验证
在master节点执行kubectl get nodes ,查看到节点注册成功,则表明系统安装正常
#kubectl cluster-info // 查看cluster info信息
四.master上的kubectrl命令为管理集群的命令
其中用的最多的就是资源管理命令:get //list资源,也是查询
describe //查询具体,相信的信息
create
replace
patch
delete
#kubectl describe --help //查看帮助
#kubectl get namespace //这里的namespace不是用来隔离的,而是用来实现不用租户的资源配额。
五.发布个nginx应用
vi nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
#kubectl create -f nginx-pod.yaml
此时提示
Error from server (ServerTimeout): error when creating "nginx-pod.yaml": No API token found for service account "default", retry after the token is automatically created and added to the service account
解决:
修改master节点上的/etc/kubernetes/apiserver 文件,去掉vi /etc/kubernetes/apiserver 中的“ServiceAccount”,保存,重启api-server服务(#systemctl restart kube-apiserver)
然后可以通过# kubectl get pods 查看pod进展
也可以通过# kubectl describe pods nginx 查看详细
注意:create资源之前,需要先将/etc/kubernetes/kubelet中的那个 镜像 先下载到各个节点上(如registry.access.redhat.com/rhel7/pod-infrastructure:latest)。
六.创建Kubernetes覆盖网络
Kubernetes的网络模型要求每个Pod都拥有一个扁平化共享网络命名空间的IP,Pod之间能够通过跨主机通信,要实现这种需求,需要在Kubernetes集群中创建一个覆盖网络来联通各个节点,目前可以通过第三方网络插件来创建覆盖网络,如Flannel,Open vSwitch(OVS)和 calico.
Flannel
i.在每台节点进行安装#yum install flannel ,当前是flannel.x86_64 0:0.7.0-1.el7版本
ii.修改配置文件 /etc/sysconfig/flanneld,
FLANNEL_ETCD_ENDPOINTS="http://192.168.125.164:2379" (如果集群每个节点都安装了etcd的话这里就可以保持默认,我这里是就在master节点安装了etcd)
FLANNEL_ETCD_PREFIX="/atomic.io/network" --------可以保持默认
iii.执行etcdctl mkdir /atomic.io/network && etcdctl set /k8s/network/config '{"Network":"172.200.0.0/16"}'(在etcd中创建目录及config配置文件,docker运行的container实例的地址,都在 172.200.0.0/16网段中)
flanneld会读取/atomic.io/network目录中config值,然后接管docker的地址分配,并把docker和宿主机之间的网络桥接起来。、
注意:如果执行命令出现
Error: client: etcd cluster is unavailable or misconfigured
error #0: dial tcp 127.0.0.1:2379: getsockopt: connection refused
error #1: dial tcp 127.0.0.1:4001: getsockopt: connection refuse
解决办法:vi /etc/etcd/etcd.conf
ETCD_LISTEN_CLIENT_URLS="http://127.0.0.1:2379,http://192.168.125.164:2379",保存,重启etcd服务
vi.执行 #systemctl enable flanneld.service
#systemctl stop docker
#systemctl star flanneld.service
#systemctl star docker
注意:后续确保系统启动时flannel需要遭遇docker服务(可将docker服务systemctl disable docker)
集成calico网络
i.准备工作:
. 每个节点修改/etc/kubernetes/config中的 KUBE_ALLOW_PRIV="--allow-privileged=true" ,不然发布calico.yaml时报错
The DaemonSet "calico-node" is invalid: spec.template.spec.containers[0].securityContext.privileged: Forbidden: disallowed by policy
.因为用到quay.io/calico/node:v1.0.2,calico/cni:v1.5.6镜像,为了加快部署速度,可以现在每个节点docker pull下来。
ii.配置安装
.下载calico.yaml,地址为http://docs.projectcalico.org/v2.0/getting-started/kubernetes/installation/hosted/calico.yaml
.修改calico.yaml文件中的etcd的地址
etcd_endpoints: "http://192.168.125.164:2379"
.通过#kubectl apply -f calico.yaml部署上去
七.kubernetes相关组件安装
1.dashboard
#kubectl create -f https://git.io/kube-dashboard-no-rbac
出现的问题“Error syncing pod, skipping: failed to "StartContainer" for "kubernetes-dashboard" with ImagePullBackOff: "Back-off pulling image \"gcr.io/google_containers/kubernetes-dashboard-amd64:v1.6.0\""”
解决办法:
方法一:
方法二:修改hosts文件(这里我用的是“61.91.161.217 gcr.io”,但是可能会失效)
方法三:从其他源下载该容器,然后打tag为“gcr.io/。。。。”