一、Rancher安装

1、Rancher HA架构
图片1.png

ip hostname 功能
172.16.0.101 rancher-master-1 etcd、k8s master
172.16.0.102 rancher-master-2 etcd、k8s master
172.16.0.103 rancher-master-3 etcd、k8s master
172.16.0.104 rancehr-worker-1 k8s worker
172.16.0.105 rancehr-worker-2 k8s worker
172.16.0.106 rancehr-worker-3 k8s worker
172.16.0.107 负载均衡器




2、对应节点分别配置主机名

  1. hostnamectl set-hostname rancher-master-1
  2. hostnamectl set-hostname rancher-master-2
  3. hostnamectl set-hostname rancher-master-3
  4. hostnamectl set-hostname rancher-worker-1
  5. hostnamectl set-hostname rancher-worker-2
  6. hostnamectl set-hostname rancher-worker-3


3、分别配置每台主机的hosts(/etc/hosts),添加host_ip $hostname到/etc/hosts文件中。

  1. 172.16.0.101 rancher-master-1
  2. 172.16.0.102 rancher-master-2
  3. 172.16.0.103 rancher-master-3
  4. 172.16.0.104 rancher-worker-1
  5. 172.16.0.104 rancher-worker-2
  6. 172.16.0.104 rancher-worker-3



4、关闭防火墙

  1. systemctl stop firewalld.service && systemctl disable firewalld.service


5、关闭SeLinux

  1. sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config


6、内核性能调优:

  1. cat >> /etc/sysctl.conf<<EOF
  2. net.ipv4.ip_forward=1
  3. net.bridge.bridge-nf-call-iptables=1
  4. net.bridge.bridge-nf-call-ip6tables=1
  5. vm.swappiness=0
  6. vm.max_map_count=655360
  7. EOF
  8. sysctl --system


7、禁用swap

  1. swapoff -a && sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab


8、安装docker、配置容器目录、镜像加速地址等。

  1. https://www.rancher.cn/docs/rancher/v2.x/cn/install-prepare/basic-environment-configuration/

9、添加一个新的用户以创建rke集群:

  1. groupadd docker
  2. useradd rancher -G docker
  3. echo "gl2Hfc^JEF" | passwd --stdin rancher


10、从172.16.0.101配置免密登录到其他节点

  1. su - rancher
  2. ssh-keygen
  3. ssh-copy-id rancher@172.16.0.101
  4. ssh-copy-id rancher@172.16.0.102
  5. ssh-copy-id rancher@172.16.0.103
  6. ssh-copy-id rancher@172.16.0.104
  7. ssh-copy-id rancher@172.16.0.105
  8. ssh-copy-id rancher@172.16.0.106


11、在172.16.0.101安装rke、kubectl、helm

  1. su root
  2. sudo wget https://www.cnrancher.com/download/rke/v0.2.2-rke_linux-amd64
  3. sudo wget https://www.cnrancher.com/download/kubernetes/linux-amd64-v1.13.6-kubectl
  4. sudo wget https://www.cnrancher.com/download/helm/helm-v2.14.0-linux-amd64.tar.gz
  5. sudo cp v0.2.2-rke_linux-amd64 /usr/bin/rke
  6. sudo chmod +x /usr/bin/rke
  7. sudo cp linux-amd64-v1.13.6-kubectl /usr/bin/kubectl
  8. sudo chmod +x /usr/bin/kubectl
  9. sudo tar -xvf helm-v2.14.0-linux-amd64.tar.gz
  10. sudo mv linux-amd64/helm /usr/bin/helm
  11. sudo mv linux-amd64/tiller /usr/bin/tiller
  12. sudo rm -rf linux-amd64/

