Kubernetes 从入门到进阶 第一个Helloworld程序

本文详细介绍了如何使用kubernetes-ansible在四台机器上安装Kubernetes,并通过一个Redmine部署示例展示了Kubernetes的基本操作流程,包括创建deployment、暴露服务、查看节点分配情况等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.youkuaiyun.com/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                       
 

本文将使用kubernetes-ansible进行安装,由于其kubernetes-ansible更新很快,细小的地方又发生了一定变化,简单记录一下,同时由于特殊原因,一般网内用户都无法正常运行k8s。所以这个系列准备写一些咱们自己能用的例子来学习k8s的基础知识。

构成说明

 

演示程序使用下列的集群构成。

                                               
NotypeIPOS
1master192.168.32.131CENTOS7.2
2etcd192.168.32.131CENTOS7.2
3minion192.168.32.132CENTOS7.2
3minion192.168.32.133CENTOS7.2
3minion192.168.32.134CENTOS7.2

Step 1:安装ansible

 

在192.168.32.131上安装ansible

[root@host131 local]# yum -y install epel-release[root@host131 local]# yum -y install ansible
  
  • 1
  • 2
 

确认安装

[root@host131 local]# ansible --versionansible 2.1.0.0  config file = /etc/ansible/ansible.cfg  configured module search path = Default w/o overrides[root@host131 local]#
  
  • 1
  • 2
  • 3
  • 4
  • 5

Step 2:设定ssh通路和ansible

 

分别在4台机器上生成ssh的key
  设定4台机器的/etc/hosts
  设定4台机器ssh通路
  在ansible所安装的机器上,追加机器信息到/etc/ansible/hosts中
  确认ansible正常动作

Step 3:下载contrib

[root@host31 local]# cd ..[root@host31 /]# mkdir -p /local; cd /local[root@host131 local]# git clone https://github.com/kubernetes/contribCloning into 'contrib'...remote: Counting objects: 30938, done.remote: Compressing objects: 100% (31/31), done.remote: Total 30938 (delta 12), reused 0 (delta 0), pack-reused 30907Receiving objects: 100% (30938/30938), 32.69 MiB | 153.00 KiB/s, done.Resolving deltas: 100% (14922/14922), done.Checking out files: 100% (16114/16114), done.[root@host131 local]#
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

Step 4:生成kubernetes-ansible设定文件

按照构成说明中的4台机器构成,设定inventory文件

[root@host131 inventory]# pwd/local/contrib/ansible/inventory[root@host131 inventory]# lltotal 4drwxr-xr-x. 2 root root  20 Aug 29 20:06 group_vars-rw-r--r--. 1 root root 132 Aug 29 20:06 localhost.ini[root@host131 inventory]# cp localhost.ini inventory[root@host131 inventory]# vi inventory[root@host131 inventory]# cat inventory[masters]host131[nodes]host132host133host134[etcd]host131[root@host131 inventory]#
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
 

设定inventory/group_vars/all.yml,将kube-ui打开,从default的false该为true
      85 # Turn to false to disable the kube-ui addon for this cluster
       86 kube-ui: false

Step 6:安装所需package

 

依赖关系整理的真不错,只有一个package在github的说明中虽然没有提到,但是实际是需要提前安装的

               
Nopackage机器安装命令
1python-netaddr192.168.32.131yum -y install python-netaddr

Step 7:执行安装文件

 

执行安装文件deploy-cluster.sh

[root@host131 ansible]# pwd/local/contrib/ansible[root@host131 ansible]# cd scripts/ && ./deploy-cluster.sh
  
  • 1
  • 2
  • 3

Step 8:确认kubernetes

 

确认node
  确认services
  确认版本

[root@host131 scripts]# kubectl versionClient Version: version.Info{Major:"1", Minor:"2", GitVersion:"v1.2.0", GitCommit:"ec7364b6e3b155e78086018aa644057edbe196e5", GitTreeState:"clean"}Server Version: version.Info{Major:"1", Minor:"2", GitVersion:"v1.2.0", GitCommit:"ec7364b6e3b155e78086018aa644057edbe196e5", GitTreeState:"clean"}[root@host131 scripts]#[root@host131 scripts]# kubectl get nodesNAME      STATUS    AGEhost132   Ready     2mhost133   Ready     2mhost134   Ready     2m[root@host131 scripts]# kubectl get serviceNAME         CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGEkubernetes   10.254.0.1   <none>        443/TCP   12m[root@host131 scripts]#
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

Hello world示例

事前准备

 

因为pod在创建的时候会用到一个helper的镜像,以前叫pause-amd64,在k8s1.2之后都是叫做pause,因为它需要从gcr.io下载,所以大部分用户都无法下载,所以pod的状态会一直是ContainerCreating。我把这个放到一个docker用户都可以访问的地方,你也可以使用各种方法,只要取到对应的版本,将其部署到各个节点即可。在本文示例中,如下操作将在4个node都同样作一下。

[root@host131 ~]# docker search liumiaocnINDEX       NAME                              DESCRIPTION                                     STARS     OFFICIAL   AUTOMATEDdocker.io   docker.io/liumiaocn/pause-amd64   gcr.io/google_containers/pause:2.0: Kubern...   0                    [OK][root@host131 ~]# docker pull docker.io/liumiaocn/pause-amd64Using default tag: latestTrying to pull repository docker.io/liumiaocn/pause-amd64 ...latest: Pulling from docker.io/liumiaocn/pause-amd648f216013977d: Pull completea3ed95caeb02: Pull completeDigest: sha256:51dbbd8636156e778a30c9eaf481b6e751552aac4fc654795d58cdbfb516d25eStatus: Downloaded newer image for docker.io/liumiaocn/pause-amd64:latest[root@host131 ~]# docker tag docker.io/liumiaocn/pause-amd64 gcr.io/google_containers/pause:2.0[root@host131 ~]# docker images |grep 2.0gcr.io/google_containers/pause    2.0                 c5147e5f6708        Less than a second ago   350.2 kB[root@host131 ~]#
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

