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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22penn@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基础软件安装
1
2
3
4
5root@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修改主机名
1
2root@ubuntu:~# echo 192.168.99.177 > /etc/hostname
root@ubuntu:~# hostname 192.168.99.17
docker install
1 | penn@ubuntu:~$ sudo apt-get -y install apt-transport-https ca-certificates |
补充:1
2
3
4penn@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 | penn@ubuntu:~$ tar xzf kubernetes.tar.gz |
k8s binary copy
master:
1
2
3
4
5
6
7
8
9
10
11
12
13penn@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-schedulerminion:
1
2
3
4
5
6
7
8penn@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:
停止docker服务
1
root@ubuntu:~# systemctl stop docker.service
启动etcd服务并设置网段
1
2
3root@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"}'启动flanneld
1
2
3
4
5
6root@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重新初始化docker0网卡,启动docker
1
2
3root@ubuntu:~# source /run/flannel/subnet.env
root@ubuntu:~# ifconfig docker0 ${FLANNEL_SUBNET}
root@ubuntu:~# /usr/bin/dockerd --bip=${FLANNEL_SUBNET} --mtu=${FLANNEL_MTU} &启动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 &
启动kube-controller-manager
1
root@ubuntu:~# /opt/bin/kube-controller-manager --address=0.0.0.0 --master=127.0.0.1:8080 --v=0 &
启动kube-scheduler
1
root@ubuntu:~# /opt/bin/kube-scheduler --address=0.0.0.0 --master=127.0.0.1:8080 --v=0 &
minion:
停止docker服务
1
root@ubuntu:~# systemctl stop docker.service
启动flanneld服务
1
2
3
4
5
6root@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重新初始化docker0网卡,启动docker
1
2
3root@ubuntu:~# source /run/flannel/subnet.env
root@ubuntu:~# ifconfig docker0 ${FLANNEL_SUBNET}
root@ubuntu:~# /usr/bin/dockerd --bip=${FLANNEL_SUBNET} --mtu=${FLANNEL_MTU} &启动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 &
启动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 |