涉及安装包:
containernetworking-cni-0.6.0-3.el7.x86_64.rpm
flannel-v0.11.0-linux-amd64.tar.gz
kubernetes-kubeadm-1.10.3-0.el7.x86_64.rpm
etcd-3.3.11-2.el7.centos.x86_64.rpm
kubernetes-1.10.3-0.el7.x86_64.rpm
kubernetes-master-1.10.3-0.el7.x86_64.rpm
flannel-0.7.1-4.el7.x86_64.rpm
kubernetes-client-1.10.3-0.el7.x86_64.rpm kubernetes-node-1.10.3-0.el7.x86_64.rpm
相关安装包链接(k8s_soft.zip):
链接:https://pan.baidu.com/s/12PLmCcyuTFOgKL2eFBobSA
提取码:sgfq
环境介绍:
192.168.120.162 master(registry私有仓库)
192.168.120.163 node1
192.168.120.164 node2
步骤一、环境配置(所有主机)
1、关闭firewalld和selinux
# vi /etc/selinux/config
SELINUX=disabled
# setenforce 0
# systemctl stop firewalld
# systemctl disable firewalld
2、配置解析/etc/hosts
# vim /etc/hosts
192.168.120.162 master
192.168.120.163 node1
192.168.120.164 node2
3、添加内核参数文件 /etc/sysctl.d/k8s.conf
# vi /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
vm.swappiness = 0
4、执行命令
# modprobe br_netfilter
# sysctl -p /etc/sysctl.d/k8s.conf
5、关闭swap
# yum install -y yum-utils device-mapper-persistent-data lvm2
# swapoff -a
# sysctl -p /etc/sysctl.d/k8s.conf
注释掉/etc/fstab中的swap条目
# mount -a
# echo "KUBELET_EXTRA_ARGS=--fail-swap-on=false" > /etc/sysconfig/kubelet
步骤二、配置私有仓库registry(master操作)
# yum -y install docker-distribution.x86_64 //直接使用华为云提供的docker仓库包
# yum -y install docker
注意:由于使用的是华为云上的docker仓库,由于版本不一致导致/etc/sysconfig/docker/里面少了私有仓库 的选项我们需要手动加进去–add-registry=“192.168.0.23:5000” – insecure registry=“192.168.0.23:5000”
# vim /etc/sysconfig/docker
4 OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false --add-registry="192.168.120.162:5000" --insecure-registry="192.168.120.162:5000"'
# systemctl start docker-distribution
# systemctl enable docker-distribution
# systemctl start docker
# systemctl enable docker
打标记把需要的镜像上传仓库:
# docker pull docker.io/centos:latest
# docker tag docker.io/centos 192.168.120.162:5000/docker.io/centos:latest
# docker pull docker.io/anjia0532/kubernetes-dashboard-amd64:v1.8.3
# docker tag docker.io/anjia0532/kubernetes-dashboard-amd64:v1.8.3 192.168.120.162:5000/kubernetes-dashboard-amd64:v1.8.3
# docker pull docker.io/tianyebj/pod-infrastructure:latest
# docker tag docker.io/tianyebj/pod-infrastructure:latest 192.168.120.162:5000/pod-infrastructure:latest
# docker push 192.168.120.162:5000/docker.io/centos:latest
# docker push 192.168.120.162:5000/kubernetes-dashboard-amd64:v1.8.3
# docker push 192.168.120.162:5000/pod-infrastructure:latest
镜像上传私有仓库完后可以验证一下:
curl http://192.168.0.23:5000/v2/_catalog
{"repositories":["docker.io/centos","kubernetes-dashboard-amd64","pod-infrastructure"]} //能看到上传的包就代表成功了
步骤三、master、node1、node2机器上操作
# yum -y install docker
# scp root@192.168.120.162:/etc/sysconfig/docker /etc/sysconfig/docker
# curl http://192.168.120.162:5000/v2/_catalog {"repositories":["docker.io/centos","kubernetes-dashboard-amd64","pod-infrastructure"]}
步骤四、配置kubernets(master上操作)
1、安装包
# unzip k8s_soft.zip
# cd k8s_soft
# yum -y install kubernetes-master-1.10.3-0.el7.x86_64.rpm etcd-3.3.11-2.el7.centos.x86_64.rpm kubernetes-client-1.10.3-0.el7.x86_64.rpm
2、修改配置文件
第一个文件是etcd服务的配置文件:
# vim /etc/etcd/etcd.conf
6 ETCD_LISTEN_CLIENT_URLS="http://192.168.120.162:2379" //etcd服务监听的地址(etcd需要监听 mastar的地址)
第二个文件是kubeconfig的配置文件:
# vim /etc/kubernetes/config
22 KUBE_MASTER="--master=http://192.168.120.162:8080" //配置kuber-master的ip地址
第三个文件:
# vim /etc/kubernetes/apiserver
8 KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0" //监听访问的地址
17 KUBE_ETCD_SERVERS="--etcd-servers=http://127.0.0.1:2379,http://192.168.120.162:2379" //监听etcd 所在的IP地址
23 删除ServiceAccount 参数 //这个是证书的认证参数
第四个文件:/etc/kubernetes/controller-manager
第五个文件:/etc/kubernetes/scheduler
3、启动服务
# systemctl restart kube-apiserver kube-controller-manager kube-scheduler etcd
# systemctl enable kube-apiserver kube-controller-manager kube-scheduler etcd
验证:
# kubectl get cs //kubectl主要是控制k8s的命令,验证服务的状态
NAME STATUS MESSAGE ERROR
scheduler Healthy ok
controller-manager Healthy ok
etcd-1 Healthy {"health":"true"}
etcd-0 Healthy {"health":"true"}
# kubectl get csr
# kubectl get node
步骤五、配置node节点(node1、node2操作)
1、安装包
# yum -y install kubernetes-node-1.10.3-0.el7.x86_64.rpm docker
2、修改配置文件
# vim /etc/sysconfig/docker
4 添加参数 --add-registry="192.168.120.162:5000" --insecure-registry="192.168.120.162:5000"
# vim /etc/kubernetes/config
22 KUBE_MASTER="--master=http://192.168.120.162:8080"
# vim /etc/kubernetes/kubelet
5 KUBELET_ADDRESS="--address=0.0.0.0" //kubelet监听的地址,网络监听
11 KUBELET_HOSTNAME="--hostname-override=node1" //本机主机名
14添加参数: KUBELET_ARGS="--cgroup-driver=systemd --fail-swap-on=false -- kubeconfig=/etc/kubern etes/kubelet.kubeconfig --pod-infra-container-image=pod- infrastructure:latest" //上传镜像的参数
# vim /etc/kubernetes/kubelet.kubeconfig //yaml文件
apiVersion: v1
kind: Config
clusters:
- cluster:
server: http://192.168.120.162:8080
name: local
contexts:
- context:
cluster: local
name: local
current-context: local
3、启动服务
# systemctl start kubelet kube-proxy docker //启动服务
# systemctl status kubelet kube-proxy docker //检查三个服务必须启动
# systemctl enable kubelet kube-proxy docker
步骤六、在master上验证
# kubectl get node
NAME STATUS ROLES AGE VERSION
node1 Ready <none> 1d v1.10.3
node2 Ready <none> 1d v1.10.3
步骤七、配置网络
1、master上操作
# vim /etc/etcd/etcd.conf //配置数据库监听地址
6 ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379" //改成让监听所有地址
# systemctl restart etcd.service
# etcdctl mk /atomic.io/network/config '{"Network":"10.254.0.0/16","Backend":{"Type":"vxlan"}}' //给数据库添加网络字段给nova机器分网段,此命令只能执行一次 {"Network":"10.254.0.0/16","Backend":{"Type":"vxlan"}}
# etcdctl get /atomic.io/network/config //验证查看 {"Network":"10.254.0.0/16","Backend":{"Type":"vxlan"}}
2、maste和node上操作
# yum -y install flannel
# vim /etc/sysconfig/flanneld
4 FLANNEL_ETCD_ENDPOINTS="http://192.168.120.162:2379"
# systemctl stop docker
# systemctl start flanneld //注:这里需要注意的是启动flanneld前 docker需要关闭,必须先起flanneld再起docker
# systemctl status flanneld
# systemctl enable flanneld
# systemctl start docker
步骤八、创建容器,测试
# vim baseos.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: test-os
spec:
replicas: 1
template:
metadata:
labels:
app: test_os
spec:
containers:
- name: centos
image: 192.168.120.162:5000/docker.io/centos:latest
stdin: true
tty: true
# kubectl create -f baseos.yaml deployment.extensions "test-os" created
# kubeclt get pod
NAME READY STATUS RESTARTS AGE
test-os-56bd58c8dc-gzgfw 0/1 ContainerCreating 0 4s
状态一直是ContainerCreating,查看日志
# kubectl describe pod test-os-56bd58c8dc-gzgfw
报错:kubelet does not have ClusterDNS IP configured and cannot create Pod using "ClusterFirst" policy. Falling back to "Default" policy.
# vim /etc/kubernetes/kubelet
KUBELET_ARGS="--cgroup-driver=systemd --fail-swap-on=false --cluster-dns=192.168.120.162 --cluster-domain=playcrab-inc.com"
systemctl daemon-reload
# systemctl restart kubelet
# systemctl status kubelet
# kubectl describe pod test-os-56bd58c8dc-gzgfw
# kubectl delete deployment test-os //删除容器,重新创建验证
# kubectl create -f baseos.yaml
# kubectl get pod -o wide //查看容器详细信息
NAME READY STATUS RESTARTS AGE IP NODE
test-os-56bd58c8dc-mlkp5 1/1 Running 0 7m 10.254.38.2 node1
# kubectl exec -it test-os-56bd58c8dc-mlkp5 -- /bin/bash //进入创建的容器
步骤九、部署Kubernetes Dashboard
# vim kube-dashboard.yaml //启动一个web页面管理的文件
kind: Deployment
apiVersion: apps/v1
metadata:
labels:
k8s-app: kubernetes-dashboard
name: kubernetes-dashboard
namespace: kube-system
spec:
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
k8s-app: kubernetes-dashboard
template:
metadata:
labels:
k8s-app: kubernetes-dashboard
spec:
containers:
- name: kubernetes-dashboard
image: 192.168.120.162:5000/kubernetes-dashboard-amd64:v1.8.3
ports:
- containerPort: 9090
protocol: TCP
args:
- --apiserver-host=http://192.168.120.162:8080
volumeMounts:
- mountPath: /tmp
name: tmp-volume
livenessProbe:
httpGet:
path: /
port: 9090
initialDelaySeconds: 30
timeoutSeconds: 30
volumes:
- name: tmp-volume
emptyDir: {}
serviceAccountName: kubernetes-dashboard
# 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
name: kubernetes-dashboard
namespace: kube-system
spec:
type: NodePort
ports:
- port: 80
targetPort: 9090
nodePort: 30090
selector:
k8s-app: kubernetes-dashboard
# kubectl -n kube-system get pod -o wide //查看启动的kube- dashboard服务
NAME READY STATUS RESTARTS AGE IP NODE
kubernetes-dashboard-7b7d58fdb7-x29bt 1/1 Running 0 10m 10.254.38.3 node1
在node1上验证:
# ss -nutlp | grep 30090
tcp LISTEN 0 1024 :::30090 (("kube-proxy",pid=9169,fd=6))
# ss -nutlp | grep 30090
tcp LISTEN 0 1024 :::30090 (("kube-proxy",pid=9169,fd=6))
使用浏览器进行访问http//192.168.120.163:30090