安装步骤
- 安装docker 1.12
- pull k8s(1.7.2)需要用到的 image
- 安装k8s
- 安装flannel
- 安装dashboard(head)
安装docker
# 执行
./install.sh docker
pull k8s 镜像
# 执行, 直接从线上下载
./install.sh pull
或者直接把我已经打好包的镜像 load 到本地
下载地址: 百度网盘 密码: uxe8
# 执行导入镜像
docker load < kube-proxy-amd64_v1.7.2.tar
docker load < kube-scheduler-amd64_v1.7.2.tar
docker load < kube-controller-manager-amd64_v1.7.2.tar
docker load < kube-apiserver-amd64_v1.7.2.tar
docker load < pause-amd64_3.0.tar
docker load < etcd-amd64_3.0.17.tar
docker load < k8s-dns-sidecar-amd64_1.14.4.tar
docker load < k8s-dns-kube-dns-amd64_1.14.4.tar
docker load < k8s-dns-dnsmasq-nanny-amd64_1.14.4.tar
docker load < flannel-v0.8.0-amd64.tar
docker load < kubernetes-dashboard-amd64-head.tar
安装k8s
下载我们已经编译好的deb包(k8s_bin.zip),并且解压
下载地址: 百度网盘 密码: xprx
# 执行安装
./install install
启动kube
./install start
安装flannel, 把文件保存到当前目录
新建文件: kube-flannel.yaml
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: flannel
namespace: kube-system
---
kind: ConfigMap
apiVersion: v1
metadata:
name: kube-flannel-cfg
namespace: kube-system
labels:
tier: node
app: flannel
data:
cni-conf.json: |
{
"name": "cbr0",
"type": "flannel",
"delegate": {
"isDefaultGateway": true
}
}
net-conf.json: |
{
"Network": "10.244.0.0/16",
"Backend": {
"Type": "vxlan"
}
}
---
apiVersion: extensions/v1beta1
kind: DaemonSet
metadata:
name: kube-flannel-ds
namespace: kube-system
labels:
tier: node
app: flannel
spec:
template:
metadata:
labels:
tier: node
app: flannel
spec:
hostNetwork: true
nodeSelector:
beta.kubernetes.io/arch: amd64
tolerations:
- key: node-role.kubernetes.io/master
operator: Exists
effect: NoSchedule
serviceAccountName: flannel
containers:
- name: kube-flannel
image: registry.cn-hangzhou.aliyuncs.com/szss_k8s/flannel:v0.8.0-amd64
command: [ "/opt/bin/flanneld", "--ip-masq", "--kube-subnet-mgr" ]
securityContext:
privileged: true
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
valueFrom:
fieldRef:
fieldPath: metadata.namespace
volumeMounts:
- name: run
mountPath: /run
- name: flannel-cfg
mountPath: /etc/kube-flannel/
- name: install-cni
image: registry.cn-hangzhou.aliyuncs.com/szss_k8s/flannel:v0.8.0-amd64
command: [ "/bin/sh", "-c", "set -e -x; cp -f /etc/kube-flannel/cni-conf.json /etc/cni/net.d/10-flannel.conf; while true; do sleep 3600; done" ]
volumeMounts:
- name: cni
mountPath: /etc/cni/net.d
- name: flannel-cfg
mountPath: /etc/kube-flannel/
volumes:
- name: run
hostPath:
path: /run
- name: cni
hostPath:
path: /etc/cni/net.d
- name: flannel-cfg
configMap:
name: kube-flannel-cfg
新建文件: kube-flannel-rbac.yaml
, 把文件保存到当前目录
# Create the clusterrole and clusterrolebinding:
# $ kubectl create -f kube-flannel-rbac.yml
# Create the pod using the same namespace used by the flannel serviceaccount:
# $ kubectl create --namespace kube-system -f kube-flannel.yml
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: flannel
rules:
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- apiGroups:
- ""
resources:
- nodes
verbs:
- list
- watch
- apiGroups:
- ""
resources:
- nodes/status
verbs:
- patch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: flannel
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: flannel
subjects:
- kind: ServiceAccount
name: flannel
namespace: kube-system
安装flannel服务
#
./install flannel
启动代理
kubectl proxy --port=8080 --address='0.0.0.0' --accept-hosts='^*$' &
安装dashboard
保存文件 kubernetes-dashboard-head.yaml
到当前目录
注意: 修改 - --apiserver-host=http://masterIP:8080
masterIP为主服务器ip
# Copyright 2015 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Configuration to deploy head version of the Dashboard UI compatible with
# Kubernetes 1.6 (RBAC enabled).
#
# Example usage: kubectl create -f <this_file>
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
k8s-app: kubernetes-dashboard-head
name: kubernetes-dashboard-head
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: kubernetes-dashboard-head
labels:
k8s-app: kubernetes-dashboard-head
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
subjects:
- kind: ServiceAccount
name: kubernetes-dashboard-head
namespace: kube-system
---
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
labels:
k8s-app: kubernetes-dashboard-head
name: kubernetes-dashboard-head
namespace: kube-system
spec:
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
k8s-app: kubernetes-dashboard-head
template:
metadata:
labels:
k8s-app: kubernetes-dashboard-head
spec:
containers:
- name: kubernetes-dashboard-head
image: kubernetesdashboarddev/kubernetes-dashboard-amd64:head
# Image is tagged and updated with :head, so always pull it.
imagePullPolicy: Always
ports:
- containerPort: 9090
protocol: TCP
args:
# Uncomment the following line to manually specify Kubernetes API server Host
# If not specified, Dashboard will attempt to auto discover the API server and connect
# to it. Uncomment only if the default does not work.
- --apiserver-host=http://<masterIP>:8080
livenessProbe:
httpGet:
path: /
port: 9090
initialDelaySeconds: 30
timeoutSeconds: 30
serviceAccountName: kubernetes-dashboard-head
# Comment the following tolerations if Dashboard must not be deployed on master
tolerations:
- key: node-role.kubernetes.io/master
effect: NoSchedule
---
kind: Service
apiVersion: v1
metadata:
labels:
k8s-app: kubernetes-dashboard-head
name: kubernetes-dashboard-head
namespace: kube-system
spec:
type: NodePort
ports:
- port: 80
targetPort: 9090
nodePort: 30000
selector:
k8s-app: kubernetes-dashboard-head
启动dashboard 服务
kubectl create -f ./kubernetes-dashboard-head.yaml
访问dashboard: http://192.168.7.65:30000/
脚本install.sh
以下脚本被部署过程用到的脚本, 把以下脚本保存为 install.sh (修改为可执行文件)
#!/bin/bash
#
# 参考: http://blog.csdn.net/zhuchuangang/article/details/76572157
#set -x
#set -e
#
root=$(id -u)
if [ "$root" -ne 0 ] ;then
echo must run as root
exit 1
fi
HOST_NAME="k8s-node"
# 服务器 ip
HOST_IP_ADDR='192.168.1.xxx' # 修改成服务器IP
#
POD_NETWORK_CIDR='10.244.0.0/16'
###################################安装docker#########################
kube::install_docker(){
echo '执行安装docker...'
which docker > /dev/null 2>&1
i=$?
if [ $i -ne 0 ]; then
apt-get update
apt install -y apt-transport-https ca-certificates
apt-key adv --keyserver hkp://p80.pool.sks-keyservers.net:80 --recv-keys 58118E89F3A912897C070ADBF76221572C52609D
cat > /etc/apt/sources.list.d/Docker.list <<EOF
# Ubuntu Precise
deb https://apt.dockerproject.org/repo ubuntu-precise main
# Ubuntu Trusty
deb https://apt.dockerproject.org/repo ubuntu-trusty main
# Ubuntu Vivid
deb https://apt.dockerproject.org/repo ubuntu-vivid main
# Ubuntu Wily
deb https://apt.dockerproject.org/repo ubuntu-wily main
EOF
apt-get update
apt-get purge lxc-docker*
apt-get install -y docker-engine=1.12.6-0~ubuntu-wily
fi
echo 'docker 已安装'
systemctl enable docker.service && systemctl start docker.service
}
##################################end##################################
################################## 拉取镜像 ############################
# 版本配置
KUBE_VERSION="v1.7.2"
KUBE_PAUSE_VERSION="3.0"
ETCD_VERSION="3.0.17"
DNS_VERSION="1.14.4"
# 镜像源
ALIYUN_URL="registry.cn-hangzhou.aliyuncs.com/szss_k8s"
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!修改 URL
kube::pull_k8s_images(){
images=(kube-proxy-amd64:${KUBE_VERSION}
kube-scheduler-amd64:${KUBE_VERSION}
kube-controller-manager-amd64:${KUBE_VERSION}
kube-apiserver-amd64:${KUBE_VERSION}
pause-amd64:${KUBE_PAUSE_VERSION}
etcd-amd64:${ETCD_VERSION}
k8s-dns-sidecar-amd64:${DNS_VERSION}
k8s-dns-kube-dns-amd64:${DNS_VERSION}
k8s-dns-dnsmasq-nanny-amd64:${DNS_VERSION})
for imageName in ${images[@]} ; do
docker pull $ALIYUN_URL/$imageName
done
}
##################################end##################################
################################## 本地 安装kubeadm ############################
kube::install_bin()
{
echo '执行安装依赖包...'
apt install -y socat
# 判断是否安装 依赖包(只判断kubeadm,其它依赖包暂时忽略)
IS_INSTALL_K8S=`dpkg -l |grep kubeadm | awk 'NR==1{print $2}'`
if [ "$IS_INSTALL_K8S" != 'kubeadm' ];then
# 安装依赖包
dpkg -i ./k8s_bin/*.deb
fi
# 修改配置文件
echo "修改配置文件"
cat > /etc/systemd/system/kubelet.service.d/20-pod-infra-image.conf <<EOF
[Service]
Environment="KUBELET_EXTRA_ARGS=--pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/szss_k8s/pause-amd64:3.0"
EOF
sed -i 's/cgroup-driver=systemd/cgroup-driver=cgroupfs/g' /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
}
################################## 启动服务 ############################
kube::start_kube()
{
echo '启动kube...'
systemctl enable docker
systemctl enable kubelet
systemctl start docker
systemctl start kubelet
#kubeadm会自动检查当前环境是否有上次命令执行的“残留”.如果有,必须清理后再行执行init
kubeadm reset
export KUBE_REPO_PREFIX="registry.cn-hangzhou.aliyuncs.com/szss_k8s"
export KUBE_ETCD_IMAGE="registry.cn-hangzhou.aliyuncs.com/szss_k8s/etcd-amd64:3.0.17"
# 执行 journalctl -xeu kubelet, 查看启动日志
kubeadm init --apiserver-advertise-address=$HOST_IP_ADDR --kubernetes-version=$KUBE_VERSION --pod-network-cidr=$POD_NETWORK_CIDR
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
kubectl get nodes
}
##################################end##################################
################################## flannel ############################
kube::startup_flannel(){
kubectl --namespace kube-system apply -f ./kube-flannel-rbac.yml
kubectl --namespace kube-system apply -f ./kube-flannel.yml
# 验证
echo "验证启动状态"
kubectl get cs
# kubectl proxy --port=8080 --address='0.0.0.0' --accept-hosts='^*$'
}
##################################end##################################
################################## dashboard ############################
kube::startup_dashboard(){
kubectl --namespace kube-system create -f ./kubernetes-dashboard-head.yaml
# 验证
echo "check"
kubectl --namespace=kube-system describe svc kubernetes-dashboard
}
##################################end##################################
main()
{
case $1 in
"docker" )
kube::install_docker
;;
"p" | "pull" )
kube::pull_k8s_images
;;
"i" | "install" )
kube::install_bin
;;
"s" | "start" )
kube::start_kube
;;
"f" | "flannel" )
kube::startup_flannel
;;
"d" | "dashboard" )
kube::startup_dashboard
;;
*)
echo "-> step 1: $0 docker -> 安装docker 版本 1.12.6-0~ubuntu-wily "
echo "-> step 2: $0 pull -> 下载镜像"
echo "-> step 3: $0 install -> 安装k8s"
echo "-> step 4: $0 start -> 启动k8s "
echo "-> step 5: $0 flannel -> 启动flannel "
echo "-> step 6: $0 dashboard -> 启动dashboard "
echo " unkown command $0 $@"
;;
esac
}
main $@