nginx+keepalived
启动两台虚拟机,分别安装两个nginx,然后使用keepalived为两个虚拟机做一个虚拟ip实现负载均衡
编写一个YAML文件nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
然后执行kubectl create -f nginx-deployment.yaml
由志愿者推动,社区发起的一个k8s部署工具,使用此工具可以快速的部署k8s集群
1.服务器规划
kubernetes01 角色 master 内网ip 192.168.217.149 CentOS7
kubernetes02 角色 worker 内网ip 192.168.217.150 CentOS7
2.master节点部署过程
cat /etc/redhat-release
hostnamectl set-hostname kubernetes01
echo 192.168.217.149 kubernetes01 >> /etc/hosts
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
vim /etc/selinux/config 修改selinux=disabled
swapoff -a # 禁用swap
vim /etc/fatab # 同时修改fstab,禁掉swap开机自启动
# 时区设置&时间同步
timedatectl set-timezone Asia/Shanghai
yum -y install ntpdate
ntpdate cn.pool.ntp.org
cat > /etc/sysctl.d/k8s.conf << EOF
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
vm.swappiness=0
EOF
# 更新配置
sysctl --system
(之前装过docker建议卸载,版本对不上就炸裂)
yum -y install yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum -y install docker-ce-18.06.1.ce
systemctl enable docker.service && systemctl start docker.service
docker --version
还要一下配置
配置
[root@localhost ~]#cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://c3dw7mfv.mirror.aliyuncs.com"]
}
配置
修改系统配置文件/etc/sysconfig/docker
添加镜像仓库–registry-mirror=http://f2d6cb40.m.daocloud.io
OPTIONS=’ --selinux-enabled --log-driver=journald --registry-
mirror=http://f2d6cb40.m.daocloud.io’
重启docker服务
service docker restart
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
EOF
rpm -import https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
yum install -y kubelet-1.12.1
yum install -y kubectl-1.12.1
yum install -y kubeadm-1.12.1
# 启动服务
systemctl enable kubelet docker && systemctl start kubelet docker
kubelet --version
kubectl version
kubeadm version
因为由于不可描述的原因下载失败,改用其他方式下载
k8s.gcr.io/kube-apiserver:v1.12.1
k8s.gcr.io/kube-controller-manager:v1.12.1
k8s.gcr.io/kube-scheduler:v1.12.1
k8s.gcr.io/kube-proxy:v1.12.1
k8s.gcr.io/pause:3.1
k8s.gcr.io/etcd:3.2.24
k8s.gcr.io/coredns:1.2.2
就用别人的吧
建议进文本复制粘贴。。不然有些东西会不见
cat > pull_k8s_images.sh <
运行脚本
sh pull_k8s_images.sh
查看镜像信息
docker images
kubeadm init --kubernetes-version=v1.12.1
1.配置kubectl与apiserver的认证
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
2.记录worker节点加入集群命令
kubeadm join 192.168.217.149:6443 –token w155mk.vmabpoqjyyqhnj0f –discovery-token-ca-cert-hash sha256:d62d8d418a8be6fd1ce903ee313fa7f8946d030cf547087e7c178e235dd9ffee
3.健康状态检查
kubectl get cs
4.查看master节点状态
kubectl get node
5.查看节点详细信息
kubectl describe node kubernetes01
Weave是一个比较热门的容器网络方案,具有良好的易用性功能也很强大
kubectl apply -f https://git.io/weave-kube-1.6
kubectl get nodes
耐心等待,不行先重启,再试几次就ok了。
1.获取可视化插件docker镜像,修改tag
docker pull anjia0532/google-containers.kubernetes-dashboard-amd64:v1.10.0
docker tag anjia0532/google-containers.kubernetes-dashboard-amd64:v1.10.0 k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1
docker rmi anjia0532/google-containers.kubernetes-dashboard-amd64:v1.10.0
2.获取并修改可视化插件YAML文件的最后部分
wget https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml
修改kubernetes-dashboard.yaml文件
主要两个地方
type: NodePort
nodePort: 30001
3.部署可视化插件
kubectl apply -f kubernetes-dashboard.yaml
4.查看可视化插件对应的Pod状态
kubectl get pods -n kube-system | grep dash
5.获取登录用的token
kubectl -n kube-system describe $(kubectl -n kube-system get secret -n kube-system -o name | grep namespace) | grep token
6.访问https://192.168.217.149:30001通过token登陆控制面板,注意是https协议!
Rook项目是基于Ceph的Kubernetes存储插件,一个可用于生产级别的做持久化存储的插件
cd /usr/local/src
yum -y install git
git clone https://github.com/rook/rook.git
cd /usr/local/src/rook/cluster/examples/kubernetes/ceph
kubectl apply -f common.yaml
kubectl apply -f operator.yaml
kubectl apply -f cluster.yaml
默认情况下 Master 节点是不允许运行用户 Pod 的。而 Kubernetes 做到这一点,依靠的是 Kubernetes 的 Taint/Toleration 机制。
删除master的污点
kubectl taint nodes --all node-role.kubernetes.io/master-
3.查看状态
节点
kubectl get nodes
1.worker节点部署脚本
#!/bin/bash
#pre config
swapoff -a
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
vm.swappiness=0
EOF
sysctl --system
#install docker-ce
yum -y install yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum -y install docker-ce-18.06.1.ce
/bin/systemctl start docker.service
# install kubeadm
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
EOF
wget https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
rpm -import rpm-package-key.gpg
yum install -y kubelet-1.12.1
yum install -y kubectl-1.12.1
yum install -y kubeadm-1.12.1
# jingxiangjiasu
cat > /etc/docker/daemon.json << EOF
{
"registry-mirrors": ["https://c3dw7mfv.mirror.aliyuncs.com"]
}
EOF
# jingxiang
cat > /etc/sysconfig/docker << EOF
--registry-mirror=http://f2d6cb40.m.daocloud.io OPTIONS=' --selinux-enabled --log-driver=journald --registry- mirror=http://f2d6cb40.m.daocloud.io'
EOF
systemctl restart docker
# install kube-proxy and pause
images=(kube-proxy:v1.12.1 pause:3.1 )
for imageName in ${images[@]} ; do
docker pull anjia0532/google-containers.$imageName
docker tag anjia0532/google-containers.$imageName k8s.gcr.io/$imageName
docker rmi anjia0532/google-containers.$imageName
done
2.加入cluster
kubeadm join 192.168.217.149:6443 --token w155mk.vmabpoqjyyqhnj0f --discovery-token-ca-cert-hash sha256:d62d8d418a8be6fd1ce903ee313fa7f8946d030cf547087e7c178e235dd9ffee
3.echo export KUBECONFIG=/etc/kubernetes/kubelet.conf >> ~/.bashrc
4.source ~/.bashrc
调试
kubectl describe node master
kubectl get pods -n kube-system
kubectl get pods --all-namespaces
1.编写配置文件
nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
指定API对象类型
Deployment
多副本Pod对象,副本发生变化,对副本进行滚动更新
对象独有的定义,用来描述它所要表达的功能
API 对象的“标识”,即元数据,它也是我们从 Kubernetes 里找到这个对象的主要依据。
spec.Metadata.Labels
Labels 就是一组 key-value 格式的标签
Deployment 控制器对象,可以通过这个 Labels 字段从 Kubernetes 中过滤出它所关心的被控制对象
metadata
对象的通用元数据
2.发布应用
kubectl create -f nginx.yaml
kubectl get pods -l app=nginx
kubectl describe查看对象细节
kubectl describe pod nginx-deployment-67594d6bf6-p2zrm
升级应用
...
spec:
containers:
- name: nginx
image: nginx:1.8 #这里被从1.7.9修改为1.8
ports:
- containerPort: 80
kubectl apply -f nginx-deployment.yaml
3.声明Volume
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.8
ports:
- containerPort: 80
volumeMounts:
- mountPath: "/usr/share/nginx/html"
name: nginx-vol
volumes:
- name: nginx-vol
hostPath:
path: /var/data
spec.volumes
定义了这个Pod的所有Volume
名字叫作 nginx-vol,类型是 emptyDir。
emptyDir 隐式声明宿主机目录的 Volume。所以,Kubernetes 也会在宿主机上创建一个临时目录,这个目录将来就会被绑定挂载到容器所声明的 Volume 目录上。
Hostpath 显示声明
用法
...
volumes:
- name: nginx-vol
hostPath:
path: /var/data
应用更新
kubectl apply -f nginx.yaml
旧pods会被逐一更新,最终保留新pods
用kubectl get pods
4.进入Pod中
kubectl exec -it nginx-deployment-5c678cfb6d-h77mw -- /bin/bash
5.删除API对象
kubectl delete -f nginx.yaml
kubeadm token create
kubeadm token list
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
ubeadm join 192.168.217.149:6443 --token w155mk.vmabpoqjyyqhnj0f --discovery-token-ca-cert-hash sha256:d62d8d418a8be6fd1ce903ee313fa7f8946d030cf547087e7c178e235dd9ffee
上面命令的整合
kubeadm token create --print-join-command