k8s install

Kubernets 是什么?

Kubernetes是Google开源的容器集群管理系统,其提供应用部署、维护、扩展机制等功能,利用Kubernetes能方便地管理跨机器运行容器化的应用。

Kubernets 主要功能:

  • 使用Docker对应用程序包装(package),实例化(instantiate),运行(run)
  • 以集群的方式运行,管理跨机器的容器
  • 解决Docker跨机器容器之间的通讯问题
  • Kubernetes的自我修复机制使得容器集群总是运行在用户期望的状态

Kubernets 搭建

名称 IP 说明
node.1 192.168.99.182 kubernets-minion
node.2 192.168.99.177 kubernets-minion
node.3 192.168.99.142 kubernets-master

基础安装

  1. 更新源

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    penn@ubuntu:~$ sudo su -
    root@ubuntu:~# cp /etc/apt/{sources.list,sources.list.bak}
    root@ubuntu:~# vim /etc/apt/sources.list
    deb-src http://archive.ubuntu.com/ubuntu xenial main restricted
    deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
    deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe
    deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
    deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe
    deb http://mirrors.aliyun.com/ubuntu/ xenial universe
    deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
    deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
    deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
    deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
    deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
    deb http://archive.canonical.com/ubuntu xenial partner
    deb-src http://archive.canonical.com/ubuntu xenial partner
    deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
    deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe
    deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
    deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse
    root@ubuntu:~# apt-get clean
    root@ubuntu:~# apt-get update
  2. 基础软件安装

    1
    2
    3
    4
    5
    root@ubuntu:~# apt-get -y install lrzsz curl wget dstat vim tree git
    root@ubuntu:~# apt-get -y install ntpdate
    root@ubuntu:~# cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
    root@ubuntu:~# crontab -e
    5 0 * * * /usr/sbin/ntpdate -s cn.pool.ntp.org >/dev/null 2>&1
  3. 修改主机名

    1
    2
    root@ubuntu:~# echo 192.168.99.177 > /etc/hostname
    root@ubuntu:~# hostname 192.168.99.17

docker install

1
2
3
4
5
6
7
8
9
10
11
12
penn@ubuntu:~$ sudo apt-get -y install apt-transport-https ca-certificates
penn@ubuntu:~$ sudo apt-key adv --keyserver hkp://ha.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
penn@ubuntu:~$ echo "deb https://apt.dockerproject.org/repo ubuntu-xenial main" | sudo tee /etc/apt/sources.list.d/docker.list
penn@ubuntu:~$ sudo apt-get update
penn@ubuntu:~$ apt-cache policy docker-engine
penn@ubuntu:~$ sudo apt-get -y install linux-image-extra-$(uname -r) linux-image-extra-virtual
penn@ubuntu:~$ apt-cache madison docker-engine
penn@ubuntu:~$ sudo apt-get -y install docker-engine
penn@ubuntu:~$ sudo systemctl enable docker
penn@ubuntu:~$ sudo systemctl restart docker.service
penn@ubuntu:~$ docker -v
Docker version 1.12.3, build 6b644ec

补充:

1
2
3
4
penn@ubuntu:~$ sudo apt-get -y upgrade docker-engine  更新
penn@ubuntu:~$ sudo apt-get -y purge docker-engine 卸载
penn@ubuntu:~$ sudo apt-get -y autoremove --purge docker-engine 卸载和依赖包
penn@ubuntu:~$ rm -rf /var/lib/docker


k8s binary download

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
penn@ubuntu:~$ tar xzf kubernetes.tar.gz
penn@ubuntu:~$ cd kubernetes
penn@ubuntu:~$ cd kubernetes/cluster/ubuntu/
penn@ubuntu:~/kubernetes/cluster/ubuntu$ vim download-release.sh
... ...
# flannel
FLANNEL_VERSION=${FLANNEL_VERSION:-"0.5.5"}
... ...
# ectd
ETCD_VERSION=${ETCD_VERSION:-"2.3.1"}
... ...
# k8s
function get_latest_version_number {
local -r latest_url="https://storage.googleapis.com/kubernetes-release/release/stable.txt"
if [[ $(which wget) ]]; then
wget -qO- ${latest_url}
elif [[ $(which curl) ]]; then
curl -Ss ${latest_url}
else
echo "Couldn't find curl or wget. Bailing out." >&2
exit 4
fi
}
if [ -z "$KUBE_VERSION" ]; then
KUBE_VERSION=$(get_latest_version_number | sed 's/^v//')
fi
penn@ubuntu:~/kubernetes/cluster/ubuntu$ sudo ./download-release.sh

