使用Rancher简单几步快速搭建Kubernetes集群

Kubernetes (K8s) 是非常精彩的容器编排(管理)软件。
Rancher是一个为多种容器编排软件(?)提供图形界面的工具。
使用Rancher搭建Kubernetes集群,可以很简单。本篇暂只记录HTTP only的环境搭建步骤。

首先准备好机器

Rancher: 2 vCPU, 8+G vRAM, 20+G Disk, with Ubuntu 16.04, Docker 1.12.6
K8s:  Ubuntu 16.04, Docker 1.12.6,CPU, memory 和Disk区别于K8s node 的角色,worker 需要的资源多一些。

Rancher

配置Rancher

Rancher需要一个独立的MYSQL,建议不要起在docker里,如果是,最好mount个本地存储路径。
为MYSQL 创建Rancher需要的database,user。
CREATE DATABASE IF NOT EXISTS cattle1 COLLATE = 'utf8_general_ci' CHARACTER SET = 'utf8';
GRANT ALL ON cattle1.* TO 'cattle'@'%' IDENTIFIED BY 'cattle';
GRANT ALL ON cattle1.* TO 'cattle'@'localhost' IDENTIFIED BY 'cattle';

创建一个新的docker network

docker network create --driver=bridge --subnet=172.27.1.0/24 --ip-range=172.27.1.0/24 --gateway=172.27.1.1 rancher

运行Rancher

docker run -d --restart=unless-stopped --name rancher1 \
 --network rancher --ip 172.27.1.21 \
 -e JAVA_OPTS="-Xmx2560m" \
 rancher/server:v1.5.9 \
 --db-host <db_host> --db-port 3306 \
 --db-user <db_user> --db-pass <db_password> --db-name <db_name> \
 --advertise-address 172.27.1.21
  
docker run -d --restart=unless-stopped --name rancher2 \
 --network rancher --ip 172.27.1.22 \
 -e JAVA_OPTS="-Xmx2560m" \
 rancher/server:v1.5.9 \
 --db-host <db_host> --db-port 3306 \
 --db-user <db_user> --db-pass <db_password> --db-name <db_name> \
 --advertise-address 172.27.1.22

运行HAProxy

docker run -d --restart=unless-stopped --name haproxy \
 -p 80:80 -p 443:443 \
 --network rancher --ip 172.27.1.10 \
 -v ~/haproxy/calix.io.pem:/etc/ssl/cert.pem \
 -v ~/haproxy:/usr/local/etc/haproxy:ro \
 haproxy

HAProxy配置文件参考内容如下(HTTP Only):

global
  maxconn 4096
  ssl-server-verify none
  tune.ssl.default-dh-param 2048
defaults
  mode http
  balance roundrobin
  option redispatch
  option forwardfor
  timeout connect 5s
  timeout queue 5s
  timeout client 36000s
  timeout server 36000s
frontend http-in
  bind *:80
  mode http
  default_backend rancher_servers
  acl is_websocket hdr(Upgrade) -i WebSocket
  acl is_websocket hdr_beg(Host) -i ws
  use_backend rancher_servers if is_websocket
backend rancher_servers
  # Add your Rancher server instance here
  server web1 rancher1:8080 weight 1 maxconn 1024
  server web2 rancher2:8080 weight 1 maxconn 1024
  server web3 rancher3:8080 weight 1 maxconn 1024


Kubernetes

创建kubernetes环境

使用浏览器打开Rancher界面
操作界面到环境管理页面,"Environment -> Manage Environments", 点击"Add Environment"
输入环境名称"name" , 选择"Kubernetes" 模板(template), 点击"Create"创建环境。
为Kubernetest环境增加主机
点击"Environment -> <你的环境名称>"
点击"Infrastructure -> Hosts"
点击"Add Host"
点击"Add Label" 两次,添加labels: etcd=true, orchestration=true (此步骤创建的是k8s结点同时在一台机器启动etcd和master的组件)
重复以上步骤创建其他结点

为workder结点添加区别于master的label: compute=true, lb=true 


等待K8S 服务启动

点击"Environment -> <你的环境名称>", 等待K8S服务启动成功。


配置kubectl

点击"Environment -> <你的环境名称>"
点击"Kubernetes -> CLI"
点击"Generate Config"
将生产的config内容复制到k8s机器的  ~/.kube/config 文件
参考 https://kubernetes.io/docs/tasks/tools/install-kubectl/ 安装启用kubectl


到这里基本配置完成,可能还需要配置DNS和Rancher上Kubernetes管理的Dashbaord。

猜你喜欢

转载自blog.csdn.net/qq_41963758/article/details/80041622