由於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主機上運行完命令後,單擊「完成」。