kubernetes怎么用
这篇文章主要介绍了kubernetes怎么用,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
1 主备宿主机centos
2 关闭防火墙
systemctl stop firewalld && systemctl disable firewalld
3 安装etcd 和kubernetes
yum install etcd kubernetes -y
3 修改docker 配置文件
在docker配置文件/etc/sysconfig/docker 中 给OPTIONS 参数后面添加上 --insecure-registry gcr.io,如下:
4 修改kubernetes 配置文件
在配置文件/etc/kubernetes/apiserver 把参数KUBE_ADMISSION_CONTROL 中的ServiceAccount 删除
5 一次启动kubectl 服务
systemctl start etcd
systemctl start docker
systemctl start kube-apiserver
systemctl start kube-controller-manager
systemctl start kube-scheduler
systemctl start kubelet
systemctl start kube-proxy
6 下载案例所需镜像
docker pull docker.io/kubeguide/redis-master
dokcer pull docker.io/kubeguide/guestbook-redis-slave
docker pull docker.io/kubeguide/guestbook-php-frontend
docker pull registry.access.redhat.com/rhel7/pod-infrastructure:latest
在拉镜像 registry.access.redhat.com/rhel7/pod-infrastructure:latest 的时候 可能会报错拉不下来。解决办法:
yum install -y *rhsm*
wget http://mirror.centos.org/centos/7/os/x86_64/Packages/python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm
rpm2cpio python-rhsm-certificates-1.19.10-1.el7_4.x86_64.rpm | cpio -iv --to-stdout ./etc/rhsm/ca/redhat-uep.pem | tee /etc/rhsm/ca/redhat-uep.pem
重新拉registry.access.redhat.com/rhel7/pod-infrastructure:latest ,这个镜像拉的时候比较慢,请耐心等待。
7 创建redis-master 的RC和service
# 编写rc的yaml文件[root@node1 redis]# cat redis-master-controller.yaml apiVersion: v1kind: ReplicationControllermetadata: name: redis-master // rc 的名称 labels: name: redis-master // rc 的标签spec: replicas: 1 // rc 创建pod的副本数量 selector: name: redis-master // rc 使用的标签选择器 template: // 使用模板创建pod metadata: labels: name: redis-master // 创建的pod 使用的标签,这里一定要和上面rc 的标签选择器一致 spec: containers: - name: master image: kubeguide/redis-master ports: - containerPort: 6379
# 创建rckubectl create -f ./redis-master-controller.yaml[root@node1 redis]# kubectl get rc|grep masterredis-master 1 1 1 5h[root@node1 redis]# kubectl get pods|grep masterredis-master-k9k5n 1/1 Running 0 5h
# 编写service 的yaml 文件[root@node1 redis]# cat redis-master-service.yaml apiVersion: v1kind: Servicemetadata: name: redis-master labels: name: redis-masterspec: ports: - port: 6379 # service 使用的虚拟端口 targetPort: 6379 # service 管理的pod 内部的端口 selector: name: redis-master # 这里的标签选择器要和上面RC文件里面创建pod 的标签 一致
# 创建servicekubectl create -f ./redis-master-service.yaml[root@node1 redis]# kubectl get services|grep masterredis-master 10.254.38.1726379/TCP 2h
8 创建redis-slave 的RC和service
[root@node1 redis]# cat redis-slave-controller.yaml apiVersion: v1kind: ReplicationControllermetadata: name: redis-slave labels: name: redis-slavespec: replicas: 2 selector: name: redis-slave template: metadata: labels: name: redis-slave spec: containers: - name: slave image: kubeguide/guestbook-redis-slave env: - name: GET_HOSTS_FROM value: env ports: - containerPort: 6379
[root@node1 redis]# kubectl get pods|grep slaveredis-slave-dt0vk 1/1 Running 0 2hredis-slave-lf79v 1/1 Running 0 2h
[root@node1 redis]# cat redis-slave-service.yaml apiVersion: v1kind: Servicemetadata: name: redis-slave labels: name: redis-slavespec: ports: - port: 6379 targetPort: 6379 selector: name: redis-slave
[root@node1 redis]# kubectl get services|grep slaveredis-slave 10.254.194.2536379/TCP 2h
9 创建frontend 的RC和service
[root@node1 redis]# cat frontend-controller.yaml apiVersion: v1kind: ReplicationControllermetadata: name: frontend labels: name: frontendspec: replicas: 3 selector: name: frontend template: metadata: labels: name: frontend spec: containers: - name: frontend image: kubeguide/guestbook-php-frontend env: - name: GET_HOSTS_FROM value: env ports: - containerPort: 80
[root@node1 redis]# cat frontend-service.yaml apiVersion: v1kind: Servicemetadata: name: frontend labels: name: frontendspec: type: NodePort ports: - port: 80 nodePort: 30001 selector: name: frontend# 由于 frontend 服务是需要外部访问的,所以需要把service的虚拟端口映射到宿主机上,所以添加了 nodePort 端口,# nodePort 范围在30000 到 32767 都可以,否则报错。
[root@node1 redis]# kubectl get pods|grep frontfrontend-069sm 1/1 Running 0 2hfrontend-bksm5 1/1 Running 0 2hfrontend-q2vpv 1/1 Running 0 2h[root@node1 redis]# kubectl get rc|grep frontfrontend 3 3 3 2h[root@node1 redis]# kubectl get service|grep frontfrontend 10.254.27.21480:30001/TCP 2h
9 实验
感谢你能够认真阅读完这篇文章,希望小编分享的"kubernetes怎么用"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!