创建一个deployment

 

以前的RC现在逐渐会被deployment所替代,但是yml写法基本没有变法,不过cli的方式下有了明显的变化。用如下的命令创建一个运行起2个实例的redmine,端口为80。为了提高速度,可以事前在node的各节点将redmine的镜像提前pull下来。

kubectl run redminemgnt --image=redmine --replicas=2 --port=80
  
  • 1
 

确认结果

[root@host131 ~]# kubectl run redminemgnt --image=redmine --replicas=2 --port=80deployment "redminemgnt" created[root@host131 ~]# kubectl get podsNAME                          READY     STATUS    RESTARTS   AGEredminemgnt-298222028-85fi9   1/1       Running   0          3mredminemgnt-298222028-tfqi2   1/1       Running   0          3m[root@host131 ~]# kubectl get deploymentNAME          DESIRED   CURRENT   UP-TO-DATE   AVAILABLE   AGEredminemgnt   2         2         2            2           4m[root@host131 ~]#
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

expose对外的port

 

export对外的port,以使得其他机器可以访问

[root@host131 ~]# kubectl expose deployment redminemgnt --port=80 --type=LoadBalancerservice "redminemgnt" exposed[root@host131 ~]#
  
  • 1
  • 2
  • 3

确认2个实例如何启动在3个node上

 

通过如下可以看到,当我们把2个redmine的实例启动在3个node上的时候,k8s自动的进行的分配到了host133和host134,关于分配策略可以自己通过调整scale进行实验确认以加深理解。

[root@host131 ~]# kubectl get pods -o wideNAME                          READY     STATUS    RESTARTS   AGE       NODEredminemgnt-298222028-85fi9   1/1       Running   0          10m       host134redminemgnt-298222028-tfqi2   1/1       Running   0          10m       host133[root@host131 ~]#
  
  • 1
  • 2
  • 3
  • 4
  • 5
 

确认deploy

[root@host131 ~]# kubectl describe deployment redminemgntName:                   redminemgntNamespace:              defaultCreationTimestamp:      Tue, 30 Aug 2016 00:10:46 -0400Labels:                 run=redminemgntSelector:               run=redminemgntReplicas:               2 updated | 2 total | 2 available | 0 unavailableStrategyType:           RollingUpdateMinReadySeconds:        0RollingUpdateStrategy:  1 max unavailable, 1 max surgeOldReplicaSets:         <none>NewReplicaSet:          redminemgnt-298222028 (2/2 replicas created)Events:  FirstSeen     LastSeen        Count   From                            SubobjectPath   Type            Reason                  Message  ---------     --------        -----   ----                            -------------   --------        ------                  -------  13m           13m             1       {deployment-controller }                        Normal          ScalingReplicaSet       Scaled up replica set redminemgnt-298222028 to 2[root@host131 ~]#
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

确认pod

 

describe命令把各个组成部分清晰的列出来,所以对我们加深理解有很好的作用

[root@host131 ~]# kubectl describe pod redminemgnt-298222028-85fi9Name:           redminemgnt-298222028-85fi9Namespace:      defaultNode:           host134/192.168.32.134Start Time:     Tue, 30 Aug 2016 00:10:47 -0400Labels:         pod-template-hash=298222028,run=redminemgntStatus:         RunningIP:             172.16.64.4Controllers:    ReplicaSet/redminemgnt-298222028Containers:  redminemgnt:    Container ID:       docker://61e5320fc6877707c9cabeed8a272a41515d2a4ccf63f137d2eff760741e3865    Image:              redmine    Image ID:           docker://sha256:55ccd586ad119ceeb784ac924a15dd1060fa935beced3ce32e74967a0bc0ffac    Port:               80/TCP    QoS Tier:      cpu:              BestEffort      memory:           BestEffort    State:              Running      Started:          Tue, 30 Aug 2016 00:11:56 -0400    Ready:              True    Restart Count:      0    Environment Variables:Conditions:  Type          Status  Ready         TrueVolumes:  default-token-8209b:    Type:       Secret (a volume populated by a Secret)    SecretName: default-token-8209bEvents:  FirstSeen     LastSeen        Count   From                    SubobjectPath                   Type            Reason          Message  ---------     --------        -----   ----                    -------------                   --------        ------          -------  13m           13m             1       {default-scheduler }                                    Normal          Scheduled       Successfully assigned redminemgnt-298222028-85fi9 to host134  12m           12m             1       {kubelet host134}       spec.containers{redminemgnt}    Normal          Pulling         pulling image "redmine"  12m           12m             1       {kubelet host134}       spec.containers{redminemgnt}    Normal          Pulled          Successfully pulled image "redmine"  12m           12m             1       {kubelet host134}       spec.containers{redminemgnt}    Normal          Created         Created container with docker id 61e5320fc687  12m           12m             1       {kubelet host134}       spec.containers{redminemgnt}    Normal          Started         Started container with docker id 61e5320fc687[root@host131 ~]#
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
 

这样第一个简单的kubernetes的Helloworld就完成了,接下我们会更加详细的学习k8s的其他基础知识。

           

给我老师的人工智能教程打call!http://blog.youkuaiyun.com/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值