kubernetes安装与配置

kubernetes安装与配置

说明

最近在研究docker容器的相关应用,搭建一下可作为docker管理器的kubernetes系统,本文主要描述kubernetes如何安装,与简单的配置,并使用flannel作为网络插件进行搭建。

本文主要介绍使用centos-7.3 64位系统作为基础环境

安装多台虚拟机或物理机,配置相应管理IP

主机名 功能 IP
master master/etcd/node 192.168.1.31
node1 etcd/node 192.168.1.32
node2 etcd/node 192.168.1.33

准备工作

a. 每台设备修改相应hostname,重启终端即生效

[root@centos7 ~]# hostnamectl --static set-hostname master

b. 所有设备关闭防火墙

[root@master ~]# systemctl disable firewalld.service
[root@master ~]# systemctl stop firewalld.service

c. 关闭selinux,修改/etc/selinux/config

SELINUX=disabled

d. 软件包安装,所有设备安装必要的软件包

[root@master ~]# yum install -y etcd docker kubernetes

部署etcd集群

etcd集群在kubernetes中的作用就是一个中心数据库,用于保存一些配置,并且具有高可用的特性。

master etcd配置

a. 修改配置文件/etc/etcd/etcd.conf

# [member]
ETCD_NAME=master
ETCD_DATA_DIR="/var/lib/etcd/master.etcd"
#ETCD_WAL_DIR=""
#ETCD_SNAPSHOT_COUNT="10000"
#ETCD_HEARTBEAT_INTERVAL="100"
#ETCD_ELECTION_TIMEOUT="1000"
ETCD_LISTEN_PEER_URLS="http://0.0.0.0:2380"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379,http://0.0.0.0:4001"
#ETCD_MAX_SNAPSHOTS="5"
#ETCD_MAX_WALS="5"
#ETCD_CORS=""
#
#[cluster]
#ETCD_INITIAL_ADVERTISE_PEER_URLS="http://localhost:2380"
# if you use different ETCD_NAME (e.g. test), set ETCD_INITIAL_CLUSTER value for this name, i.e. "test=http://..."
#ETCD_INITIAL_CLUSTER="default=http://localhost:2380"
ETCD_INITIAL_CLUSTER="master=http://192.168.1.31:2380,node1=http://192.168.1.32:2380,node2=http://192.168.1.33:2380"
#ETCD_INITIAL_CLUSTER_STATE="new"
#ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.1.31:2379,http://192.168.1.31:4001"
#ETCD_DISCOVERY=""
#ETCD_DISCOVERY_SRV=""
#ETCD_DISCOVERY_FALLBACK="proxy"
#ETCD_DISCOVERY_PROXY=""
#

node etcd配置

a. 修改配置文件/etc/etcd/etcd.conf,与master配置类似

# [member]
ETCD_NAME=node1
ETCD_DATA_DIR="/var/lib/etcd/node1.etcd"
#ETCD_WAL_DIR=""
#ETCD_SNAPSHOT_COUNT="10000"
#ETCD_HEARTBEAT_INTERVAL="100"
#ETCD_ELECTION_TIMEOUT="1000"
ETCD_LISTEN_PEER_URLS="http://0.0.0.0:2380"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379,http://0.0.0.0:4001"
#ETCD_MAX_SNAPSHOTS="5"
#ETCD_MAX_WALS="5"
#ETCD_CORS=""
#
#[cluster]
#ETCD_INITIAL_ADVERTISE_PEER_URLS="http://localhost:2380"
# if you use different ETCD_NAME (e.g. test), set ETCD_INITIAL_CLUSTER value for this name, i.e. "test=http://..."
#ETCD_INITIAL_CLUSTER="default=http://localhost:2380"
ETCD_INITIAL_CLUSTER="master=http://192.168.1.31:2380,node1=http://192.168.1.32:2380,node2=http://192.168.1.33:2380"
#ETCD_INITIAL_CLUSTER_STATE="new"
#ETCD_INITIAL_CLUSTER_TOKEN="etcd-cluster"
ETCD_ADVERTISE_CLIENT_URLS="http://192.168.1.32:2379,http://192.168.1.32:4001"
#ETCD_DISCOVERY=""
#ETCD_DISCOVERY_SRV=""
#ETCD_DISCOVERY_FALLBACK="proxy"
#ETCD_DISCOVERY_PROXY=""
#ETCD_STRICT_RECONFIG_CHECK="false"
#ETCD_AUTO_COMPACTION_RETENTION="0"
#

启动etcd

a. 启动etcd并设置开机自动启动

[root@master ~]# systemctl enable etcd
[root@master ~]# systemctl start etcd

集群状态验证

使用etcdctl命令对etcd集群进行状态的验证
a. 查看etcd集群所有成员

[root@master ~]# etcdctl member list

b. 查看etcd cluster健康状态

[root@master ~]# etcdctl cluster-health