k8s binary copy

  • master:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    penn@ubuntu:~$ sudo su -
    root@ubuntu:~# sudo mkdir /opt/bin
    root@ubuntu:~# cp kubernetes/cluster/ubuntu/binaries/master/* /opt/bin/
    root@ubuntu:~# cp kubernetes/cluster/ubuntu/binaries/kubectl /opt/bin/
    root@ubuntu:~# ls -l /opt/bin/
    total 487708
    -rwxr-xr-x 1 root root 16687744 Dec 7 03:21 etcd
    -rwxr-xr-x 1 root root 14347456 Dec 7 03:21 etcdctl
    -rwxr-xr-x 1 root root 16581152 Dec 7 03:21 flanneld
    -rwxr-x--- 1 root root 151001624 Dec 7 03:21 kube-apiserver
    -rwxr-x--- 1 root root 141028448 Dec 7 03:21 kube-controller-manager
    -rwxr-x--- 1 root root 79537832 Dec 7 03:26 kubectl
    -rwxr-x--- 1 root root 80201536 Dec 7 03:22 kube-scheduler
  • minion:

    1
    2
    3
    4
    5
    6
    7
    8
    penn@ubuntu:~$ sudo su -
    root@ubuntu:~# sudo mkdir /opt/bin
    root@ubuntu:~# cp kubernetes/cluster/ubuntu/binaries/minion/* /opt/bin/
    root@ubuntu:~# ls -l /opt/bin/
    total 212976
    -rwxr-xr-x 1 root root 16581152 Dec 7 03:23 flanneld
    -rwxr-x--- 1 root root 129126768 Dec 7 03:23 kubelet
    -rwxr-x--- 1 root root 72367392 Dec 7 03:23 kube-proxy

k8s deploy

  • master:

    1. 停止docker服务

      1
      root@ubuntu:~# systemctl stop docker.service
    2. 启动etcd服务并设置网段

      1
      2
      3
      root@ubuntu:~# mkdir -p  /opt/data/etcd
      root@ubuntu:~# /opt/bin/etcd --data-dir '/opt/data/etcd' --listen-peer-urls 'http://0.0.0.0:2380,http://0.0.0.0:7001' --listen-client-urls 'http://0.0.0.0:2379,http://0.0.0.0:4001' --initial-advertise-peer-urls 'http://0.0.0.0:2380,http://0.0.0.0:7001' --initial-cluster 'default=http://0.0.0.0:2380,default=http://0.0.0.0:7001' --advertise-client-urls 'http://0.0.0.0:2379,http://0.0.0.0:4001' &
      root@ubuntu:~# /opt/bin/etcdctl set /coreos.com/network/config '{"Network":"172.16.0.0/16"}'
    3. 启动flanneld

      1
      2
      3
      4
      5
      6
      root@ubuntu:~# /opt/bin/flanneld --etcd-endpoints="http://127.0.0.1:4001" -iface=enp0s3 &
      root@ubuntu:~# cat /run/flannel/subnet.env
      FLANNEL_NETWORK=172.16.0.0/16
      FLANNEL_SUBNET=172.16.24.1/24
      FLANNEL_MTU=1472
      FLANNEL_IPMASQ=false
    4. 重新初始化docker0网卡,启动docker

      1
      2
      3
      root@ubuntu:~# source /run/flannel/subnet.env
      root@ubuntu:~# ifconfig docker0 ${FLANNEL_SUBNET}
      root@ubuntu:~# /usr/bin/dockerd --bip=${FLANNEL_SUBNET} --mtu=${FLANNEL_MTU} &
    5. 启动kube-apiserver

      1
      root@ubuntu:~# /opt/bin/kube-apiserver --address=0.0.0.0 --port=8080 --etcd_servers=http://127.0.0.1:4001 --portal_net=172.16.1.0/24 --allow_privileged=false --kubelet_port=10250 --v=0  &
    6. 启动kube-controller-manager

      1
      root@ubuntu:~# /opt/bin/kube-controller-manager --address=0.0.0.0 --master=127.0.0.1:8080  --v=0 &
    7. 启动kube-scheduler

      1
      root@ubuntu:~# /opt/bin/kube-scheduler --address=0.0.0.0 --master=127.0.0.1:8080 --v=0 &
  • minion:

    1. 停止docker服务

      1
      root@ubuntu:~# systemctl stop docker.service
    2. 启动flanneld服务

      1
      2
      3
      4
      5
      6
      root@ubuntu:~# /opt/bin/flanneld --etcd-endpoints="http://192.168.99.142:4001" -iface=enp0s3 &
      root@ubuntu:~# cat /run/flannel/subnet.env
      FLANNEL_NETWORK=172.16.0.0/16
      FLANNEL_SUBNET=172.16.44.1/24
      FLANNEL_MTU=1472
      FLANNEL_IPMASQ=false
    3. 重新初始化docker0网卡,启动docker

      1
      2
      3
      root@ubuntu:~# source /run/flannel/subnet.env
      root@ubuntu:~# ifconfig docker0 ${FLANNEL_SUBNET}
      root@ubuntu:~# /usr/bin/dockerd --bip=${FLANNEL_SUBNET} --mtu=${FLANNEL_MTU} &
    4. 启动kubelet

      1
      root@ubuntu:~# /opt/bin/kubelet --address=0.0.0.0 --port=10250 --api_servers=http://192.168.99.142:8080 --enable_server=true --v=0 &
    5. 启动kube-proxy

      1
      root@ubuntu:~# /opt/bin/kube-proxy --master=http://192.168.99.142:8080 --v=0 &

k8s work test

1
root@ubuntu:~# /opt/bin/kubectl get nodes