设为首页 收藏本站
查看: 1174|回复: 0

[经验分享] Kubernetes 和 flannel

[复制链接]

尚未签到

发表于 2018-9-16 07:52:58 | 显示全部楼层 |阅读模式
  Kubernetes is a system for managing containerized applications in a clustered environment. It provides basic mechanisms for deployment, maintenance and scaling of applications on public, private or hybrid setups. It also comes with self-healing features where containers can be auto provisioned, restarted or even replicated.
  Kubernetes is still at an early stage, please expect design and API changes over the coming year. In this blog post, we’ll show you how to install a Kubernetes cluster with three minions on CentOS 7, with an example on how to manage pods and services.
  Kubernetes Components
  Kubernetes works in server-client setup, where it has a master providing centralized control for a number of minions. We will be deploying a Kubernetes master with three minions, as illustrated in the diagram further below.
  Kubernetes has several components:

  •   etcd - A highly available key-value store for shared configuration and service discovery.
  •   flannel - An etcd backed network fabric for containers.
  •   kube-apiserver - Provides the API for Kubernetes orchestration.
  •   kube-controller-manager - Enforces Kubernetes services.
  •   kube-scheduler - Schedules containers on hosts.
  •   kubelet - Processes a container manifest so the containers are launched according to how they are described.
  •   kube-proxy - Provides network proxy services.
  Deployment on CentOS 7
  We will need 4 servers, running on CentOS 7.1 64 bit with minimal install. All components are available directly from the CentOS extras repository which is enabled by default. The following architecture diagram illustrates where the Kubernetes components should reside:
DSC0000.png

  Prerequisites
  1. Disable iptables on each node to avoid conflicts with Docker iptables rules:
$ systemctl stop firewalld  
$ systemctl disable firewalld
  2. Install NTP and make sure it is enabled and running:
$ yum -y install ntp  
$ systemctl start ntpd
  
$ systemctl enable ntpd
  Setting up the Kubernetes Master
  The following steps should be performed on the master.
  1. Install etcd and Kubernetes through yum:
$ yum -y install etcd kubernetes  2. Configure etcd to listen to all IP addresses inside /etc/etcd/etcd.conf. Ensure the following lines are uncommented, and assign the following values:
ETCD_NAME=default  
ETCD_DATA_DIR="/var/lib/etcd/default.etcd"
  
ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"
  
ETCD_ADVERTISE_CLIENT_URLS="http://localhost:2379"
  3. Configure Kubernetes API server inside /etc/kubernetes/apiserver. Ensure the following lines are uncommented, and assign the following values:
KUBE_API_ADDRESS="--address=0.0.0.0"KUBE_API_PORT="--port=8080"KUBELET_PORT="--kubelet_port=10250"KUBE_ETCD_SERVERS="--etcd_servers=http://127.0.0.1:2379"KUBE_SERVICE_ADDRESSES="--service-cluster-ip-range=10.254.0.0/16"KUBE_ADMISSION_CONTROL="--admission_control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"KUBE_API_ARGS=""  4. Start and enable etcd, kube-apiserver, kube-controller-manager and kube-scheduler:
$ for SERVICES in etcd kube-apiserver kube-controller-manager kube-scheduler; do  
    systemctl restart $SERVICES
  
    systemctl enable $SERVICES
  
    systemctl status $SERVICES
  
done
  5. Define flannel network configuration in etcd. This configuration will be pulled by flannel service on minions:
$ etcdctl mk /atomic.io/network/config '{"Network":"172.17.0.0/16"}'  6. At this point, we should notice that nodes' status returns nothing because we haven’t started any of them yet:
$ kubectl get nodes  
NAME             LABELS              STATUS
  Setting up Kubernetes Minions (Nodes)
  The following steps should be performed on minion1, minion2 and minion3 unless specified otherwise.
  1. Install flannel and Kubernetes using yum:
$ yum -y install flannel kubernetes  2. Configure etcd server for flannel service. Update the following line inside /etc/sysconfig/flanneld to connect to the respective master:
FLANNEL_ETCD="http://192.168.50.130:2379"  3. Configure Kubernetes default config at /etc/kubernetes/config, ensure you update the KUBE_MASTER value to connect to the Kubernetes master API server:
KUBE_MASTER="--master=http://192.168.50.130:8080"  4. Configure kubelet service inside /etc/kubernetes/kubelet as below:
  minion1:
KUBELET_ADDRESS="--address=0.0.0.0"  
KUBELET_PORT="--port=10250"
  
# change the hostname to this host’s IP address
  
KUBELET_HOSTNAME="--hostname_override=192.168.50.131"
  
KUBELET_API_SERVER="--api_servers=http://192.168.50.130:8080"
  
KUBELET_ARGS=""
  minion2:
KUBELET_ADDRESS="--address=0.0.0.0"  
KUBELET_PORT="--port=10250"
  
# change the hostname to this host’s IP address
  
KUBELET_HOSTNAME="--hostname_override=192.168.50.132"
  
KUBELET_API_SERVER="--api_servers=http://192.168.50.130:8080"
  
KUBELET_ARGS=""
  minion3:
KUBELET_ADDRESS="--address=0.0.0.0"  
KUBELET_PORT="--port=10250"
  
# change the hostname to this host’s IP address
  
KUBELET_HOSTNAME="--hostname_override=192.168.50.133"
  
KUBELET_API_SERVER="--api_servers=http://192.168.50.130:8080"
  
KUBELET_ARGS=""
  5. Start and enable kube-proxy, kubelet, docker and flanneld services:
$ for SERVICES in kube-proxy kubelet docker flanneld; do  
    systemctl restart $SERVICES
  
    systemctl enable $SERVICES
  
    systemctl status $SERVICES
  
