事先准备:
Nginx,作为负载均衡使用。nginx需要有stream模块
# 安装过程略,配置文件如下
# 按需更换ip
[root@nginx ~]# cat /etc/nginx/nginx.conf
worker_processes auto;
worker_rlimit_nofile 40000;
events {
worker_connections 8192;
}
stream {
upstream rancher_servers_http {
least_conn;
server 10.0.0.30:80 max_fails=3 fail_timeout=5s;
server 10.0.0.31:80 max_fails=3 fail_timeout=5s;
server 10.0.0.32:80 max_fails=3 fail_timeout=5s;
}
server {
listen 80;
proxy_pass rancher_servers_http;
}
upstream rancher_servers_https {
least_conn;
server 10.0.0.30:443 max_fails=3 fail_timeout=5s;
server 10.0.0.31:443 max_fails=3 fail_timeout=5s;
server 10.0.0.32:443 max_fails=3 fail_timeout=5s;
}
server {
listen 443;
proxy_pass rancher_servers_https;
}
}
# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
# 关闭selinux
setenforce 0
grep SELINUX /etc/selinux/config
SELINUX=disabled
# 关闭swap
swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
cat > /etc/hosts << EOF
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.16.1.30 rancher0
172.16.1.31 rancher1
172.16.1.32 rancher2
EOF
# 文件打开数
echo -e "root soft nofile 65535\nroot hard nofile 65535\n* soft nofile 65535\n* hard nofile 65535\n" >> /etc/security/limits.conf
sed -i 's#4096#65535#g' /etc/security/limits.d/20-nproc.conf
# kernel
cat >> /etc/sysctl.conf<
# 在第一台机器rancher0执行即可。
groupadd docker
useradd rancher -G docker
echo "123456" | passwd --stdin rancher
#授权
rancher ALL=(ALL) NOPASSWD: ALL
su - rancher
ssh-keygen
ssh-copy-id [email protected]
ssh-copy-id [email protected]
ssh-copy-id [email protected]
# 1) 安装必要的一些系统工具
yum install -y yum-utils device-mapper-persistent-data lvm2
# 2) 添加软件源信息
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# 3) 更新并安装 Docker-CE
yum makecache fast
yum -y install docker-ce
# 4) 修改为国内镜像源
mkdir /etc/docker
cat >> /etc/docker/daemon.json<
下载rke文件并移动到/usr/sbin
[root@rancher0 ~]# wget https://github.com/rancher/rke/releases/download/v1.1.2/rke_linux-amd64 \
&& chmod +x rke_linux-amd64 \
&& mv rke_linux-amd64 /usr/bin/rke
安装kubectl
[root@rancher0 ~]# wget https://docs.rancher.cn/download/kubernetes/linux-amd64-v1.18.3-kubectl \
&& chmod +x linux-amd64-v1.18.3-kubectl \
&& mv linux-amd64-v1.18.3-kubectl /usr/bin/kubectl
安装helm
[root@rancher0 ~]# wget https://docs.rancher.cn/download/helm/helm-v3.0.3-linux-amd64.tar.gz \
&& tar xf helm-v3.0.3-linux-amd64.tar.gz \
&& cd linux-amd64 \
&& mv helm /usr/sbin/
[root@rancher0 opt]# su - rancher
[rancher@rancher0 ~]$ pwd
/home/rancher
[rancher@rancher0 ~]$ vim rancher-cluster.yml
nodes:
- address: 10.0.0.30
internal_address: 172.16.1.30
user: rancher
role: [controlplane, worker, etcd]
- address: 10.0.0.31
internal_address: 172.16.1.31
user: rancher
role: [controlplane, worker, etcd]
- address: 10.0.0.32
internal_address: 172.16.1.32
user: rancher
role: [controlplane, worker, etcd]
services:
etcd:
snapshot: true
creation: 6h
retention: 24h
# 当使用外部 TLS 终止,并且使用 ingress-nginx v0.22或以上版本时,必须。
ingress:
provider: nginx
options:
use-forwarded-headers: "true"
常用RKE节点选项
选项 | 必填 | 描述 |
---|---|---|
address | 是 | 公用 DNS 或 IP 地址 |
user | 是 | 可以运行 docker 命令的用户 |
role | 是 | 分配给节点的 Kubernetes 角色列表 |
internal_address | 是 | 内部集群流量的专用 DNS 或 IP 地址 |
ssh_key_path | 否 | 用于对节点进行身份验证的 SSH 私钥的路径(默认为~/.ssh/id_rsa) |
[rancher@rancher0 ~]$ rke up --config ./rancher-cluster.yml
完成后,它应该以这样一行结束: Finished building Kubernetes cluster successfully.
#安装kubectl
[root@rancher0 ~]# wget https://docs.rancher.cn/download/kubernetes/linux-amd64-v1.18.3-kubectl
[root@rancher0 ~]# chmod +x linux-amd64-v1.18.3-kubectl
[root@rancher0 ~]# mv linux-amd64-v1.18.3-kubectl /usr/sbin/kubectl
[rancher@rancher0 ~]$ mkdir -p /home/rancher/.kube
[rancher@rancher0 ~]$ cp kube_config_rancher-cluster.yml $HOME/.kube/config
[rancher@rancher0 ~]$ kubectl get nodes
NAME STATUS ROLES AGE VERSION
10.0.0.30 Ready controlplane,etcd,worker 58m v1.17.5
10.0.0.31 Ready controlplane,etcd,worker 58m v1.17.5
10.0.0.32 Ready controlplane,etcd,worker 58m v1.17.5
[rancher@rancher0 ~]$ kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
ingress-nginx default-http-backend-67cf578fc4-v8jwc 0/1 ContainerCreating 0 58m
ingress-nginx nginx-ingress-controller-k84wz 1/1 Running 0 58m
ingress-nginx nginx-ingress-controller-psnkg 1/1 Running 0 58m
ingress-nginx nginx-ingress-controller-sxlvw 1/1 Running 0 58m
kube-system canal-b2h58 2/2 Running 0 58m
kube-system canal-qdxc2 2/2 Running 0 58m
kube-system canal-qtx47 2/2 Running 0 58m
kube-system coredns-7c5566588d-2tprw 1/1 Running 0 58m
kube-system coredns-7c5566588d-66k9h 1/1 Running 0 55m
kube-system coredns-autoscaler-65bfc8d47d-pmbv6 1/1 Running 0 58m
kube-system metrics-server-6b55c64f86-qhpzg 1/1 Running 0 58m
kube-system rke-coredns-addon-deploy-job-xkqxf 0/1 Completed 0 58m
kube-system rke-ingress-controller-deploy-job-dndcq 0/1 Completed 0 58m
kube-system rke-metrics-addon-deploy-job-kh9vv 0/1 Completed 0 58m
kube-system rke-network-plugin-deploy-job-4k4wp 0/1 Completed 0 58m
将以下文件的副本保存在安全的位置:
rancher-cluster.yml: RKE 集群配置文件。
kube_config_rancher-cluster.yml: 集群的Kubeconfig 文件,此文件包含用于访问集群的凭据。
rancher-cluster.rkestate: Kubernetes 集群状态文件,此文件包含用于完全访问集群的凭据。
# kubectl
wget https://docs.rancher.cn/download/kubernetes/linux-amd64-v1.18.3-kubectl
chmod +x linux-amd64-v1.18.3-kubectl
mv linux-amd64-v1.18.3-kubectl /usr/share/kubectl
#heml
wget https://docs.rancher.cn/download/helm/helm-v3.0.3-linux-amd64.tar.gz
[rancher@rancher0 ~]$ tar xf helm-v3.0.3-linux-amd64.tar.gz
[rancher@rancher0 ~]$ cd linux-amd64/
[rancher@rancher0 linux-amd64]$ sudo mv helm /usr/sbin/
[rancher@rancher0 linux-amd64]$ helm repo add rancher-stable https://releases.rancher.com/server-charts/stable
"rancher-stable" has been added to your repositories
[rancher@rancher0 linux-amd64]$ kubectl create namespace cattle-system
namespace/cattle-system created
如果使用自己的证书文件 ingress.tls.source=secret或者使用外部 TLS 负载均衡器可以跳过此步骤。
# 安装 CustomResourceDefinition 资源
kubectl apply --validate=false -f https://github.com/jetstack/cert-manager/releases/download/v0.15.0/cert-manager.crds.yaml
# **重要:**
# 如果您正在运行 Kubernetes v1.15 或更低版本,
# 则需要在上方的 kubectl apply 命令中添加`--validate=false`标志,
# 否则您将在 cert-manager 的 CustomResourceDefinition 资源中收到与
# x-kubernetes-preserve-unknown-fields 字段有关的验证错误。
# 这是一个良性错误,是由于 kubectl 执行资源验证的方式造成的。
# 为 cert-manager 创建命名空间
kubectl create namespace cert-manager
# 添加 Jetstack Helm 仓库
helm repo add jetstack https://charts.jetstack.io
# 更新本地 Helm chart 仓库缓存
helm repo update
# 安装 cert-manager Helm chart
helm install \
cert-manager jetstack/cert-manager \
--namespace cert-manager \
--version v0.15.0
安装完 cert-manager 后,您可以通过检查 cert-manager 命名空间中正在运行的 Pod 来验证它是否已正确部署:
kubectl get pods --namespace cert-manager
NAME READY STATUS RESTARTS AGE
cert-manager-5c6866597-zw7kh 1/1 Running 0 2m
cert-manager-cainjector-577f6d9fd7-tr77l 1/1 Running 0 2m
cert-manager-webhook-787858fcdb-nlzsq 1/1 Running 0 2m
(其他方式可见官网)
-- 将hostname设置为您指向负载均衡器的 DNS 名称。
-- 如果您在安装 alpha 版本,需要把--devel 选项添加到下面到 Helm 命令中。
-- 要安装指定版本的 Rancher,请使用--version选项,例如:--version 2.3.6。
域名加入host解析,每台都要加入包括nginx和本地电脑
echo "172.16.1.29 rancher.com" >> /etc/hosts
helm install rancher rancher-stable/rancher \
--namespace cattle-system \
--set hostname=rancher.com
检查 Rancher Server 是否运行成功:
kubectl -n cattle-system rollout status deploy/rancher
Waiting for deployment "rancher" rollout to finish: 0 of 3 updated replicas are available...
deployment "rancher" successfully rolled out
通过运行以下命令来检查 deployment 的状态:
kubectl -n cattle-system get deploy rancher
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
rancher 3 3 3 3 3m
#DESIRED和AVAILABLE应该显示相同的个数。
本地电脑host映射后,访问:https://rancher.com
完成登录
若出现以下错误:
解决办法:
由于我们通过hosts文件来添加映射,所以需要为Agent Pod添加主机别名(/etc/hosts):
kubectl -n cattle-system patch deployments cattle-cluster-agent --patch '{
"spec": {
"template": {
"spec": {
"hostAliases": [
{
"hostnames":
[
"rancher.com"
],
"ip": "10.0.0.29"
}
]
}
}
}
}'
kubectl -n cattle-system patch daemonsets cattle-node-agent --patch '{
"spec": {
"template": {
"spec": {
"hostAliases": [
{
"hostnames":
[
"rancher.com"
],
"ip": "10.0.0.29"
}
]
}
}
}
}'
详细说明见官网:官网.docs