或者

[root@node1 ~]# etcdctl -C http://192.168.1.31:2379 cluster-health

配置docker

a. 修改docker配置文件/etc/sysconfig/docker,使其允许从本地registry中拉取镜像

# /etc/sysconfig/docker

# Modify these options if you want to change the way the docker daemon runs
OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false --insecure-registry=192.168.1.31:5000'
if [ -z "${DOCKER_CERT_PATH}" ]; then
    DOCKER_CERT_PATH=/etc/docker
fi

b. 设置docker服务开机自启动并开启服务

[root@master ~]# systemctl enable docker.service
[root@master ~]# systemctl start docker.service

部署kubernetes

a. 修改公共配置文件/etc/kubernetes/config,适用于master和node节点

###
# kubernetes system config
#
# The following values are used to configure various aspects of all
# kubernetes services, including
#
#   kube-apiserver.service
#   kube-controller-manager.service
#   kube-scheduler.service
#   kubelet.service
#   kube-proxy.service
# logging to stderr means we get it in the systemd journal
KUBE_LOGTOSTDERR="--logtostderr=true"

# journal message level, 0 is debug
KUBE_LOG_LEVEL="--v=0"

# Should this cluster be allowed to run privileged docker containers
KUBE_ALLOW_PRIV="--allow-privileged=true"

# How the controller-manager, scheduler, and proxy find the apiserver
KUBE_MASTER="--master=http://192.168.1.31:8080"

kubernetes master配置

在kubernetes master上需要运行以下组件

  • Kubernets API Server
  • Kubernets Controller Manager
  • Kubernets Scheduler

API Server配置

a. 修改配置文件/etc/kubernetes/apiserver

###
# kubernetes system config
#
# The following values are used to configure the kube-apiserver
#

# The address on the local server to listen to.
KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0"

# The port on the local server to listen on.
KUBE_API_PORT="--port=8080"

# Port minions listen on
KUBELET_PORT="--kubelet-port=10250"

# Comma separated list of nodes in the etcd cluster
KUBE_ETCD_SERVERS="--etcd-servers=http://192.168.1.31:2379"

# Address range to use for services
KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16"

# default admission control policies
#KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota"
KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"

# Add your own!
KUBE_API_ARGS=""

b. 启动服务并设置开机自启动

[root@master ~]# systemctl enable kube-apiserver.service
[root@master ~]# systemctl start kube-apiserver.service
[root@master ~]# systemctl enable kube-controller-manager.service
[root@master ~]# systemctl start kube-controller-manager.service
[root@master ~]# systemctl enable kube-scheduler.service
[root@master ~]# systemctl start kube-scheduler.service

kubernetes node配置

在kubernetes node上需要运行以下组件

  • Kubelet
  • Kubernets Proxy

此例中,master也行使node的角色,所以master也启动kubelet和proxy

Kubelet配置

a. 修改配置文件/etc/kubernetes/kubelet

###
# kubernetes kubelet (minion) config

# The address for the info server to serve on (set to 0.0.0.0 or "" for all interfaces)
KUBELET_ADDRESS="--address=0.0.0.0"

# The port for the info server to serve on
# KUBELET_PORT="--port=10250"

# You may leave this blank to use the actual hostname
KUBELET_HOSTNAME="--hostname-override=192.168.1.32"

# location of the api-server
KUBELET_API_SERVER="--api-servers=http://192.168.1.31:8080"

# pod infrastructure container
KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-image=registry.access.redhat.com/rhel7/pod-infrastructure:latest"

# Add your own!
KUBELET_ARGS="--cluster-dns=10.254.0.254 --cluster-domain=cluster.local"

b. 启动服务并设置开机自启动

[root@node1 ~]# systemctl enable kubelet.service
[root@node1 ~]# systemctl start kubelet.service
[root@node1 ~]# systemctl enable kube-proxy.service
[root@node1 ~]# systemctl start kube-proxy.service

flannel网络部署

安装Flannel

[root@master ~]# yum install -y flannel

配置Flannel

a. 配置/etc/sysconfig/flanneld文件

# Flanneld configuration options  

# etcd url location.  Point this to the server where etcd runs
FLANNEL_ETCD_ENDPOINTS="http://192.168.1.31:2379"

# etcd config key.  This is the configuration key that flannel queries
# For address range assignment
FLANNEL_ETCD_PREFIX="/coreos.com/network"

# Any additional options that you want to pass
#FLANNEL_OPTIONS=""

b. 配置etcd,指定flannel给pods分配的子网

[root@master ~]# etcdctl mk /coreos.com/network/config '{ "Network": "172.16.0.0/16" }'

启动flannel

[root@master ~]# systemctl enable flanneld.service
[root@master ~]# systemctl start flanneld.service

ps: 启动flannel之后,需要依次重启docker、kubernetes

你可能感兴趣的:(docker,kubernetes)