---
title: "kubernetes 安装手册"
author: "hpgary"
date: "2017-09-14T15:19:15Z"
tags: []
categories: ["企业架构"]
slug: "kubernetes"
draft: false
lastmod: "2018-04-24T11:01:05Z"
id: 2393230
url: "https://hpgary.iteye.com/blog/2393230"
---

一、简介

Kubernetes是用于自动化部署，扩容和管理集装箱化应用程序的开源系统。

学习kubernetes技术基础：

必会Docker，会写Dockerfile和Linux日常使用，如果这两个技能不熟，可以先学习这两个技能。

二、安装

a、配置kubernetes yum源：

vim /etc/yum.repos.d/kubernetes.repo 

```
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
       https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
```

b、配置docker-engine源：

vim /etc/yum.repos.d/mritd.repo

```
[mritdrepo]
name=Mritd Repository
baseurl=https://yum.mritd.me/centos/7/x86_64
enabled=1
gpgcheck=0
gpgkey=https://cdn.mritd.me/keys/rpm.public.key
```

 

c、安装Docker

 

```
yum install -y docker-engine
```

d、启动Docker

 

 

```
systemctl enable docker 
systemctl start docker 
```

 e、查看docker info

 

 

```
docker info

 Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
 Library Version: 1.02.107-RHEL7 (2016-06-09)
Logging Driver: journald
#特别关注这一行，如果是cgroupfs就正常了，不是就重启下电脑
Cgroup Driver: cgroupfs
```

 f、安装kubernetes

 

 

```
yum install -y kubeadm kubectl kubelet kubernetes-cni
```

g、关闭selinx

 

 

```
setenforce 0
```

 永久关闭

 

 

```
vi /etc/selinux/config
```

 

 

\# This file controls the state of SELinux on the system.

\# SELINUX= can take one of these three values:

\#     enforcing - SELinux security policy is enforced.

\#     permissive - SELinux prints warnings instead of enforcing.

\#     disabled - No SELinux policy is loaded.

SELINUX=disabled

\# SELINUXTYPE= can take one of three two values:

\#     targeted - Targeted processes are protected,

\#     minimum - Modification of targeted policy. Only selected processes are protected.

\#     mls - Multi Level Security protection.

SELINUXTYPE=targeted

h、关闭防火墙

 

```
systemctl disable firewalld
systemctl stop firewalld
```

i、修改root目录下的.bathrc文件

 

\# .bashrc

\# User specific aliases and functions

 

alias rm='rm -i'

alias cp='cp -i'

alias mv='mv -i'

 

\# Source global definitions

if \[ -f /etc/bashrc ]; then

. /etc/bashrc

fi

export KUBECONFIG=/etc/kubernetes/admin.conf

 

j、添加两条开机启动命令  
vi /etc/rc.d/rc.local

touch /var/lock/subsys/local

echo 1 &gt; /proc/sys/net/bridge/bridge-nf-call-iptables

echo 1 &gt; /proc/sys/net/bridge/bridge-nf-call-ip6tables

 

设置文件开机启动

 chmod +x /etc/rc.d/rc.local

 

k、重启电脑

 

```
reboot
```

三、初始化kubernater

 

a、修改启动参数：

vi /etc/systemd/system/kubelet.service.d/10-kubeadm.conf

\[Service]

Environment="KUBELET\_KUBECONFIG\_ARGS=--kubeconfig=/etc/kubernetes/kubelet.conf --require-kubeconfig=true"

Environment="KUBELET\_SYSTEM\_PODS\_ARGS=--pod-manifest-path=/etc/kubernetes/manifests --allow-privileged=true"

Environment="KUBELET\_NETWORK\_ARGS=--network-plugin=cni --cni-conf-dir=/etc/cni/net.d --cni-bin-dir=/opt/cni/bin"

Environment="KUBELET\_DNS\_ARGS=--cluster-dns=10.96.0.10 --cluster-domain=cluster.local"

Environment="KUBELET\_AUTHZ\_ARGS=--authorization-mode=Webhook --client-ca-file=/etc/kubernetes/pki/ca.crt"

Environment="KUBELET\_CGROUP\_ARGS=--cgroup-driver=cgroupfs"

\# 1.9 后加下面这行

Environment="KUBELET\_EXTRA\_ARGS=--v=2 --fail-swap-on=false --pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google-containers/pause-amd64:3.0"

 

ExecStart=

ExecStart=/usr/bin/kubelet $KUBELET\_KUBECONFIG\_ARGS $KUBELET\_SYSTEM\_PODS\_ARGS $KUBELET\_NETWORK\_ARGS $KUBELET\_DNS\_ARGS $KUBELET\_AUTHZ\_ARGS $KUBELET\_CADVISOR\_ARGS $KUBELET\_CGROUP\_ARGS $KUBELET\_EXTRA\_ARGS

 

b、初始化kubernetes

  1.9 以上需要关闭swap，使用命令  swapoff -a

```
/**k8s 下载国内镜像*/

export KUBE_REPO_PREFIX=registry.cn-hangzhou.aliyuncs.com/google-containers
 export KUBE_HYPERKUBE_IMAGE=registry.cn-hangzhou.aliyuncs.com/google-containers/hyperkube-amd64 
 export  KUBE_DISCOVERY_IMAGE=registry.cn-hangzhou.aliyuncs.com/google-containers/kube-discovery-amd64
export KUBE_ETCD_IMAGE=registry.cn-hangzhou.aliyuncs.com/google-containers/etcd-amd64
```

 

 

```
kubeadm init --kubernetes-version=v1.7.5
```

 c、启动kubernetes

 

```
systemctl start kubelet
systemctl enable kubelet
```

 

d、让Master节点也成为工作节点

```
kubectl taint nodes --all node-role.kubernetes.io/master-
```

e、安装 kubernets 网络

```
kubectl apply -f https://git.io/weave-kube-1.6
```

f、 然后就等待b步的初始化工作完成，可以使用命令查看kuberlet状态

```
kubectl get pods --all-namespaces
kubectl get nodes
kubectl get all
```

 

 四、helloword程序

准备文件 hello.yaml

\```apiVersion: v1

kind: Pod

metadata:

  name: nginx

  labels:

     app: nginx    

spec:

     containers:

        - name: nginx

          image: nginx

          imagePullPolicy: IfNotPresent

          ports:

          - containerPort: 80

     restartPolicy: Always

\---

apiVersion: v1

kind: Service

metadata:

  name: nginx-service

spec:

  type: NodePort

  sessionAffinity: ClientIP

  selector:

    app: nginx

  ports:

    - port: 80

      nodePort: 30080

\```

启动helloword.yaml

```
kubelet create -f hello.yaml
```

 

启动完成后，可访问http://linuxip:30080/  访问

kubernetes 常用命令：

```
kubectl get pods --all-namespaces
kubectl get nodes
kubectl get all
kubectl get pod

kubectl create -f hello.yaml
```