工作环境
虚拟机软禁 VirtualBox
系统 Centos7 配置 2 core 2G 3台
1.Docker安装
删除docker
sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
配置阿里yum源
### 安装yum工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
### 添加阿里yum源
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
查看docker版本
yum list docker-ce --showduplicates
安装docker
sudo yum install docker:版本
#explame
sudo yum install docker-ce-18.06.3.ce
设置镜像加速
登录:https://cr.console.aliyun.com ,选择镜像加速
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://v16stybc.mirror.aliyuncs.com"]
}
EOF
启动docker
systemctl enable docker && systemctl start docker
安装命令补全
yum -y install bash-completion
source /etc/profile.d/bash_completion.sh
三台节点机器都需要安装docker 步骤同上
2.配置Master节点
修改主机名
hostnamectl set-hostname master
cat /etc/hostname
修改hosts文件
cat >> /etc/hosts << EOF
192.168.3.233 master
192.168.3.234 node01
192.168.3.235 node02
EOF
验证mac地址、UUID
cat /sys/class/net/ens33/address
cat /sys/class/dmi/id/product_uuid
禁用swap
# 当前进程禁用
swapoff -a
# 永久禁用
sed -i.bak '/swap/s/^/#/' /etc/fstab
修改内核参数
# 临时修改
sysctl net.bridge.bridge-nf-call-iptables=1
sysctl net.bridge.bridge-nf-call-ip6tables=1
# 永久修改
cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl -p /etc/sysctl.d/k8s.conf
修改Cgroup Dirver
more /etc/docker/daemon.json
{
"registry-mirrors": ["https://v16stybc.mirror.aliyuncs.com"],
"exec-opts": ["native.cgroupdriver=systemd"]
}
# 生效
systemctl daemon-reload & systemctl restart docker
设置Kubernetes源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum clean all
yum -y makecache
- [] 中括号中的是repository id,唯一,用来标识不同仓库
- name 仓库名称,自定义
- baseurl 仓库地址
- enable 是否启用该仓库,默认为1表示启用
- gpgcheck 是否验证从该仓库获得程序包的合法性,1为验证
- repo_gpgcheck 是否验证元数据的合法性 元数据就是程序包列表,1为验证
- gpgkey=URL 数字签名的公钥文件所在位置,如果gpgcheck值为1,此处就需要指定gpgkey文件的位置,如果gpgcheck值为0就不需要此项了
查看版本
yum list kubelet --showduplicates | sort -r
安装 kebelet、kubeadm、kubectl
yum install -y kubelet-1.14.2 kubeadm-1.14.2 kubectl-1.14.2
kubelet 运行在集群所有节点上,用于启动Pod和容器等对象的工具
kubeadm 用于初始化集群,启动集群的命令工具
kubectl 用于和集群通信的命令行,通过kubectl可以部署和管理应用,查看各种资源,创建、删除和更新各种组件
启动Kubelet
systemctl enable kubelet && systemctl start kubelet
kubelet 命令补全
echo "source <(kubectl completion bash)" >> ~/.bash_profile
source .bash_profile
下载镜像Shell脚本
more image.sh
#!/bin/bash
url=registry.cn-hangzhou.aliyuncs.com/google_containers
version=v1.14.2
images=(`kubeadm config images list --kubernetes-version=$version|awk -F '/' '{print $2}'`)
for imagename in ${images[@]} ; do
docker pull $url/$imagename
docker tag $url/$imagename k8s.gcr.io/$imagename
docker rmi -f $url/$imagename
done
下载镜像
sh image.sh
docker images
初始化Master
kubeadm init --apiserver-advertise-address 192.168.3.233 --pod-network-cidr=10.244.0.0/16
配置完成后最后一行会有一个kubeadm join命令,后需要这个命令加入集群中
加载环境变量
root账号
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
source .bash_profile
非Root账号
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
安装Pod网络(flannel)
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
master节点配置
taint:污点的意思。如果一个节点被打上了污点,那么pod是不允许运行在这个节点上面的
删除master节点默认污点
默认情况下集群不会在master上调度pod,如果偏想在master上调度Pod,可以执行如下操作:
查看污点:
kubectl describe node master|grep -i taints
删除默认污点:
kubectl taint nodes master node-role.kubernetes.io/master-
污点机制
kubectl taint node [node] key=value[effect]
其中[effect] 可取值: [ NoSchedule | PreferNoSchedule | NoExecute ]
NoSchedule: 一定不能被调度
PreferNoSchedule: 尽量不要调度
NoExecute: 不仅不会调度, 还会驱逐Node上已有的Pod
设置污点
kubectl taint node master key1=value1:NoSchedule
kubectl describe node master|grep -i taints
删除污点
kubectl taint nodes master key1-
kubectl describe node master|grep -i taints
Node节点安装
1. 安装kubelet、kubeadm和kubectl
同master节点
2. 下载镜像
同master节点
3. 加入集群
以下操作master上执行
[root@master ~]# kubeadm token list
TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA GROUPS
j5eoyz.zu0x6su7wzh752b3 <invalid> 2019-06-04T17:40:41+08:00 authentication,signing The default bootstrap token generated by 'kubeadm init'. system:bootstrappers:kubeadm:default-node-token
发现之前初始化时的令牌已过期
3.2 生成新的令牌
[root@master ~]# kubeadm token create
1zl3he.fxgz2pvxa3qkwxln
3.3 生成新的加密串
[root@master ~]# openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | \
openssl dgst -sha256 -hex | sed 's/^.* //'
3.4 node节点加入集群
在node节点上分别执行如下操作:
[root@node01 ~]# kubeadm join 172.27.9.131:6443 --token 1zl3he.fxgz2pvxa3qkwxln --discovery-token-ca-cert-hash sha256:5f656ae26b5e7d4641a979cbfdffeb7845cc5962bbfcd1d5435f00a25c02ea50
Dashboard安装
1.下载Yaml
wget https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/kubernetes-dashboard.yaml
其他版本github地址:https://github.com/kubernetes/dashboard/releases
2.配置Yaml
2.1 修改镜像地址
sed -i 's/k8s.gcr.io/loveone/g' kubernetes-dashboard.yaml
2.2访问外网
sed -i '/targetPort:/a\ \ \ \ \ \ nodePort: 30001\n\ \ type: NodePort' kubernetes-dashboard.yaml
2.3 提示tls问题
2.4新增管理员账号
cat >> kubernetes-dashboard.yaml << EOF
---
# ------------------- dashboard-admin ------------------- #
apiVersion: v1
kind: ServiceAccount
metadata:
name: dashboard-admin
namespace: kube-system
---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:
name: dashboard-admin
subjects:
- kind: ServiceAccount
name: dashboard-admin
namespace: kube-system
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: cluster-admin
EOF
部署Dashboard
kubectl apply -f kubernetes-dashboard.yaml
状态查看
kubectl get deployment kubernetes-dashboard -n kube-system
kubectl get pods -n kube-system -o wide
kubectl get services -n kube-system
令牌查看
kubectl describe secrets -n kube-system dashboard-admin
访问
用https scheme访问,否则无法访问!
集群测试
kubectl run httpd-app --image=httpd --replicas=3
cat >> nginx.yml << EOF
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
restartPolicy: Always
containers:
- name: nginx
image: nginx:latest
EOF
kubectl apply -f nginx.yml