ubuntu16.04 安装 kubernetes 1.7.2

安装步骤

  • 安装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 $@

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,923评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,154评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,775评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,960评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,976评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,972评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,893评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,709评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,159评论 1 308
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,400评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,552评论 1 346
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,265评论 5 341
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,876评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,528评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,701评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,552评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,451评论 2 352

推荐阅读更多精彩内容

  • Kubernetes作为容器应用的管理中心,通过对Pod的数量进行监控,并且根据主机或容器失效的状态将新的Pod调...
    辉耀辉耀阅读 4,603评论 0 13
  • 环境规划 手里的环境是四台安装了CentOS 7的主机。环境规划如下: Kubernetes Master 节点:...
    负二贷阅读 3,256评论 6 26
  • 安装k8s Master高可用集群 主机 角色 组件 172.18.6.101 K8S Master Kubele...
    jony456123阅读 8,051评论 0 9
  • 雨后清霜,风湮怅惘,月隐初透凄凉。 倚栏凝处,谁遣夜无央? 灯火珊珊弄影,思量是,不似寻常。 欢颜尽,潇潇冷怨,流...
    若_如初阅读 440评论 4 6
  • 身无饥寒,父母未曾亏我! 人无长进,我何以对父母? 飞机划破天际,终于降落,机舱里的人迫不及待的起身等待机舱门打开...
    温默阅读 281评论 0 0