CentOS7.6部署k8s环境
测试环境:
节点名称 |
节点IP |
节点功能 |
K8s-master |
10.10.1.10/24 |
Master、etcd、registry |
K8s-node-1 |
10.10.1.20/24 |
node-1 |
K8s-node-2 |
10.10.1.30/24 |
node-2 |
步骤:
- 修改hosts文件
[root@Node-1 ~]# hostnamectl --static set-hostname k8s-master
[root@Node-1 ~]# vi /etc/hosts
10.10.1.10 k8s-master
10.10.1.10 etcd
10.10.1.10 registry
10.10.1.20 k8s-node-1
10.10.1.30 k8s-node-2
- 部署etcd(注:本次只master节点安装etcd)
[root@node-1 ~]# yum install etcd –y
[root@node-1 ~]# vi /etc/etcd/etcd.conf
#[Member]
#ETCD_CORS=""
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
#ETCD_WAL_DIR=""
#ETCD_LISTEN_PEER_URLS="http://localhost:2380"
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379,http://0.0.0.0:4001"
ETCD_NAME="master"
#[Clustering]
#ETCD_INITIAL_ADVERTISE_PEER_URLS="http://localhost:2380"
ETCD_ADVERTISE_CLIENT_URLS=http://etcd:2379,http://etcd:4001
启动服务
[root@node-1 ~]# systemctl start etcd.service
[root@node-1 ~]# systemctl enable etcd.service
验证集群状态
[root@node-1 ~]# etcdctl -C http://etcd:4001 cluster-health
member 8e9e05c52164694d is healthy: got healthy result from http://etcd:2379
cluster is healthy
[root@node-1 ~]#
- 部署master
3.1.安裝docker
[root@node-1 ~]# yum install docker
[root@node-1 ~]# vi /etc/sysconfig/docker
# /etc/sysconfig/docker
# Modify these options if you want to change the way the docker daemon runs
OPTIONS='--selinux-enabled --log-driver=journald --signature-verification=false'
if [ -z "${DOCKER_CERT_PATH}" ]; then
DOCKER_CERT_PATH=/etc/docker
fi
OPTIONS='--insecure-registry registry:5000'
3.2.启动docker服务并设置开机启动
[root@node-1 ~]# systemctl start docker.service
[root@node-1 ~]# systemctl enable docker.service
3.3.安裝kubernets
[root@node-1 ~]# yum install kubernetes
3.4. 配置并且启动kubernets服务(该步骤只在master节点)
Kubernets API Server
Kubernets Controller Manager
Kubernets Scheduler
[root@node-1 ~]# vi /etc/kubernetes/apiserver
# kubernetes system config
#
# The following values are used to configure the kube-apiserver
#
# The address on the local server to listen to.
KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0"
# The port on the local server to listen on.
KUBE_API_PORT="--port=8080"
# Port minions listen on
# KUBELET_PORT="--kubelet-port=10250"
# Comma separated list of nodes in the etcd cluster
KUBE_ETCD_SERVERS="--etcd-servers=http://etcd:2379"
# Address range to use for services
KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16"
# default admission control policies
#KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ServiceAccount,ResourceQuota"
KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"
# Add your own!
KUBE_API_ARGS=""
[root@node-1 ~]# vi /etc/kubernetes/config
# How the controller-manager, scheduler, and proxy find the apiserver
KUBE_MASTER="--master=http://k8s-master:8080"
3.5. 启动服务并设置开机启动
[root@k8s-master ~]# systemctl enable kube-apiserver.service
[root@k8s-master ~]# systemctl start kube-apiserver.service
[root@k8s-master ~]# systemctl enable kube-controller-manager.service
[root@k8s-master ~]# systemctl start kube-controller-manager.service
[root@k8s-master ~]# systemctl enable kube-scheduler.service
[root@k8s-master ~]# systemctl start kube-scheduler.service
- 部署节点
4.1.部署和3.1-3.3相同
4.2.修改配置文件
[root@Node-2 ~]# vi /etc/kubernetes/config
# How the controller-manager, scheduler, and proxy find the apiserver
KUBE_MASTER="--master=http://k8s-master:8080"
[root@Node-2 ~]# vi //etc/kubernetes/kubelet
KUBELET_ADDRESS="--address=0.0.0.0"
KUBELET_HOSTNAME="--hostname-override=k8s-node-1"
KUBELET_API_SERVER="--api-servers=http://etcd:8080"
4.3.启动服务并设置开机启动
[root@k8s-node-1 ~]# systemctl enable kubelet.service
[root@k8s-node-1 ~]# systemctl start kubelet.service
[root@k8s-node-1 ~]# systemctl enable kube-proxy.service
[root@k8s-node-1~]# systemctl start kube-proxy.service
- 查看群集状态
[root@k8s-master ~]# kubectl get node
NAME STATUS AGE
k8s-node-1 Ready 14h
k8s-node-2 Ready 14h
- 安装Flannel(所有节点)
[root@node-1 ~]# yum install flannel
[root@node-1 ~]# vi /etc/sysconfig/flannel
# Flanneld configuration options
# etcd url location. Point this to the server where etcd runs
FLANNEL_ETCD_ENDPOINTS="http://etcd:2379"
# etcd config key. This is the configuration key that flannel queries
# For address range assignment
FLANNEL_ETCD_PREFIX="/atomic.io/network"
- 配置etcd中关于flannel的key
[root@node-1 ~]# etcdctl mk /atomic.io/network/config '{ "Network": "10.0.0.0/16" }'
设置flannel服务启动和开机启动:
[root@node-1 ~]# systemctl enable flanneld.service
[root@node-1 ~]# systemctl start flanneld.serivice
管理节点执行:
service docker restart
systemctl restart kube-apiserver.service
systemctl restart kube-controller-manager.service
systemctl restart kube-scheduler.service
业务节点执行
service docker restart
systemctl restart kubelet.service
systemctl restart kube-proxy.service
业务节点拉取image
[root@Node-2 ~]# docker pull winstonpro/lnmp
[root@Node-2 ~]# docker pull tomcat
[root@Node-2 ~]# docker pull httpd
管理节点创建实例
kubectl run web --image=winstonpro/lnmp --port=80
管理节点做svc映射
kubectl expose deployment web --port=80 --target-port=80 --external-ip=10.10.1.30
常用命令:
[root@node-1 ~]# kubectl get node -o wide
NAME STATUS AGE EXTERNAL-IP
k8s-node-1 Ready 14h <none>
k8s-node-2 Ready 14h <none>
[root@node-1 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE
app-556711052-ps9kr 1/1 Running 3 7h 10.0.53.2 k8s-node-1
tomcat-3343039334-0z187 1/1 Running 0 2h 10.0.74.3 k8s-node-2
web-3818241055-g11q8 1/1 Running 3 8h 10.0.74.2 k8s-node-2
[root@node-1 ~]# kubectl get svc -o wide
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes 10.254.0.1 <none> 443/TCP 15h <none>
tomcat 10.254.69.86 10.10.1.30 7777/TCP 2h run=tomcat
web 10.254.76.251 10.10.1.30 80/TCP 6h run=web
[root@node-1 ~]# kubectl get deployments
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
app 1 1 1 1 7h
tomcat 1 1 1 1 2h
web 1 1 1 1 8h
关于外网无法访问:
[root@Node-2 ~]# vi /etc/sysctl.conf
net.ipv4.ip_forward = 1
[root@Node-2 ~]# sysctl -p
iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -F