k8s入门案例
1、关闭CentOS自带的防火墙服务: systemctl disable firewalld systemctl stop firewalld 2、安装etcd和Kubernetes软件(会自动安装docker软件): yum install -y etcd kubernates 3、修改配置文件:a、docker配置文件/etc/sysconfig/...
·
1、关闭CentOS自带的防火墙服务:
systemctl disable firewalld
systemctl stop firewalld
2、安装etcd和Kubernetes软件(会自动安装docker软件):
yum install -y etcd kubernates
3、修改配置文件:
a、docker配置文件/etc/sysconfig/docker,其中OPTIONS的内容设置为
OPTIONS='--selinux-enabled=false
b、Kubernetes apiserver配置文件/etc/kubernetes/apiserver,把--admission-control参数中的ServiceAccount删除。
4、按顺序启动各个服务:
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
5、下载镜像
docker pull kubeguide/guestbook-redis-slave
docker pull kubeguide/guestbook-php-frontend
docker pull kubeguide/redis-master
6、 先定义RC来创建pod,然后定义与之关联的service。
为redis-master服务新建一个名为redis-master-controller.yaml的RC定义文件,内容为:
apiVersion: v1
kind: ReplicationController
metadata:
name: redis-master
labels:
name: redis-master
spec:
replicas: 1
selector:
name: redis-master
template:
metadata:
labels:
name: redis-master
spec:
containers:
- name: master
image: kubeguide/redis-master
ports:
- containerPort: 6379
新建好文件之后执行如下命令:
kuberctl create -f redis-master-controller.yaml
删除:将create换为delete即可。
查看刚刚新建的redis-master:
kuberctl get rc
检查pod信息:
kuberctl get pods
7、下面新建与上面建的pod相关联的service,service对应的文件redis-master-service.yaml,内容如下:
apiVersion: v1
kind: Service
metadata:
name: redis-master
labels:
name: redis-master
spec:
ports:
- port: 6379
targetPort: 6379
selector:
name: redis-master
创建service:
kubectl create -f redis-master-service.yaml
查看新建的service:
kubectl get services
8、 redis-salve Pod和服务
步骤和redis-master的一样。
新建文件redis-salve-controller.yaml,内容如下:
apiVersion: v1
kind: ReplicationController
metadata:
name: redis-slave
labels:
name: redis-slave
spec:
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
运行kubectl create命令:
kubectl create -f redis-salve-controller.yaml
查看RC:
kubectl get rc
查看pod:
kubectl get pods
9、配置文件redis-salve-service.yaml内容如下:
apiVersion: v1
kind: Service
metadata:
name: redis-slave
labels:
name: redis-slave
spec:
ports:
- port: 6379
selector:
name: redis-slave
创建service:
kubectl create -f redis-slave-service.yaml
检查service:
kubectl get services;
10、 创建frontend Pod和服务
文件frontend-controller.yaml内容如下:
apiVersion: v1
kind: ReplicationController
metadata:
name: frontend
labels:
name: frontend
spec:
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
执行命令kubectl create:
kubectl create -f frontend-controller.yaml
检查RC:
kunectl get rc
检查pod:
kubectl get pods
11、文件frontend-service.yaml内容如下:
apiVersion: v1
kind: Service
metadata:
name: frontend
labels:
name: frontend
spec:
type: NodePort
ports:
- port: 80
nodePort: 30001
selector:
name: frontend
创建服务:
kubectl create -f frontend-service.yaml
检查service:
kubectl get services;
12、 访问网站
如我的虚拟机IP为192.168.153.46
则浏览器访问: 192.168.153.46:30001
更多推荐
已为社区贡献1条内容
所有评论(0)