done
  6. On each minion, you should notice that you will have two new interfaces added, docker0 and flannel0. You should get different range of IP addresses on flannel0 interface on each minion, similar to below:
  minion1:
$ ip a | grep flannel | grep inet  
inet 172.17.45.0/16 scope global flannel0
  minion2:
$ ip a | grep flannel | grep inet  
inet 172.17.38.0/16 scope global flannel0
  minion3:
$ ip a | grep flannel | grep inet  
inet 172.17.93.0/16 scope global flannel0
  6. Now login to Kubernetes master node and verify the minions’ status:
$ kubectl get nodes  
NAME             LABELS                                  STATUS
  
192.168.50.131   kubernetes.io/hostname=192.168.50.131   Ready
  
192.168.50.132   kubernetes.io/hostname=192.168.50.132   Ready
  
192.168.50.133   kubernetes.io/hostname=192.168.50.133   Ready
  You are now set. The Kubernetes cluster is now configured and running. We can start to play around with pods.
  Creating Pods (Containers)
  To create a pod, we need to define a yaml file in the Kubernetes master, and use the kubectl command to create it based on the definition. Create a mysql.yaml file:
$ mkdir pods  
$ cd pods
  
$ vim mysql.yaml
  And add the following lines:
apiVersion: v1beta3  
kind: Pod
  
metadata:
  
  name: mysql
  
  labels:
  
    name: mysql
  
spec:
  
  containers:
  
    - resources:
  
        limits :
  
          cpu: 1
  
      image: mysql
  
      name: mysql
  
      env:
  
        - name: MYSQL_ROOT_PASSWORD
  
          # change this
  
          value: yourpassword
  
      ports:
  
        - containerPort: 3306
  
          name: mysql
  Create the pod:
$ kubectl create -f mysql.yaml  It may take a short period before the new pod reaches the Running state. Verify the pod is created and running:
$ kubectl get pods  
POD       IP            CONTAINER(S)   IMAGE(S)   HOST                            LABELS       STATUS    CREATED
  
mysql     172.17.38.2   mysql          mysql      192.168.50.132/192.168.50.132   name=mysql   Running   3 hours
  So, Kubernetes just created a Docker container on 192.168.50.132. We now need to create a Service that lets other pods access the mysql database on a known port and host.
  Creating Service
  At this point, we have a MySQL pod inside 192.168.50.132. Define a mysql-service.yaml as below:
apiVersion: v1beta3  
kind: Service
  
metadata:
  
  labels:
  
    name: mysql
  
  name: mysql
  
spec:
  
  publicIPs:
  
    - 192.168.50.132
  
  ports:
  
    # the port that this service should serve on
  
    - port: 3306
  
  # label keys and values that must match in order to receive traffic for this service
  
  selector:
  
    name: mysql
  Start the service:
$ kubectl create -f mysql-service.yaml  You should get a 10.254.x.x IP range assigned to the mysql service. This is the Kubernetes internal IP address defined in /etc/kubernetes/apiserver. This IP is not routable outside, so we defined the public IP instead (the interface that connected to external network for that minion):
$ kubectl get services  
NAME            LABELS                                    SELECTOR     IP               PORT(S)
  
kubernetes      component=apiserver,provider=kubernetes          10.254.0.2       443/TCP
  
kubernetes-ro   component=apiserver,provider=kubernetes          10.254.0.1       80/TCP
  
mysql           name=mysql                                name=mysql   10.254.13.156    3306/TCP
  
                                                                       192.168.50.132
  Let’s connect to our database server from outside (we used MariaDB client on CentOS 7):
$ mysql -uroot -p -h192.168.50.132  
Welcome to the MariaDB monitor.  Commands end with ; or \g.
  
Your MySQL connection id is 4
  
Server version: 5.6.24 MySQL Community Server (GPL)
  

  
Copyright (c) 2000, 2014, Oracle, MariaDB Corporation Ab and others.
  

  
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  

  
MySQL [(none)]> show variables like '%version%';
  
+-------------------------+------------------------------+
  
| Variable_name           | Value                        |
  
+-------------------------+------------------------------+
  
| innodb_version          | 5.6.24                       |
  
| protocol_version        | 10                           |
  
| slave_type_conversions  |                              |
  
| version                 | 5.6.24                       |
  
| version_comment         | MySQL Community Server (GPL) |
  
| version_compile_machine | x86_64                       |
  
| version_compile_os      | Linux                        |
  
+-------------------------+------------------------------+
  
7 rows in set (0.01 sec)
  That’s it! You should now be able to connect to the MySQL container that resides on minion2.
  Check out the Kubernetes guestbook example on how to build a simple, multi-tier web application with Redis in master-slave setup. In a follow-up blog post, we are going to play around with Galera cluster containers on Kubernetes. Stay tuned!
  References

  •   Creating a Kubernetes Cluster to Run Docker Formatted Container Images -https://access.redhat.com/articles/1353773
  •   Kubernetes Github - https://github.com/googlecloudplatform/kubernetes
  •   Persistent Installation of MySQL and WordPress on Kubernetes -https://github.com/GoogleCloudPlatform/kubernetes/tree/master/examples/mysql-wordpress-pd
  原文链接:
  http://severalnines.com/blog/installing-kubernetes-cluster-minions-centos7-manage-pods-services
  补充:
  http://sudhaker.com/41/multi-node-kubernetes-on-centos-7-x-with-flannel
  这篇文章更加的具体,应为上一篇文章没有docker相关的修改,否则还是无法使用;



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-584316-1-1.html 上篇帖子: Kubernetes 安装配置笔记 下篇帖子: Configure a Highly Available Kubernetes / etcd Cluster with Pacemaker on Fedora
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表