12、在172.16.0.101创建rancher集群配置文件:

  1. su - rancher
  2. cat > rancher-cluster.yml << EOF
  3. nodes:
  4. - address: 172.16.0.101
  5. user: rancher
  6. role: [controlplane,etcd]
  7. - address: 172.16.0.102
  8. user: rancher
  9. role: [controlplane,etcd]
  10. - address: 172.16.0.103
  11. user: rancher
  12. role: [controlplane,etcd]
  13. - address: 172.16.0.104
  14. user: rancher
  15. role: [worker]
  16. - address: 172.16.0.105
  17. user: rancher
  18. role: [worker]
  19. - address: 172.16.0.106
  20. user: rancher
  21. role: [worker]
  22. services:
  23. etcd:
  24. snapshot: true
  25. creation: 6h
  26. retention: 24h
  27. EOF

13、在172.16.0.101上启动rke集群

  1. rke up --config ./rancher-cluster.yml


14、增删节点、修改12中的配置文件,执行13中的命令
15、在172.16.0.101配置环境变量:

  1. su - root
  2. echo export KUBECONFIG=/home/rancher/kube_config_rancher-cluster.yml >> /etc/profile
  3. source /etc/profile


16、在172.16.0.101配置kubectl命令补全

  1. echo "source <(kubectl completion bash)" >> ~/.bashrc
  2. source ~/.bashrc
  3. su - rancher
  4. echo "source <(kubectl completion bash)" >> ~/.bashrc
  5. source ~/.bashrc


17、在172.16.0.101配置Helm客户端访问权限

  1. (1)、kubectl -n kube-system create serviceaccount tiller
  2. (2)、kubectl create clusterrolebinding tiller --clusterrole cluster-admin --serviceaccount=kube-system:tiller

18、在172.16.0.101安装Helm客户端

  1. helm init --service-account tiller --tiller-image registry.cn-hangzhou.aliyuncs.com/google_containers/tiller:v2.14.0 --stable-repo-url https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts

18、在172.16.0.101添加Chart仓库地址

  1. helm repo add rancher-stable https://releases.rancher.com/server-charts/stable


19、使用权威CA机构颁发的证书安装rancher-server
(1)、创建secret

  1. kubectl create namespace cattle-system
  2. kubectl -n cattle-system \
  3. create secret tls tls-rancher-ingress \
  4. --cert=./cartechfin.com.pem \
  5. --key=./cartechfin.com.key


(2)、在172.16.0.101上使用helm安装rancher-server,域名为financial-k8s.coolops.cn

  1. helm install rancher-stable/rancher \
  2. --name rancher \
  3. --namespace cattle-system \
  4. --set hostname=financial-k8s.coolops.cn \
  5. --set ingress.tls.source=secret


20、配置负载均衡,使用四层负载将ingress节点80、443端口配置到负载均衡,并将域名financial-k8s.coolops.cn解析到负载均衡外网IP。

二、其他设置

1、Ingress-NginX传递自定义header:

  1. kind: ConfigMap
  2. apiVersion: v1
  3. metadata:
  4. name: nginx-configuration
  5. namespace: ingress-nginx
  6. labels:
  7. app: ingress-nginx
  8. data:
  9. enable-underscores-in-headers: "true"


2、配置集群内部使用内网域名访问(非必须)

  1. 1kubectl -n cattle-system patch daemonsets cattle-node-agent --patch '{
  2. "spec": {
  3. "template": {
  4. "spec": {
  5. "hostAliases": [
  6. {
  7. "hostnames":
  8. [
  9. "financial-k8s.coolops.cn"
  10. ],
  11. "ip": "172.16.0.107"
  12. }
  13. ]
  14. }
  15. }
  16. }
  17. }'
  18. 2 kubectl -n cattle-system patch deployments cattle-cluster-agent --patch '{
  19. "spec": {
  20. "template": {
  21. "spec": {
  22. "hostAliases": [
  23. {
  24. "hostnames":
  25. [
  26. "financial-k8s.coolops.cn"
  27. ],
  28. "ip": "172.16.0.107"
  29. }
  30. ]
  31. }
  32. }
  33. }
  34. }'