kubernetes最小集群搭建
修改记录
- Master运行服务中,增加kube-proxy服务
1. 环境
Virtual Box
CentOS 7
yum install docker kubernetes flannel
虚拟机网络设置
新建虚拟网络A(在主机网络管理菜单),定义网段192.168.56.1/24,打开DHCP
所有主机创建两块网卡,外网NAT,内网选择虚拟网络vbox1
- 3台虚拟机设置
主机名 | IP | 配置 |
---|---|---|
Master | 192.168.56.4 | 1CPU,1G |
node1 | 192.168.56.5 | 1CPU,1G |
node2 | 192.168.56.6 | 1CPU,1G |
2. 执行环境(无特殊说明,全部都要这样做)
三台主机互通
master ssh passwdless 访问其它两台主机
打开内核转发
添加net.ipv4.ip_forward 到/etc/sysctl.conf
- 修改Docker配置
修改/usr/lib/systemd/system/docker.service
service段增加 ExecStartPost=/sbin/iptables -I FORWARD -s 0.0.0.0/0 -j ACCEPT
修改/etc/sysconfig/docker
OPTIONS="--insecure-registry gcr.io --selinux-enabled=false"
- 禁用防火墙
systemctl stop firewalld
systemctl disable firewalld
- 关闭SElinux
修改/etc/selinux/config
修改SELINUX=disabled
- 修改Docker配置,增加镜像
{
"registry-mirrors":["http://hub-mirror.c.163.com"]
}
- 设置时区与时间同步
yum -y install ntp
timedatectl set-timezone Asia/Shanghai
3. 节点功能规划
Master 部分增加了kube-proxy,第一版忘记加了
主机名 | 运行的服务 |
---|---|
Master | docker etcd kube-apiserver flanneld kube-proxy kube-controller-manager kube-scheduler |
node1 | docker flanneld kubelet kube-proxy |
node2 | docker flanneld kubelet kube-proxy |
4. Master设置
-
设置etcd参数并启动
vim /etc/etcd/config ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379" # 要注意这个参数的IP,这里因为是连接服务,不是监听,所以必须用IP,不能用0.0.0.0 ETCD_ADVERTISE_CLIENT_URLS="http://192.168.56.4:2379"
service etcd restart
其实ETCD_LISTEN_CLIENT_URLS怎么设置都无所谓,只要正常监听就可以。放到这里,可以设置为http://192.168.56.4:2379
-
设置flannel
vim /etc/sysconfig/flanneld FLANNEL_ETCD_ENDPOINTS="http://192.168.56.4:2379" FLANNEL_ETCD_PREFIX="/coreos.com/network"
稍后补充为什么这么设置
- 手动向etcd,设置flannel网段信息
etcdctl set /coreos.com/network/config '{ "Network": "10.254.0.0/16" }'
这里采用kubenetes默认值
- 修改kube-apiserver配置文件
vim /etc/kubernetes/apiserver
KUBE_ETCD_SERVERS="--etcd-servers=http://192.168.56.4:2379"
稍后测试,是否设置为127.0.0.1会影响node1,2的etcd获取。如果仅仅是影响api-server并且etcd与
api-server在同一台服务器,那么127应该也是可以的。
-
修改controller-manager
vim /etc/kubernetes/controller-manager KUBE_CONTROLLER_MANAGER_ARGS="--node-monitor-grace-period=10s --pod-eviction-timeout=10s"
- 编辑kubenetes配置
vim /etc/kubenetes/config
KUBE_MASTER="--master=http://192.168.56.4:8080"
- 设置服务并启动
systemctl enable docker flanneld kube-apiserver kube-controller-manager kube-scheduler
systemctl start flanneld docker kube-proxy kube-apiserver kube-controller-manager kube-scheduler
5. Node节点设置
- 修改kubenetes配置
vim /etc/kubernetes/config
KUBE_MASTER="--master=http://192.168.56.4:8080"
- 修改kubelets配置
vim /etc/kubernetes/kubelet
KUBELET_ADDRESS="--address=0.0.0.0"
KUBELET_HOSTNAME="--hostname-override=192.168.56.5"
KUBELET_API_SERVER="--api-servers=http://192.168.56.4:8080"
-
修改flanneld
vim /etc/sysconfig/flanneld FLANNEL_ETCD_ENDPOINTS="http://192.168.56.4:2379" FLANNEL_ETCD_PREFIX="/coreos.com/network"
-
设置服务并启动
systemctl enable docker flanneld kubelet kube-proxy systemctl start flanneld docker kubelet kube-proxy
6. 验证
- 验证节点
$kubectl get nodes -o wide
NAME STATUS AGE EXTERNAL-IP
192.168.56.5 Ready 1h <none>
192.168.56.6 Ready 1h <none>
- 创建RC文件mysql-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: mysql-rc
spec:
replicas: 1
selector:
app: mysql-pod
template:
metadata:
labels:
app: mysql-pod
spec:
containers:
- name: mysql
image: mysql:5.6
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: "123"
- 创建Service文件mysql-svc.yaml
apiVersion: v1
kind: Service
metadata:
name: mysql-svc
labels:
name: mysql-svc
spec:
type: NodePort
ports:
- port: 3306
protocol: TCP
targetPort: 3306
name: http
nodePort: 30006
selector:
app: mysql-pod
- 运行
kubectl create -f mysql-rc.yaml
kubectl create -f mysql-svc.yaml
- 结果检查
$kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE
mysql-rc-82g2m 1/1 Running 0 1h 172.17.0.2 192.168.56.5
$kubectl get svc -o wide
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes 10.254.0.1 <none> 443/TCP 2d <none>
mysql-svc 10.254.255.244 <nodes> 3306:30006/TCP 1h app=mysql-pod
$kubectl get endpoints
NAME ENDPOINTS AGE
kubernetes 192.168.1.114:6443 2d
mysql-svc 172.17.0.2:3306 1h
全部关联上,创建没问题
$ mysql -uroot -p123 -P30006 -h 192.168.56.5
mysql>
mysql连接成功