首页 > 代码库 > Kubernetes单机安装部署

Kubernetes单机安装部署

系统环境:

Ubuntu 16.04.2 LTS

软件环境:

Docker 1.12.6

Go 1.8.3

Etcd 3.1.8

Flannel 0.7.1

Kubernetes master 1.7.0-alpha

技术分享

 安装步骤:

1 安装Docker

1) apt-get update

2) apt-get upgrade

3) apt-get install docker.io

4) service docker restart

5) docker run hello-world

6) docker run -it ubuntu bash

 

2 安装Go
1) 下载Go语言压缩包
wget https://golang.org/dl/go1.8.3.linux-amd64.tar.gz
2) 解压压缩包
tar -C /usr/local -xzf go1.8.3.linux-amd64.tar.gz
3) 添加下列环境变量到/etc/profile(for a system-wide installation) or $HOME/.profile
export PATH=$PATH:/usr/local/go/bin
4) 编写hello.Go文件,填写下列内容,放到$HOME/go/src/hello文件夹中
package main
import "fmt"
func main() {
    fmt.Printf("hello, world\n")
}
5) 测试Go语言环境,$HOME/go/默认为/root/go/
go run ~/go/src/hello/hello.go
 
3 安装Etcd
1) 下载Etcd压缩包
wget https://github.com/coreos/etcd/releases/download/v3.1.8/etcd-v3.1.8-linux-amd64.tar.gz

2) 解压Etcd压缩包

tar -xzf etcd-v3.1.8-linux-amd64.tar.gz

3) 拷贝Etcd可执行程序

cd etcd-v3.1.8-linux-amd64

cp etcd etcdctl /usr/bin

 

4 安装Flannel
1) 下载Flannel压缩包
wget https://github.com/coreos/flannel/releases/download/v0.7.1/flannel-v0.7.1-linux-amd64.tar.gz

2) 解压Flannel压缩包

tar -xzf flannel-v0.7.1-linux-amd64.tar.gz

3) 拷贝Flannel可执行程序

cd flannel-v0.7.1-linux-amd64

cp flannelid mk-docker-opts.sh /usr/bin

 

5 安装K8S
1) 获取K8S Master版本(1.7.0-alpha)
git clone https://github.com/kubernetes/kubernetes.git

2) 编译K8S

cd kubernetes

make quick-release

编译完成后:

K8S Server文件:~/go/src/kubernetes/_output/release-stage/server/linux-amd64/kubernetes/server/bin

K8S Client文件: ~/go/src/kubernetes/_output/release-stage/client/linux-amd64/kubernetes/client/bin

3) 拷贝K8S Server和Client可执行文件

cd ~/go/src/kubernetes/_output/release-stage/server/linux-amd64/kubernetes/server/bin

cp * /usr/bin

cd ~/go/src/kubernetes/_output/release-stage/client/linux-amd64/kubernetes/client/bin

cp * /usr/bin

注: 下述操作中的10.229.43.237请替换为你安装K8S的主机的IP地址 

6 启动和测试Etcd
K8S使用Etcd保存Master和Node节点的信息,所以启动K8S之前需要先启动Etcd
1) 启动Etcd
etcd -initial-advertise-peer-urls http://10.229.43.237:2380 -listen-peer-urls http://10.229.43.237:2380 -listen-client-urls http://10.229.43.237:2379,http://127.0.0.1:2379 -advertise-client-urls http://10.229.43.237:2379 
2) 读写Etcd Key和Value
etcdctl set foo bar

etcdctl get foo

 

7 启动Flannel并关联Docker
1) 启动Flannel
flanneld
 
2) 设置Etcd配置Flannel
etcdctl set /coreos.com/network/config ‘{"Network":"172.17.0.0/16"}‘
etcdctl set /coreos.com/network/subnets/172.17.10.0-24 ‘{"PublicIP":"10.229.43.237"}‘
 
3) 设置Flannel关联Docker

mk-docker-opts.sh -i
source /run/flannel/subnet.env
rm /var/run/docker.pid
ifconfig docker0 ${FLANNEL_SUBNET}
service docker restart

  
8 启动K8S
1) 启动kube-apiserver
kube-apiserver --address=0.0.0.0  --insecure-port=8080 --service-cluster-ip-range=‘10.10.10.1/24‘ --log_dir=/usr/local/kubernete_test/logs/kube --kubelet_port=10250 --v=0 --logtostderr=false --etcd_servers=http://10.229.43.237:2379 --allow_privileged=false 
 
2) 启动kube-controller-manager
kube-controller-manager  --v=0 --logtostderr=false --log_dir=/usr/local/kubernete_test/logs/kube --master=10.229.43.237:8080
 
3) 启动kube-scheduler 
kube-controller-manager  --v=0 --logtostderr=false --kube-scheduler  --master=‘10.229.43.237:8080‘ --v=0  --log_dir=/usr/local/kubernete_test/logs/kube
 
4) 测试master节点是否正常启动
kubectl get componentstatuses
 
5) 启动kube-proxy 
kube-proxy  --logtostderr=false --v=0 --master=http://10.229.43.237:8080
 
6) 启动kubelet  
kubelet  --logtostderr=false --v=0 --allow-privileged=false  --log_dir=/usr/local/kubernete_test/logs/kube  --address=0.0.0.0  --port=10250  --hostname_override=10.229.43.237  --api_servers=http://10.229.43.237:8080
 
7) 在Node上设置集群Context
kubectl config set-cluster test-cluster --server=http://10.229.43.237:8080
kubectl config set-context test-cluster --cluster=test-cluster
kubectl config use-context test-cluster 
 
8) 准备kubernetes/pause镜像
由于K8S Pod需要Pause镜像来启动Pause容器,所以需要准备kubernetes/pause镜像
docker pull docker.io/kubernetes/pause
docker tag f9d5de079539 gcr.io/google_containers/pause:2.0
 
这里相当于是把docker.io重命名为gcr.io,f9d5de079539为拉下来的镜像ID(docker images)
 
9) 确认K8S是否启动成功
kubectl get nodes
  
9 部署Nginx集群
1) 创建Nginx Pods
kubectl run nginx --image=nginx --port=80  --replicas=5
kubectl get pods
 
2) 创建nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
  name: nginx
  labels:
    app: nginx
spec:
  ports:
  - port: 80
    targetPort: 9090
  selector:
    name: nginx 
3) 根据Pods创建Service
kubectl create -f nginx-service.yaml
kubectl get services
 
到此K8S集群部署完成。

Kubernetes单机安装部署