cenots7单机安装Kubernetes

关于什么是Kubernetes请看另一篇内容:http://www.cnblogs.com/boshen-hzb/p/6482734.html

一、环境搭建

master安装的组件有:

  • docker

  • etcd       可以理解为是k8s的数据库,存储所有节点、pods、网络信息

  • kube-proxy    提供service服务的基础组件

  • kubelet    管理k8s节点的组件,因为这台master同时也是nodes,所以也要安装

  • kube-apiserver   k8s提供API的接口,是整个k8s的核心

  • kube-controller-manager 管理分配资源的组件

  • kube-scheduler    调度资源的组件

  • flanneld     整个k8s的网络组件

nodes安装的组件有:

  • docker

  • kube-proxy

  • kubelet

  • flanneld

由于是单机安装,所以只需要安装master节点就可以。

二、安装步骤

1、关闭防火墙

systemctl stop firewalld.service

systemctl disable firewalld.service

2、更新yum源

cat <<EOF> /etc/yum.repos.d/virt7-docker-common-release.repo
[virt7-docker-common-release]
name=virt7-docker-common-release
baseurl=http://cbs.centos.org/repos/virt7-docker-common-release/x86_64/os/
gpgcheck=0
EOF

3、执行yum -y update

4、执行安装命令

yum install -y etcd kubernetes flannel

如果系统已经存在安装好的docker引擎,上面的命令会报错,解决方法就是:先删除掉docker,因为上面的命令当中,会自动安装docker 

5、配置etcd服务器

/etc/etcd/etcd.conf

# [member]
ETCD_NAME=default
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
ETCD_LISTEN_CLIENT_URLS="http://localhost:2379,http://10.111.131.51:2379"
ETCD_ADVERTISE_CLIENT_URLS="http://10.111.131.51:2379"
启动服务
systemctl start etcd
systemctl enable etcd

检查etcd cluster状态

  [root@localhost ~]# etcdctl cluster-health
  member eb1f405cbdb8358 is healthy: got healthy result from http://localhost:2379
  cluster is healthy
  [root@localhost ~]#



检查etcd集群成员列表,这里只有一台

  [root@localhost ~]# etcdctl member list
  eb1f405cbdb8358: name=default peerURLs=http://10.111.131.51:2380 clientURLs=http://localhost:2379 isLeader=true
  [root@localhost ~]

配置防火墙   
firewall-cmd --zone=public --add-port=2379/tcp --permanent
firewall-cmd --zone=public --add-port=2380/tcp --permanent
firewall-cmd --reload
firewall-cmd --list-all

6、配置master服务器

 1) 配置kube-apiserver配置文件

 /etc/kubernetes/config
 

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=false"

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

/etc/kubernetes/apiserver

KUBE_API_ADDRESS="--insecure-bind-address=127.0.0.1"

# 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://10.111.131.51: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"

# Add your own!
KUBE_API_ARGS=""

 2) 配置kube-controller-manager配置文件

# The following values are used to configure the kubernetes controller-manager

# defaults from config and apiserver should be adequate

# Add your own!
KUBE_CONTROLLER_MANAGER_ARGS=""

 3) 配置kube-scheduler配置文件
 /etc/kubernetes/scheduler

###
# kubernetes scheduler config

# default config should be adequate

# Add your own!
KUBE_SCHEDULER_ARGS="--address=0.0.0.0"

 4) 启动服务

 service kube-apiserver restart

 service kube-controller-manager restart

 service kube-scheduler restart

以下是将本机当作node节点的配置

 5)配置etcd

[root@localhost ~]# etcdctl set /k8s/network/config '{"Network": "10.111.131.0/24"}'
{"Network": "10.111.131.0/24"}
[root@localhost ~]#

 6)配置node的network,本实例采用flannel方式来配置,如需其他方式,请参考Kubernetes官网。

 先用ifconfig查找到本机网络10.111.131.51对应的网卡:

eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.111.131.51  netmask 255.255.255.0  broadcast 10.111.131.255
        inet6 fe80::20c:29ff:fedf:f83b  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:df:f8:3b  txqueuelen 1000  (Ethernet)
        RX packets 161627  bytes 208913594 (199.2 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 113194  bytes 72446146 (69.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

打开/etc/sysconfig/flanneld,进行以下配置

# Flanneld configuration options  

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

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

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

 7)配置node的kube-proxy

/etc/kubernetes/config (注意:本机是master也是node,因为前面已经配过,所以这里不用再配)

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=false"

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

/etc/kubernetes/proxy

进行以下配置

 config should be adequate

# Add your own!
KUBE_PROXY_ARGS="--bind=address=0.0.0.0"

 8) 配置node的kubelet

在/etc/hosts下加入:10.111.131.51 k8s-master

/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=127.0.0.1"

# 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=k8s-master"

# location of the api-server
KUBELET_API_SERVER="--api-servers=http://10.111.131.51: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=""

 9) 启动node服务

  service flanneld start

  service kube-proxy start

  service kubelet start

 10)至此,整个Kubernetes单机版环境搭建完,下面创建一个deployment进行测试

mysql-deployment.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mysql-test
spec:

  replicas: 1
  template:
    metadata:
      labels:
        app: redis
    spec:
      containers:
      - name: mysql-test
        image: 72.16.101.192/common/mysql:5.6
        resources:
          requests:
            cpu: 100m
            memory: 100Mi
        ports:
        - containerPort: 3306

在机器上执行以下命令:

[root@localhost ~]#  kubectl create -f mysql-deployment.yaml 
deployment "mysql-test" created
[root@localhost ~]# 

查看刚才创建的deployment信息。

[root@localhost ~]# kubectl get deploy
NAME         DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGE
mysql-test   1         0         0            0           1m
[root@localhost ~]#

出现上面的信息,表示Kubernetes安装成功。

原文地址:https://www.cnblogs.com/boshen-hzb/p/6639865.html