超新手 Operator 入門 101
印章
2021/01/26
1. Operator Framework Introduction
2. Shell Operator
3. Ansible Operator
4. Compare with Helm3
5. Demo (Ansible Operator 101)
印章 (seal.tw ),不是海豹
本名 吳易璋
Gitlab Taiwan社群成員
「前」某醫學中心打雜
兼任On-Permise Cloud 架構師
兼任 Infra 維運
兼任 SRE
現職 無業遊民
賭神從來不拍照
FROM kubespheredev/shell-operator:v1.0.0-beta.5-alpine3.12
ENV ANSIBLE_ROLES_PATH /kubesphere/installer/roles
WORKDIR /kubesphere
ADD controller/* /hooks/kubesphere/
ADD roles /kubesphere/installer/roles
ADD env /kubesphere/results/env
ADD playbooks /kubesphere/playbooks
USER kubesphere
$ mkdir memcached-operator
$ cd memcached-operator
$ operator-sdk init --plugins=ansible --domain example.com
# or
$ git clone https://github.com/djzager/ansible-role-hello-world-k8s.git
FROM quay.io/operator-framework/ansible-operator:v0.8.1
COPY watches.yaml ${HOME}/watches.yaml
COPY roles/ ${HOME}/roles/
COPY playbook.yml ${HOME}/playbook.yml
- name: 'Set hello-world objects state={{ state }}'
k8s:
state: '{{ state }}'
definition: "{{ lookup('template', item.name) | from_yaml }}"
loop:
- name: deployment.yml.j2
- name: service.yml.j2
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: helloworlds.examples.djzager.io
spec:
group: examples.djzager.io
names:
kind: HelloWorld
listKind: HelloWorldList
plural: helloworlds
singular: helloworld
scope: Namespaced
version: v1alpha1
subresources:
status: {}
# Prepare hello world operator image
$ docker build . -t hello-world-operator:latest
$ docker tag hello-world-operator:latest quay.io/rockwyc992/hello-world-operator:latest
$ docker push quay.io/rockwyc992/hello-world-operator:latest
# Create Kubernetes Resource from yaml file
$ kubectl create -f deploy/namespace.yaml \
-f deploy/service_account.yaml \
-f deploy/role.yaml \
-f deploy/role_binding.yaml \
-f deploy/deployment.yaml \
-f deploy/crds/helloworld_crd.yaml
$ kubectl -n mynamespace get pod -w
$ kubectl create -f deploy/crds/helloworld_cr.yaml
$ docker build . -t hello-world-operator:latest
$ kubectl create -f deploy/deployment.yaml \
-f deploy/crds/helloworld_crd.yaml
$ kubectl create -f deploy/crds/helloworld_cr.yaml
apiVersion: examples.djzager.io/v1alpha1
kind: HelloWorld
metadata:
name: example-helloworld
spec:
size: 3
image: nginx:alpine
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ meta.name }}
namespace: {{ meta.namespace }}
spec:
replicas: {{ size }}
template:
spec:
containers:
- image: {{ image }}
name: {{ meta.namespace }}
ports:
- containerPort: 80
protocol: TCP
$ ansible-galaxy collection install community.kubernetes
$ cat tasks/main.yaml
- name: Create a Deployment by reading the definition from a local file
community.kubernetes.k8s:
state: present
src: test-deployment.yml
$ cat tasks/main-legacy.yaml
- name: Create a Deployment by reading the definition from a local file
k8s:
state: present
src: test-deployment.yml
- name: Create a Deployment by reading the definition from a local file
k8s:
state: present
src: test-deployment.yml
- name: Create a Service object from an inline definition
k8s:
state: present
definition:
apiVersion: v1
kind: Service
metadata:
name: web
spec:
selector:
app: galaxy
ports:
- protocol: TCP
targetPort: 8000
port: 8000
- name: Read definition file from parsing template file
k8s:
state: present
definition: "{{ lookup('file', '/testing/deployment.yaml.j2') | from_yaml }}"
- name: Read definition template file
k8s:
state: present
template: '/testing/service.yaml.j2'
- name: Remove an existing Service object
k8s:
state: absent
api_version: v1
kind: Service
namespace: testing
name: web
Operator Framework:
Shell Operator:
Ansible Operator: