kubernetes环境搭建 -k8s笔记(一)

一、环境准备

1、硬件及版本信息:

  cpu&内存:2核心,2G

  网络: 每台vm主机2块网卡,一块NAT用于上网,别一块配置成 "仅主机模式",网段为192.168.100.X

  OS:centos7.4

  k8s:v1.13.3

  docker: 18.09.2

2、一些初始化工作:

  关闭防火墙

  关闭SELinux

  配置ntp

  配置hosts

# /etc/hosts文件如下:每台主机都要配置
# tail -3 /etc/hosts
192.168.100.71 master.perry.com master 192.168.100.72 node1.perry.com node1 192.168.100.73 node2.perry.com node2

  关闭swap

注释掉/etc/fstab中swap那行,并重启主机生效
# /dev/mapper/centos-swap swap                    swap    defaults        0 0

  开启透明网桥

# echo "net.bridge.bridge-nf-call-iptables=1" >> /etc/sysctl.conf
# echo "net.bridge.bridge-nf-call-ip6tables=1" >> /etc/sysctl.conf
# systctl -p

 

3、配置docker yum仓库

# wget -O /etc/yum.repos.d/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo

4、配置k8s yum仓库

# cat > /etc/yum.repos.d/kubernetes.repo <<EOF
[kubernetes]
name=kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
gpgcheck=1
enabled=1
EOF

二、安装软件

master和node三台主机都安装 docker-ce kubelet kubeadm kubectl

# yum install docker-ce kubelet kubeadm kubectl

 

三、初始化master

# kubeadm init --kubernetes-version=v1.13.3 --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12 --apiserver-advertise-address=192.168.100.71

1、参数说明:

--kubernetes-version  # 指定要安装的k8s版本
--pod-network-cidr    # 指定pod网络地址范围
--service-cidr   # 指定service网络地址范围
--apiserver-advertise-address #指定api地址,这里配置成了master的私网接口IP

2、kubeadm init时,会因为无法访问google,导致镜像下载失败,解决办法有如下2种:

1)想办法使用代理上google,有朋友要找到好用的代理告诉我下,^_^。

  配置docker使用代理,配置完成后,重启docker服务

# vi /usr/lib/systemd/system/docker.service 

[Service]
……
Environment="HTTPS_PROXY=http://www.ik8s.io:10080"
Environment="NO_PROXY=127.0.0.0/8,192.168.0.0/16"
……

2)根据kubeadm init执行时的报错提示,可以获知需要下载哪些镜像,这些镜像可以先从阿里云的registry下载,下载完成后再重新打tag,具体操作如下:

[root@master scripts]# cat get_k8s_img_from_aliyun.sh
#!/bin/bash

# need get images
images=(
k8s.gcr.io/kube-apiserver:v1.13.3
k8s.gcr.io/kube-controller-manager:v1.13.3
k8s.gcr.io/kube-scheduler:v1.13.3
k8s.gcr.io/kube-proxy:v1.13.3
k8s.gcr.io/pause:3.1
k8s.gcr.io/etcd:3.2.24
k8s.gcr.io/coredns:1.2.6
)

k8s_registry=k8s.gcr.io
aliyun_registry=registry.cn-hangzhou.aliyuncs.com

# get images from aliyun registry
function get_aliyun_k8s_images() {
for k8s_image in ${images[*]};do
image=` echo $k8s_image | awk -F '/' '{print $2}'`
docker pull $aliyun_registry/google_containers/$image
done
}

# tag images to k8s.gcr.io registry
function tag_images() {
for k8s_image in ${images[*]};do
image=` echo $k8s_image | awk -F '/' '{print $2}'`
docker tag $aliyun_registry/google_containers/$image $k8s_registry/$image
done
}

get_aliyun_k8s_images
tag_images

docker pull quay.io/coreos/flannel:v0.11.0-amd64
docker images

kubeadm init 初始化成功后,可以看到如下信息:

Your Kubernetes master has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
https://kubernetes.io/docs/concepts/cluster-administration/addons/

You can now join any number of machines by running the following on each node
as root:

kubeadm join 192.168.100.71:6443 --token rfhs54.1i7ol2gt68pmnsl6 --discovery-token-ca-cert-hash sha256:75e6507e0cd820fae522348f40c9a6fd7318e5ebf34829cebafdec0acc368749

 

四、将node节点加入k8s集群中

node节点需要以下三个镜像

k8s.gcr.io/kube-proxy:v1.13.3
k8s.gcr.io/pause:3.1
quay.io/coreos/flannel:v0.11.0-amd64

#根据maser上 kubeadmin init执行结果的提示,在2台node上分别执行如下命令
# kubeadm join 192.168.100.71:6443 --token rfhs54.1i7ol2gt68pmnsl6 --discovery-token-ca-cert-hash sha256:75e6507e0cd820fae522348f40c9a6fd7318e5ebf34829cebafdec0acc368749

 

说明:

  1)master和node节点上如果 kubeadm执行不成功,或者需要重新执行,需要先使用kubeadm reset 恢复初始状态

  2)master和node节点上需要开启kubectl开机自启动 systemctl enable kubectl, 否则主机重启后组件类容器不能启动

 

五、验证:

1、查看节点信息

kubectl get nodes

[root@master ~]# kubectl get nodes
NAME               STATUS   ROLES    AGE   VERSION
master.perry.com   Ready    master   32h   v1.13.3
node1.perry.com    Ready    <none>   25h   v1.13.3
node2.perry.com    Ready    <none>   25h   v1.13.3

 

六、发布一个service,体验一下k8s

一个简单的Demo,创建/发布/扩容/缩容/

部署myapp应用

1、在master创建一个myapp的应用,副本数量为2
# kubectl run myapp --image=ikubernetes/myapp:v1 --replicas=2

可以通过以下一些命令查看应用的状态等信息
# kubectl get deployment
# kubectl get deployment -w
# kubectl get deployment 
# kubectl get pods -o wide

2、发布应用
# kubectl expose deployment myapp --name=myapp --port 80
# kubectl get svc

3、发布应用,外部可以访问
# kubectl eiit svc myapp
type: ClusterIP 将 ClusterIP 修改为NodePort

kubectl get svc
在集群外部访问http://192.168.100.72:32602/

4、扩容myapp应用
# kubectl scale --replicas=5 deployment myapp
# kubectl get pod -o wide

5、缩容myapp应用
# kubectl scale --replicas=3 deployment myapp
# kubectl get pod -o wide

6、滚动更新
# kubectl set image deployment myapp myapp=ikubernetes/myapp:v2

7、查看滚动更新过程
# kubectl rollout status deployment myapp

8、更新完成后,查看镜像版本
# kubectl describe pods myapp-65899575cd-df62l

9、回滚
# kubectl rollout undo deployment myapp

10、删除应用
# kubectl delete deployment myapp
# kubectl delete svc myapp

 

posted @ 2019-02-22 00:14  fang9045315  阅读(7055)  评论(0编辑  收藏  举报