由于K8S的规定,主机名只支持包含 -
和 .
(中横线和点)两种特殊符号,而且主机名不能出现重复。node
配置每台主机的hosts(/etc/hosts),添加host_ip $hostname
到/etc/hosts
文件中。linux
sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
一、Centosnginx
systemctl stop firewalld.service && systemctl disable firewalld.service
二、Ubuntugit
ufw disable
查看时区github
date -R 或者 timedatectl
修改时区docker
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
修改系统语言环境shell
sudo echo 'LANG="en_US.UTF-8"' >> /etc/profile;source /etc/profile
cat >> /etc/sysctl.conf<<EOF net.ipv4.ip_forward=1 net.bridge.bridge-nf-call-iptables=1 net.ipv4.neigh.default.gc_thresh1=4096 net.ipv4.neigh.default.gc_thresh2=6144 net.ipv4.neigh.default.gc_thresh3=8192 EOF sysctl –p
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak cat > /etc/apt/sources.list << EOF deb http://mirrors.aliyun.com/ubuntu/ xenial main deb-src http://mirrors.aliyun.com/ubuntu/ xenial main deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main deb http://mirrors.aliyun.com/ubuntu/ xenial universe deb-src http://mirrors.aliyun.com/ubuntu/ xenial universe deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates universe deb http://mirrors.aliyun.com/ubuntu/ xenial-security main deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security universe EOF
# 定义安装版本 export docker_version=17.03.2 # step 1: 安装必要的一些系统工具 sudo apt-get update sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common bash-completion # step 2: 安装GPG证书 sudo curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - # Step 3: 写入软件源信息 sudo add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" # Step 4: 更新并安装 Docker-CE sudo apt-get -y update version=$(apt-cache madison docker-ce|grep ${docker_version}|awk '{print $3}') # --allow-downgrades 容许降级安装 sudo apt-get -y install docker-ce=${version} --allow-downgrades # 设置开机启动 sudo systemctl enable docker
sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak cat > /etc/yum.repos.d/CentOS-Base.repo << EOF [base] name=CentOS-$releasever - Base - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/ http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/ http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/ gpgcheck=1 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 #released updates [updates] name=CentOS-$releasever - Updates - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/ http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/ http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/ gpgcheck=1 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 #additional packages that may be useful [extras] name=CentOS-$releasever - Extras - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/ http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/ http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/ gpgcheck=1 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 #additional packages that extend functionality of existing packages [centosplus] name=CentOS-$releasever - Plus - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/ http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/ http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/ gpgcheck=1 enabled=0 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 #contrib - packages by Centos Users [contrib] name=CentOS-$releasever - Contrib - mirrors.aliyun.com failovermethod=priority baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/ http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/ http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/ gpgcheck=1 enabled=0 gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7 EOF
# 添加用户(可选) sudo adduser `<user>` # 为新用户设置密码 sudo passwd `<user>` # 为新用户添加sudo权限 sudo echo '<user> ALL=(ALL) ALL' >> /etc/sudoers # 卸载旧版本Docker软件 sudo yum remove docker \ docker-client \ docker-client-latest \ docker-common \ docker-latest \ docker-latest-logrotate \ docker-logrotate \ docker-selinux \ docker-engine-selinux \ docker-engine \ container* # 定义安装版本 export docker_version=17.03.2 # step 1: 安装必要的一些系统工具 sudo yum update -y sudo yum install -y yum-utils device-mapper-persistent-data lvm2 bash-completion # Step 2: 添加软件源信息 sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo # Step 3: 更新并安装 Docker-CE sudo yum makecache all version=$(yum list docker-ce.x86_64 --showduplicates | sort -r|grep ${docker_version}|awk '{print $2}') sudo yum -y install --setopt=obsoletes=0 docker-ce-${version} docker-ce-selinux-${version} # 若是已经安装高版本Docker,可进行降级安装(可选) yum downgrade --setopt=obsoletes=0 -y docker-ce-${version} docker-ce-selinux-${version} # 把当前用户加入docker组 sudo usermod -aG docker `<new_user>` # 设置开机启动 sudo systemctl enable docker
能够配置多条,以数组的形式编写,地址须要添加协议头。编辑/etc/docker/daemon.json
加入如下内容json
{ "registry-mirrors": ["https://z34wtdhg.mirror.aliyuncs.com","https://$IP:$PROT"] }
Docker默认只信任TLS加密的仓库地址(https),全部非https仓库默认没法登录也没法拉取镜像。insecure-registries
字面意思为不安全的仓库,经过添加这个参数对非https仓库进行授信。能够设置多个insecure-registries
地址,以数组形式书写,地址不能添加协议头(http)。编辑/etc/docker/daemon.json
加入如下内容:ubuntu
{ "insecure-registries":["harbor.httpshop.com","bh-harbor.suixingpay.com"] }
存储驱动有不少种,例如:overlay、overlay二、devicemapper等,前二者是OverlayFS类型的,是一个新一代的联合文件系统,相似于AUFS,但速度更快,更加稳定。这里推荐新版的overlay2。centos
要求:
编辑/etc/docker/daemon.json
加入如下内容
{ "storage-driver": "overlay2", "storage-opts": ["overlay2.override_kernel_check=true"] }
容器在运行时会产生大量日志文件,很容易占满磁盘空间。经过配置日志驱动来限制文件大小与文件的数量。 >限制单个日志文件为100M
,最多产生3
个日志文件
{ "log-driver": "json-file", "log-opts": { "max-size": "100m", "max-file": "3" } }
daemon.json的
样例{ "registry-mirrors": ["https://z34wtdhg.mirror.aliyuncs.com"], "insecure-registries":["harbor.httpshop.com","bh-harbor.suixingpay.com"], "storage-driver": "overlay2", "storage-opts": ["overlay2.override_kernel_check=true"] } { "log-driver": "json-file", "log-opts": { "max-size": "100m", "max-file": "3" } }
默认状况下,Rancher会自动生成一个用于加密的自签名证书。从你的Linux主机运行Docker命令来安装Rancher,而不须要任何其余参数:
docker run -d --restart=unless-stopped \ -p 80:80 -p 443:443 \ -v /root/var/log/auditlog:/var/log/auditlog \ -e AUDIT_LEVEL=3 \ rancher/rancher:latest
咱们将使用Nginx做为四层负载均衡器,Nginx会将全部的链接转发到rancher节点之一。注意:不要使用任何rancher节点做为负载均衡器,会出现端口冲突。
须要定制的话,就本身源码安装便可,这里就使用yum或者apt-get
yum install nginx
#复制下面的代码到文本编辑器,保存为nginx.conf。 worker_processes 4; worker_rlimit_nofile 40000; events { worker_connections 8192; } http { server { listen 80; return 301 https://$host$request_uri; } } stream { upstream rancher_servers { least_conn; server <IP_NODE_1>:443 max_fails=3 fail_timeout=5s; server <IP_NODE_2>:443 max_fails=3 fail_timeout=5s; server <IP_NODE_3>:443 max_fails=3 fail_timeout=5s; } server { listen 443; proxy_pass rancher_servers; } }
这里的IP_NODE_一、IP_NODE_二、IP_NODE_3替换成真实环境中的Rancher节点的IP地址。
三、加载配置文件
nginx -s reload
wget https://github.com/rancher/rke/releases/download/v0.1.11/rke_linux-amd64
mv rke_linux-amd64 rke && chmod 755 rke && mv rke /usr/local/bin
rke --version
cat << EOF >>/etc/hosts > 10.10.3.201 rancher-master01 > 10.10.3.202 rancher-master02 > 10.10.3.204 rancher-master03 > EOF
ssh-keygen -t rsa ssh-copy-id -i .ssh/id_rsa.pub root@10.10.3.201 ssh-copy-id -i .ssh/id_rsa.pub root@10.10.3.202 ssh-copy-id -i .ssh/id_rsa.pub root@10.10.3.204
rancher-cluster.yml
文件使用下面的示例建立rancher-cluster.yml
文件,使用建立的3个节点的IP地址替换列表中的IP地址。
nodes: - address: 10.10.3.201 user: root role: [controlplane,worker,etcd] - address: 10.10.3.202 user: root role: [controlplane,worker,etcd] - address: 10.10.3.204 user: root role: [controlplane,worker,etcd] services: etcd: snapshot: true creation: 6h retention: 24h
rke up --config ./rancher-cluster.yml
安装完成后,会产生一个kube_config_rancher-cluster.yml文件,这个是集群的认证文件。
mkdir ~/.kube cat kube_config_rancher-cluster.yml >> ~/.kube/config
经过kubectl 测试链接集群,查看全部节点状态
$ kubectl get nodes NAME STATUS AGE VERSION 10.10.3.201 Ready 19m v1.11.3 10.10.3.202 Ready 19m v1.11.3 10.10.3.204 Ready 19m v1.11.3
Helm在集群上安装tiller
服务以管理charts
. 因为RKE默认启用RBAC, 所以咱们须要使用kubectl
来建立一个serviceaccount
,clusterrolebinding
才能让tiller
具备部署到集群的权限。
kubectl -n kube-system create serviceaccount tiller
kubectl create clusterrolebinding tiller --clusterrole cluster-admin --serviceaccount=kube-system:tiller
二进制安装方法:
一、下载预期的版本 releases
二、解压缩
tar zxvf helm-v2.11.0-linux-amd64.tar.gz
三、把helm二进制文件拷贝到系统环境里
mv linux-amd64/helm /usr/bin/
Helm的服务器端部分Tiller,一般运行在Kubernetes集群内部。可是对于开发,它也能够在本地运行,并配置为与远程Kubernetes集群通讯。
快速集群内安装
helm init --service-account tiller --tiller-image registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.11.0 --stable-repo-url https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
helm init --service-account tiller --canary-image
使用helm repo add
命令添加Rancher chart仓库地址
helm repo add rancher-latest https://releases.rancher.com/server-charts/latest
从Helm stable目录安装cert-manager
helm install stable/cert-manager \ --name cert-manager \ --namespace kube-system
Rancher server设计默认须要开启SSL/TLS配置来保证安全。
helm install rancher-latest/rancher \ --name rancher \ --namespace cattle-system \ --set hostname=rancher.httpshop.com
hostname根据本身状况定义
到此为止Rancher-server 已经安装完成。
一、从"集群"页面,单击"添加集群"。
二、选择“CUSTOM”
三、输入“集群名称”
四、使用“成员角色”配置集群用户的受权
五、使用“集群选项”,选择Docker版本、kubernetes版本、网络组件等相关配置,点击下一步。
六、从主机角色列表中选择节点所要添加的角色。
七、将屏幕上显示的命令复制到剪贴板。
八、使用首选shell登陆Linux主机,例如PuTTy或远程终端链接。运行复制到剪贴板的命令。
九、在Linux主机上运行完命令后,单击“完成”。
您能够导入现有的Kubernetes集群,而后使用Rancher进行管理。
若是现有Kubernetes集群已cluster-admin
定义角色,则必须具备此cluster-admin
权限才能将集群导入Rancher。要应用权限,您须要kubectl create clusterrolebinding cluster-admin-binding --clusterrole cluster-admin --user [USER_ACCOUNT]
在运行kubectl
命令以前运行以导入群集。
一、从“ 群集”页面中,单击“ 添加群集”。
二、选择“Import”
三、输入集群名称
四、使用“ 成员角色”配置群集的用户受权。
五、单击建立
六、将显示的第一个命令复制到剪贴板。
七、使用首选shell(例如PuTTy或远程终端链接)登陆到其中一个群集节点。运行复制到剪贴板的命令。
八、若是收到消息certificate signed by unknown authority
,请将Rancher中显示的第二个命令复制到剪贴板。而后在群集节点上运行该命令。
九、在Linux主机上运行完命令后,单击“完成”。